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.core.text.Convert;
import com.ruoyi.common.enums.PublicStateEnum;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yanzhu.jh.project.mapper.SurProjectCheckingMapper;
import com.yanzhu.jh.project.domain.SurProjectChecking;
import com.yanzhu.jh.project.service.ISurProjectCheckingService;
import com.ruoyi.common.utils.SecurityUtils;
/**
 * 项目验收Service业务层处理
 * 
 * @author JiangYuQi
 * @date 2023-08-18
 */
@Service
public class SurProjectCheckingServiceImpl implements ISurProjectCheckingService 
{
    @Autowired
    private SurProjectCheckingMapper surProjectCheckingMapper;

    /**
     * 查询项目验收
     * 
     * @param id 项目验收主键
     * @return 项目验收
     */
    @Override
    public SurProjectChecking selectSurProjectCheckingById(Long id)
    {
        return surProjectCheckingMapper.selectSurProjectCheckingById(id);
    }

    /**
     * 查询项目验收列表
     * 
     * @param surProjectChecking 项目验收
     * @return 项目验收
     */
    @Override
    public List<SurProjectChecking> selectSurProjectCheckingList(SurProjectChecking surProjectChecking)
    {
        return surProjectCheckingMapper.selectSurProjectCheckingList(surProjectChecking);
    }

    /**
     * 查询项目验收列表
     *
     * @param surProjectChecking 项目验收
     * @return 项目验收集合
     */
    @Override
    public List<SurProjectChecking> selectBgscreenProjectCheckingList(SurProjectChecking surProjectChecking) {
        return surProjectCheckingMapper.selectBgscreenProjectCheckingList(surProjectChecking);
    }

    /**
     * 查询项目验收统计
     *
     * @param surProjectChecking 项目验收
     * @return 项目验收集合
     */
    @Override
    public Map<String, Object> selectBgscreenProjectCheckingView(SurProjectChecking surProjectChecking) {
        List<Map<String, Object>> list = surProjectCheckingMapper.selectBgscreenProjectCheckingView(surProjectChecking);
        Map<String, Object> dataMap = new HashMap<>();
        int sum=0;
        int okSum=0;
        for(Map<String, Object> map:list){
            sum += Convert.toInt(map.get("total"));
            //"1"为验收合格
            if("1".equals(map.get("check_result").toString())){
                okSum+=Convert.toInt(map.get("total"));
            }
        }
        dataMap.put("sum",sum);//验收数
        if(sum>0){
            dataMap.put("okSum",okSum*100/sum);//合格率
        }else{
            dataMap.put("okSum",100);//合格率
        }
        return  dataMap;
    }

    /**
     * 新增项目验收
     * 
     * @param surProjectChecking 项目验收
     * @return 结果
     */
    @Override
    public int insertSurProjectChecking(SurProjectChecking surProjectChecking)
    {

        surProjectChecking.setCheckType(PublicStateEnum.PROJECT_CHECK_TYPE_JPYS.getCode());
        surProjectChecking.setWorkingPositionType(PublicStateEnum.PROJECT_CHECK_POSITION_TYPE.getCode());
        //默认第一张图是主图
        surProjectChecking.setMainImage(surProjectChecking.getImageUrls().split(",")[0]);
        surProjectChecking.setIsDel(Convert.toLong(PublicStateEnum.OK.getCode()));
        surProjectChecking.setCreateTime(DateUtils.getNowDate());
        return surProjectCheckingMapper.insertSurProjectChecking(surProjectChecking);
    }

    /**
     * 修改项目验收
     * 
     * @param surProjectChecking 项目验收
     * @return 结果
     */
    @Override
    public int updateSurProjectChecking(SurProjectChecking surProjectChecking)
    {
        surProjectChecking.setMainImage(surProjectChecking.getImageUrls().split(",")[0]);
        surProjectChecking.setUpdateBy(SecurityUtils.getUsername());
        surProjectChecking.setUpdateTime(DateUtils.getNowDate());
        return surProjectCheckingMapper.updateSurProjectChecking(surProjectChecking);
    }

    /**
     * 批量删除项目验收
     * 
     * @param ids 需要删除的项目验收主键
     * @return 结果
     */
    @Override
    public int deleteSurProjectCheckingByIds(Long[] ids)
    {
        return surProjectCheckingMapper.deleteSurProjectCheckingByIds(ids);
    }

    /**
     * 删除项目验收信息
     * 
     * @param id 项目验收主键
     * @return 结果
     */
    @Override
    public int deleteSurProjectCheckingById(Long id)
    {
        return surProjectCheckingMapper.deleteSurProjectCheckingById(id);
    }
}
