CourseDictServiceImpl.java 4.16 KB
package com.subsidy.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.subsidy.common.exception.HttpException;
import com.subsidy.dto.course.QueryCoursesDTO;
import com.subsidy.mapper.CourseDictMapper;
import com.subsidy.mapper.RoleAdministerMappingMapper;
import com.subsidy.model.CourseDictDO;
import com.subsidy.model.RoleAdministerMappingDO;
import com.subsidy.service.CourseDictService;
import com.subsidy.util.ConstantUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author DengMin
 * @since 2021-10-11
 */
@Service
public class CourseDictServiceImpl extends ServiceImpl<CourseDictMapper, CourseDictDO> implements CourseDictService {

    @Autowired
    private RoleAdministerMappingMapper roleAdministerMappingMapper;

    public String addCourse(CourseDictDO courseDictDO) {

        int count;

        if (null != courseDictDO.getCompanyId()) {
            count = this.baseMapper.selectCount(new QueryWrapper<CourseDictDO>()
                    .lambda()
                    .eq(CourseDictDO::getCourseName, courseDictDO.getCourseName())
                    .isNull(CourseDictDO::getCompanyId));

        } else {
            count = this.baseMapper.selectCount(new QueryWrapper<CourseDictDO>()
                    .lambda()
                    .eq(CourseDictDO::getCourseName, courseDictDO.getCourseName())
                    .eq(CourseDictDO::getCompanyId, courseDictDO.getCompanyId()));
        }

        if (count > 0) {
            throw new HttpException(20001);
        }

        this.baseMapper.insert(courseDictDO);
        return ConstantUtils.ADD_SUCCESS;
    }

    public String deleteCourse(CourseDictDO courseDictDO) {
        this.baseMapper.deleteById(courseDictDO.getId());
        return ConstantUtils.DELETE_SUCCESS;
    }

    public IPage queryCourses(QueryCoursesDTO queryCoursesDTO) {
        Page pager = new Page(queryCoursesDTO.getPageNum(), queryCoursesDTO.getPageSize());

        //判断当前用户是运营者还是企业者
        Integer count = roleAdministerMappingMapper.selectCount(new QueryWrapper<RoleAdministerMappingDO>()
                .lambda()
                .eq(RoleAdministerMappingDO::getAdministerId,queryCoursesDTO.getAdministerId())
                .eq(RoleAdministerMappingDO::getRoleId,1));

        if (count > 0){
            //后台管理员
            queryCoursesDTO.setCompanyId(null);
        }

        return this.baseMapper.queryCourses(pager, queryCoursesDTO.getCourseName(), queryCoursesDTO.getCategoryId(), queryCoursesDTO.getCourseType(), queryCoursesDTO.getCourseSource(), queryCoursesDTO.getCompanyId());
    }

    public String updateCourses(CourseDictDO courseDictDO) {
        int count;

        if (null != courseDictDO.getCompanyId()) {
            count = this.baseMapper.selectCount(new QueryWrapper<CourseDictDO>()
                    .lambda()
                    .eq(CourseDictDO::getCourseName, courseDictDO.getCourseName())
                    .ne(CourseDictDO::getId, courseDictDO.getCompanyId()));

        } else {
            count = this.baseMapper.selectCount(new QueryWrapper<CourseDictDO>()
                    .lambda()
                    .eq(CourseDictDO::getCourseName, courseDictDO.getCourseName())
                    .isNull(CourseDictDO::getCompanyId));
        }

        if (count > 0) {
            throw new HttpException(20001);
        }
        this.baseMapper.updateById(courseDictDO);
        return ConstantUtils.DELETE_SUCCESS;
    }

    public IPage queryCompanyCourse(QueryCoursesDTO queryCoursesDTO) {
        Page pager = new Page(queryCoursesDTO.getPageNum(), queryCoursesDTO.getPageSize());
        return this.baseMapper.queryCompanyCourse(pager, queryCoursesDTO.getCourseName(), queryCoursesDTO.getCategoryId(), queryCoursesDTO.getCourseType(), queryCoursesDTO.getCourseSource(), queryCoursesDTO.getCompanyId());
    }


}