package com.bofeng.service;

import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.bofeng.dao.*;
import com.bofeng.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
public class UserOpenService {

    @Autowired
    private UserOpenMapper userOpenMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserPlaceMapper userPlaceMapper;

    public UserOpen getUserByOpenId(String openId) {
        return userOpenMapper.selectById(openId);
    }

    public UserOpen getUserByUserId(Long userId) {
        return userOpenMapper.selectByUserId(userId);
    }

    @Transactional
    public int bindUserOpen(UserOpen userOpen, User user) {
        return userOpenMapper.insert(userOpen) + userMapper.insert(user);
    }

    @Transactional
    public int bindUser(UserOpen userOpen, User user, UserPlace userPlace) {

        if (userOpenMapper.selectById(userOpen.getOpenId())==null) {
            userOpenMapper.insert(userOpen);
        }
        if (userMapper.selectById(user.getUserId())==null) {

            userMapper.insert(user);
        }
        return userPlaceMapper.insert(userPlace);

    }

    @Autowired
    private UptownHomeMapper uptownHomeMapper;
    @Autowired
    private OwnerMapper ownerMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private UptownHouseMapper uptownHouseMapper;

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void saveUserInfo(Long userId, Integer userType, String linkman, String phone, Integer area, Long uptownId, Long unitId, String doorplate) {
        UserRole userRole = new UserRole();
        userRole.setUrId(IdWorker.getId());
        userRole.setUserId(userId);
        userRole.setRoleId(1);
        userRole.setUserCreate(userId);
        userRole.setUserUpdate(userId);
        UptownHouse uptownHouse = new UptownHouse();
        uptownHouse.setHouseId(IdWorker.getId());
        uptownHouse.setUnitId(unitId);
        uptownHouse.setDoorplate(doorplate);
        uptownHouse.setStatus(1);
        uptownHouseMapper.insert(uptownHouse);
        UptownHome uptownHome = new UptownHome();
        uptownHome.setHouseId(uptownHouse.getHouseId());
        uptownHome.setLinkman(linkman);
        uptownHome.setPhone(phone);
        uptownHome.setYoung(0);
        uptownHome.setMiddle(0);
        uptownHome.setOld(0);
        uptownHome.setRemark("");
        uptownHome.setStatus(1);
        uptownHomeMapper.insert(uptownHome);
        userRole.setPropertyId(uptownHome.getHouseId());
        if (userType == 2) {
            Owner owner = new Owner();
            owner.setOwnerId(IdWorker.getId());
            owner.setUptownId(uptownId);
            owner.setLinkman(linkman);
            owner.setPhone(phone);
            owner.setUserCreate(userId);
            owner.setUserUpdate(userId);
            ownerMapper.insert(owner);
            UserRole userRole1 = new UserRole();
            userRole1.setUrId(IdWorker.getId());
            userRole1.setUserId(userId);
            userRole1.setRoleId(userType);
            userRole1.setUserCreate(userId);
            userRole1.setUserUpdate(userId);
            userRole1.setPropertyId(owner.getOwnerId());
            userRoleMapper.insert(userRole1);
        }
        userRoleMapper.insert(userRole);
    }
}
