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

import com.ruoyi.common.core.domain.entity.SysDept;
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.jPush.JPushToolUtil;
import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.framework.manager.factory.AsyncFactory;
import com.ruoyi.system.domain.projectV2.*;
import com.ruoyi.system.domain.projectV2.common.SendSmsConfig;
import com.ruoyi.system.mapper.projectV2.ZsyzFjMapper;
import com.ruoyi.system.mapper.projectV2.ZsyzKgxxMapper;
import com.ruoyi.system.mapper.projectV2.ZsyzLcjlMapper;
import com.ruoyi.system.mapper.projectV2.ZsyzSbbzbMapper;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.projectV2.IZsyzKgxxService;
import com.ruoyi.system.service.projectV2.common.ISendSmsConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

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

/**
 * 招商引资_开工信息Service业务层处理
 *
 * @author boman
 * @date 2023-02-22
 */
@Service
public class ZsyzKgxxServiceImpl implements IZsyzKgxxService {
    @Autowired
    private ZsyzKgxxMapper zsyzKgxxMapper;

    @Autowired
    private ZsyzFjMapper zsyzFjMapper;

    @Autowired
    private ZsyzSbbzbMapper zsyzSbbzbMapper;

    @Autowired
    private ZsyzLcjlMapper zsyzLcjlMapper;

    @Autowired
    private ISendSmsConfigService sendSmsConfigService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询招商引资_开工信息
     *
     * @param id 招商引资_开工信息ID
     * @return 招商引资_开工信息
     */
    @Override
    @Transactional
    public ZsyzKgxx selectZsyzKgxxById(Long id) {
        ZsyzKgxx zsyzKgxx = zsyzKgxxMapper.selectZsyzKgxxById(id);
        if (zsyzKgxx != null) {
            ZsyzFj zsyzFj = new ZsyzFj();
            zsyzFj.setSourceId(zsyzKgxx.getId());
            zsyzFj.setType(THR);
            List<ZsyzFj> zsyzFjList = zsyzFjMapper.selectZsyzFjList(zsyzFj);
            if (zsyzFjList != null && zsyzFjList.size() > 0) {
                zsyzKgxx.setZsyzFjList(zsyzFjList);
            }
        }
        return zsyzKgxx;
    }

    /**
     * 查询招商引资_开工信息列表
     *
     * @param zsyzKgxx 招商引资_开工信息
     * @return 招商引资_开工信息
     */
    @Override
    public List<ZsyzKgxx> selectZsyzKgxxList(ZsyzKgxx zsyzKgxx) {
        return zsyzKgxxMapper.selectZsyzKgxxList(zsyzKgxx);
    }

    /**
     * 新增招商引资_开工信息
     *
     * @param zsyzKgxx 招商引资_开工信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertZsyzKgxx(ZsyzKgxx zsyzKgxx) {
        zsyzKgxx.setCreateTime(DateUtils.getNowDate());
        int count = zsyzKgxxMapper.insertZsyzKgxx(zsyzKgxx);
        //先去判断项目状态
        ZsyzSbbzb zsyzSbbzb = zsyzSbbzbMapper.selectZsyzSbbzbById(zsyzKgxx.getXmId());
        if (zsyzSbbzb == null || (!THR.equals(zsyzSbbzb.getProgress()) && !TWO.equals(zsyzSbbzb.getXmStatus()))) {
            return 0;
        }
        zsyzSbbzb.setProgress(FOR);
        zsyzSbbzb.setXmStatus(ONE);
        zsyzSbbzbMapper.updateZsyzSbbzbProgress(zsyzSbbzb);
        //保存附件信息
        List<ZsyzFj> zsyzFjList = zsyzKgxx.getZsyzFjList();
        if (zsyzFjList != null) {
            for (ZsyzFj zsyzFj : zsyzFjList) {
                zsyzFj.setSourceId(zsyzKgxx.getId());
                zsyzFj.setType(THR);
                zsyzFj.setXmId(zsyzKgxx.getXmId());
                zsyzFj.setXmbh(zsyzKgxx.getXmbh());
                zsyzFjMapper.insertZsyzFj(zsyzFj);
            }
        }
        //插入流程记录表
        SysDept dept = SecurityUtils.getLoginUser().getUser().getDept();
        ZsyzLcjl zsyzLcjl = new ZsyzLcjl();
        zsyzLcjl.setXmId(zsyzSbbzb.getId());
        zsyzLcjl.setXmmc(zsyzKgxx.getXmmc());
        zsyzLcjl.setXmbh(zsyzKgxx.getXmbh());
        zsyzLcjl.setDeptId(dept.getDeptId());
        String deptName = dept.getDeptName();
        zsyzLcjl.setDeptName(deptName);
        zsyzLcjl.setProgress(FOR);
        zsyzLcjl.setXmStatus(ONE);
        zsyzLcjlMapper.insertZsyzLcjl(zsyzLcjl);

        //发送短信
        SendSmsConfig sendSmsConfig = new SendSmsConfig();
        sendSmsConfig.setProgress(TWO);
        List<SendSmsConfig> sendSmsConfigs = sendSmsConfigService.selectSendSmsConfigList(sendSmsConfig);
        if (sendSmsConfigs != null){
            AsyncManager.me().execute(AsyncFactory.sendSms(sendSmsConfigs,"SMS_271490074"));
        }

        //发极光推送
        List<SysUser> sysUsers = sysUserService.selectUserByDeptId(100L);
        if(sysUsers!=null && sysUsers.size()>0){
            String notificationTitle = "消息通知";
            String msgTitle = "开工审核";
            String msgContent = "您有一条开工审核信息，请及时处理！";
            String jPushVO = "";
            List<String> listS = sysUsers.stream().map(SysUser::getJgId).collect(Collectors.toList());
            listS.removeAll(Collections.singleton(null));
            if(listS!=null && listS.size()>0){
                String[]  registrationId = listS.toArray(new String[listS.size()]);
                JPushToolUtil.sendToRegistrationId(notificationTitle,msgTitle,msgContent,jPushVO,registrationId);
            }
        }



        return count;
    }

    /**
     * 修改招商引资_开工信息
     *
     * @param zsyzKgxx 招商引资_开工信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateZsyzKgxx(ZsyzKgxx zsyzKgxx) {

        zsyzKgxx.setUpdateTime(DateUtils.getNowDate());
        int count = zsyzKgxxMapper.updateZsyzKgxx(zsyzKgxx);
        //先去判断项目状态
        ZsyzSbbzb zsyzSbbzb = zsyzSbbzbMapper.selectZsyzSbbzbById(zsyzKgxx.getXmId());
        if (zsyzSbbzb == null || (!FOR.equals(zsyzSbbzb.getProgress()) && TWO.equals(zsyzSbbzb.getXmStatus()))) {
            return 0;
        }
        zsyzSbbzb.setProgress(FOR);
        zsyzSbbzb.setXmStatus(ONE);
        zsyzSbbzbMapper.updateZsyzSbbzbProgress(zsyzSbbzb);
        //修改附件
        if (zsyzKgxx.getZsyzFjList() != null && zsyzKgxx.getZsyzFjList().size() > 0) {
            ZsyzFj zsyzFjDelete = new ZsyzFj();
            zsyzFjDelete.setSourceId(zsyzKgxx.getId());
            zsyzFjDelete.setType(THR);
            //先删除相关附件
            zsyzFjMapper.deleteZsyzFjBySourceId(zsyzFjDelete);
            //再将文件新增进数据库
            List<ZsyzFj> zsyzFjList = zsyzKgxx.getZsyzFjList();
            for (ZsyzFj zsyzFj : zsyzFjList) {
                zsyzFj.setSourceId(zsyzKgxx.getId());
                zsyzFj.setType(THR);
                zsyzFj.setXmId(zsyzKgxx.getXmId());
                zsyzFj.setXmbh(zsyzKgxx.getXmbh());
                zsyzFjMapper.insertZsyzFj(zsyzFj);
            }
        }

        //插入流程记录表
        SysDept dept = SecurityUtils.getLoginUser().getUser().getDept();
        ZsyzLcjl zsyzLcjl = new ZsyzLcjl();
        zsyzLcjl.setXmId(zsyzSbbzb.getId());
        zsyzLcjl.setXmmc(zsyzKgxx.getXmmc());
        zsyzLcjl.setXmbh(zsyzKgxx.getXmbh());
        zsyzLcjl.setDeptId(dept.getDeptId());
        String deptName = dept.getDeptName();
        zsyzLcjl.setDeptName(deptName);
        zsyzLcjl.setProgress(FOR);
        zsyzLcjl.setXmStatus(ONE);
        zsyzLcjlMapper.insertZsyzLcjl(zsyzLcjl);

        //发送短信
        SendSmsConfig sendSmsConfig = new SendSmsConfig();
        sendSmsConfig.setProgress(TWO);
        List<SendSmsConfig> sendSmsConfigs = sendSmsConfigService.selectSendSmsConfigList(sendSmsConfig);
        if (sendSmsConfigs != null){
            AsyncManager.me().execute(AsyncFactory.sendSms(sendSmsConfigs,"SMS_271490074"));
        }

        //发极光推送
        List<SysUser> sysUsers = sysUserService.selectUserByDeptId(100L);
        if(sysUsers!=null && sysUsers.size()>0){
            String notificationTitle = "消息通知";
            String msgTitle = "开工审核";
            String msgContent = "您有一条开工审核信息，请及时处理！";
            String jPushVO = "";
            List<String> listS = sysUsers.stream().map(SysUser::getJgId).collect(Collectors.toList());
            listS.removeAll(Collections.singleton(null));
            if(listS!=null && listS.size()>0){
                String[]  registrationId = listS.toArray(new String[listS.size()]);
                JPushToolUtil.sendToRegistrationId(notificationTitle,msgTitle,msgContent,jPushVO,registrationId);
            }
        }

        return count;
    }

    /**
     * 修改开工投产时间的接口
     * @param zsyzKgxx
     * @return
     */
    @Override
    public int updateZsyzKgxxTc(ZsyzKgxx zsyzKgxx) {
        zsyzKgxx.setUpdateTime(DateUtils.getNowDate());
        return zsyzKgxxMapper.updateZsyzKgxx(zsyzKgxx);
    }

    /**
     * 批量删除招商引资_开工信息
     *
     * @param ids 需要删除的招商引资_开工信息ID
     * @return 结果
     */
    @Override
    public int deleteZsyzKgxxByIds(Long[] ids) {
        return zsyzKgxxMapper.deleteZsyzKgxxByIds(ids);
    }

    /**
     * 删除招商引资_开工信息信息
     *
     * @param id 招商引资_开工信息ID
     * @return 结果
     */
    @Override
    public int deleteZsyzKgxxById(Long id) {
        return zsyzKgxxMapper.deleteZsyzKgxxById(id);
    }
}
