fuliqi
2024-09-03 461ec2d155d8dd35d8f73c35e7c2a33e040bf862
ycl-server/src/main/java/com/ycl/platform/service/impl/CheckScoreServiceImpl.java
New file
@@ -0,0 +1,382 @@
package com.ycl.platform.service.impl;
import annotation.DataScope;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycl.platform.domain.dto.CheckScoreDTO;
import com.ycl.platform.domain.dto.CheckScoreIndexDTO;
import com.ycl.platform.domain.dto.ScoreIndexDTO;
import com.ycl.platform.domain.entity.*;
import com.ycl.platform.domain.vo.CheckScoreDetailVO;
import com.ycl.platform.mapper.CheckRuleMapper;
import com.ycl.platform.mapper.CheckScoreMapper;
import com.ycl.platform.mapper.CheckTemplateMapper;
import com.ycl.platform.mapper.CheckTemplateRuleMapper;
import com.ycl.platform.service.ICheckIndexCarService;
import com.ycl.platform.service.ICheckIndexFaceService;
import com.ycl.platform.service.ICheckIndexVideoService;
import com.ycl.platform.service.ICheckScoreService;
import com.ycl.system.entity.SysRole;
import com.ycl.system.entity.SysUser;
import com.ycl.system.service.ISysDeptService;
import com.ycl.utils.DateUtils;
import com.ycl.utils.SecurityUtils;
import com.ycl.utils.StringUtils;
import constant.CheckConstants;
import enumeration.general.PublishType;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import pojo.ExcelExp;
import utils.poi.ExcelUtilManySheet;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
/**
 * 考核积分明细Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-22
 */
@Service
@Slf4j
public class CheckScoreServiceImpl extends ServiceImpl<CheckScoreMapper, CheckScore> implements ICheckScoreService {
    @Autowired
    private CheckScoreMapper scoreMapper;
    @Autowired
    private CheckTemplateMapper templateMapper;
    @Autowired
    private CheckTemplateRuleMapper templateRuleMapper;
    @Autowired
    private CheckRuleMapper ruleMapper;
    @Autowired
    private ICheckIndexCarService indexCarService;
    @Autowired
    private ICheckIndexFaceService indexFaceService;
    @Autowired
    private ICheckIndexVideoService indexVideoService;
    @Autowired
    private ISysDeptService deptService;
    /**
     * 查询考核积分指标
     *
     * @param id 考核积分主键
     * @return 考核积分
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public CheckScoreDetailVO selectCheckScoreById(CheckScoreIndexDTO checkScoreIndexDTO) {
        Long checkScoreId = checkScoreIndexDTO.getId();
        CheckScoreDetailVO checkScoreDetailVO = new CheckScoreDetailVO();
        //根据id读取score
        CheckScore checkScore = scoreMapper.selectCheckScoreById(checkScoreId);
        Integer templateId = 0;
        //方便切换改为:根据score考核类别和dto考核标签查模板规则和权重,注意同种类同标签只能有一个模板
        QueryWrapper<CheckTemplate> wrapper = new QueryWrapper<>();
        wrapper.eq("examine_tag",checkScoreIndexDTO.getExamineTag());
        wrapper.eq("examine_category",checkScore.getExamineCategory());
        Optional<CheckTemplate> first = templateMapper.selectList(wrapper).stream().findFirst();
        if(first.isPresent()){
            CheckTemplate checkTemplate = first.get();
            templateId = checkTemplate.getId();
        }
        //读取规则以及权重
        List<CheckTemplateRule> templateRuleList = templateRuleMapper.selectListByTemplateId(templateId);
        List<CheckRule> checkRules = new ArrayList<>();
        //读取模板对应所有规则
        Map<String, Object> scoreMap = new HashMap<>();
        List<Integer> ruleIds = templateRuleList.stream().map(checkTemplateRule -> checkTemplateRule.getCheckRuleId())
                .collect(Collectors.toList());
        List<CheckRule> ruleIndex = ruleMapper.selectBatchIds(ruleIds);
        checkRules.addAll(ruleIndex);
        //根据examineCategory 读取不同index表
        Short examineCategory = checkScore.getExamineCategory();
        String[] indexTableArr = {"", "t_check_index_video", "t_check_index_car", "t_check_index_face"};
        String tableName = indexTableArr[examineCategory];
        ScoreIndexDTO scoreIndexDTO = new ScoreIndexDTO()
                .setTableName(tableName)
                .setDeptId(checkScore.getDeptId())
                .setDate(checkScoreIndexDTO.getDate())
                .setQuarter(checkScoreIndexDTO.getQuarter());
        scoreIndexDTO.setParams(checkScoreIndexDTO.getParams());
        scoreIndexDTO.setExamineTag(checkScoreIndexDTO.getExamineTag());
        //获当月份
        if (StringUtils.isEmpty(scoreIndexDTO.getDate()) && CollectionUtils.isEmpty(checkScoreIndexDTO.getQuarter())) {
            //如果查询条件不含参数,查询积分对应创建时间
            Date createTime = checkScore.getCreateTime();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
            scoreIndexDTO.setDate(formatter.format(createTime));
        }
        List<Map> map = scoreMapper.selectScoreIndex(scoreIndexDTO);
        scoreMap.put("tableData", checkRules);
        scoreMap.put("dataMap", map);
        checkScoreDetailVO.setCheckRuleList(templateRuleList);
        checkScoreDetailVO.setScoreMap(scoreMap);
        return checkScoreDetailVO;
    }
    /**
     * 查询考核积分卡片列表
     *
     * @param checkScore 考核积分
     * @return 考核积分
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public Map<Long, List<CheckScore>> selectCheckScoreList(CheckScore checkScore) {
        //区县只能看已发布
        roleControl(checkScore);
        // 获取数据日期时间
        Calendar calendar = Calendar.getInstance();
        getCheckScore(checkScore, calendar);
        // 一号查询之前的数据
        if (LocalDateTime.now().getDayOfMonth() == 1) {
            calendar.add(Calendar.DAY_OF_MONTH, -1);
        }
        List<CheckScore> checkScores = scoreMapper.selectCheckScoreMap(checkScore);
        // 如果数据为空,则查询之前的数据
        if (checkScores.isEmpty()) {
            CheckScore one = scoreMapper.getLast(checkScore);
            if (Objects.nonNull(one)) {
                calendar.setTime(one.getCreateTime());
                getCheckScore(checkScore, calendar);
                checkScores = scoreMapper.selectCheckScoreMap(checkScore);
            }
        }
        return checkScores.stream().collect(Collectors.groupingBy(CheckScore::getDeptId));
    }
    private void getCheckScore(CheckScore checkScore, Calendar calendar) {
        // 0 省厅月度 1 市局月度 2 省厅季度 3 市局季度
        switch (checkScore.getExamineTag()) {
            case 0, 1:
                checkScore.setEndDate(calendar.getTime());
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                checkScore.setStartDate(calendar.getTime());
                break;
            case 2, 3:
                checkScore.setStartDate(DateUtils.getQuarterStart(calendar).getTime());
                checkScore.setEndDate(DateUtils.getQuarterEnd(calendar).getTime());
                checkScore.setExamineTag(checkScore.getExamineTag() == 2 ? 0 : 1);
                break;
        }
    }
    /**
     * 查询考核积分卡片列表
     *
     * @param checkScore 考核积分
     * @return 考核积分
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public Map<Long, List<CheckScore>> selectCheckScoreChart(CheckScore checkScore) {
        //区县只能看已发布
        roleControl(checkScore);
        //查询成绩
        String date = checkScore.getDate();
        if (StringUtils.isEmpty(date)) {
            //如果为空查本月的数据
            Calendar now = Calendar.getInstance();
            //取昨天
            // now.add(Calendar.DATE, -1);
            Date yesterday = now.getTime();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
            date = simpleDateFormat.format(yesterday);
            checkScore.setDate(date);
        }
        List<CheckScore> checkScores = scoreMapper.selectCheckScoreList(checkScore);
        return checkScores.stream().collect(Collectors.groupingBy(CheckScore::getDeptId));
    }
    /**
     * 区县详情
     *
     * @param checkScore 考核积分
     * @return 考核积分
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<CheckScore> page(CheckScore checkScore) {
        //区县只能看已发布
        roleControl(checkScore);
        return scoreMapper.selectCheckScoreList(checkScore);
    }
    /**
     * 发布考核积分信息
     *
     * @param checkScoreDTO 考核积分
     * @return 结果
     */
    @Override
    public int publishCheckScore(CheckScoreDTO checkScoreDTO) {
        if (CollectionUtils.isEmpty(checkScoreDTO.getId())) {
            return 0;
        }
        PublishType publishType = null;
        try {
            publishType = PublishType.valueOf(checkScoreDTO.getPublish());
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("参数类型不匹配");
        }
        String code = publishType.getCode();
        checkScoreDTO.setPublish(code);
        return scoreMapper.publishCheckScore(checkScoreDTO);
    }
    /**
     * 导出指标
     *
     * @param checkScore 考核积分
     * @return 考核积分
     */
    @Override
    public void exportIndex(HttpServletResponse response, CheckScore checkScoreDTO) throws IOException {
        String date = checkScoreDTO.getDate();
        //如果请求参数时间为空,查这条记录的时间
        if (StringUtils.isEmpty(checkScoreDTO.getDate()) && CollectionUtils.isEmpty(checkScoreDTO.getQuarter())) {
            CheckScore checkScore = getById(checkScoreDTO.getId());
            Date createTime = checkScore.getCreateTime();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
            date = formatter.format(createTime);
        }
        /** 导三张sheet */
        //车辆
        CheckIndexCar checkIndexCar = new CheckIndexCar();
        checkIndexCar.setDate(date);
        checkIndexCar.setQuarter(checkScoreDTO.getQuarter());
        List<CheckIndexCar> checkIndexCars = indexCarService.selectCheckIndexCarList(checkIndexCar);
        for (CheckIndexCar indexCar : checkIndexCars) {
            indexCar.setExamineTagStr(CheckConstants.Examine_Tag_Province.equals(indexCar.getExamineTag()) ? "省厅考核" : "区县考核");
        }
        ExcelExp e1 = new ExcelExp("车辆考核指标数据", checkIndexCars, CheckIndexCar.class);
        //人脸
        CheckIndexFace checkIndexFace = new CheckIndexFace();
        checkIndexFace.setDate(date);
        checkIndexFace.setQuarter(checkScoreDTO.getQuarter());
        List<CheckIndexFace> checkIndexFaces = indexFaceService.selectCheckIndexFaceList(checkIndexFace);
        for (CheckIndexFace indexFace : checkIndexFaces) {
            indexFace.setExamineTagStr(CheckConstants.Examine_Tag_Province.equals(indexFace.getExamineTag()) ? "省厅考核" : "区县考核");
        }
        ExcelExp e2 = new ExcelExp("人脸考核指标数据", checkIndexFaces, CheckIndexFace.class);
        //视频
        CheckIndexVideo checkIndexVideo = new CheckIndexVideo();
        checkIndexVideo.setDate(date);
        checkIndexVideo.setQuarter(checkScoreDTO.getQuarter());
        List<CheckIndexVideo> checkIndexVideos = indexVideoService.selectCheckIndexVideoList(checkIndexVideo);
        for (CheckIndexVideo indexVideo : checkIndexVideos) {
            indexVideo.setExamineTagStr(CheckConstants.Examine_Tag_Province.equals(indexVideo.getExamineTag()) ? "省厅考核" : "区县考核");
        }
        ExcelExp e3 = new ExcelExp("视频考核指标数据", checkIndexVideos, CheckIndexVideo.class);
        List<ExcelExp> mysheet = new ArrayList<>();
        mysheet.add(e1);
        mysheet.add(e2);
        mysheet.add(e3);
        ExcelUtilManySheet<List<ExcelExp>> util = new ExcelUtilManySheet<>(mysheet);
        util.exportExcelManySheet(response, mysheet);
    }
    private void roleControl(CheckScore checkScore) {
        List<SysRole> roles = SecurityUtils.getLoginUser().getUser().getRoles();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (!user.isAdmin()) {
            for (SysRole role : roles) {
                if (role.getPermissions().contains("check:score:role:publish")) {
                    Map<String, Object> params = checkScore.getParams();
                    params.put("publish", PublishType.PUBLISHED.getCode());
                }
            }
        }
    }
    @Override
    public List<Map<String, Object>> home() {
        return baseMapper.home();
    }
    @Override
    public List<Map<String, Object>> calculate(String category) {
        Calendar calendar = Calendar.getInstance();
        Date endDate = calendar.getTime();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        Date startDate = calendar.getTime();
        return scoreMapper.calculate(startDate, endDate, category);
    }
    //    /**
//     * 新增考核积分
//     *
//     * @param checkScore 考核积分
//     * @return 结果
//     */
//    @Override
//    public int insertCheckScore(CheckScore checkScore)
//    {
//        checkScore.setCreateTime(DateUtils.getNowDate());
//        return scoreMapper.insertCheckScore(checkScore);
//    }
//
//    /**
//     * 修改考核明细
//     *
//     * @param checkScore 考核积分明细
//     * @return 结果
//     */
//    @Override
//    public int updateCheckScore(CheckScore checkScore)
//    {
//
//        return scoreMapper.updateCheckScore(checkScore);
//    }
//
//    /**
//     * 批量删除考核积分
//     *
//     * @param ids 需要删除的考核积分主键
//     * @return 结果
//     */
//    @Override
//    public int deleteCheckScoreByIds(Long[] ids)
//    {
//        return scoreMapper.deleteCheckScoreByIds(ids);
//    }
//
//    /**
//     * 删除考核积分信息
//     *
//     * @param id 考核积分主键
//     * @return 结果
//     */
//    @Override
//    public int deleteCheckScoreById(Long id)
//    {
//        return scoreMapper.deleteCheckScoreById(id);
//    }
}