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

import java.util.Date;
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.common.utils.StringUtils;
import com.yanzhu.jh.project.domain.SurProjectAuditinfo;
import com.yanzhu.jh.project.mapper.SurProjectAuditinfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yanzhu.jh.project.mapper.SurProjectCheckDetectionMapper;
import com.yanzhu.jh.project.domain.SurProjectCheckDetection;
import com.yanzhu.jh.project.service.ISurProjectCheckDetectionService;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.transaction.annotation.Transactional;

/**
 * 材料送检Service业务层处理
 * 
 * @author JiangYuQi
 * @date 2023-08-31
 */
@Service
public class SurProjectCheckDetectionServiceImpl implements ISurProjectCheckDetectionService 
{
    @Autowired
    private SurProjectAuditinfoMapper surProjectAuditinfoMapper;

    @Autowired
    private SurProjectCheckDetectionMapper surProjectCheckDetectionMapper;

    /**
     * 查询材料送检
     * 
     * @param id 材料送检主键
     * @return 材料送检
     */
    @Override
    public SurProjectCheckDetection selectSurProjectCheckDetectionById(Long id)
    {
        return surProjectCheckDetectionMapper.selectSurProjectCheckDetectionById(id);
    }

    /**
     * 查询材料送检列表
     * 
     * @param surProjectCheckDetection 材料送检
     * @return 材料送检
     */
    @Override
    public List<SurProjectCheckDetection> selectSurProjectCheckDetectionList(SurProjectCheckDetection surProjectCheckDetection)
    {
        return surProjectCheckDetectionMapper.selectSurProjectCheckDetectionList(surProjectCheckDetection);
    }

    /**
     * 新增材料送检
     * 
     * @param surProjectCheckDetection 材料送检
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSurProjectCheckDetection(SurProjectCheckDetection surProjectCheckDetection)
    {
        surProjectCheckDetection.setCheckState(PublicStateEnum.CHECK_STATE_SJ.getCode());
        surProjectCheckDetection.setIsDel(PublicStateEnum.OK.getCode());
        if(surProjectCheckDetection.getCreateBy()==null){
            surProjectCheckDetection.setCreateBy(SecurityUtils.getUsername());
        }
        surProjectCheckDetection.setCreateTime(DateUtils.getNowDate());
        return surProjectCheckDetectionMapper.insertSurProjectCheckDetection(surProjectCheckDetection);
    }

    /**
     * 修改材料送检
     * 
     * @param surProjectCheckDetection 材料送检
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSurProjectCheckDetection(SurProjectCheckDetection surProjectCheckDetection)
    {

        //操作日志
        if(StringUtils.isNotBlank(surProjectCheckDetection.getApproveStatus())){
            SurProjectAuditinfo surProjectAuditinfo = new SurProjectAuditinfo();
            surProjectAuditinfo.setFromType(PublicStateEnum.AUDIT_TYPE_QYFS.getCode());
            surProjectAuditinfo.setFromId(surProjectCheckDetection.getId());
            surProjectAuditinfo.setApproveStatus(surProjectCheckDetection.getApproveStatus());
            if(StringUtils.isNotBlank(surProjectCheckDetection.getComment())){
                surProjectAuditinfo.setComment(surProjectCheckDetection.getComment());
            }
            surProjectAuditinfo.setCreateBy(surProjectCheckDetection.getUpdateBy());
            surProjectAuditinfo.setCreateTime(new Date());
            surProjectAuditinfoMapper.insertSurProjectAuditinfo(surProjectAuditinfo);
        }

        //修改人和修改时间为登记检测结果信息，审批合格或不合格时不修改这个字段
        if(PublicStateEnum.AUDITINFO_DSH.getCode().equals(surProjectCheckDetection.getApproveStatus())){
            // 非提交时保存最后修改人
            if(surProjectCheckDetection.getUpdateBy()==null){
                surProjectCheckDetection.setUpdateBy(SecurityUtils.getUsername());
            }
            surProjectCheckDetection.setUpdateTime(new Date());
        }

        return surProjectCheckDetectionMapper.updateSurProjectCheckDetection(surProjectCheckDetection);
    }

    /**
     * 批量删除材料送检
     * 
     * @param ids 需要删除的材料送检主键
     * @return 结果
     */
    @Override
    public int deleteSurProjectCheckDetectionByIds(Long[] ids)
    {
        return surProjectCheckDetectionMapper.deleteSurProjectCheckDetectionByIds(ids);
    }

    /**
     * 删除材料送检信息
     * 
     * @param id 材料送检主键
     * @return 结果
     */
    @Override
    public int deleteSurProjectCheckDetectionById(Long id)
    {
        return surProjectCheckDetectionMapper.deleteSurProjectCheckDetectionById(id);
    }

    /**
     * 分组查询检测统计
     *
     * @param where 材料送检
     * @return 结果
     */
    @Override
    public List<SurProjectCheckDetection> groupByCheckType(SurProjectCheckDetection where) {
        return surProjectCheckDetectionMapper.groupByCheckType(where);
    }

    /**
     * 查询材料送检统计
     *
     * @param surProjectCheckDetection 材料送检
     * @return 材料送检集合
     */
    @Override
    public Map<String,Object> findGroupCountByApprove(SurProjectCheckDetection surProjectCheckDetection){
        Map<String,Object> dataMap = new HashMap<>();
        surProjectCheckDetection.setActiveName("jxz");
        List<SurProjectCheckDetection> awaitList = surProjectCheckDetectionMapper.selectSurProjectCheckDetectionList(surProjectCheckDetection);
        if(awaitList!=null){
            dataMap.put("jxz",awaitList.size());
        }else{
            dataMap.put("jxz",0);
        }
        surProjectCheckDetection.setActiveName("ywc");
        List<SurProjectCheckDetection> finishedList = surProjectCheckDetectionMapper.selectSurProjectCheckDetectionList(surProjectCheckDetection);
        if(finishedList!=null){
            dataMap.put("ywc",finishedList.size());
        }else{
            dataMap.put("ywc",0);
        }
        return dataMap;
    }

    /**
     * 根据项目统计材料送检
     *
     * @param projectId 项目主键
     * @return 结果
     */
    @Override
    public List<Map<String,Object>> findGroupCountByProjectId(Long projectId){
        return surProjectCheckDetectionMapper.findGroupCountByProjectId(projectId);
    }

    @Override
    public List<SurProjectCheckDetection> getList(SurProjectCheckDetection where) {
        return surProjectCheckDetectionMapper.getList(where);
    }

}
