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.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.ProposalInfo;
import com.ruoyi.system.domain.ProposalUnitReply;
import com.ruoyi.system.domain.ZxFj;
import com.ruoyi.system.domain.sqmy.SqmyInfo;
import com.ruoyi.system.domain.sqmy.SqmyUnitReply;
import com.ruoyi.system.mapper.SqmyUnitReplyMapper;
import com.ruoyi.system.mapper.ZxFjMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SqmyInfoMapper;
import com.ruoyi.system.service.ISqmyInfoService;

/**
 * 社情民意信息Service业务层处理
 * 
 * @author boman
 * @date 2024-03-14
 */
@Service
public class SqmyInfoServiceImpl implements ISqmyInfoService 
{
    @Autowired
    private SqmyInfoMapper sqmyInfoMapper;
    @Autowired
    private ZxFjMapper zxFjMapper;
    @Autowired
    private SqmyUnitReplyMapper sqmyUnitReplyMapper;

    /**
     * 查询社情民意信息
     * 
     * @param sqmyId 社情民意信息主键
     * @return 社情民意信息
     */
    @Override
    public SqmyInfo selectSqmyInfoBySqmyId(Long sqmyId)
    {
        SqmyInfo sqmyInfo = sqmyInfoMapper.selectSqmyInfoBySqmyId(sqmyId);
        //查询附件
        ZxFj zxFj = new ZxFj();
        zxFj.setMainId(sqmyId);
        List<ZxFj> zxFjList = zxFjMapper.selectZxFjList(zxFj);
        if(zxFjList!=null && zxFjList.size()>0){
            sqmyInfo.setZxFjList(zxFjList);
        }
        //查询答复
        SqmyUnitReply sqmyUnitReply = new SqmyUnitReply();
        sqmyUnitReply.setSqmyId(sqmyId);
        List<SqmyUnitReply> sqmyUnitReplyList = sqmyUnitReplyMapper.selectSqmyUnitReplyList(sqmyUnitReply);
        if(sqmyUnitReplyList!=null && sqmyUnitReplyList.size()>0){
            for (SqmyUnitReply unitReply : sqmyUnitReplyList) {
                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);
            }
            sqmyInfo.setSqmyUnitReplyList(sqmyUnitReplyList);
        }

        return sqmyInfo;
    }

    /**
     * 查询社情民意信息列表
     * 
     * @param sqmyInfo 社情民意信息
     * @return 社情民意信息
     */
    @Override
    public List<SqmyInfo> selectSqmyInfoList(SqmyInfo sqmyInfo)
    {
        return sqmyInfoMapper.selectSqmyInfoList(sqmyInfo);
    }

    /**
     * 列表页统计
     */
    @Override
    public AjaxResult statistics(SqmyInfo sqmyInfo) {
        Map<String,Object> map = new HashMap<>();
        List<SqmyInfo> sqmyInfos = sqmyInfoMapper.selectSqmyInfoList(sqmyInfo);
        int totle = sqmyInfos.size();
        //待审核
        int dsh = 0;
        //已审核
        int ysh = 0;
        for (SqmyInfo info : sqmyInfos) {
            if("1".equals(info.getSqmyProgress())){
                dsh++;
            }else {
                ysh++;
            }
        }
        //待交办
        int djb = 0;
        //以交办
        int yjb = 0;
        //已办结
        int ybj = 0;
        //未办结
        int wbj = 0;
        for (SqmyInfo info : sqmyInfos) {
            if(Integer.parseInt(info.getSatisfaction())>4){
                yjb++;
            }
            if(Integer.parseInt(info.getSatisfaction())<=4){
                djb++;
            }
            if("8".equals(info.getSqmyProgress())) {
                ybj++;
            }
            if(!"8".equals(info.getSqmyProgress())) {
                wbj++;
            }
        }
        map.put("totle",totle);
        map.put("dsh",dsh);
        map.put("ysh",ysh);

        map.put("djb",djb);
        map.put("djb",djb);
        map.put("ybj",ybj);
        map.put("wbj",wbj);

        return AjaxResult.success(map);
    }

    /**
     * 新增社情民意信息
     * 
     * @param sqmyInfo 社情民意信息
     * @return 结果
     */
    @Override
    public int insertSqmyInfo(SqmyInfo sqmyInfo)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        sqmyInfo.setSqmyUserId(user.getUserId());
        sqmyInfo.setSqmyProgress("1");
        sqmyInfo.setCreateTime(DateUtils.getNowDate());
        int i = sqmyInfoMapper.insertSqmyInfo(sqmyInfo);
        if(sqmyInfo.getZxFjList()!=null && sqmyInfo.getZxFjList().size()>0){
            for (ZxFj zxFj : sqmyInfo.getZxFjList()) {
                zxFj.setMainId(sqmyInfo.getSqmyId());
                zxFj.setSourceId(sqmyInfo.getSqmyId());
                zxFj.setType("2");
                zxFj.setStytle("4");
                zxFjMapper.insertZxFj(zxFj);
            }
        }

        return i;
    }

    /**
     * 修改社情民意信息
     * 
     * @param sqmyInfo 社情民意信息
     * @return 结果
     */
    @Override
    public int updateSqmyInfo(SqmyInfo sqmyInfo)
    {
        //删除附件
        zxFjMapper.deleteZxFjBySourceId(sqmyInfo.getSqmyId());
        if(sqmyInfo.getZxFjList()!=null && sqmyInfo.getZxFjList().size()>0){
            for (ZxFj zxFj : sqmyInfo.getZxFjList()) {
                zxFj.setMainId(sqmyInfo.getSqmyId());
                zxFj.setSourceId(sqmyInfo.getSqmyId());
                zxFj.setType("2");
                zxFj.setStytle("4");
                zxFjMapper.insertZxFj(zxFj);
            }
        }
        sqmyInfo.setUpdateTime(DateUtils.getNowDate());
        return sqmyInfoMapper.updateSqmyInfo(sqmyInfo);
    }

    /**
     * 批量删除社情民意信息
     * 
     * @param sqmyIds 需要删除的社情民意信息主键
     * @return 结果
     */
    @Override
    public int deleteSqmyInfoBySqmyIds(Long[] sqmyIds)
    {
        return sqmyInfoMapper.deleteSqmyInfoBySqmyIds(sqmyIds);
    }

    /**
     * 删除社情民意信息信息
     * 
     * @param sqmyId 社情民意信息主键
     * @return 结果
     */
    @Override
    public int deleteSqmyInfoBySqmyId(Long sqmyId)
    {
        return sqmyInfoMapper.deleteSqmyInfoBySqmyId(sqmyId);
    }

    /***
     * 是否立案
     */
    @Override
    public AjaxResult isRecord(SqmyInfo sqmyInfo) {
        //不立案，指定答复单位
        if("1".equals(sqmyInfo.getIsRecord())){
            sqmyInfo.setSqmyProgress("1");
            List<SqmyUnitReply> sqmyUnitReplyList = sqmyInfo.getSqmyUnitReplyList();
            for (SqmyUnitReply sqmyUnitReply : sqmyUnitReplyList) {
                sqmyUnitReply.setSqmyId(sqmyInfo.getSqmyId());
                sqmyUnitReply.setType("1");
                sqmyUnitReply.setStartTime(DateUtils.getNowDate());
                sqmyUnitReplyMapper.insertSqmyUnitReply(sqmyUnitReply);
            }
        }else{
            sqmyInfo.setSqmyProgress("2");
        }
        sqmyInfo.setUpdateTime(DateUtils.getNowDate());
        int i = sqmyInfoMapper.updateSqmyInfo(sqmyInfo);
        return i > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /***
     * 交办
     */
    @Override
    public AjaxResult assign(SqmyInfo sqmyInfo) {
        List<SqmyUnitReply> sqmyUnitReplyList = sqmyInfo.getSqmyUnitReplyList();
        SqmyUnitReply zbUnitReply = sqmyInfo.getZbUnitReply();
        if(sqmyUnitReplyList!=null && sqmyUnitReplyList.size()>0){
            for (SqmyUnitReply sqmyUnitReply : sqmyUnitReplyList) {
                sqmyUnitReply.setType("2");
                sqmyUnitReply.setSqmyId(sqmyInfo.getSqmyId());
                sqmyUnitReply.setStartTime(DateUtils.getNowDate());
                sqmyUnitReplyMapper.insertSqmyUnitReply(sqmyUnitReply);
            }
        }
        zbUnitReply.setSqmyId(sqmyInfo.getSqmyId());
        zbUnitReply.setType("1");
        zbUnitReply.setStartTime(DateUtils.getNowDate());
        sqmyUnitReplyMapper.insertSqmyUnitReply(zbUnitReply);

        sqmyInfo.setSqmyProgress("3");
        sqmyInfo.setUpdateTime(DateUtils.getNowDate());
        int i = sqmyInfoMapper.updateSqmyInfo(sqmyInfo);
        return i > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /***
     * 委员意见
     */
    @Override
    public AjaxResult idea(SqmyInfo sqmyInfo) {
        sqmyInfo.setSqmyProgress("5");
        sqmyInfo.setUpdateTime(DateUtils.getNowDate());
        int i = sqmyInfoMapper.updateSqmyInfo(sqmyInfo);
        return i > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /***
     * 政协意见
     */
    @Override
    public AjaxResult zxIdea(SqmyInfo sqmyInfo) {
        sqmyInfo.setSqmyProgress("5");
        sqmyInfo.setUpdateTime(DateUtils.getNowDate());
        int i = sqmyInfoMapper.updateSqmyInfo(sqmyInfo);
        return i > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /***
     * 推荐重点
     */
    @Override
    public AjaxResult keyPoint(SqmyInfo sqmyInfo) {
        sqmyInfo.setUpdateTime(DateUtils.getNowDate());
        int i = sqmyInfoMapper.updateSqmyInfo(sqmyInfo);
        return i > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /***
     * 推荐优秀
     */
    @Override
    public AjaxResult outstanding(SqmyInfo sqmyInfo) {
        sqmyInfo.setUpdateTime(DateUtils.getNowDate());
        int i = sqmyInfoMapper.updateSqmyInfo(sqmyInfo);
        return i > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /***
     * 交办审核
     */
    @Override
    public AjaxResult assignsh(SqmyInfo sqmyInfo) {
        sqmyInfo.setUpdateTime(DateUtils.getNowDate());
        int i = sqmyInfoMapper.updateSqmyInfo(sqmyInfo);
        return i > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    public List<SqmyInfo> unitList(SqmyInfo sqmyInfo) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        sqmyInfo.setDeptId(user.getDeptId());
        return sqmyInfoMapper.selectSqmyInfoListByUnit(sqmyInfo);
    }

}
