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.SurProjectMeasureMapper;
import com.yanzhu.jh.project.domain.SurProjectMeasure;
import com.yanzhu.jh.project.service.ISurProjectMeasureService;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.transaction.annotation.Transactional;

/**
 * 实测实量Service业务层处理
 * 
 * @author JiangYuQi
 * @date 2023-08-22
 */
@Service
public class SurProjectMeasureServiceImpl implements ISurProjectMeasureService 
{
    @Autowired
    private SurProjectAuditinfoMapper surProjectAuditinfoMapper;

    @Autowired
    private SurProjectMeasureMapper surProjectMeasureMapper;

    /**
     * 查询实测实量
     * 
     * @param id 实测实量主键
     * @return 实测实量
     */
    @Override
    public SurProjectMeasure selectSurProjectMeasureById(Long id)
    {
        return surProjectMeasureMapper.selectSurProjectMeasureById(id);
    }

    /**
     * 查询实测实量列表
     * 
     * @param surProjectMeasure 实测实量
     * @return 实测实量
     */
    @Override
    public List<SurProjectMeasure> selectSurProjectMeasureList(SurProjectMeasure surProjectMeasure)
    {
        return surProjectMeasureMapper.selectSurProjectMeasureList(surProjectMeasure);
    }

    /**
     * 统计材料封样
     *
     * @param surProjectMeasure 材料封样
     * @return 项目验收集合
     */
    @Override
    public Map<String,Object> findGroupCountByApprove(SurProjectMeasure surProjectMeasure) {
        Map<String,Object> dataMap = new HashMap<>();
        surProjectMeasure.setActiveName("jxz");
        List<SurProjectMeasure> awaitList = surProjectMeasureMapper.selectSurProjectMeasureList(surProjectMeasure);
        if(awaitList!=null){
            dataMap.put("jxz",awaitList.size());
        }else{
            dataMap.put("jxz",0);
        }
        surProjectMeasure.setActiveName("ywc");
        List<SurProjectMeasure> finishedList = surProjectMeasureMapper.selectSurProjectMeasureList(surProjectMeasure);
        if(finishedList!=null){
            dataMap.put("ywc",finishedList.size());
        }else{
            dataMap.put("ywc",0);
        }
        return dataMap;
    }

    /**
     * 新增实测实量
     * 
     * @param surProjectMeasure 实测实量
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSurProjectMeasure(SurProjectMeasure surProjectMeasure)
    {
        if(surProjectMeasure.getCreateBy()==null){
            surProjectMeasure.setCreateBy(SecurityUtils.getUsername());
        }
        surProjectMeasure.setMainImage(surProjectMeasure.getImageUrls().split(",")[0]);
        surProjectMeasure.setIsDel(PublicStateEnum.OK.getCode());
        surProjectMeasure.setCreateTime(DateUtils.getNowDate());

        int res = surProjectMeasureMapper.insertSurProjectMeasure(surProjectMeasure);
        //操作日志
        if(StringUtils.isNotBlank(surProjectMeasure.getApproveStatus())){
            SurProjectAuditinfo surProjectAuditinfo = new SurProjectAuditinfo();
            surProjectAuditinfo.setFromType(PublicStateEnum.AUDIT_TYPE_SCSL.getCode());
            surProjectAuditinfo.setFromId(surProjectMeasure.getId());
            surProjectAuditinfo.setApproveStatus(surProjectMeasure.getApproveStatus());
            if(StringUtils.isNotBlank(surProjectMeasure.getComment())){
                surProjectAuditinfo.setComment(surProjectMeasure.getComment());
            }
            surProjectAuditinfo.setCreateBy(surProjectMeasure.getCreateBy());
            surProjectAuditinfo.setCreateTime(new Date());
            surProjectAuditinfoMapper.insertSurProjectAuditinfo(surProjectAuditinfo);
        }

        return res;
    }

    /**
     * 修改实测实量
     * 
     * @param surProjectMeasure 实测实量
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSurProjectMeasure(SurProjectMeasure surProjectMeasure)
    {
        if(surProjectMeasure.getUpdateBy()==null){
            surProjectMeasure.setUpdateBy(SecurityUtils.getUsername());
        }
        if(surProjectMeasure.getImageUrls()!=null){
            surProjectMeasure.setMainImage(surProjectMeasure.getImageUrls().split(",")[0]);
        }
        surProjectMeasure.setUpdateTime(DateUtils.getNowDate());

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

        return surProjectMeasureMapper.updateSurProjectMeasure(surProjectMeasure);
    }

    /**
     * 批量删除实测实量
     * 
     * @param ids 需要删除的实测实量主键
     * @return 结果
     */
    @Override
    public int deleteSurProjectMeasureByIds(Long[] ids)
    {
        return surProjectMeasureMapper.deleteSurProjectMeasureByIds(ids);
    }

    /**
     * 删除实测实量信息
     * 
     * @param id 实测实量主键
     * @return 结果
     */
    @Override
    public int deleteSurProjectMeasureById(Long id)
    {
        return surProjectMeasureMapper.deleteSurProjectMeasureById(id);
    }

    /**
     * 根据项目统计实测实量
     *
     * @param projectId 项目主键
     * @return 结果
     */
    public List<Map<String,Object>> findGroupCountByProjectId(Long projectId) {
        return surProjectMeasureMapper.findGroupCountByProjectId(projectId);
    }

    @Override
    public List<SurProjectMeasure> groupMeasureInfo(SurProjectMeasure where) {
        return surProjectMeasureMapper.groupMeasureInfo(where);
    }

    @Override
    public List<SurProjectMeasure> getList(SurProjectMeasure where) {
        return surProjectMeasureMapper.getList(where);
    }
}
