package com.yanzhu.manage.service.impl;

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

import com.yanzhu.common.core.constant.SecurityConstants;
import com.yanzhu.common.core.domain.R;
import com.yanzhu.common.core.enums.CardTypeEnums;
import com.yanzhu.common.core.enums.UserTypeEnums;
import com.yanzhu.common.core.utils.DateUtils;
import com.yanzhu.common.core.utils.StringUtils;
import com.yanzhu.common.security.utils.SecurityUtils;
import com.yanzhu.manage.domain.ProProjectInfoSubdepts;
import com.yanzhu.manage.mapper.ProProjectInfoSubdeptsMapper;
import com.yanzhu.system.api.RemoteUserService;
import com.yanzhu.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yanzhu.manage.mapper.ProProjectInfoSubdeptsGroupMapper;
import com.yanzhu.manage.domain.ProProjectInfoSubdeptsGroup;
import com.yanzhu.manage.service.IProProjectInfoSubdeptsGroupService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 分包单位班组Service业务层处理
 * 
 * @author JiangYuqi
 * @date 2024-08-25
 */
@Service
public class ProProjectInfoSubdeptsGroupServiceImpl implements IProProjectInfoSubdeptsGroupService 
{

    @Autowired
    private RemoteUserService remoteUserService;

    @Autowired
    private ProProjectInfoSubdeptsMapper proProjectInfoSubdeptsMapper;

    @Autowired
    private ProProjectInfoSubdeptsGroupMapper proProjectInfoSubdeptsGroupMapper;

    /**
     * 查询分包单位班组
     * 
     * @param id 分包单位班组主键
     * @return 分包单位班组
     */
    @Override
    public ProProjectInfoSubdeptsGroup selectProProjectInfoSubdeptsGroupById(Long id)
    {
        return proProjectInfoSubdeptsGroupMapper.selectProProjectInfoSubdeptsGroupById(id);
    }

    /**
     * 查询分包单位班组列表
     * 
     * @param proProjectInfoSubdeptsGroup 分包单位班组
     * @return 分包单位班组
     */
    @Override
    public List<ProProjectInfoSubdeptsGroup> selectProProjectInfoSubdeptsGroupList(ProProjectInfoSubdeptsGroup proProjectInfoSubdeptsGroup)
    {
        proProjectInfoSubdeptsGroup.setActiveComId(SecurityUtils.getLoginUser().getProjectDeptId());
        proProjectInfoSubdeptsGroup.setActiveProjectId(SecurityUtils.getLoginUser().getProjectId());
        return proProjectInfoSubdeptsGroupMapper.selectProProjectInfoSubdeptsGroupList(proProjectInfoSubdeptsGroup);
    }

    /**
     * 新增分包单位班组
     * 
     * @param proProjectInfoSubdeptsGroup 分包单位班组
     * @return 结果
     */
    @Override
    @Transactional
    public int insertProProjectInfoSubdeptsGroup(ProProjectInfoSubdeptsGroup proProjectInfoSubdeptsGroup)
    {
        ProProjectInfoSubdepts info = proProjectInfoSubdeptsMapper.selectProProjectInfoSubdeptsById(proProjectInfoSubdeptsGroup.getSubDeptId());
        if(Objects.nonNull(info)){
            proProjectInfoSubdeptsGroup.setComId(info.getComId());
            proProjectInfoSubdeptsGroup.setSubDeptName(info.getSubDeptName());
            proProjectInfoSubdeptsGroup.setSubDeptCode(info.getSubDeptCode());
            proProjectInfoSubdeptsGroup.setSubDeptType(info.getSubDeptType());
        }
        // 单位委托人信息
        if(StringUtils.isNotEmpty(proProjectInfoSubdeptsGroup.getGroupLeaderName()) && StringUtils.isNotEmpty(proProjectInfoSubdeptsGroup.getGroupLeaderCode()) && StringUtils.isNotEmpty(proProjectInfoSubdeptsGroup.getGroupLeaderPhone())){
            SysUser sysUser = new SysUser();
            sysUser.setUserName(proProjectInfoSubdeptsGroup.getGroupLeaderPhone());
            sysUser.setNickName(proProjectInfoSubdeptsGroup.getGroupLeaderName());
            sysUser.setUserType(UserTypeEnums.FBRY.getCode());
            sysUser.setCardType(CardTypeEnums.SFZ.getCode());
            sysUser.setCardCode(proProjectInfoSubdeptsGroup.getGroupLeaderCode());
            sysUser.setUserPicture(proProjectInfoSubdeptsGroup.getGroupLeaderUserPicture());
            sysUser.setCardImgPos(proProjectInfoSubdeptsGroup.getGroupLeaderCardImgPos());
            sysUser.setCardImgInv(proProjectInfoSubdeptsGroup.getGroupLeaderCardImgInv());
            sysUser.setSex(StringUtils.judgeGender(proProjectInfoSubdeptsGroup.getGroupLeaderCode()));
            sysUser.setCreateBy(SecurityUtils.getUsername());
            sysUser.setCreateTime(DateUtils.getNowDate());
            sysUser.setRemark(proProjectInfoSubdeptsGroup.getSubDeptName());
            try {
                remoteUserService.registerUserInfo(sysUser, SecurityConstants.INNER);
            }catch (Exception e){
                // 已注册账号！！忽略异常...
            }

            // TODO:实名制接入
        }
        proProjectInfoSubdeptsGroup.setCreateBy(SecurityUtils.getUsername());
        proProjectInfoSubdeptsGroup.setCreateTime(DateUtils.getNowDate());
        return proProjectInfoSubdeptsGroupMapper.insertProProjectInfoSubdeptsGroup(proProjectInfoSubdeptsGroup);
    }

    /**
     * 修改分包单位班组
     * 
     * @param proProjectInfoSubdeptsGroup 分包单位班组
     * @return 结果
     */
    @Override
    @Transactional
    public int updateProProjectInfoSubdeptsGroup(ProProjectInfoSubdeptsGroup proProjectInfoSubdeptsGroup)
    {
        // 单位委托人信息
        if(StringUtils.isNotEmpty(proProjectInfoSubdeptsGroup.getGroupLeaderName()) && StringUtils.isNotEmpty(proProjectInfoSubdeptsGroup.getGroupLeaderCode()) && StringUtils.isNotEmpty(proProjectInfoSubdeptsGroup.getGroupLeaderPhone())){
            SysUser sysUser = new SysUser();
            sysUser.setUserName(proProjectInfoSubdeptsGroup.getGroupLeaderPhone());
            sysUser.setNickName(proProjectInfoSubdeptsGroup.getGroupLeaderName());
            sysUser.setUserType(UserTypeEnums.FBRY.getCode());
            sysUser.setCardType(CardTypeEnums.SFZ.getCode());
            sysUser.setCardCode(proProjectInfoSubdeptsGroup.getGroupLeaderCode());
            sysUser.setUserPicture(proProjectInfoSubdeptsGroup.getGroupLeaderUserPicture());
            sysUser.setCardImgPos(proProjectInfoSubdeptsGroup.getGroupLeaderCardImgPos());
            sysUser.setCardImgInv(proProjectInfoSubdeptsGroup.getGroupLeaderCardImgInv());
            sysUser.setSex(StringUtils.judgeGender(proProjectInfoSubdeptsGroup.getGroupLeaderCode()));
            sysUser.setCreateBy(SecurityUtils.getUsername());
            sysUser.setCreateTime(DateUtils.getNowDate());
            sysUser.setRemark(proProjectInfoSubdeptsGroup.getSubDeptName());
            try {
                remoteUserService.registerUserInfo(sysUser, SecurityConstants.INNER);
            }catch (Exception e){
                // 已注册账号！！忽略异常...
            }

            // TODO:实名制接入
        }
        proProjectInfoSubdeptsGroup.setUpdateBy(SecurityUtils.getUsername());
        proProjectInfoSubdeptsGroup.setUpdateTime(DateUtils.getNowDate());
        return proProjectInfoSubdeptsGroupMapper.updateProProjectInfoSubdeptsGroup(proProjectInfoSubdeptsGroup);
    }

    /**
     * 批量删除分包单位班组
     * 
     * @param ids 需要删除的分包单位班组主键
     * @return 结果
     */
    @Override
    public int deleteProProjectInfoSubdeptsGroupByIds(Long[] ids)
    {
        return proProjectInfoSubdeptsGroupMapper.deleteProProjectInfoSubdeptsGroupByIds(ids);
    }

    /**
     * 删除分包单位班组信息
     * 
     * @param id 分包单位班组主键
     * @return 结果
     */
    @Override
    public int deleteProProjectInfoSubdeptsGroupById(Long id)
    {
        return proProjectInfoSubdeptsGroupMapper.deleteProProjectInfoSubdeptsGroupById(id);
    }
}
