package com.ruoyi.framework.web.service;

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

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
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.fgw.FgwJdapDw;
import com.ruoyi.system.domain.fgw.FgwJdapFj;
import com.ruoyi.system.domain.fgw.FgwXmsb;
import com.ruoyi.system.domain.projectV2.common.SendSmsConfig;
import com.ruoyi.system.domain.vo.JdapVo;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.fgw.FgwJdapDwMapper;
import com.ruoyi.system.mapper.fgw.FgwJdapFjMapper;
import com.ruoyi.system.mapper.fgw.FgwXmsbMapper;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.fgw.FgwJdapMapper;
import com.ruoyi.system.domain.fgw.FgwJdap;
import com.ruoyi.system.service.fgw.IFgwJdapService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 发改委_节点安排Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-11
 */
@Service
public class FgwJdapServiceImpl implements IFgwJdapService {
    @Autowired
    private FgwJdapMapper fgwJdapMapper;
    @Autowired
    private FgwJdapDwMapper fgwJdapDwMapper;
    @Autowired
    private FgwJdapFjMapper fgwJdapFjMapper;
    @Autowired
    private FgwXmsbMapper fgwXmsbMapper;
    @Autowired
    private SysUserMapper userMapper;

    /**
     * 查询发改委_节点安排
     *
     * @param id 发改委_节点安排主键
     * @return 发改委_节点安排
     */
    @Override
    public FgwJdap selectFgwJdapById(Long id) {
        FgwJdap fgwJdap = fgwJdapMapper.selectFgwJdapById(id);
        //查询当前项目的所有人员
        FgwJdapDw fgwJdapDw = new FgwJdapDw();
        fgwJdapDw.setJdapId(id);
        List<FgwJdapDw> fgwJdapDwList = fgwJdapDwMapper.selectFgwJdapDwList(fgwJdapDw);

        List<FgwJdapDw> fgwJdapZrDwList = new ArrayList<>();
        List<FgwJdapDw> fgwJdapXtDwList = new ArrayList<>();
        if (fgwJdapDwList != null && fgwJdapDwList.size() > 0) {
            for (FgwJdapDw jdapDw : fgwJdapDwList) {
                if ("0".equals(jdapDw.getDeptType())) {
                    fgwJdapZrDwList.add(jdapDw);
                } else {
                    fgwJdapXtDwList.add(jdapDw);
                }
            }
        }
        fgwJdap.setFgwJdapZrDwList(fgwJdapZrDwList);
        fgwJdap.setFgwJdapXtDwList(fgwJdapXtDwList);
        //查询附件
        FgwJdapFj fgwJdapFj = new FgwJdapFj();
        fgwJdapFj.setJdapId(id);
        List<FgwJdapFj> fgwJdapFjList = fgwJdapFjMapper.selectFgwJdapFjList(fgwJdapFj);
        fgwJdap.setFgwJdapFjList(fgwJdapFjList);
        return fgwJdap;
    }

    /**
     * 查询发改委_节点安排列表
     *
     * @param fgwJdap 发改委_节点安排
     * @return 发改委_节点安排
     */
    @Override
    public List<FgwJdap> selectFgwJdapList(FgwJdap fgwJdap) {

        List<FgwJdap> fgwJdapList = fgwJdapMapper.selectFgwJdapList(fgwJdap);
        //查询当前项目的所有人员
        FgwJdapDw fgwJdapDw = new FgwJdapDw();
        fgwJdapDw.setXmId(fgwJdap.getXmId());
        List<FgwJdapDw> fgwJdapDwList = fgwJdapDwMapper.selectFgwJdapDwList(fgwJdapDw);
        //查询附件
        FgwJdapFj fgwJdapFj = new FgwJdapFj();
        fgwJdapFj.setXmId(fgwJdap.getXmId());
        List<FgwJdapFj> fgwJdapFjList = fgwJdapFjMapper.selectFgwJdapFjList(fgwJdapFj);
        for (FgwJdap jdap : fgwJdapList) {
            List<FgwJdapDw> fgwJdapZrDwList = new ArrayList<>();
            List<FgwJdapDw> fgwJdapXtDwList = new ArrayList<>();
            List<FgwJdapFj> fgwJdapFjs = new ArrayList<>();
            if (fgwJdapDwList != null && fgwJdapDwList.size() > 0) {
                for (FgwJdapDw jdapDw : fgwJdapDwList) {
                    if (jdap.getId().equals(jdapDw.getJdapId())) {
                        if ("0".equals(jdapDw.getDeptType())) {
                            fgwJdapZrDwList.add(jdapDw);
                        } else {
                            fgwJdapXtDwList.add(jdapDw);
                        }
                    }
                }
            }
            if (fgwJdapFjList != null && fgwJdapFjList.size() > 0) {
                for (FgwJdapFj jdapFj : fgwJdapFjList) {
                    if (jdap.getId().equals(jdapFj.getJdapId())) {
                        fgwJdapFjs.add(jdapFj);
                    }
                }
            }
            jdap.setFgwJdapZrDwList(fgwJdapZrDwList);
            jdap.setFgwJdapXtDwList(fgwJdapXtDwList);
            jdap.setFgwJdapFjList(fgwJdapFjs);
        }

        return fgwJdapList;
    }

    /**
     * 新增发改委_节点安排
     *
     * @param jdapVo 发改委_节点安排
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult insertFgwJdap(JdapVo jdapVo) {
        List<FgwJdap> fgwJdapList = jdapVo.getFgwJdapList();

        //判断新增节点中有没有重复的
        for (FgwJdap fgwJdap : fgwJdapList) {
            String[] t = String.valueOf(fgwJdap.getJdsj()).split("-");
            fgwJdap.setYear(t[0]);
            fgwJdap.setMonth(t[1]);
            int i = 0;
            for (FgwJdap fgwJdapz : fgwJdapList) {
                String[] t1 = String.valueOf(fgwJdapz.getJdsj()).split("-");
                fgwJdapz.setYear(t1[0]);
                fgwJdapz.setMonth(t1[1]);
                if(fgwJdap.getYear().equals(fgwJdapz.getYear()) && fgwJdap.getMonth().equals(fgwJdapz.getMonth())){
                    i++;
                }
            }
            if(i>1){
                return AjaxResult.error("存在相同的月份数据，请检查后重新保存");
            }
        }
        //获取当前年月
        String time = DateUtils.getYearMonth()+"-01";

        for (FgwJdap fgwJdap : fgwJdapList) {
            //截取时间字符串
            if (StringUtils.isNotEmpty(fgwJdap.getJdsj())) {
                String[] t = String.valueOf(fgwJdap.getJdsj()).split("-");

                //判断库里是否存在相同年份和月份的数据
                FgwJdap fgwJdaps = new FgwJdap();
                fgwJdaps.setYear(t[0]);
                fgwJdaps.setMonth(t[1]);
                fgwJdaps.setXmId(fgwJdap.getXmId());
                List<FgwJdap> fgwJdapss = fgwJdapMapper.selectFgwJdapList(fgwJdaps);
                if(fgwJdapss!=null && fgwJdapss.size()>0){
                    return AjaxResult.error("存在相同的月份数据，请检查后重新保存");
                }

                if(DateUtils.daysBetween(fgwJdap.getJdsj(),time)>0){
                    fgwJdap.setSfwc("3");
                }
                fgwJdap.setYear(t[0]);
                fgwJdap.setMonth(t[1]);
                fgwJdap.setCreateTime(DateUtils.getNowDate());
                fgwJdap.setUpdateTime(DateUtils.getNowDate());
                fgwJdapMapper.insertFgwJdap(fgwJdap);

                List<FgwJdapDw> fgwJdapZrDwList = fgwJdap.getFgwJdapZrDwList();
                List<FgwJdapDw> fgwJdapXtDwList = fgwJdap.getFgwJdapXtDwList();


                if (fgwJdapZrDwList != null && fgwJdapZrDwList.size() > 0) {
                    for (FgwJdapDw fgwJdapDw : fgwJdapZrDwList) {
                        fgwJdapDw.setXmId(fgwJdap.getXmId());
                        fgwJdapDw.setJdapId(fgwJdap.getId());
                        fgwJdapDw.setDeptType("0");
                        fgwJdapDw.setCreateTime(DateUtils.getNowDate());
                        fgwJdapDw.setUpdateTime(DateUtils.getNowDate());
                        fgwJdapDwMapper.insertFgwJdapDw(fgwJdapDw);
                    }
                }

                if (fgwJdapXtDwList != null && fgwJdapXtDwList.size() > 0) {
                    for (FgwJdapDw fgwJdapDw : fgwJdapXtDwList) {
                        fgwJdapDw.setXmId(fgwJdap.getXmId());
                        fgwJdapDw.setJdapId(fgwJdap.getId());
                        fgwJdapDw.setDeptType("1");
                        fgwJdapDw.setCreateTime(DateUtils.getNowDate());
                        fgwJdapDw.setUpdateTime(DateUtils.getNowDate());
                        fgwJdapDwMapper.insertFgwJdapDw(fgwJdapDw);
                    }
                }

                List<FgwJdapFj> fgwJdapFjList = fgwJdap.getFgwJdapFjList();
                if (fgwJdapFjList != null && fgwJdapFjList.size() > 0) {
                    for (FgwJdapFj fgwJdapFj : fgwJdapFjList) {
                        fgwJdapFj.setXmId(fgwJdap.getXmId());
                        fgwJdapFj.setJdapId(fgwJdap.getId());
                        fgwJdapFjMapper.insertFgwJdapFj(fgwJdapFj);
                    }
                }
            }
        }
        return AjaxResult.success("保存成功");
    }

    /**
     * 修改发改委_节点安排
     *
     * @param fgwJdap 发改委_节点安排
     * @return 结果
     */
    @Override
    public AjaxResult updateFgwJdap(FgwJdap fgwJdap) {
        //截取时间字符串
        String[] t = String.valueOf(fgwJdap.getJdsj()).split("-");
        fgwJdap.setYear(t[0]);
        fgwJdap.setMonth(t[1]);
        fgwJdap.setUpdateTime(DateUtils.getNowDate());

        //判断库里是否存在相同年份和月份的数据
        FgwJdap fgwJdaps = new FgwJdap();
        fgwJdaps.setYear(t[0]);
        fgwJdaps.setMonth(t[1]);
        fgwJdaps.setXmId(fgwJdap.getXmId());
        List<FgwJdap> fgwJdapList = fgwJdapMapper.selectFgwJdapList(fgwJdaps);
        if(fgwJdapList!=null && fgwJdapList.size()>0){
            if(fgwJdapList.size()==1 && fgwJdapList.get(0).getId().equals(fgwJdap.getId())){
            }else{
                return AjaxResult.error("存在相同的月份数据，请检查后重新保存");
            }

        }


        //先删除人员信息
        fgwJdapDwMapper.deleteFgwJdapDwByJdid(fgwJdap.getId());
        //删除附件
        fgwJdapFjMapper.deleteFgwJdapFjByJdId(fgwJdap.getId());


        List<FgwJdapDw> fgwJdapZrDwList = fgwJdap.getFgwJdapZrDwList();
        List<FgwJdapDw> fgwJdapXtDwList = fgwJdap.getFgwJdapXtDwList();


        if (fgwJdapZrDwList != null && fgwJdapZrDwList.size() > 0) {
            for (FgwJdapDw fgwJdapDw : fgwJdapZrDwList) {
                fgwJdapDw.setXmId(fgwJdap.getXmId());
                fgwJdapDw.setJdapId(fgwJdap.getId());
                fgwJdapDw.setDeptType("0");
                fgwJdapDw.setCreateTime(DateUtils.getNowDate());
                fgwJdapDw.setUpdateTime(DateUtils.getNowDate());
                fgwJdapDwMapper.insertFgwJdapDw(fgwJdapDw);
            }
        }

        if (fgwJdapXtDwList != null && fgwJdapXtDwList.size() > 0) {
            for (FgwJdapDw fgwJdapDw : fgwJdapXtDwList) {
                fgwJdapDw.setXmId(fgwJdap.getXmId());
                fgwJdapDw.setJdapId(fgwJdap.getId());
                fgwJdapDw.setDeptType("1");
                fgwJdapDw.setCreateTime(DateUtils.getNowDate());
                fgwJdapDw.setUpdateTime(DateUtils.getNowDate());
                fgwJdapDwMapper.insertFgwJdapDw(fgwJdapDw);
            }
        }
        List<FgwJdapFj> fgwJdapFjList = fgwJdap.getFgwJdapFjList();
        if (fgwJdapFjList != null && fgwJdapFjList.size() > 0) {
            for (FgwJdapFj fgwJdapFj : fgwJdapFjList) {
                fgwJdapFj.setXmId(fgwJdap.getXmId());
                fgwJdapFj.setJdapId(fgwJdap.getId());
                fgwJdapFjMapper.insertFgwJdapFj(fgwJdapFj);
            }
        }
        int i = fgwJdapMapper.updateFgwJdap(fgwJdap);
        return i>0?AjaxResult.success("操作成功"):AjaxResult.error("操作失败");
    }

    /**
     * 批量删除发改委_节点安排
     *
     * @param ids 需要删除的发改委_节点安排主键
     * @return 结果
     */
    @Override
    public int deleteFgwJdapByIds(Long[] ids) {
        //删除发改委节点
        int i = fgwJdapMapper.deleteFgwJdapByIds(ids);
        //删除人员信息
        fgwJdapDwMapper.deleteFgwJdapDwByJdids(ids);
        //删除附件信息
        fgwJdapFjMapper.deleteFgwJdapFjByJdIds(ids);
        return i;
    }

    /**
     * 删除发改委_节点安排信息
     *
     * @param id 发改委_节点安排主键
     * @return 结果
     */
    @Override
    public int deleteFgwJdapById(Long id) {
        //删除发改委节点
        int i = fgwJdapMapper.deleteFgwJdapById(id);
        //删除人员信息
        fgwJdapDwMapper.deleteFgwJdapDwByJdid(id);
        //删除附件信息
        fgwJdapFjMapper.deleteFgwJdapFjByJdId(id);
        return i;
    }

    @Override
    public int jdsh(String ids, String shyj, String shjg) {
        String []idss = ids.split(",");
        return fgwJdapMapper.updateFgwJdapJdsh(idss, shyj, shjg);
    }

    @Override
    public AjaxResult cb(Long id) {
        //查找节点信息
        FgwJdap fgwJdap = fgwJdapMapper.selectFgwJdapById(id);
        //查询当前项目的所有人员
        FgwJdapDw fgwJdapDw = new FgwJdapDw();
        fgwJdapDw.setJdapId(id);
        List<FgwJdapDw> fgwJdapDwList = fgwJdapDwMapper.selectFgwJdapDwList(fgwJdapDw);
        //查找项目信息
        FgwXmsb fgwXmsb = fgwXmsbMapper.selectFgwXmsbById(fgwJdap.getXmId());
        //将消息推送给相关人员
        List<SendSmsConfig> sendSmsConfigs = new ArrayList<>();
        //发送短信
        List<Long> deptIdList = new ArrayList<>();
        for (FgwJdapDw jdapDw : fgwJdapDwList) {
            deptIdList.add(jdapDw.getDeptId());
        }

        List<SysUser> sysUsers = userMapper.selectUserByDeptIdList(deptIdList);
        SysUser sysUse = userMapper.selectUserById(fgwXmsb.getUserId());
        sysUsers.add(sysUse);
        if (sysUsers != null) {
            //发极光推送
            String notificationTitle = "消息通知";
            String msgTitle = "节点催办";
            String msgContent = "您有一条节点催办信息，请及时处理！节点时间："+fgwJdap.getJdsj();
            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);
            }

            for (SysUser sysUser : sysUsers) {
                if (StringUtils.isNotEmpty(sysUser.getPhonenumber())) {
                    SendSmsConfig sendSmsConfig = new SendSmsConfig();
                    sendSmsConfig.setPhone(sysUser.getPhonenumber());
                    sendSmsConfigs.add(sendSmsConfig);
                }
            }
            if (sendSmsConfigs.size() > 0) {
                //TOdo 模板
                //AsyncManager.me().execute(AsyncFactory.sendSms(sendSmsConfigs, "SMS_272470945"));
            }
        }

        return AjaxResult.success("成功");
    }

    /**
     * 发改委_项目详情附件_催报
     * id 项目id
     * time 催办时限
     * name 附件类型名称
     */
    @Override
    public AjaxResult fjcb(Long id, String time, String name) {

        //查找项目信息
        FgwXmsb fgwXmsb = fgwXmsbMapper.selectFgwXmsbById(id);
        //将消息推送给相关人员
        List<SendSmsConfig> sendSmsConfigs = new ArrayList<>();
        //发送短信
        List<SysUser> sysUsers = new ArrayList<>();
        SysUser sysUse = userMapper.selectUserById(fgwXmsb.getUserId());
        sysUsers.add(sysUse);
        if (sysUsers != null) {
            //发极光推送
            String notificationTitle = "消息通知";
            String msgTitle = "附件催办";
            String msgContent = "您有一条项目文件催办信息，请及时处理！附件类型："+name+"，截止时间："+time;
            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);
            }

            for (SysUser sysUser : sysUsers) {
                if (StringUtils.isNotEmpty(sysUser.getPhonenumber())) {
                    SendSmsConfig sendSmsConfig = new SendSmsConfig();
                    sendSmsConfig.setPhone(sysUser.getPhonenumber());
                    sendSmsConfigs.add(sendSmsConfig);
                }
            }
            if (sendSmsConfigs.size() > 0) {
                //TOdo 模板
                //AsyncManager.me().execute(AsyncFactory.sendSms(sendSmsConfigs, "SMS_272470945"));
            }
        }

        return AjaxResult.success("成功");
    }

    @Override
    public AjaxResult sycb(List<Long> idList) {
        if(idList!=null && idList.size()>0){
            //将消息推送给相关人员
            List<SendSmsConfig> sendSmsConfigs = new ArrayList<>();
            //查询所有项目
            List<FgwXmsb> fgwXmsbList = fgwXmsbMapper.selectFgwXmsbByIdList(idList);
            List<Long> userIdList = fgwXmsbList.stream().map(FgwXmsb::getUserId).collect(Collectors.toList());
            List<SysUser> sysUsers = userMapper.selectUserByUserIdList(userIdList);
            if (sysUsers != null) {
                //发极光推送
                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);
                }

                for (SysUser sysUser : sysUsers) {
                    if (StringUtils.isNotEmpty(sysUser.getPhonenumber())) {
                        SendSmsConfig sendSmsConfig = new SendSmsConfig();
                        sendSmsConfig.setPhone(sysUser.getPhonenumber());
                        sendSmsConfigs.add(sendSmsConfig);
                    }
                }
                if (sendSmsConfigs.size() > 0) {
                    //TOdo 模板
                    //AsyncManager.me().execute(AsyncFactory.sendSms(sendSmsConfigs, "SMS_272470945"));
                }
            }
        }

        return AjaxResult.success("成功");
    }
}
