package com.yanzhu.manage.service.impl;

import com.yanzhu.common.core.constant.CacheConstants;
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.redis.service.RedisService;
import com.yanzhu.common.security.utils.SecurityUtils;
import com.yanzhu.manage.config.ProfileConfig;
import com.yanzhu.manage.domain.ProProjectInfo;
import com.yanzhu.manage.domain.ProProjectInfoSubdepts;
import com.yanzhu.manage.mapper.ProProjectInfoSubdeptsMapper;
import com.yanzhu.manage.service.IProProjectInfoSubdeptsService;
import com.yanzhu.manage.utils.WxQrCodeUtils;
import com.yanzhu.system.api.RemoteUserService;
import com.yanzhu.system.api.domain.SysUser;
import me.chanjar.weixin.mp.api.WxMpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 分包单位Service业务层处理
 * 
 * @author JiangYuQi
 * @date 2024-08-25
 */
@Service
public class ProProjectInfoSubdeptsServiceImpl implements IProProjectInfoSubdeptsService 
{
    @Autowired
    private WxMpService wxMpService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private ProfileConfig profileConfig;

    @Autowired
    private RemoteUserService remoteUserService;

    @Autowired
    private ProProjectInfoSubdeptsMapper proProjectInfoSubdeptsMapper;

    private static final Logger log = LoggerFactory.getLogger(ProProjectInfoSubdeptsServiceImpl.class);

    /**
     * 查询分包单位
     * 
     * @param id 分包单位主键
     * @return 分包单位
     */
    @Override
    public ProProjectInfoSubdepts selectProProjectInfoSubdeptsById(Long id)
    {
        return proProjectInfoSubdeptsMapper.selectProProjectInfoSubdeptsById(id);
    }

    /**
     * 查询分包单位列表
     * 
     * @param proProjectInfoSubdepts 分包单位
     * @return 分包单位
     */
    @Override
    public List<ProProjectInfoSubdepts> selectProProjectInfoSubdeptsList(ProProjectInfoSubdepts proProjectInfoSubdepts)
    {
        proProjectInfoSubdepts.setActiveComId(SecurityUtils.getLoginUser().getProjectDeptId());
        proProjectInfoSubdepts.setActiveProjectId(SecurityUtils.getLoginUser().getProjectId());
        return proProjectInfoSubdeptsMapper.selectProProjectInfoSubdeptsList(proProjectInfoSubdepts);
    }

    /**
     * 新增分包单位
     * 
     * @param proProjectInfoSubdepts 分包单位
     * @return 结果
     */
    @Override
    @Transactional
    public int insertProProjectInfoSubdepts(ProProjectInfoSubdepts proProjectInfoSubdepts)
    {
        ProProjectInfo info = redisService.getCacheObject(CacheConstants.PRO_PROJECT+proProjectInfoSubdepts.getProjectId());
        proProjectInfoSubdepts.setComId(info.getComId());
        if(Objects.equals(proProjectInfoSubdepts.getSubDeptType(),"4") || Objects.equals(proProjectInfoSubdepts.getSubDeptType(),"4")){
            try {
                String accessToken = wxMpService.getAccessToken();
                String imgPath = profileConfig.getPath()+"/"+System.currentTimeMillis()+".png";
                log.info("accessToken...{}",accessToken);
                WxQrCodeUtils.generateQrCode(imgPath, "pages/index/index", "aa=108&bb=2&cc=3", accessToken);
                // TODO:生成二维码
                //proProjectInfoSubdepts.setQrCode();
            }catch (Exception e){
                log.error(e.getMessage());
            }
            // 单位委托人信息
            if(StringUtils.isNotEmpty(proProjectInfoSubdepts.getSubDeptLeaderName()) && StringUtils.isNotEmpty(proProjectInfoSubdepts.getSubDeptLeaderCode()) && StringUtils.isNotEmpty(proProjectInfoSubdepts.getSubDeptLeaderPhone())){
                SysUser sysUser = new SysUser();
                sysUser.setUserName(proProjectInfoSubdepts.getSubDeptLeaderPhone());
                sysUser.setNickName(proProjectInfoSubdepts.getSubDeptLeaderName());
                sysUser.setUserType(UserTypeEnums.FBDL.getCode());
                sysUser.setCardType(CardTypeEnums.SFZ.getCode());
                sysUser.setCardCode(proProjectInfoSubdepts.getSubDeptLeaderCode());
                sysUser.setUserPicture(proProjectInfoSubdepts.getLeaderUserPicture());
                sysUser.setCardImgPos(proProjectInfoSubdepts.getLeaderCardImgPos());
                sysUser.setCardImgInv(proProjectInfoSubdepts.getLeaderCardImgInv());
                sysUser.setSex(StringUtils.judgeGender(proProjectInfoSubdepts.getSubDeptLeaderCode()));
                sysUser.setCreateBy(SecurityUtils.getUsername());
                sysUser.setCreateTime(DateUtils.getNowDate());
                sysUser.setRemark(proProjectInfoSubdepts.getSubDeptName());
                try {
                    R<Long> userResult = remoteUserService.registerUserInfo(sysUser, SecurityConstants.INNER);
                    proProjectInfoSubdepts.setSubDeptLeaderId(userResult.getData());
                }catch (Exception e){
                    // 已注册账号！！忽略异常...
                }

                // TODO:实名制接入
            }
        }
        proProjectInfoSubdepts.setCreateBy(SecurityUtils.getUsername());
        proProjectInfoSubdepts.setCreateTime(DateUtils.getNowDate());
        return proProjectInfoSubdeptsMapper.insertProProjectInfoSubdepts(proProjectInfoSubdepts);
    }

    /**
     * 修改分包单位
     * 
     * @param proProjectInfoSubdepts 分包单位
     * @return 结果
     */
    @Override
    @Transactional
    public int updateProProjectInfoSubdepts(ProProjectInfoSubdepts proProjectInfoSubdepts)
    {
        if(Objects.equals(proProjectInfoSubdepts.getSubDeptType(),"4") || Objects.equals(proProjectInfoSubdepts.getSubDeptType(),"4")){
            if(StringUtils.isEmpty(proProjectInfoSubdepts.getQrCode())){
                // TODO:生成二维码

                // 单位委托人信息
                if(StringUtils.isNotEmpty(proProjectInfoSubdepts.getSubDeptLeaderName()) && StringUtils.isNotEmpty(proProjectInfoSubdepts.getSubDeptLeaderCode()) && StringUtils.isNotEmpty(proProjectInfoSubdepts.getSubDeptLeaderPhone())){
                    SysUser sysUser = new SysUser();
                    sysUser.setUserName(proProjectInfoSubdepts.getSubDeptLeaderPhone());
                    sysUser.setNickName(proProjectInfoSubdepts.getSubDeptLeaderName());
                    sysUser.setUserType(UserTypeEnums.FBDL.getCode());
                    sysUser.setCardType(CardTypeEnums.SFZ.getCode());
                    sysUser.setCardCode(proProjectInfoSubdepts.getSubDeptLeaderCode());
                    sysUser.setUserPicture(proProjectInfoSubdepts.getLeaderUserPicture());
                    sysUser.setCardImgPos(proProjectInfoSubdepts.getLeaderCardImgPos());
                    sysUser.setCardImgInv(proProjectInfoSubdepts.getLeaderCardImgInv());
                    sysUser.setSex(StringUtils.judgeGender(proProjectInfoSubdepts.getSubDeptLeaderCode()));
                    sysUser.setCreateBy(SecurityUtils.getUsername());
                    sysUser.setCreateTime(DateUtils.getNowDate());
                    sysUser.setRemark(proProjectInfoSubdepts.getSubDeptName());
                    try {
                        R<Long> userResult = remoteUserService.registerUserInfo(sysUser, SecurityConstants.INNER);
                        proProjectInfoSubdepts.setSubDeptLeaderId(userResult.getData());
                    }catch (Exception e){
                        // 已注册账号！！忽略异常...
                    }

                    // TODO:实名制接入
                }
            }
        }
        proProjectInfoSubdepts.setUpdateBy(SecurityUtils.getUsername());
        proProjectInfoSubdepts.setUpdateTime(DateUtils.getNowDate());
        return proProjectInfoSubdeptsMapper.updateProProjectInfoSubdepts(proProjectInfoSubdepts);
    }

    /**
     * 修改分包单位
     *
     * @param proProjectInfoSubdepts 分包单位
     * @return 结果
     */
    @Override
    public int updateProProjectInfoSubdeptsEntity(ProProjectInfoSubdepts proProjectInfoSubdepts){
        proProjectInfoSubdepts.setUpdateBy(SecurityUtils.getUsername());
        proProjectInfoSubdepts.setUpdateTime(DateUtils.getNowDate());
        return proProjectInfoSubdeptsMapper.updateProProjectInfoSubdepts(proProjectInfoSubdepts);
    }

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

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