package com.ruoyi.framework.web.service.projectV2;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.framework.manager.factory.AsyncFactory;
import com.ruoyi.system.domain.fgw.FgwLdps;
import com.ruoyi.system.domain.fgw.FgwXmsb;
import com.ruoyi.system.domain.projectV2.ZsyzDbd;
import com.ruoyi.system.domain.projectV2.ZsyzSbbzb;
import com.ruoyi.system.domain.projectV2.common.SendSmsConfig;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.projectV2.ZsyzSbbzbMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.projectV2.ZsyzLdpsMapper;
import com.ruoyi.system.domain.projectV2.ZsyzLdps;
import com.ruoyi.system.service.projectV2.IZsyzLdpsService;

import static com.ruoyi.common.constant.CommonConstants.*;

/**
 * 招商引资_领导批示Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-02-26
 */
@Service
public class ZsyzLdpsServiceImpl implements IZsyzLdpsService 
{
    @Autowired
    private ZsyzLdpsMapper zsyzLdpsMapper;
    @Autowired
    private ZsyzSbbzbMapper zsyzSbbzbMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询招商引资_领导批示
     * 
     * @param id 招商引资_领导批示主键
     * @return 招商引资_领导批示
     */
    @Override
    public ZsyzLdps selectZsyzLdpsById(Long id)
    {
        return zsyzLdpsMapper.selectZsyzLdpsById(id);
    }

    /**
     * 查询招商引资_领导批示列表
     * 
     * @param zsyzLdps 招商引资_领导批示
     * @return 招商引资_领导批示
     */
    @Override
    public List<ZsyzLdps> selectZsyzLdpsList(ZsyzLdps zsyzLdps)
    {

        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        String roleKey = "";
        // 如果是超级管理员/管理员，则不过滤数据
        List<SysRole> roles = SecurityUtils.getLoginUser().getUser().getRoles();
        if (roles != null && roles.size() > 0) {
            roleKey = roles.get(0).getRoleKey();
        }
        if (LEADER.equals(roleKey)) {
        //判断账号权限是不是领导，领导只能看见自己的
            zsyzLdps.setPsnrId(currentUser.getUserId());
        }
        return zsyzLdpsMapper.selectZsyzLdpsList(zsyzLdps);
    }

    /**
     * 新增招商引资_领导批示
     * 
     * @param zsyzLdps 招商引资_领导批示
     * @return 结果
     */
    @Override
    public int insertZsyzLdps(ZsyzLdps zsyzLdps)
    {
        //获取当前人员信息
        SysUser user = SecurityUtils.getLoginUser().getUser();
        zsyzLdps.setDeptId(user.getDeptId());
        zsyzLdps.setPsr(user.getNickName());
        zsyzLdps.setXgr(user.getUserName());
        zsyzLdps.setPsnrId(user.getUserId());
        zsyzLdps.setXgsj(DateUtils.getNowDate());
        zsyzLdps.setCjsj(DateUtils.getNowDate());
        if(!"1".equals(zsyzLdps.getType())){
            //修改主表中的领导批示内容
            ZsyzSbbzb zsyzSbbzb = new ZsyzSbbzb();
            zsyzSbbzb.setId(zsyzLdps.getXmId());
            zsyzSbbzb.setPsnr(zsyzLdps.getPsnr());
            zsyzSbbzbMapper.updateZsyzSbbzb(zsyzSbbzb);

            //发送短信（给承接地和创建线索的部门人员）
            List<SendSmsConfig> sendSmsConfigs = new ArrayList<>();
            //查询项目信息
            ZsyzSbbzb zsyzSbbzbs = zsyzSbbzbMapper.selectZsyzSbbzbById(zsyzLdps.getXmId());
            //查询承接地
            List<SysUser> sysUsers = new ArrayList<>();
            if(zsyzSbbzbs.getCjdId()!=null && zsyzSbbzbs.getCjdId()!=0L){
                List<SysUser> sysUserList = sysUserMapper.selectUserByDeptId(zsyzSbbzbs.getCjdId());
                sysUsers.addAll(sysUserList);
            }
            //查询申请人部门所有人都发
            List<SysUser> sysUserSqr = sysUserMapper.selectUserByDeptId(zsyzSbbzbs.getDeptId());
            sysUsers.addAll(sysUserSqr);
            if(sysUsers!=null && sysUsers.size()>0){
                for (SysUser sysUser : sysUsers) {
                    if (StringUtils.isNotEmpty(sysUser.getPhonenumber())) {
                        SendSmsConfig sendSmsConfig = new SendSmsConfig();
                        sendSmsConfig.setPhone(sysUser.getPhonenumber());
                        sendSmsConfigs.add(sendSmsConfig);
                    }
                }
                if (sendSmsConfigs.size() > 0) {
                    AsyncManager.me().execute(AsyncFactory.sendSms(sendSmsConfigs, "SMS_273745572"));
                }
            }

        }

        return zsyzLdpsMapper.insertZsyzLdps(zsyzLdps);
    }

    /**
     * 修改招商引资_领导批示
     * 
     * @param zsyzLdps 招商引资_领导批示
     * @return 结果
     */
    @Override
    public int updateZsyzLdps(ZsyzLdps zsyzLdps)
    {
        //获取当前人员信息
        SysUser user = SecurityUtils.getLoginUser().getUser();
        zsyzLdps.setXgsj(DateUtils.getNowDate());
        zsyzLdps.setXgr(user.getUserName());
        zsyzLdps.setPsr(user.getNickName());
        if(!"1".equals(zsyzLdps.getType())){
            zsyzLdps.setZcpsnr("");
            //修改主表中的领导批示内容
            ZsyzSbbzb zsyzSbbzb = new ZsyzSbbzb();
            zsyzSbbzb.setId(zsyzLdps.getXmId());
            zsyzSbbzb.setPsnr(zsyzLdps.getPsnr());
            zsyzSbbzbMapper.updateZsyzSbbzb(zsyzSbbzb);
        }
        return zsyzLdpsMapper.updateZsyzLdps(zsyzLdps);
    }

    /**
     * 批量删除招商引资_领导批示
     * 
     * @param ids 需要删除的招商引资_领导批示主键
     * @return 结果
     */
    @Override
    public int deleteZsyzLdpsByIds(Long[] ids)
    {
        return zsyzLdpsMapper.deleteZsyzLdpsByIds(ids);
    }

    /**
     * 删除招商引资_领导批示信息
     * 
     * @param id 招商引资_领导批示主键
     * @return 结果
     */
    @Override
    public int deleteZsyzLdpsById(Long id,Long xmId)
    {


        //查询最新的领导批示信息
        List<ZsyzLdps> zsyzLdpsList = zsyzLdpsMapper.selectZsyzLdpsNew(xmId);
        if(zsyzLdpsList.size()==1){
            //如果只有一条数据，则说明当前删除的就是最新的
            ZsyzSbbzb zsyzSbbzb = new ZsyzSbbzb();
            zsyzSbbzb.setId(xmId);
            zsyzSbbzb.setPsnr("");
            zsyzSbbzbMapper.updateZsyzSbbzb(zsyzSbbzb);
        }else{
            //判断第一条是否与要删除的ID相同
            if(zsyzLdpsList.get(0).getId().equals(id)){
                //将主表中的批示内容换成第二条
                ZsyzSbbzb zsyzSbbzb = new ZsyzSbbzb();
                zsyzSbbzb.setId(xmId);
                zsyzSbbzb.setPsnr(zsyzLdpsList.get(1).getPsnr());
                zsyzSbbzbMapper.updateZsyzSbbzb(zsyzSbbzb);
            }
        }
        return zsyzLdpsMapper.deleteZsyzLdpsById(id);
    }

    @Override
    public ZsyzLdps getInfoZc(ZsyzLdps zsyzLdps) {
        //获取当前人员信息
        SysUser user = SecurityUtils.getLoginUser().getUser();
        zsyzLdps.setPsnrId(user.getUserId());
        return zsyzLdpsMapper.getInfoZc(zsyzLdps);
    }
}
