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.ZsyzLcjlMapper;
import com.ruoyi.system.mapper.projectV2.ZsyzSbbzbMapper;
import com.ruoyi.system.mapper.projectV2.ZsyzTcxxMapper;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.projectV2.IZsyzTcxxService;
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 ZsyzTcxxServiceImpl implements IZsyzTcxxService {
    @Autowired
    private ZsyzTcxxMapper zsyzTcxxMapper;

    @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 ZsyzTcxx selectZsyzTcxxById(Long id) {
        ZsyzTcxx zsyzTcxx = zsyzTcxxMapper.selectZsyzTcxxById(id);
        if (zsyzTcxx != null) {
            ZsyzFj zsyzFj = new ZsyzFj();
            zsyzFj.setSourceId(zsyzTcxx.getId());
            zsyzFj.setType(FOR);
            List<ZsyzFj> zsyzFjList = zsyzFjMapper.selectZsyzFjList(zsyzFj);
            if (zsyzFjList != null && zsyzFjList.size() > 0) {
                zsyzTcxx.setZsyzFjList(zsyzFjList);
            }
        }
        return zsyzTcxx;
    }

    /**
     * 查询招商引资_投产信息列表
     *
     * @param zsyzTcxx 招商引资_投产信息
     * @return 招商引资_投产信息
     */
    @Override
    public List<ZsyzTcxx> selectZsyzTcxxList(ZsyzTcxx zsyzTcxx) {
        return zsyzTcxxMapper.selectZsyzTcxxList(zsyzTcxx);
    }

    /**
     * 新增招商引资_投产信息
     *
     * @param zsyzTcxx 招商引资_投产信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertZsyzTcxx(ZsyzTcxx zsyzTcxx) {

        zsyzTcxx.setCreateTime(DateUtils.getNowDate());
        int count = zsyzTcxxMapper.insertZsyzTcxx(zsyzTcxx);
        //先去判断项目状态
        ZsyzSbbzb zsyzSbbzb = zsyzSbbzbMapper.selectZsyzSbbzbById(zsyzTcxx.getXmId());
        if (zsyzSbbzb == null || (!FOR.equals(zsyzSbbzb.getProgress()) && !TWO.equals(zsyzSbbzb.getXmStatus()))) {
            return 0;
        }
        zsyzSbbzb.setProgress(FIV);
        zsyzSbbzb.setXmStatus(ONE);
        zsyzSbbzbMapper.updateZsyzSbbzbProgress(zsyzSbbzb);
        //保存附件信息
        List<ZsyzFj> zsyzFjList = zsyzTcxx.getZsyzFjList();
        if (zsyzFjList != null){
            for (ZsyzFj zsyzFj : zsyzFjList) {
                zsyzFj.setSourceId(zsyzTcxx.getId());
                zsyzFj.setType(FOR);
                zsyzFj.setXmId(zsyzTcxx.getXmId());
                zsyzFj.setXmbh(zsyzTcxx.getXmbh());
                zsyzFjMapper.insertZsyzFj(zsyzFj);
            }
        }
        //插入流程记录表
        SysDept dept = SecurityUtils.getLoginUser().getUser().getDept();
        ZsyzLcjl zsyzLcjl = new ZsyzLcjl();
        zsyzLcjl.setXmId(zsyzSbbzb.getId());
        zsyzLcjl.setXmmc(zsyzTcxx.getXmmc());
        zsyzLcjl.setXmbh(zsyzTcxx.getXmbh());
        zsyzLcjl.setDeptId(dept.getDeptId());
        String deptName = dept.getDeptName();
        zsyzLcjl.setDeptName(deptName);
        zsyzLcjl.setProgress(FIV);
        zsyzLcjl.setXmStatus(ONE);
        zsyzLcjlMapper.insertZsyzLcjl(zsyzLcjl);
        //发送短信
        SendSmsConfig sendSmsConfig = new SendSmsConfig();
        sendSmsConfig.setProgress(FOR);
        List<SendSmsConfig> sendSmsConfigs = sendSmsConfigService.selectSendSmsConfigList(sendSmsConfig);
        if (sendSmsConfigs != null){
            AsyncManager.me().execute(AsyncFactory.sendSms(sendSmsConfigs,"SMS_271575058"));
        }

        //发极光推送
        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 zsyzTcxx 招商引资_投产信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateZsyzTcxx(ZsyzTcxx zsyzTcxx) {

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

        //插入流程记录表
        SysDept dept = SecurityUtils.getLoginUser().getUser().getDept();
        ZsyzLcjl zsyzLcjl = new ZsyzLcjl();
        zsyzLcjl.setXmId(zsyzSbbzb.getId());
        zsyzLcjl.setXmmc(zsyzTcxx.getXmmc());
        zsyzLcjl.setXmbh(zsyzTcxx.getXmbh());
        zsyzLcjl.setDeptId(dept.getDeptId());
        String deptName = dept.getDeptName();
        zsyzLcjl.setDeptName(deptName);
        zsyzLcjl.setProgress(FIV);
        zsyzLcjl.setXmStatus(ONE);
        zsyzLcjlMapper.insertZsyzLcjl(zsyzLcjl);
        //发送短信
        SendSmsConfig sendSmsConfig = new SendSmsConfig();
        sendSmsConfig.setProgress(FOR);
        List<SendSmsConfig> sendSmsConfigs = sendSmsConfigService.selectSendSmsConfigList(sendSmsConfig);
        if (sendSmsConfigs != null){
            AsyncManager.me().execute(AsyncFactory.sendSms(sendSmsConfigs,"SMS_271575058"));
        }
        //发极光推送
        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 ids 需要删除的招商引资_投产信息ID
     * @return 结果
     */
    @Override
    public int deleteZsyzTcxxByIds(Long[] ids) {
        return zsyzTcxxMapper.deleteZsyzTcxxByIds(ids);
    }

    /**
     * 删除招商引资_投产信息信息
     *
     * @param id 招商引资_投产信息ID
     * @return 结果
     */
    @Override
    public int deleteZsyzTcxxById(Long id) {
        return zsyzTcxxMapper.deleteZsyzTcxxById(id);
    }
}
