package com.changhu.module.assessmentCriteria.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.changhu.common.enums.ResultCode;
import com.changhu.common.exception.MessageException;
import com.changhu.module.assessmentCriteria.mapper.CkAssessmentRecordMapper;
import com.changhu.module.assessmentCriteria.mapper.CkProjectMapper;
import com.changhu.module.assessmentCriteria.pojo.entity.CkAssessmentRecord;
import com.changhu.module.assessmentCriteria.pojo.entity.CkAssessmentRecordDetails;
import com.changhu.module.assessmentCriteria.pojo.queryParams.AssessmentRecordPagerQueryParams;
import com.changhu.module.assessmentCriteria.pojo.vo.AssessmentRecordDetailVo;
import com.changhu.module.assessmentCriteria.pojo.vo.AssessmentRecordPagerVo;
import com.changhu.module.assessmentCriteria.pojo.vo.CkProjectDetailTableVo;
import com.changhu.module.assessmentCriteria.service.CkAssessmentRecordService;
import com.changhu.support.mybatisplus.pojo.entity.BaseEntity;
import com.changhu.support.mybatisplus.pojo.params.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ck_assessment_record (考核记录)  服务实现类
 * author: luozhun
 * desc 由groovy脚本自动生成
 */
@Service
public class CkAssessmentRecordServiceImpl extends ServiceImpl<CkAssessmentRecordMapper, CkAssessmentRecord> implements CkAssessmentRecordService {

    @Autowired
    private CkProjectMapper ckProjectMapper;

    @Override
    public Page<AssessmentRecordPagerVo> pager(PageParams<AssessmentRecordPagerQueryParams, AssessmentRecordPagerVo> queryParams) {
        return baseMapper.pager(queryParams.getPage(), queryParams.getParams());
    }

    @Override
    public List<AssessmentRecordDetailVo> deductedDetail(Long assessmentRecordId) {
        CkAssessmentRecord ckAssessmentRecord = Db.lambdaQuery(CkAssessmentRecord.class)
                .eq(BaseEntity::getSnowFlakeId, assessmentRecordId)
                .oneOpt()
                .orElseThrow(() -> new MessageException(ResultCode.DATA_NOT_FOUND));

        List<CkProjectDetailTableVo> ckProjectDetailTableVos = ckProjectMapper.ckProjectDetail(ckAssessmentRecord.getCkProjectId());

        List<Long> assessmentRecordDetailCkStandardIds = Db.lambdaQuery(CkAssessmentRecordDetails.class)
                .select(CkAssessmentRecordDetails::getCkStandardId)
                .eq(CkAssessmentRecordDetails::getCkAssessmentRecordId, assessmentRecordId)
                .list()
                .stream()
                .map(CkAssessmentRecordDetails::getCkStandardId)
                .toList();

        return ckProjectDetailTableVos.stream()
                .map(item -> {
                    AssessmentRecordDetailVo assessmentRecordDetailVo = BeanUtil.copyProperties(item, AssessmentRecordDetailVo.class);
                    if (assessmentRecordDetailCkStandardIds.contains(item.getCkStandardId())) {
                        assessmentRecordDetailVo.setIsSelected(true);
                    }
                    return assessmentRecordDetailVo;
                })
                .toList();
    }
}
