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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qmth.distributed.print.business.bean.dto.professional.ProfessionalDto;
import com.qmth.distributed.print.business.entity.TPProfessional;
import com.qmth.distributed.print.business.entity.TPRequirement;
import com.qmth.distributed.print.business.enums.ProfessionalCertificationEnum;
import com.qmth.distributed.print.business.mapper.TPProfessionalMapper;
import com.qmth.distributed.print.business.service.TPProfessionalService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.distributed.print.business.service.TPRequirementService;
import com.qmth.teachcloud.common.bean.dto.DataPermissionRule;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.service.BasicRoleDataPermissionService;
import com.qmth.teachcloud.common.util.ServletUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 专业认证专业表 服务实现类
 * </p>
 *
 * @author xf
 * @since 2024-04-22
 */
@Service
public class TPProfessionalServiceImpl extends ServiceImpl<TPProfessionalMapper, TPProfessional> implements TPProfessionalService {

    @Resource
    private TPRequirementService tpRequirementService;
    @Resource
    private BasicRoleDataPermissionService basicRoleDataPermissionService;

    @Override
    public IPage<ProfessionalDto> pageData(Page<ProfessionalDto> page, String name, Long orgId) {
        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        DataPermissionRule dpr = basicRoleDataPermissionService.findDataPermission(schoolId, requestUser.getId(), ServletUtil.getRequest().getServletPath());
        return this.baseMapper.pageData(page, schoolId, name, orgId, dpr);
    }

    @Transactional
    @Override
    public Boolean saveData(TPProfessional tpProfessional) {
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        tpProfessional.insertInfo(sysUser.getId());
        tpProfessional.setSchoolId(sysUser.getSchoolId());
        this.save(tpProfessional);

        List<TPRequirement> tpRequirementList = new ArrayList<>();
        // 新增默认毕业要求
        ProfessionalCertificationEnum.listTypes().forEach(m -> {
            TPRequirement tpRequirement = new TPRequirement();
            tpRequirement.setProfessionalId(tpProfessional.getId());
            tpRequirement.setCode(m.getCode());
            tpRequirement.setName(m.getName());
            tpRequirement.setSortNum(m.getOrdinal() + 1);
            tpRequirement.insertInfo(sysUser.getId());
            tpRequirementList.add(tpRequirement);
        });
        tpRequirementService.saveBatch(tpRequirementList);
        return true;
    }
}
