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

/**
 * 材料封样Service业务层处理
 * 
 * @author JiangYuQi
 * @date 2023-08-22
 */
@Service
public class SurProjectMaterialSealServiceImpl implements ISurProjectMaterialSealService 
{

    @Autowired
    private SurProjectAuditinfoMapper surProjectAuditinfoMapper;

    @Autowired
    private SurProjectMaterialSealMapper surProjectMaterialSealMapper;

    /**
     * 查询材料封样
     * 
     * @param id 材料封样主键
     * @return 材料封样
     */
    @Override
    public SurProjectMaterialSeal selectSurProjectMaterialSealById(Long id)
    {
        return surProjectMaterialSealMapper.selectSurProjectMaterialSealById(id);
    }

    /**
     * 查询材料封样列表
     * 
     * @param surProjectMaterialSeal 材料封样
     * @return 材料封样
     */
    @Override
    public List<SurProjectMaterialSeal> selectSurProjectMaterialSealList(SurProjectMaterialSeal surProjectMaterialSeal)
    {
        return surProjectMaterialSealMapper.selectSurProjectMaterialSealList(surProjectMaterialSeal);
    }

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

    /**
     * 新增材料封样
     * 
     * @param surProjectMaterialSeal 材料封样
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSurProjectMaterialSeal(SurProjectMaterialSeal surProjectMaterialSeal)
    {
        if(surProjectMaterialSeal.getCreateBy()==null){
            surProjectMaterialSeal.setCreateBy(SecurityUtils.getUsername());
        }
        surProjectMaterialSeal.setIsDel(PublicStateEnum.OK.getCode());
        surProjectMaterialSeal.setMainImage(surProjectMaterialSeal.getImageUrls().split(",")[0]);
        surProjectMaterialSeal.setCreateTime(DateUtils.getNowDate());

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

        return res;
    }

    /**
     * 修改材料封样
     * 
     * @param surProjectMaterialSeal 材料封样
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSurProjectMaterialSeal(SurProjectMaterialSeal surProjectMaterialSeal)
    {
        if(surProjectMaterialSeal.getUpdateBy()==null){
            surProjectMaterialSeal.setUpdateBy(SecurityUtils.getUsername());
        }
        if(surProjectMaterialSeal.getImageUrls()!=null){
            surProjectMaterialSeal.setMainImage(surProjectMaterialSeal.getImageUrls().split(",")[0]);
        }
        surProjectMaterialSeal.setUpdateTime(DateUtils.getNowDate());

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

        return surProjectMaterialSealMapper.updateSurProjectMaterialSeal(surProjectMaterialSeal);
    }

    /**
     * 批量删除材料封样
     * 
     * @param ids 需要删除的材料封样主键
     * @return 结果
     */
    @Override
    public int deleteSurProjectMaterialSealByIds(Long[] ids)
    {
        return surProjectMaterialSealMapper.deleteSurProjectMaterialSealByIds(ids);
    }

    /**
     * 删除材料封样信息
     * 
     * @param id 材料封样主键
     * @return 结果
     */
    @Override
    public int deleteSurProjectMaterialSealById(Long id)
    {
        return surProjectMaterialSealMapper.deleteSurProjectMaterialSealById(id);
    }

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

    @Override
    public List<SurProjectMaterialSeal> selectTop20(SurProjectMaterialSeal where) {
        return surProjectMaterialSealMapper.selectTop20(where);
    }

    @Override
    public List<SurProjectMaterialSeal> groupTop12Month(SurProjectMaterialSeal where) {
        return surProjectMaterialSealMapper.groupTop12Month(where);
    }

    @Override
    public List<SurProjectMaterialSeal> selectTop12Month(SurProjectMaterialSeal where) {
        return surProjectMaterialSealMapper.selectTop12Month(where);
    }
}
