AcademyDictServiceImpl.java 9.75 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.subsidy.common.exception.HttpException;
import com.subsidy.dto.academy.AcademiesDTO;
import com.subsidy.dto.academy.AcademyAccountDTO;
import com.subsidy.dto.academy.AddAcademyAccountDTO;
import com.subsidy.dto.academy.AddAcademyDTO;
import com.subsidy.mapper.AdministerAcademyMappingMapper;
import com.subsidy.mapper.AdministerMapper;
import com.subsidy.model.AcademyDictDO;
import com.subsidy.mapper.AcademyDictMapper;
import com.subsidy.model.AdministerAcademyMappingDO;
import com.subsidy.model.AdministerCompanyMappingDO;
import com.subsidy.model.AdministerDO;
import com.subsidy.service.AcademyDictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.subsidy.util.ConstantUtils;
import com.subsidy.vo.academy.AcademiesVO;
import com.subsidy.vo.academy.AcademyAccountVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.ws.Action;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 院校字典表 服务实现类
 * </p>
 *
 * @author Tuyp
 * @since 2024-02-23
 */
@Service
public class AcademyDictServiceImpl extends ServiceImpl<AcademyDictMapper, AcademyDictDO> implements AcademyDictService {

    @Autowired
    private AdministerMapper administerMapper;

    @Autowired
    private AdministerAcademyMappingMapper administerAcademyMappingMapper;

    public IPage<AcademiesVO> academies(AcademiesDTO academiesDTO) {
        Page pager = new Page(academiesDTO.getPageNum(), academiesDTO.getPageSize());
        return this.baseMapper.academies(pager, academiesDTO.getAcademyName());
    }

    @Transactional(rollbackFor = Exception.class)
    public String addAcademy(AddAcademyDTO addAcademyDTO) {

        int count = this.baseMapper.selectCount(new QueryWrapper<AcademyDictDO>()
                .lambda()
                .eq(AcademyDictDO::getAcademyName, addAcademyDTO.getAcademyName()));

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

        //院校
        AcademyDictDO academyDictDO = new AcademyDictDO();
        BeanUtils.copyProperties(addAcademyDTO, academyDictDO);
        this.baseMapper.insert(academyDictDO);

        //账号
        int count1 = administerMapper.selectCount(new QueryWrapper<AdministerDO>()
                .lambda()
                .eq(AdministerDO::getAccountName, addAcademyDTO.getAccountName()));
        if (count1 > 0) {
            throw new HttpException(10014);
        }

        AdministerDO administerDO = new AdministerDO();
        BeanUtils.copyProperties(addAcademyDTO, administerDO);
        administerDO.setRole(4);
        administerDO.setStatus("1");
        administerDO.setPassword("admin321");
        administerDO.setUserName(academyDictDO.getAcademyName());
        administerMapper.insert(administerDO);

        //关联
        AdministerAcademyMappingDO administerAcademyMappingDO = new AdministerAcademyMappingDO();
        administerAcademyMappingDO.setAdministerId(administerDO.getId());
        administerAcademyMappingDO.setAcademyId(academyDictDO.getId());
        administerAcademyMappingMapper.insert(administerAcademyMappingDO);

        return ConstantUtils.ADD_SUCCESS;
    }

    public String deleteAcademy(AcademyDictDO academyDictDO) {

        this.baseMapper.deleteById(academyDictDO.getId());

        List<AdministerAcademyMappingDO> administerAcademyMappingDOS = administerAcademyMappingMapper.selectList(new QueryWrapper<AdministerAcademyMappingDO>()
                .lambda()
                .eq(AdministerAcademyMappingDO::getAcademyId, academyDictDO.getId()));

        administerAcademyMappingDOS.forEach(x -> {
            administerAcademyMappingMapper.deleteById(x.getId());
            administerMapper.deleteById(x.getAdministerId());
        });
        return ConstantUtils.DELETE_SUCCESS;
    }

    public String updateAcademy(AddAcademyDTO addAcademyDTO) {

        AcademyDictDO academyDictDO = new AcademyDictDO();
        BeanUtils.copyProperties(addAcademyDTO, academyDictDO);
        this.updateById(academyDictDO);

        //查看院校主账号

        //账号
        int count1 = administerMapper.selectCount(new QueryWrapper<AdministerDO>()
                .lambda()
                .eq(AdministerDO::getAccountName, addAcademyDTO.getAccountName())
                .ne(AdministerDO::getId, addAcademyDTO.getId()));
        if (count1 > 0) {
            throw new HttpException(10014);
        }

        AdministerDO administerDO = this.baseMapper.mainAccount(addAcademyDTO.getId());

        if (null != addAcademyDTO.getAccountName()) {
            administerDO.setAccountName(addAcademyDTO.getAccountName());
        }

        if (null != addAcademyDTO.getTelephone()) {
            administerDO.setTelephone(addAcademyDTO.getTelephone());
        }

        if (null != addAcademyDTO.getPassword()) {
            administerDO.setPassword(addAcademyDTO.getPassword());
        }

        if (null != addAcademyDTO.getUserName()) {
            administerDO.setUserName(addAcademyDTO.getUserName());
        }

        administerMapper.updateById(administerDO);

        return ConstantUtils.SUCCESS_UPDATE;
    }

    public IPage academyAccount(AcademyAccountDTO academyAccountDTO) {

        Page pager = new Page(academyAccountDTO.getPageNum(), academyAccountDTO.getPageSize());

        IPage<AcademyAccountVO> academyAccountVOIPage = this.baseMapper.academyAccount(pager, academyAccountDTO.getAcademyName());

        List<AcademyAccountVO> academyAccountVOS = academyAccountVOIPage.getRecords();

        for (AcademyAccountVO aav : academyAccountVOS) {
            //账户关联的院校
            List<AcademyDictDO> academyDictDOS = this.baseMapper.accountAcademies(aav.getId());
            aav.setAcademyDictDOS(academyDictDOS);
        }
        return academyAccountVOIPage;
    }

    public String addAcademyAccount(AddAcademyAccountDTO addAcademyAccountDTO) {

        //账户
        int count1 = administerMapper.selectCount(new QueryWrapper<AdministerDO>()
                .lambda()
                .eq(AdministerDO::getAccountName, addAcademyAccountDTO.getAccountName()));
        if (count1 > 0) {
            throw new HttpException(10014);
        }
        AdministerDO administerDO = new AdministerDO();

        BeanUtils.copyProperties(addAcademyAccountDTO, administerDO);
        administerDO.setRole(5);
        administerMapper.insert(administerDO);

        //映射关系
        List<Long> academyIds = addAcademyAccountDTO.getAcademyIds();
        for (Long academyId : academyIds) {
            AdministerAcademyMappingDO administerAcademyMappingDO = new AdministerAcademyMappingDO();
            administerAcademyMappingDO.setAcademyId(academyId);
            administerAcademyMappingDO.setAdministerId(administerDO.getId());
            administerAcademyMappingMapper.insert(administerAcademyMappingDO);
        }

        return ConstantUtils.ADD_SUCCESS;
    }

    public String deleteAcademyAccount(AdministerDO administerDO) {

        administerMapper.deleteById(administerDO.getId());

        List<AdministerAcademyMappingDO> administerAcademyMappingDOS = administerAcademyMappingMapper.selectList(new QueryWrapper<AdministerAcademyMappingDO>()
                .lambda()
                .eq(AdministerAcademyMappingDO::getAdministerId, administerDO.getId()));

        for (AdministerAcademyMappingDO administerAcademyMappingDO : administerAcademyMappingDOS) {
            administerAcademyMappingMapper.deleteById(administerAcademyMappingDO.getId());
        }

        return ConstantUtils.DELETE_SUCCESS;
    }

    public String updateAcademyAccount(AddAcademyAccountDTO addAcademyAccountDTO) {

        //编辑人员信息
        //账号
        int count1 = administerMapper.selectCount(new QueryWrapper<AdministerDO>()
                .lambda()
                .eq(AdministerDO::getAccountName, addAcademyAccountDTO.getAccountName())
                .ne(AdministerDO::getId, addAcademyAccountDTO.getId()));
        if (count1 > 0) {
            throw new HttpException(10014);
        }
        AdministerDO administerDO = new AdministerDO();
        BeanUtils.copyProperties(addAcademyAccountDTO, administerDO);
        administerMapper.updateById(administerDO);

        //已关联院校信息
        List<Long> oldIds = this.baseMapper.accountAcademyIds(addAcademyAccountDTO.getId());

        //去旧加新
        List<Long> newIds = addAcademyAccountDTO.getAcademyIds();

        List<Long> newAcademyIds2 = new ArrayList<>();
        for (Long lg : newIds) {
            newAcademyIds2.add(lg);
        }

        newIds.removeAll(oldIds); //
        if (newIds.size() > 0) {
            for (Long lg : newIds) {
                AdministerAcademyMappingDO administerAcademyMappingDO = new AdministerAcademyMappingDO();
                administerAcademyMappingDO.setAdministerId(addAcademyAccountDTO.getId());
                administerAcademyMappingDO.setAcademyId(lg);
                administerAcademyMappingMapper.insert(administerAcademyMappingDO);
            }
        }

        oldIds.removeAll(newAcademyIds2);
        if (oldIds.size() > 0) {
            for (Long lg : oldIds) {
                administerAcademyMappingMapper.delete(new QueryWrapper<AdministerAcademyMappingDO>()
                        .lambda()
                        .eq(AdministerAcademyMappingDO::getAdministerId, addAcademyAccountDTO.getId())
                        .eq(AdministerAcademyMappingDO::getAcademyId, lg));
            }
        }

        return ConstantUtils.SUCCESS_UPDATE;
    }

}