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

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.changhu.common.db.enums.UserType;
import com.changhu.common.enums.ResultCode;
import com.changhu.common.exception.MessageException;
import com.changhu.common.utils.UserUtil;
import com.changhu.module.superManagement.mapper.AssessmentCriteriaMapper;
import com.changhu.module.superManagement.mapper.AssessmentRecordMapper;
import com.changhu.module.superManagement.pojo.queryParams.AssessmentRecordPagerQueryParams;
import com.changhu.module.superManagement.pojo.vo.AssessmentRecordDetailVo;
import com.changhu.module.superManagement.pojo.vo.AssessmentRecordPagerVo;
import com.changhu.module.superManagement.pojo.vo.CkProjectDetailTableVo;
import com.changhu.module.superManagement.service.AssessmentRecordService;
import com.changhu.pojo.entity.CkAssessmentRecord;
import com.changhu.pojo.entity.CkAssessmentRecordDetails;
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;

/**
 * @author 20252
 * @createTime 2024/11/22 下午3:05
 * @desc AssessmentRecordServiceImpl...
 */
@Service
public class AssessmentRecordServiceImpl implements AssessmentRecordService {

    @Autowired
    private AssessmentCriteriaMapper assessmentCriteriaMapper;

    @Autowired
    private AssessmentRecordMapper assessmentRecordMapper;

    @Override
    public Page<AssessmentRecordPagerVo> pager(PageParams<AssessmentRecordPagerQueryParams, AssessmentRecordPagerVo> queryParams) {
        if (UserType.MANAGEMENT_POLICE.equals(UserUtil.getUserType())) {
            return assessmentRecordMapper.pager(queryParams.getPage(), UserUtil.getUnitId(), queryParams.getParams());
        }
        return assessmentRecordMapper.pager(queryParams.getPage(), null, 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 = assessmentCriteriaMapper.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();
    }
}
