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.ZsyzFj;
import com.ruoyi.system.domain.projectV2.ZsyzLcjl;
import com.ruoyi.system.domain.projectV2.ZsyzQyxx;
import com.ruoyi.system.domain.projectV2.ZsyzSbbzb;
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.ZsyzQyxxMapper;
import com.ruoyi.system.mapper.projectV2.ZsyzSbbzbMapper;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.projectV2.IZsyzQyxxService;
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 ZsyzQyxxServiceImpl implements IZsyzQyxxService {
    @Autowired
    private ZsyzQyxxMapper zsyzQyxxMapper;

    @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 ZsyzQyxx selectZsyzQyxxById(Long id) {
        ZsyzQyxx zsyzQyxx = zsyzQyxxMapper.selectZsyzQyxxById(id);
        if (zsyzQyxx != null) {
            ZsyzFj zsyzFj = new ZsyzFj();
            zsyzFj.setSourceId(zsyzQyxx.getId());
            zsyzFj.setType(TWO);
            List<ZsyzFj> zsyzFjList = zsyzFjMapper.selectZsyzFjList(zsyzFj);
            if (zsyzFjList != null && zsyzFjList.size() > 0) {
                zsyzQyxx.setZsyzFjList(zsyzFjList);
            }
        }
        return zsyzQyxx;
    }

    /**
     * 查询招商引资_签约信息列表
     *
     * @param zsyzQyxx 招商引资_签约信息
     * @return 招商引资_签约信息
     */
    @Override
    public List<ZsyzQyxx> selectZsyzQyxxList(ZsyzQyxx zsyzQyxx) {
        return zsyzQyxxMapper.selectZsyzQyxxList(zsyzQyxx);
    }

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

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

        //发极光推送
        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 zsyzQyxx 招商引资_签约信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateZsyzQyxx(ZsyzQyxx zsyzQyxx) {
        zsyzQyxx.setUpdateBy(SecurityUtils.getLoginUser().getUser().getDept().getDeptName());
        zsyzQyxx.setUpdateTime(DateUtils.getNowDate());
        int count = zsyzQyxxMapper.updateZsyzQyxx(zsyzQyxx);
        //先去判断项目状态
        ZsyzSbbzb zsyzSbbzb = zsyzSbbzbMapper.selectZsyzSbbzbById(zsyzQyxx.getXmId());
        if (zsyzSbbzb == null || (!THR.equals(zsyzSbbzb.getProgress()) && TWO.equals(zsyzSbbzb.getXmStatus()))){
            return 0;
        }
        zsyzSbbzb.setProgress(THR);
        zsyzSbbzb.setXmStatus(ONE);
        zsyzSbbzbMapper.updateZsyzSbbzbProgress(zsyzSbbzb);
        //修改附件
        if (zsyzQyxx.getZsyzFjList() != null && zsyzQyxx.getZsyzFjList().size() > 0) {
            ZsyzFj zsyzFjDelete = new ZsyzFj();
            zsyzFjDelete.setSourceId(zsyzQyxx.getId());
            zsyzFjDelete.setType(TWO);
            //先删除相关附件
            zsyzFjMapper.deleteZsyzFjBySourceId(zsyzFjDelete);
            //再将文件新增进数据库
            List<ZsyzFj> zsyzFjList = zsyzQyxx.getZsyzFjList();
            if (zsyzFjList != null){
                for (ZsyzFj zsyzFj : zsyzFjList) {
                    zsyzFj.setSourceId(zsyzQyxx.getId());
                    zsyzFj.setType(TWO);
                    zsyzFj.setXmId(zsyzQyxx.getXmId());
                    zsyzFj.setXmbh(zsyzQyxx.getXmbh());
                    zsyzFjMapper.insertZsyzFj(zsyzFj);
                }
            }
        }

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

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

        //发极光推送
        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 deleteZsyzQyxxByIds(Long[] ids) {
        return zsyzQyxxMapper.deleteZsyzQyxxByIds(ids);
    }

    /**
     * 删除招商引资_签约信息信息
     *
     * @param id 招商引资_签约信息ID
     * @return 结果
     */
    @Override
    public int deleteZsyzQyxxById(Long id) {
        return zsyzQyxxMapper.deleteZsyzQyxxById(id);
    }
}
