package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.constant.AppLoginEnum;
import com.ruoyi.common.constant.AppUserTypeEnum;
import com.ruoyi.common.constant.DeletedEnum;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.ContractorUser;
import com.ruoyi.system.domain.ContractorUserProfessionRelation;
import com.ruoyi.system.dto.ContractorUserDTO;
import com.ruoyi.system.mapper.ContractorUserMapper;

import com.ruoyi.system.mapper.ContractorUserProfessionRelationMapper;
import com.ruoyi.system.service.IContractorUserService;
import com.ruoyi.system.domain.AppLogin;
import com.ruoyi.system.mapper.AppLoginMapper;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * 承包商用户Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-04
 */
@Service
public class ContractorUserServiceImpl implements IContractorUserService {
    @Autowired
    private ContractorUserMapper contractorUserMapper;
    @Autowired
    private ContractorUserProfessionRelationMapper contractorUserProfessionRelationMapper;
    @Autowired
    private AppLoginMapper appLoginMapper;

    /**
     * 查询承包商用户
     *
     * @param id 承包商用户主键
     * @return 承包商用户
     */
    @Override
    public ContractorUserDTO selectContractorUserById(String id) {
        ContractorUser contractorUser = contractorUserMapper.selectById(id);
        if (Objects.nonNull(contractorUser)) {
            ContractorUserDTO contractorUserDTO = new ContractorUserDTO();
            BeanUtils.copyProperties(contractorUser, contractorUserDTO);
            contractorUserDTO.setPassword(null);
            //查询用户岗位
            LambdaQueryWrapper<ContractorUserProfessionRelation> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ContractorUserProfessionRelation::getContractorUserId, id);
            queryWrapper.eq(ContractorUserProfessionRelation::getIsDel, DeletedEnum.VALID.getValue());
            List<ContractorUserProfessionRelation> contractorUserProfessionRelations = contractorUserProfessionRelationMapper.selectList(queryWrapper);
            contractorUserDTO.setContractorProfessionList(contractorUserProfessionRelations);
            return contractorUserDTO;
        }
        return null;
    }


    /**
     * 查询承包商用户列表
     *
     * @param contractorUserDTO 承包商用户
     * @return 承包商用户
     */
    @Override
    public List<ContractorUserDTO> selectContractorUserList(ContractorUserDTO contractorUserDTO) {
        ContractorUser contractorUser = new ContractorUser();
        BeanUtils.copyProperties(contractorUserDTO, contractorUser);
        contractorUser.setIsDel(DeletedEnum.VALID.getValue());
        List<ContractorUserDTO> contractorUserList = contractorUserMapper.selectContractorUserList(contractorUser);
        if (CollectionUtils.isEmpty(contractorUserList)) {
            return Lists.newArrayList();
        }
        for (ContractorUserDTO contractor1 : contractorUserList) {
            contractor1.setPassword(null);
        }
        return contractorUserList;
    }

    /**
     * 新增承包商用户
     *
     * @param contractorUserDTO 承包商用户
     * @return 结果
     */
    @Override
    @Transactional
    public int insertContractorUser(ContractorUserDTO contractorUserDTO) {
        ContractorUser contractorUser = new ContractorUser();
        BeanUtils.copyProperties(contractorUserDTO, contractorUser);
        contractorUser.setCreateDefault();
        if (StringUtils.isNotEmpty(contractorUser.getPassword())) {
            contractorUser.setPassword(SecurityUtils.encryptPassword(contractorUser.getPassword()));
        }
        int count = contractorUserMapper.insert(contractorUser);
        //处理用户工种关系
        List<ContractorUserProfessionRelation> contractorProfessionList = contractorUserDTO.getContractorProfessionList();
        if (!CollectionUtils.isEmpty(contractorProfessionList)) {
            for (ContractorUserProfessionRelation contractorUserProfessionRelation : contractorProfessionList) {
                contractorUserProfessionRelation.setContractorUserId(contractorUser.getId());
                contractorUserProfessionRelation.setCreateDefault();
                contractorUserProfessionRelationMapper.insert(contractorUserProfessionRelation);
            }
        }
        //判断是否创建APP账号
        if (AppLoginEnum.YES.getValue().equals(contractorUserDTO.getLoginApp())) {
            AppLogin appLogin = new AppLogin();
            appLogin.setUserName(contractorUser.getUserName());
            appLogin.setPassword(contractorUser.getPassword());
            appLogin.setUserType(AppUserTypeEnum.CONTRACTOR.getValue());
            appLogin.setContractorUserId(contractorUser.getId());
            appLogin.setCreateDefault();
            appLoginMapper.insert(appLogin);
        }
        return count;
    }

    /**
     * 修改承包商用户
     *
     * @param contractorUserDTO 承包商用户
     * @return 结果
     */
    @Override
    @Transactional
    public int updateContractorUser(ContractorUserDTO contractorUserDTO) {
        ContractorUser contractorUser = new ContractorUser();
        BeanUtils.copyProperties(contractorUserDTO, contractorUser);
        contractorUser.setUpdateDefault();
        if (!StringUtils.isEmpty(contractorUserDTO.getPassword())) {
            contractorUser.setPassword(SecurityUtils.encryptPassword(contractorUserDTO.getPassword()));
        }

        int count = contractorUserMapper.updateById(contractorUser);
        //处理用户岗位关系
        List<ContractorUserProfessionRelation> contractorProfessionList = contractorUserDTO.getContractorProfessionList();
        if (!CollectionUtils.isEmpty(contractorProfessionList)) {
            //1、删除原工种
            ContractorUserProfessionRelation contractorUserProfessionRelation = new ContractorUserProfessionRelation();
            contractorUserProfessionRelation.setIsDel(DeletedEnum.INVALID.getValue());
            contractorUserProfessionRelation.setUpdateDefault();
            LambdaQueryWrapper<ContractorUserProfessionRelation> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ContractorUserProfessionRelation::getContractorUserId, contractorUser.getId());
            contractorUserProfessionRelationMapper.update(contractorUserProfessionRelation, queryWrapper);

            //2、重新插入原工种
            for (ContractorUserProfessionRelation contractorUserProfessionRelation1 : contractorProfessionList) {
                contractorUserProfessionRelation1.setContractorUserId(contractorUser.getId());
                contractorUserProfessionRelation1.setCreateDefault();
                contractorUserProfessionRelation1.setId(null);
                contractorUserProfessionRelationMapper.insert(contractorUserProfessionRelation1);
            }
        }
        //判断app账号是否存在
        LambdaQueryWrapper<AppLogin> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(AppLogin::getContractorUserId, contractorUser.getId());
        List<AppLogin> appLogins = appLoginMapper.selectList(queryWrapper1);
        //判断是否创建APP账号
        if (AppLoginEnum.YES.getValue().equals(contractorUserDTO.getLoginApp())) {
            if (CollectionUtils.isEmpty(appLogins)) {
                AppLogin appLogin = new AppLogin();
                appLogin.setUserName(contractorUser.getUserName());
                appLogin.setPassword(contractorUser.getPassword());
                appLogin.setUserType(AppUserTypeEnum.CONTRACTOR.getValue());
                appLogin.setContractorUserId(contractorUser.getId());
                appLogin.setCreateDefault();
                appLoginMapper.insert(appLogin);
            } else {
                //判断是否是删除状态，如果是删除则改成有效
                if (appLogins.get(0).getIsDel().equals(DeletedEnum.INVALID.getValue())) {
                    AppLogin appLogin = new AppLogin();
                    appLogin.setId(appLogins.get(0).getId());
                    appLogin.setIsDel(DeletedEnum.VALID.getValue());
                    appLogin.setUpdateDefault();
                    appLoginMapper.updateById(appLogin);
                }
            }
        } else {
            //修改状态为删除
            LambdaQueryWrapper<AppLogin> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AppLogin::getContractorUserId, contractorUser.getId());
            queryWrapper.eq(AppLogin::getIsDel, DeletedEnum.VALID.getValue());
            AppLogin appLogin = new AppLogin();
            appLogin.setIsDel(DeletedEnum.INVALID.getValue());
            appLogin.setUpdateDefault();
            appLoginMapper.update(appLogin, queryWrapper);
        }
        return count;
    }

    /**
     * 批量删除承包商用户
     *
     * @param ids 需要删除的承包商用户主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteContractorUserByIds(String[] ids) {
        ContractorUser contractorUser = new ContractorUser();
        contractorUser.setIsDel(DeletedEnum.INVALID.getValue());
        contractorUser.setUpdateDefault();
        LambdaQueryWrapper<ContractorUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ContractorUser::getId, ids);
        int count = contractorUserMapper.update(contractorUser, queryWrapper);
        //删除app账号
        LambdaQueryWrapper<AppLogin> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.in(AppLogin::getContractorUserId, ids);
        AppLogin appLogin = new AppLogin();
        appLogin.setIsDel(DeletedEnum.INVALID.getValue());
        appLogin.setUpdateDefault();
        appLoginMapper.update(appLogin, queryWrapper1);
        return count;
    }

    @Override
    public List<ContractorUser> selectContractorUserByIds(List<String> contractorUserIdList) {
//        LambdaQueryWrapper<ContractorUser> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.in(ContractorUser::getId,contractorUserIdList);
        return contractorUserMapper.selectBatchIds(contractorUserIdList);
    }
}
