AdministerServiceImpl.java 12.9 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.administer.AddAdministerDTO;
import com.subsidy.dto.company.OperatorsDTO;
import com.subsidy.mapper.AdministerCompanyMappingMapper;
import com.subsidy.mapper.ServiceAdministerMappingMapper;
import com.subsidy.model.AdministerCompanyMappingDO;
import com.subsidy.model.AdministerDO;
import com.subsidy.mapper.AdministerMapper;
import com.subsidy.model.ServiceAdministerMappingDO;
import com.subsidy.service.AdministerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.subsidy.util.ConstantUtils;
import com.subsidy.util.JwtUtil;
import com.subsidy.util.Localstorage;
import com.subsidy.vo.administer.AdministerPermissionVO;
import com.subsidy.vo.administer.AdministerVO;
import com.subsidy.vo.administer.LoginVO;
import com.subsidy.vo.administer.PermissionsVO;
import com.subsidy.vo.company.GetAllCompanyVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 管理平台用户 服务实现类
 * </p>
 *
 * @author Tuyp
 * @since 2024-01-11
 */
@Service
public class AdministerServiceImpl extends ServiceImpl<AdministerMapper, AdministerDO> implements AdministerService {

    @Autowired
    private ServiceAdministerMappingMapper serviceAdministerMappingMapper;

    @Autowired
    private AdministerCompanyMappingMapper administerCompanyMappingMapper;

    public LoginVO centerLogin(AdministerDO administerDO) {

        LoginVO loginVO = new LoginVO();

        AdministerDO administerDO1 = this.baseMapper.selectOne(new QueryWrapper<AdministerDO>()
                .lambda()
                .eq(AdministerDO::getAccountName, administerDO.getAccountName())
                .eq(AdministerDO::getRole, 2));

        if (null == administerDO1) {
            throw new HttpException(10012);
        }

        if ("0".equals(administerDO1.getStatus())) {
            throw new HttpException(10013);
        }

        if (administerDO1.getPassword().equals(administerDO.getPassword())) {
            String token = JwtUtil.generateToken(administerDO1.getId(), ConstantUtils.ADMINISTER_TERMINATE);
            loginVO.setToken(token);
            return loginVO;

        } else {
            throw new HttpException(10029);
        }
    }


    public AdministerPermissionVO getPermissions() {

        AdministerPermissionVO administerPermissionVO = new AdministerPermissionVO();
        AdministerDO administerDO = (AdministerDO) Localstorage.getUser();
        if (administerDO == null) {
            throw new HttpException(10012);
        }
        BeanUtils.copyProperties(administerDO, administerPermissionVO);

        List<PermissionsVO> list = this.baseMapper.getPermissions(administerDO.getId());
        List<PermissionsVO> treeList = new ArrayList();
        if (list != null) {
            list.forEach(permission -> {
                if (permission.getParentId() == null) {
                    treeList.add(permission);
                }

                list.forEach(p -> {
                    if (null != p.getParentId() && p.getParentId().equals(permission.getId())) {
                        if (permission.getChildren() == null) {
                            permission.setChildren(new ArrayList<>());
                        }
                        permission.getChildren().add(p);
                    }
                });
            });
        }
        administerPermissionVO.setPermissions(treeList);
//        //查询角色
//        RoleAdministerMappingDO roleAdministerMappingDO = roleAdministerMappingMapper.selectOne(new QueryWrapper<RoleAdministerMappingDO>()
//                .lambda()
//                .eq(RoleAdministerMappingDO::getAdministerId, administerDO.getId()));
//        administerPermissionVO.setRole(roleAdministerMappingDO.getRoleId());

//        CompanyDictDO companyDictDO = companyDictMapper.selectById(administerDO.getCompanyId());
//        administerPermissionVO.setCompanyName(companyDictDO.getCompanyName());
//        administerPermissionVO.setShortName(companyDictDO.getShortName());

        /**
         * 所在公司
         */
        List<GetAllCompanyVO> getAllCompanyVOS = this.baseMapper.administerCompanys(administerDO.getId());
        administerPermissionVO.setGetAllCompanyVOS(getAllCompanyVOS);

        return administerPermissionVO;
    }

    public IPage<AdministerVO> operators(OperatorsDTO operatorsDTO) {
        Page pager = new Page(operatorsDTO.getPageNum(), operatorsDTO.getPageSize());

        IPage<AdministerVO> administerVOIPage = this.baseMapper.operators(pager, operatorsDTO.getUserName(), operatorsDTO.getCompanyName());

        List<AdministerVO> administerVOS = administerVOIPage.getRecords();
        for (AdministerVO administerVO : administerVOS) {
            if (0 == administerVO.getRole()) {
                //所有服务
                administerVO.setServiceDictDOS(serviceAdministerMappingMapper.administerService(administerVO.getId()));
            }
        }
        return administerVOIPage;
    }

    public IPage<AdministerVO> appOperators(OperatorsDTO operatorsDTO) {

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

        IPage<AdministerVO> administerVOIPage = this.baseMapper.appOperators(pager, operatorsDTO.getUserName(), operatorsDTO.getCompanyName());

        List<AdministerVO> administerVOS = administerVOIPage.getRecords();
        for (AdministerVO administerVO : administerVOS) {
            if (0 == administerVO.getRole()) {
                //所有服务
                administerVO.setServiceDictDOS(serviceAdministerMappingMapper.administerService(administerVO.getId()));
            }
        }
        return administerVOIPage;
    }

    public IPage<AdministerVO> clientOperators(OperatorsDTO operatorsDTO) {

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

        IPage<AdministerVO> administerVOIPage = this.baseMapper.clientOperators(pager, operatorsDTO.getUserName(), operatorsDTO.getCompanyName());

        List<AdministerVO> administerVOS = administerVOIPage.getRecords();
        for (AdministerVO administerVO : administerVOS) {
            administerVO.setGetAllCompanyVOS(this.baseMapper.administerCompanys(administerVO.getId()));
            if (0 == administerVO.getRole()) {
                //所有服务
                administerVO.setServiceDictDOS(serviceAdministerMappingMapper.administerService(administerVO.getId()));
            }
        }
        return administerVOIPage;
    }

    public String addAdminister(AddAdministerDTO addAdministerDTO) {

        int count = this.baseMapper.selectCount(new QueryWrapper<AdministerDO>()
                .lambda()
                .eq(AdministerDO::getAccountName, addAdministerDTO.getAccountName()));

        int count1 = this.baseMapper.selectCount(new QueryWrapper<AdministerDO>()
                .lambda()
                .eq(AdministerDO::getTelephone, addAdministerDTO.getTelephone()));

        if (count + count1 > 0) {
            throw new HttpException(20002);
        }

        AdministerDO administerDO = new AdministerDO();
        BeanUtils.copyProperties(addAdministerDTO, administerDO);
        this.baseMapper.insert(administerDO);

        if (null != addAdministerDTO.getCompanyIds()) {
            List<Long> companyIds = addAdministerDTO.getCompanyIds();
            for (Long companyId : companyIds) {
                AdministerCompanyMappingDO administerCompanyMappingDO = new AdministerCompanyMappingDO();
                administerCompanyMappingDO.setAdministerId(administerDO.getId());
                administerCompanyMappingDO.setCompanyId(companyId);
                administerCompanyMappingMapper.insert(administerCompanyMappingDO);
            }
        }

        if (null != addAdministerDTO.getServiceIds()) {
            List<Long> serviceIds = addAdministerDTO.getServiceIds();
            for (Long service : serviceIds) {
                ServiceAdministerMappingDO serviceAdministerMappingDO = new ServiceAdministerMappingDO();
                serviceAdministerMappingDO.setAdministerId(administerDO.getId());
                serviceAdministerMappingDO.setServiceId(service);
                serviceAdministerMappingMapper.insert(serviceAdministerMappingDO);
            }
        }

        return ConstantUtils.ADD_SUCCESS;
    }

    @Transactional(rollbackFor = Exception.class)
    public String deleteAdminister(AdministerDO administerDO) {
        this.baseMapper.deleteById(administerDO.getId());

        administerCompanyMappingMapper.delete(new QueryWrapper<AdministerCompanyMappingDO>()
                .lambda()
                .eq(AdministerCompanyMappingDO::getAdministerId, administerDO.getId()));

        serviceAdministerMappingMapper.delete(new QueryWrapper<ServiceAdministerMappingDO>()
                .lambda()
                .eq(ServiceAdministerMappingDO::getAdministerId, administerDO.getId()));

        return ConstantUtils.DELETE_SUCCESS;
    }

    public String updateAdminister(AddAdministerDTO addAdministerDTO) {

        int count = this.baseMapper.selectCount(new QueryWrapper<AdministerDO>()
                .lambda()
                .eq(AdministerDO::getAccountName, addAdministerDTO.getAccountName())
                .ne(AdministerDO::getId, addAdministerDTO.getId()));

        int count1 = this.baseMapper.selectCount(new QueryWrapper<AdministerDO>()
                .lambda()
                .eq(AdministerDO::getTelephone, addAdministerDTO.getTelephone())
                .ne(AdministerDO::getId, addAdministerDTO.getId()));

        if (count + count1 > 0) {
            throw new HttpException(20002);
        }
        AdministerDO administerDO = this.baseMapper.selectById(addAdministerDTO.getId());
        BeanUtils.copyProperties(addAdministerDTO, administerDO);
        this.baseMapper.updateById(administerDO);

        if (null != addAdministerDTO.getCompanyIds()) {

            //
            List<Long> oldIds = this.baseMapper.administerCompanyIds(addAdministerDTO.getId());
            List<Long> newIds = addAdministerDTO.getCompanyIds();

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

            newIds.removeAll(oldIds); //
            if (newIds.size() > 0) {
                for (Long lg : newIds) {
                    AdministerCompanyMappingDO administerCompanyMappingDO = new AdministerCompanyMappingDO();
                    administerCompanyMappingDO.setAdministerId(addAdministerDTO.getId());
                    administerCompanyMappingDO.setCompanyId(lg);
                    administerCompanyMappingMapper.insert(administerCompanyMappingDO);
                }
            }

            oldIds.removeAll(newCompanyIds2);
            if (oldIds.size() > 0) {
                for (Long lg : oldIds) {
                    administerCompanyMappingMapper.delete(new QueryWrapper<AdministerCompanyMappingDO>()
                            .lambda()
                            .eq(AdministerCompanyMappingDO::getAdministerId, addAdministerDTO.getId())
                            .eq(AdministerCompanyMappingDO::getCompanyId, lg));
                }
            }
        }

        if (null != addAdministerDTO.getServiceIds()) {

            //
            List<Long> oldIds = this.baseMapper.administerServiceIds(addAdministerDTO.getId());
            List<Long> newIds = addAdministerDTO.getServiceIds();

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

            newIds.removeAll(oldIds); //
            if (newIds.size() > 0) {
                for (Long lg : newIds) {
                    ServiceAdministerMappingDO serviceAdministerMappingDO = new ServiceAdministerMappingDO();
                    serviceAdministerMappingDO.setAdministerId(addAdministerDTO.getId());
                    serviceAdministerMappingDO.setServiceId(lg);
                    serviceAdministerMappingMapper.insert(serviceAdministerMappingDO);
                }
            }

            oldIds.removeAll(newServices2);
            if (oldIds.size() > 0) {
                for (Long lg : oldIds) {
                    serviceAdministerMappingMapper.delete(new QueryWrapper<ServiceAdministerMappingDO>()
                            .lambda()
                            .eq(ServiceAdministerMappingDO::getAdministerId, addAdministerDTO.getId())
                            .eq(ServiceAdministerMappingDO::getServiceId, lg));
                }
            }
        }

        return ConstantUtils.SET_SUCCESS;
    }
}