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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.distributed.print.business.bean.dto.professional.ProfessionalDto;
import com.qmth.distributed.print.business.entity.BasicProfessional;
import com.qmth.distributed.print.business.mapper.BasicProfessionalMapper;
import com.qmth.distributed.print.business.service.BasicProfessionalService;
import com.qmth.teachcloud.common.bean.dto.DataPermissionRule;
import com.qmth.teachcloud.common.entity.SysOrg;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import com.qmth.teachcloud.common.enums.OrgTypeEnum;
import com.qmth.teachcloud.common.service.BasicRoleDataPermissionService;
import com.qmth.teachcloud.common.service.SysOrgService;
import com.qmth.teachcloud.common.util.ServletUtil;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 专业表 服务实现类
 * </p>
 *
 * @author xf
 * @since 2024-04-22
 */
@Service
public class BasicProfessionalServiceImpl extends ServiceImpl<BasicProfessionalMapper, BasicProfessional> implements BasicProfessionalService {

    @Resource
    private BasicRoleDataPermissionService basicRoleDataPermissionService;
    @Resource
    private SysOrgService sysOrgService;

    @Override
    public List<ProfessionalDto> listData(String name, Long orgId) {
        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
        SysOrg sysOrg = sysOrgService.getById(orgId);
        if (sysOrg != null && OrgTypeEnum.SCHOOL.equals(sysOrg.getType())) {
            orgId = null;
        }
        return this.baseMapper.pageData(schoolId, name, orgId, null);
    }

    @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);
    }

    @Override
    public Boolean saveData(BasicProfessional basicProfessional) {
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();

        if (basicProfessional.getId() == null) {
            basicProfessional.insertInfo(sysUser.getId());
            basicProfessional.setSchoolId(sysUser.getSchoolId());
            return this.save(basicProfessional);
        } else {
            BasicProfessional basicProfessional1 = this.getOne(new QueryWrapper<BasicProfessional>().lambda().eq(BasicProfessional::getOrgId, basicProfessional.getOrgId()).eq(BasicProfessional::getName, basicProfessional.getName()));
            if (basicProfessional1 != null && basicProfessional1.getId().longValue() != basicProfessional.getId().longValue()) {
                throw ExceptionResultEnum.ERROR.exception("专业名称已存在");
            }
            basicProfessional.updateInfo(sysUser.getId());
            return this.updateById(basicProfessional);
        }
    }

    @Override
    public boolean remove(Long id) {
        return this.removeById(id);
    }
}
