package com.yanzhu.jh.project.service.impl;

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

import com.ruoyi.common.enums.PublicStateEnum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.FlowTaskEntity;
import com.ruoyi.system.mapper.FlowBusinessKeyMapper;
import com.yanzhu.jh.project.domain.*;
import com.yanzhu.jh.project.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yanzhu.jh.project.service.ISurProjectAuditinfoService;
import com.ruoyi.common.utils.SecurityUtils;
/**
 * 项目审核信息Service业务层处理
 * 
 * @author JiangYuQi
 * @date 2023-09-20
 */
@Service
public class SurProjectAuditinfoServiceImpl implements ISurProjectAuditinfoService 
{
    @Autowired
    private SurProjectAuditinfoMapper surProjectAuditinfoMapper;

    @Autowired
    private FlowBusinessKeyMapper flowBusinessKeyMapper;

    @Autowired
    private SurProjectCheckingMapper surProjectCheckingMapper;

    @Autowired
    private SurProjectCheckDetectionMapper surProjectCheckDetectionMapper;

    @Autowired
    private SurProjectMaterialSealMapper surProjectMaterialSealMapper;

    @Autowired
    private SurProjectMeasureMapper surProjectMeasureMapper;

    /**
     * 查询项目审核信息
     * 
     * @param id 项目审核信息主键
     * @return 项目审核信息
     */
    @Override
    public SurProjectAuditinfo selectSurProjectAuditinfoById(Long id)
    {
        return surProjectAuditinfoMapper.selectSurProjectAuditinfoById(id);
    }

    /**
     * 查询项目审核信息列表
     * 
     * @param surProjectAuditinfo 项目审核信息
     * @return 项目审核信息
     */
    @Override
    public List<SurProjectAuditinfo> selectSurProjectAuditinfoList(SurProjectAuditinfo surProjectAuditinfo)
    {
        return surProjectAuditinfoMapper.selectSurProjectAuditinfoList(surProjectAuditinfo);
    }

    /**
     * 查询项目审核信息列表
     *
     * @param surProjectAuditinfo 项目审核信息
     * @return 项目审核信息集合
     */
    @Override
    public List<Map<String, Object>> selectProjectAuditinfoList(SurProjectAuditinfo surProjectAuditinfo){
        return surProjectAuditinfoMapper.selectProjectAuditinfoList(surProjectAuditinfo);
    }
    /**
     * 新增项目审核信息
     * 
     * @param surProjectAuditinfo 项目审核信息
     * @return 结果
     */
    @Override
    public int insertSurProjectAuditinfo(SurProjectAuditinfo surProjectAuditinfo)
    {
        surProjectAuditinfo.setCreateBy(SecurityUtils.getUsername());
        surProjectAuditinfo.setCreateTime(DateUtils.getNowDate());
        return surProjectAuditinfoMapper.insertSurProjectAuditinfo(surProjectAuditinfo);
    }

    /**
     * 修改项目审核信息
     * 
     * @param surProjectAuditinfo 项目审核信息
     * @return 结果
     */
    @Override
    public int updateSurProjectAuditinfo(SurProjectAuditinfo surProjectAuditinfo)
    {
        surProjectAuditinfo.setUpdateBy(SecurityUtils.getUsername());
        surProjectAuditinfo.setUpdateTime(DateUtils.getNowDate());
        return surProjectAuditinfoMapper.updateSurProjectAuditinfo(surProjectAuditinfo);
    }

    /**
     * 批量删除项目审核信息
     * 
     * @param ids 需要删除的项目审核信息主键
     * @return 结果
     */
    @Override
    public int deleteSurProjectAuditinfoByIds(Long[] ids)
    {
        return surProjectAuditinfoMapper.deleteSurProjectAuditinfoByIds(ids);
    }

    /**
     * 删除项目审核信息信息
     * 
     * @param id 项目审核信息主键
     * @return 结果
     */
    @Override
    public int deleteSurProjectAuditinfoById(Long id)
    {
        return surProjectAuditinfoMapper.deleteSurProjectAuditinfoById(id);
    }

    /**
     * 查询我的代办
     *
     * @param flowTaskEntity 查询条件
     * @return 结果
     */
    public Map<String,Object> findMyTask(FlowTaskEntity flowTaskEntity) {
        Map<String,Object> dataMap = new HashMap<>();
        List<Map<String, Object>> todoList = flowBusinessKeyMapper.selectMyAwaitFlowTask(flowTaskEntity);
        if(todoList!=null){
            dataMap.put("todo",todoList.size());
        }else{
            dataMap.put("todo",0);
        }
        List<Map<String, Object>> approveList = surProjectAuditinfoMapper.selectMyAwaitFlowTask(flowTaskEntity);
        int a=0,b=0,c=0,d=0;
        if(approveList!=null){
            for(Map<String, Object> map:approveList){
                if(PublicStateEnum.AUDIT_TYPE_QYFS.getCode().equals(map.get("type"))){
                    a++;
                }else if(PublicStateEnum.AUDIT_TYPE_SCSL.getCode().equals(map.get("type"))){
                    b++;
                }else if(PublicStateEnum.AUDIT_TYPE_JPYS.getCode().equals(map.get("type"))){
                    c++;
                }else if(PublicStateEnum.AUDIT_TYPE_CLFY.getCode().equals(map.get("type"))){
                    d++;
                }
            }
            dataMap.put("approve",approveList.size());
        }else{
            dataMap.put("approve",0);
        }
        dataMap.put("approveQYFS",a);
        dataMap.put("approveSCSL",b);
        dataMap.put("approveJPYS",c);
        dataMap.put("approveCLFY",d);
        return dataMap;
    }

    /**
     * 查询我的审批
     *
     * @param flowTaskEntity 查询条件
     * @return 结果
     */
    public List<Map<String,Object>> selectMyAwaitFlowTask(FlowTaskEntity flowTaskEntity) {
        return surProjectAuditinfoMapper.selectMyAwaitFlowTask(flowTaskEntity);
    }

    /**
     * 统计我的审批
     *
     * @param flowTaskEntity 查询条件
     * @return 结果
     */
    public Map<String,Object> queryCount(FlowTaskEntity flowTaskEntity) {
        Map<String,Object> dataMap = new HashMap<>();
        flowTaskEntity.setActiveName("await");
        List<Map<String, Object>> awaitList = surProjectAuditinfoMapper.selectMyAwaitFlowTask(flowTaskEntity);
        if(awaitList!=null){
            dataMap.put("await",awaitList.size());
        }else{
            dataMap.put("await",0);
        }
        flowTaskEntity.setActiveName("finished");
        List<Map<String, Object>> finishedList = surProjectAuditinfoMapper.selectMyAwaitFlowTask(flowTaskEntity);
        if(finishedList!=null){
            dataMap.put("finished",finishedList.size());
        }else{
            dataMap.put("finished",0);
        }
        return dataMap;
    }

    private void init(){
        List<SurProjectAuditinfo> list = new ArrayList<>();
        List<SurProjectCheckDetection> surProjectCheckDetectionList = surProjectCheckDetectionMapper.selectSurProjectCheckDetectionList(new SurProjectCheckDetection());
        List<SurProjectChecking> surProjectCheckingList = surProjectCheckingMapper.selectSurProjectCheckingList(new SurProjectChecking());
        List<SurProjectMaterialSeal> surProjectMaterialSealList = surProjectMaterialSealMapper.selectSurProjectMaterialSealList(new SurProjectMaterialSeal());
        List<SurProjectMeasure> surProjectMeasureList = surProjectMeasureMapper.selectSurProjectMeasureList(new SurProjectMeasure());
        if(surProjectCheckDetectionList!=null && surProjectCheckDetectionList.size()>0){
            for(SurProjectCheckDetection surProjectCheckDetection:surProjectCheckDetectionList){
                if("2".equals(surProjectCheckDetection.getCheckState())){
                    SurProjectAuditinfo a = new SurProjectAuditinfo();
                    a.setFromType(PublicStateEnum.AUDIT_TYPE_QYFS.getCode());
                    a.setFromId(surProjectCheckDetection.getId());
                    a.setApproveStatus("1");
                    a.setComment("提交申请");
                    a.setCreateBy(surProjectCheckDetection.getCreateBy());
                    a.setCreateTime(surProjectCheckDetection.getCreateTime());
                    list.add(a);

                    SurProjectAuditinfo b = new SurProjectAuditinfo();
                    b.setFromType(PublicStateEnum.AUDIT_TYPE_QYFS.getCode());
                    b.setFromId(surProjectCheckDetection.getId());
                    b.setApproveStatus("4");
                    b.setComment("默认审核通过");
                    b.setCreateBy(surProjectCheckDetection.getWitnessUser());
                    b.setCreateTime(surProjectCheckDetection.getCreateTime());
                    list.add(b);
                }
            }
        }

        if(surProjectCheckingList!=null && surProjectCheckingList.size()>0){
            for(SurProjectChecking surProjectChecking:surProjectCheckingList){
                SurProjectAuditinfo a = new SurProjectAuditinfo();
                a.setFromType(PublicStateEnum.AUDIT_TYPE_JPYS.getCode());
                a.setFromId(surProjectChecking.getId());
                a.setApproveStatus("1");
                a.setComment("提交申请");
                a.setCreateBy(surProjectChecking.getCreateBy());
                a.setCreateTime(surProjectChecking.getCreateTime());
                list.add(a);

                SurProjectAuditinfo b = new SurProjectAuditinfo();
                b.setFromType(PublicStateEnum.AUDIT_TYPE_JPYS.getCode());
                b.setFromId(surProjectChecking.getId());
                b.setApproveStatus("4");
                b.setComment("默认审核通过");
                b.setCreateBy(surProjectChecking.getSuperviseUser());
                b.setCreateTime(surProjectChecking.getCreateTime());
                list.add(b);
            }
        }

        if(surProjectMaterialSealList!=null && surProjectMaterialSealList.size()>0){
            for(SurProjectMaterialSeal surProjectMaterialSeal:surProjectMaterialSealList){
                SurProjectAuditinfo a = new SurProjectAuditinfo();
                a.setFromType(PublicStateEnum.AUDIT_TYPE_CLFY.getCode());
                a.setFromId(surProjectMaterialSeal.getId());
                a.setApproveStatus("1");
                a.setComment("提交申请");
                a.setCreateBy(surProjectMaterialSeal.getCreateBy());
                a.setCreateTime(surProjectMaterialSeal.getCreateTime());
                list.add(a);

                SurProjectAuditinfo b = new SurProjectAuditinfo();
                b.setFromType(PublicStateEnum.AUDIT_TYPE_CLFY.getCode());
                b.setFromId(surProjectMaterialSeal.getId());
                b.setApproveStatus("4");
                b.setComment("默认审核通过");
                b.setCreateBy(surProjectMaterialSeal.getWitnessUser());
                b.setCreateTime(surProjectMaterialSeal.getCreateTime());
                list.add(b);
            }
        }

        if(surProjectMeasureList!=null && surProjectMeasureList.size()>0){
            for(SurProjectMeasure surProjectMeasure:surProjectMeasureList){
                SurProjectAuditinfo a = new SurProjectAuditinfo();
                a.setFromType(PublicStateEnum.AUDIT_TYPE_SCSL.getCode());
                a.setFromId(surProjectMeasure.getId());
                a.setApproveStatus("1");
                a.setComment("提交申请");
                a.setCreateBy(surProjectMeasure.getCreateBy());
                a.setCreateTime(surProjectMeasure.getCreateTime());
                list.add(a);

                SurProjectAuditinfo b = new SurProjectAuditinfo();
                b.setFromType(PublicStateEnum.AUDIT_TYPE_SCSL.getCode());
                b.setFromId(surProjectMeasure.getId());
                b.setApproveStatus("4");
                b.setComment("默认审核通过");
                b.setCreateBy(surProjectMeasure.getSuperviseUser());
                b.setCreateTime(surProjectMeasure.getCreateTime());
                list.add(b);
            }
        }
        surProjectAuditinfoMapper.batch(list);
    }
}
