package com.qmth.distributed.print.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.distributed.print.business.bean.dto.obe.MatrixTargetDto;
import com.qmth.distributed.print.business.bean.dto.obe.RequirementTargetMatrixDto;
import com.qmth.distributed.print.business.entity.ObeCultureProgramRequirement;
import com.qmth.distributed.print.business.entity.ObeCultureProgramTarget;
import com.qmth.distributed.print.business.entity.ObeRequirementTargetMatrix;
import com.qmth.distributed.print.business.mapper.ObeRequirementTargetMatrixMapper;
import com.qmth.distributed.print.business.service.ObeCultureProgramRequirementService;
import com.qmth.distributed.print.business.service.ObeRequirementTargetMatrixService;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.enums.mark.MarkMode;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 培养目标与毕业要求关系矩阵 服务实现类
 * </p>
 *
 * @author xf
 * @since 2024-06-05
 */
@Service
public class ObeRequirementTargetMatrixServiceImpl extends ServiceImpl<ObeRequirementTargetMatrixMapper, ObeRequirementTargetMatrix> implements ObeRequirementTargetMatrixService {

    @Resource
    private ObeCultureProgramRequirementService obeCultureProgramRequirementService;

    @Override
    public List<RequirementTargetMatrixDto> getMatrix(Long cultureProgramId) {
        List<ObeRequirementTargetMatrix> matrixList = this.baseMapper.listMatrix(cultureProgramId);
        List<RequirementTargetMatrixDto> matrixDtoList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(matrixList)) {
            LinkedMultiValueMap<String, ObeRequirementTargetMatrix> requirementMap = new LinkedMultiValueMap<>();
            for (ObeRequirementTargetMatrix matrix : matrixList) {
                requirementMap.add(matrix.getRequirementName(), matrix);
            }
            requirementMap.entrySet().stream().forEach(entry -> {
                RequirementTargetMatrixDto requirementTargetMatrixDto = new RequirementTargetMatrixDto();
                requirementTargetMatrixDto.setRequirementName(entry.getKey());

                List<MatrixTargetDto> matrixTargetDtoList = entry.getValue().stream().map(m -> {
                    MatrixTargetDto matrixRequirementDto = new MatrixTargetDto();
                    matrixRequirementDto.setId(m.getId());
                    matrixRequirementDto.setName(m.getTargetName());
                    matrixRequirementDto.setEnable(m.getEnable());
                    return matrixRequirementDto;
                }).collect(Collectors.toList());
                requirementTargetMatrixDto.setTargetList(matrixTargetDtoList);
                matrixDtoList.add(requirementTargetMatrixDto);
            });
        }
        return matrixDtoList;
    }

    @Transactional
    @Override
    public boolean saveMatrix(List<ObeRequirementTargetMatrix> obeRequirementTargetMatrixList) {
        for (ObeRequirementTargetMatrix tpMatrix : obeRequirementTargetMatrixList) {
            UpdateWrapper<ObeRequirementTargetMatrix> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().set(ObeRequirementTargetMatrix::getEnable, tpMatrix.getEnable())
                    .eq(ObeRequirementTargetMatrix::getId, tpMatrix.getId());
            this.update(updateWrapper);
        }
        return true;
    }

    @Override
    public void updateMatrix(ObeCultureProgramTarget obeCultureProgramTarget) {
        // 查询一级毕业要求
        List<ObeCultureProgramRequirement> obeCultureProgramRequirementList = obeCultureProgramRequirementService.listByCultureProgramIdAndParentId(obeCultureProgramTarget.getCultureProgramId(), null);
        List<ObeRequirementTargetMatrix> obeRequirementTargetMatrixList = obeCultureProgramRequirementList.stream().map(m -> new ObeRequirementTargetMatrix(obeCultureProgramTarget.getCultureProgramId(), m.getId(), obeCultureProgramTarget.getId())).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(obeRequirementTargetMatrixList)) {
            this.saveBatch(obeRequirementTargetMatrixList);
        }
    }

    @Override
    public void removeMatrixByCondition(ObeRequirementTargetMatrix obeRequirementTargetMatrix) {
        UpdateWrapper<ObeRequirementTargetMatrix> updateWrapper = new UpdateWrapper<>();
        LambdaUpdateWrapper<ObeRequirementTargetMatrix> lambda = updateWrapper.lambda();
        if (obeRequirementTargetMatrix.getCultureProgramId() != null) {
            lambda.eq(ObeRequirementTargetMatrix::getCultureProgramId, obeRequirementTargetMatrix.getCultureProgramId());
        }
        if (obeRequirementTargetMatrix.getObeRequirementId() != null) {
            lambda.eq(ObeRequirementTargetMatrix::getObeRequirementId, obeRequirementTargetMatrix.getObeRequirementId());
        }
        if (obeRequirementTargetMatrix.getObeTargetId() != null) {
            lambda.eq(ObeRequirementTargetMatrix::getObeTargetId, obeRequirementTargetMatrix.getObeTargetId());
        }
        this.remove(updateWrapper);
    }

    @Override
    public void copyByCultureProgramId(Long id, Long obeCultureProgramNewId, Map<Long, Long> targetIdMap, Map<Long, Long> requirementIdMap) {
        List<ObeRequirementTargetMatrix> obeRequirementTargetMatrixList = this.baseMapper.listMatrix(id);
        for (ObeRequirementTargetMatrix matrix : obeRequirementTargetMatrixList) {
            matrix.setId(SystemConstant.getDbUuid());
            matrix.setCultureProgramId(obeCultureProgramNewId);
            matrix.setObeRequirementId(requirementIdMap.get(matrix.getObeRequirementId()));
            matrix.setObeTargetId(targetIdMap.get(matrix.getObeTargetId()));
        }
        if (CollectionUtils.isNotEmpty(obeRequirementTargetMatrixList)) {
            this.saveBatch(obeRequirementTargetMatrixList);
        }
    }
}
