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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qmth.distributed.print.business.bean.dto.professional.MatrixDto;
import com.qmth.distributed.print.business.entity.TPCourse;
import com.qmth.distributed.print.business.entity.TPMatrix;
import com.qmth.distributed.print.business.entity.TPRequirement;
import com.qmth.distributed.print.business.mapper.TPRequirementMapper;
import com.qmth.distributed.print.business.service.TPCourseService;
import com.qmth.distributed.print.business.service.TPMatrixService;
import com.qmth.distributed.print.business.service.TPRequirementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.util.ServletUtil;
import org.activiti.bpmn.converter.export.CollaborationExport;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 专业认证毕业要求 服务实现类
 * </p>
 *
 * @author xf
 * @since 2024-04-22
 */
@Service
public class TPRequirementServiceImpl extends ServiceImpl<TPRequirementMapper, TPRequirement> implements TPRequirementService {

    @Resource
    private TPCourseService tpCourseService;
    @Resource
    private TPMatrixService tpMatrixService;

    @Override
    public List<TPRequirement> listRequirement(Long professionalId) {
        QueryWrapper<TPRequirement> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TPRequirement::getProfessionalId, professionalId)
                .orderByAsc(TPRequirement::getSortNum);
        return this.list(queryWrapper);
    }

    @Transactional
    @Override
    public boolean saveRequirement(TPRequirement tpRequirement) {
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        // 修改
        if (tpRequirement.getId() != null) {
            if(tpRequirement.getNodeCount() == null){
                tpRequirement.setNodeCount(null);
            } else {
                tpRequirement.setNodeCount(tpRequirement.getNodeCount() == 0 ? null : tpRequirement.getNodeCount());
            }
            tpRequirement.updateInfo(sysUser.getId());
        }
        // 新增
        else {
            tpRequirement.setSortNum(this.getMaxSortNum(tpRequirement.getProfessionalId()) + 1);
            tpRequirement.insertInfo(sysUser.getId());
        }
        this.saveOrUpdate(tpRequirement);
        // 节点数变动后，修改矩阵数据
        List<TPCourse> tpCourseList = tpCourseService.listCourse(tpRequirement.getProfessionalId());
        if (CollectionUtils.isNotEmpty(tpCourseList)) {
            for (TPCourse tpCourse : tpCourseList) {
                tpMatrixService.updateMatrixForNodeCount(tpCourse, tpRequirement);
            }
        }
        return true;
    }

    @Override
    public boolean saveSort(List<TPRequirement> requirementList) {
        requirementList.forEach(t -> {
            if (t.getId() != null && t.getSortNum() != null) {
                UpdateWrapper<TPRequirement> updateWrapper = new UpdateWrapper();
                updateWrapper.lambda().set(TPRequirement::getSortNum, t.getSortNum())
                        .eq(TPRequirement::getId, t.getId());
                this.update(updateWrapper);
            }
        });
        return true;
    }

    @Transactional
    @Override
    public boolean removeRequirement(Long id) {
        tpMatrixService.removeMatrixByCondition(null, null, id);
        return this.removeById(id);
    }

    @Override
    public int getMaxSortNum(Long professionalId) {
        QueryWrapper<TPRequirement> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TPRequirement::getProfessionalId, professionalId)
                .orderByDesc(TPRequirement::getSortNum)
                .last("limit 1");
        TPRequirement requirement = this.getOne(queryWrapper);
        if (requirement == null) {
            return 0;
        } else {
            return requirement.getSortNum();
        }
    }
}
