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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qmth.distributed.print.business.entity.*;
import com.qmth.distributed.print.business.mapper.ObeCultureProgramTargetMapper;
import com.qmth.distributed.print.business.service.ObeCultureProgramRequirementService;
import com.qmth.distributed.print.business.service.ObeCultureProgramService;
import com.qmth.distributed.print.business.service.ObeCultureProgramTargetService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.distributed.print.business.service.ObeRequirementTargetMatrixService;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.util.ServletUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 培养方案培养目标 服务实现类
 * </p>
 *
 * @author xf
 * @since 2024-06-05
 */
@Service
public class ObeCultureProgramTargetServiceImpl extends ServiceImpl<ObeCultureProgramTargetMapper, ObeCultureProgramTarget> implements ObeCultureProgramTargetService {

    @Resource
    private ObeCultureProgramRequirementService obeCultureProgramRequirementService;
    @Resource
    private ObeRequirementTargetMatrixService obeRequirementTargetMatrixService;
    @Resource
    private ObeCultureProgramService obeCultureProgramService;

    @Override
    public List<ObeCultureProgramTarget> list(Long cultureProgramId) {
        QueryWrapper<ObeCultureProgramTarget> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ObeCultureProgramTarget::getCultureProgramId, cultureProgramId)
                .orderByAsc(ObeCultureProgramTarget::getCreateTime);
        return this.list(queryWrapper);
    }

    @Transactional
    @Override
    public boolean saveData(ObeCultureProgramTarget obeCultureProgramTarget) {
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        if (obeCultureProgramTarget.getId() == null) {
            obeCultureProgramTarget.insertInfo(sysUser.getId());

            // 同时生成培养目标与毕业要求关系矩阵数据
            if (this.save(obeCultureProgramTarget)) {
                obeRequirementTargetMatrixService.updateMatrix(obeCultureProgramTarget);

                // 更新培养目标数量
                obeCultureProgramService.updateTargetCount(obeCultureProgramTarget.getCultureProgramId());
            }
            return true;
        } else {
            obeCultureProgramTarget.updateInfo(sysUser.getId());
            return this.updateById(obeCultureProgramTarget);
        }
    }

    @Transactional
    @Override
    public boolean delete(Long id) {
        ObeCultureProgramTarget obeCultureProgramTarget = this.getById(id);
        if (this.removeById(id)) {
            obeRequirementTargetMatrixService.removeMatrixByCondition(new ObeRequirementTargetMatrix(null, null, id));
            if (obeCultureProgramTarget != null) {
                obeCultureProgramService.updateTargetCount(obeCultureProgramTarget.getCultureProgramId());
            }
        }
        return true;
    }

    @Override
    public void removeByCultureProgramId(Long id) {
        this.remove(new QueryWrapper<ObeCultureProgramTarget>().lambda().eq(ObeCultureProgramTarget::getCultureProgramId, id));
    }

    @Override
    public Map<Long, Long> copyByCultureProgramId(Long id, Long obeCultureProgramNewId) {
        List<ObeCultureProgramTarget> obeCultureProgramTargetList = this.list(id);
        Map<Long, Long> idMap = new HashMap<>();
        for (ObeCultureProgramTarget obeCultureProgramTarget : obeCultureProgramTargetList) {
            Long newId = SystemConstant.getDbUuid();
            idMap.put(obeCultureProgramTarget.getId(), newId);
            obeCultureProgramTarget.setId(newId);
            obeCultureProgramTarget.setCultureProgramId(obeCultureProgramNewId);
        }
        if (CollectionUtils.isNotEmpty(obeCultureProgramTargetList)) {
            this.saveBatch(obeCultureProgramTargetList);
        }
        return idMap;
    }
}
