package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.AjaxResult;
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.StringUtils;
import com.ruoyi.system.domain.ProposalUnitReply;
import com.ruoyi.system.domain.ProposalUser;
import com.ruoyi.system.domain.ZxFj;
import com.ruoyi.system.mapper.ProposalUnitReplyMapper;
import com.ruoyi.system.mapper.ProposalUserMapper;
import com.ruoyi.system.mapper.ZxFjMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ProposalInfoMapper;
import com.ruoyi.system.domain.ProposalInfo;
import com.ruoyi.system.service.IProposalInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 提案信息Service业务层处理
 * 
 * @author boman
 * @date 2024-03-07
 */
@Service
public class ProposalInfoServiceImpl implements IProposalInfoService 
{
    @Autowired
    private ProposalInfoMapper proposalInfoMapper;
    @Autowired
    private ProposalUserMapper proposalUserMapper;
    @Autowired
    private ZxFjMapper zxFjMapper;
    @Autowired
    private ProposalUnitReplyMapper proposalUnitReplyMapper;

    /**
     * 查询提案信息
     * 
     * @param proposalId 提案信息主键
     * @return 提案信息
     */
    @Override
    public ProposalInfo selectProposalInfoByProposalId(Long proposalId)
    {
        ProposalInfo proposalInfo = proposalInfoMapper.selectProposalInfoByProposalId(proposalId);
        //查询附件
        ZxFj zxFj = new ZxFj();
        zxFj.setMainId(proposalId);
        List<ZxFj> zxFjList = zxFjMapper.selectZxFjList(zxFj);
        if(zxFjList!=null && zxFjList.size()>0){
            proposalInfo.setZxFjList(zxFjList);
        }
        //查询人员
        ProposalUser proposalUser = new ProposalUser();
        proposalUser.setProposalId(proposalId);
        List<ProposalUser> proposalUserList = proposalUserMapper.selectProposalUserList(proposalUser);
        if(proposalUserList!=null && proposalUserList.size()>0){
            proposalInfo.setProposalUserList(proposalUserList);
        }
        //查询答复
        ProposalUnitReply proposalUnitReply = new ProposalUnitReply();
        proposalUnitReply.setProposalId(proposalId);
        List<ProposalUnitReply> proposalUnitReplyList = proposalUnitReplyMapper.selectProposalUnitReplyList(proposalUnitReply);
        if(proposalUnitReplyList!=null && proposalUnitReplyList.size()>0){
            for (ProposalUnitReply unitReply : proposalUnitReplyList) {
                List<ZxFj> dfFjList = new ArrayList<>();
                //查询答复附件
                if(zxFjList!=null && zxFjList.size()>0){
                    for (ZxFj fj : zxFjList) {
                        if(fj.getStytle().equals(unitReply.getType())){
                            dfFjList.add(fj);
                        }
                    }
                }
                unitReply.setZxFjList(dfFjList);
            }
            proposalInfo.setProposalUnitReplyList(proposalUnitReplyList);
        }
        return proposalInfo;
    }

    /**
     * 查询提案信息列表
     * 
     * @param proposalInfo 提案信息
     * @return 提案信息
     */
    @Override
    public List<ProposalInfo> selectProposalInfoList(ProposalInfo proposalInfo)
    {
        return proposalInfoMapper.selectProposalInfoList(proposalInfo);
    }

    /**
     * 列表页统计
     */
    @Override
    public AjaxResult statistics(ProposalInfo proposalInfo) {
        Map<String,Object> map = new HashMap<>();
        List<ProposalInfo> proposalInfos = proposalInfoMapper.selectProposalInfoList(proposalInfo);
        int totle = proposalInfos.size();
        //待审查
        int dsc = 0;
        //以立案
        int yla = 0;
        //不予立案
        int byla = 0;
        for (ProposalInfo info : proposalInfos) {
            if("1".equals(info.getProposalProgress())){
                dsc++;
            }else if("3".equals(info.getProposalProgress())){
                byla++;
            }else if(!"0".equals(info.getProposalProgress())){
                yla++;
            }
        }
        //办理中
        int blz = 0;
        //不满意
        int bmy = 0;
        //已办结
        int ybj = 0;
        //未办结
        int wbj = 0;
        //待交办
        int djb = 0;
        //以交办
        int yjb = 0;

        for (ProposalInfo info : proposalInfos) {
            if(!"1".equals(info.getProposalProgress()) && !"3".equals(info.getProposalProgress()) && !"8".equals(info.getProposalProgress())){
                blz++;
            }
            if(StringUtils.isNotEmpty(info.getSatisfaction()) && "0".equals(info.getSatisfaction())){
                bmy++;
            }
            if("8".equals(info.getProposalProgress())) {
                ybj++;
            }
            if(!"8".equals(info.getProposalProgress())) {
                wbj++;
            }
            if(Integer.parseInt(info.getSatisfaction())>4){
                yjb++;
            }
            if(Integer.parseInt(info.getSatisfaction())<=4){
                djb++;
            }
        }


        map.put("totle",totle);
        map.put("dsc",dsc);
        map.put("yla",yla);
        map.put("byla",byla);

        map.put("blz",blz);
        map.put("bmy",bmy);
        map.put("ybj",ybj);
        map.put("wbj",wbj);
        map.put("yjb",yjb);
        map.put("djb",djb);

        return AjaxResult.success(map);
    }


    /**
     * 新增提案信息
     * 
     * @param proposalInfo 提案信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertProposalInfo(ProposalInfo proposalInfo)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        proposalInfo.setProposalUserId(user.getUserId());
        proposalInfo.setProposalProgress("1");
        proposalInfo.setCreateTime(DateUtils.getNowDate());
        int i = proposalInfoMapper.insertProposalInfo(proposalInfo);
        if(proposalInfo.getProposalUserList()!=null && proposalInfo.getProposalUserList().size()>0){
            for (ProposalUser proposalUser : proposalInfo.getProposalUserList()) {
                if(proposalUser.getType().equals("1")){
                    proposalUser.setIsAgree("1");
                }
                proposalUser.setProposalId(proposalInfo.getProposalId());
                proposalUserMapper.insertProposalUser(proposalUser);
            }
        }else {
            ProposalUser proposalUser = new ProposalUser();
            proposalUser.setUserId(user.getUserId());
            proposalUser.setProposalId(proposalInfo.getProposalId());
            proposalUser.setName(proposalInfo.getProposalName());
            proposalUser.setType("1");
            proposalUser.setIsAgree("1");
            proposalUserMapper.insertProposalUser(proposalUser);

        }
        if(proposalInfo.getZxFjList()!=null && proposalInfo.getZxFjList().size()>0){
            for (ZxFj zxFj : proposalInfo.getZxFjList()) {
                zxFj.setMainId(proposalInfo.getProposalId());
                zxFj.setSourceId(proposalInfo.getProposalId());
                zxFj.setType("1");
                zxFj.setStytle("4");
                zxFjMapper.insertZxFj(zxFj);
            }
        }
        return i;
    }

    /**
     * 修改提案信息
     * 
     * @param proposalInfo 提案信息
     * @return 结果
     */
    @Override
    public int updateProposalInfo(ProposalInfo proposalInfo)
    {
        //删除附件
        zxFjMapper.deleteZxFjBySourceId(proposalInfo.getProposalId());
        //删除人员
        proposalUserMapper.deleteProposalUserByProposalId(proposalInfo.getProposalId());
        proposalInfo.setUpdateTime(DateUtils.getNowDate());
        if(proposalInfo.getProposalUserList()!=null && proposalInfo.getProposalUserList().size()>0){
            for (ProposalUser proposalUser : proposalInfo.getProposalUserList()) {
                proposalUser.setProposalId(proposalInfo.getProposalId());
                proposalUserMapper.insertProposalUser(proposalUser);
            }
        }
        if(proposalInfo.getZxFjList()!=null && proposalInfo.getZxFjList().size()>0){
            for (ZxFj zxFj : proposalInfo.getZxFjList()) {
                zxFj.setMainId(proposalInfo.getProposalId());
                zxFj.setSourceId(proposalInfo.getProposalId());
                zxFj.setType("1");
                zxFj.setStytle("4");
                zxFjMapper.insertZxFj(zxFj);
            }
        }
        return proposalInfoMapper.updateProposalInfo(proposalInfo);
    }

    /**
     * 批量删除提案信息
     * 
     * @param proposalIds 需要删除的提案信息主键
     * @return 结果
     */
    @Override
    public int deleteProposalInfoByProposalIds(Long[] proposalIds)
    {
        return proposalInfoMapper.deleteProposalInfoByProposalIds(proposalIds);
    }

    /**
     * 删除提案信息信息
     * 
     * @param proposalId 提案信息主键
     * @return 结果
     */
    @Override
    public int deleteProposalInfoByProposalId(Long proposalId)
    {
        return proposalInfoMapper.deleteProposalInfoByProposalId(proposalId);
    }

    /***
     * 是否立案
     */
    @Override
    @Transactional
    public AjaxResult isRecord(ProposalInfo proposalInfo) {
        //不立案，指定答复单位
        if("1".equals(proposalInfo.getIsRecord())){
            proposalInfo.setProposalProgress("1");
            List<ProposalUnitReply> proposalUnitReplyList = proposalInfo.getProposalUnitReplyList();
            for (ProposalUnitReply proposalUnitReply : proposalUnitReplyList) {
                proposalUnitReply.setProposalId(proposalInfo.getProposalId());
                proposalUnitReply.setType("1");
                proposalUnitReply.setStartTime(DateUtils.getNowDate());
                proposalUnitReplyMapper.insertProposalUnitReply(proposalUnitReply);
            }
        }else{
            proposalInfo.setProposalProgress("2");
        }
        proposalInfo.setUpdateTime(DateUtils.getNowDate());
        int i = proposalInfoMapper.updateProposalInfo(proposalInfo);
        return i > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /***
     * 提案交办
     */
    @Override
    @Transactional
    public AjaxResult assign(ProposalInfo proposalInfo) {
        List<ProposalUnitReply> proposalUnitReplyList = proposalInfo.getProposalUnitReplyList();
        ProposalUnitReply zbUnitReply = proposalInfo.getZbUnitReply();

        if(proposalUnitReplyList!=null && proposalUnitReplyList.size()>0){
            for (ProposalUnitReply proposalUnitReply : proposalUnitReplyList) {
                proposalUnitReply.setType("2");
                proposalUnitReply.setProposalId(proposalInfo.getProposalId());
                proposalUnitReply.setStartTime(DateUtils.getNowDate());
                proposalUnitReplyMapper.insertProposalUnitReply(proposalUnitReply);
            }
        }
        zbUnitReply.setProposalId(proposalInfo.getProposalId());
        zbUnitReply.setType("1");
        zbUnitReply.setStartTime(DateUtils.getNowDate());
        proposalUnitReplyMapper.insertProposalUnitReply(zbUnitReply);

        proposalInfo.setProposalProgress("3");
        proposalInfo.setUpdateTime(DateUtils.getNowDate());
        int i = proposalInfoMapper.updateProposalInfo(proposalInfo);
        return i > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    public AjaxResult idea(ProposalInfo proposalInfo) {
        proposalInfo.setProposalProgress("5");
        proposalInfo.setUpdateTime(DateUtils.getNowDate());
        int i = proposalInfoMapper.updateProposalInfo(proposalInfo);
        return i > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    public AjaxResult keyPoint(ProposalInfo proposalInfo) {
        proposalInfo.setUpdateTime(DateUtils.getNowDate());
        int i = proposalInfoMapper.updateProposalInfo(proposalInfo);
        return i > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    public AjaxResult outstanding(ProposalInfo proposalInfo) {
        proposalInfo.setUpdateTime(DateUtils.getNowDate());
        int i = proposalInfoMapper.updateProposalInfo(proposalInfo);
        return i > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    public List<ProposalInfo> jointlyList(ProposalInfo proposalInfo) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        proposalInfo.setProposalUserId(user.getUserId());
        proposalInfo.setType("2");
        return proposalInfoMapper.selectProposalInfoListByUser(proposalInfo);
    }

    @Override
    public AjaxResult zxIdea(ProposalInfo proposalInfo) {
        proposalInfo.setProposalProgress("5");
        proposalInfo.setUpdateTime(DateUtils.getNowDate());
        int i = proposalInfoMapper.updateProposalInfo(proposalInfo);
        return i > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    public AjaxResult assignsh(ProposalInfo proposalInfo) {
        proposalInfo.setUpdateTime(DateUtils.getNowDate());
        int i = proposalInfoMapper.updateProposalInfo(proposalInfo);
        return i > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    public List<ProposalInfo> unitList(ProposalInfo proposalInfo) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        proposalInfo.setDeptId(user.getDeptId());
        return proposalInfoMapper.selectProposalInfoListByUnit(proposalInfo);
    }

}
