CompanyDictServiceImpl.java 5.1 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.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.subsidy.common.exception.HttpException;
import com.subsidy.dto.administer.OperatorsDTO;
import com.subsidy.dto.company.AddCompanyDTO;
import com.subsidy.mapper.AdministerMapper;
import com.subsidy.mapper.DepartmentDictMapper;
import com.subsidy.mapper.RoleAdministerMappingMapper;
import com.subsidy.model.AdministerDO;
import com.subsidy.model.CompanyDictDO;
import com.subsidy.mapper.CompanyDictMapper;
import com.subsidy.model.DepartmentDictDO;
import com.subsidy.model.RoleAdministerMappingDO;
import com.subsidy.service.CompanyDictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.subsidy.util.ConstantUtils;
import com.subsidy.vo.administer.OperatorsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 企业字典表 服务实现类
 * </p>
 *
 * @author DengMin
 * @since 2021-10-25
 */
@Service
public class CompanyDictServiceImpl extends ServiceImpl<CompanyDictMapper, CompanyDictDO> implements CompanyDictService {


    @Autowired
    private AdministerMapper administerMapper;

    @Autowired
    private RoleAdministerMappingMapper roleAdministerMappingMapper;

    @Autowired
    private DepartmentDictMapper departmentDictMapper;

    public IPage<OperatorsVO> operators(OperatorsDTO operatorsDTO) {
        Page pager = new Page(operatorsDTO.getPageNum(), operatorsDTO.getPageSize());
        //判断该用户的角色
        Integer count = roleAdministerMappingMapper.selectCount(new QueryWrapper<RoleAdministerMappingDO>()
                .lambda()
                .eq(RoleAdministerMappingDO::getAdministerId,operatorsDTO.getId())
                .eq(RoleAdministerMappingDO::getRoleId,1));

        if (count > 0){
            operatorsDTO.setId(null);
        }

        return this.baseMapper.operators(pager, operatorsDTO.getCompanyName(), operatorsDTO.getId(),operatorsDTO.getUserName(),1);
    }

    public String addOperator(AddCompanyDTO addCompanyDTO) {

        Integer count = this.baseMapper.selectCount(new QueryWrapper<CompanyDictDO>()
                .lambda()
                .eq(CompanyDictDO::getCompanyName, addCompanyDTO.getAccountName()));

        Integer count1 = administerMapper.selectCount(new QueryWrapper<AdministerDO>()
        .lambda()
        .eq(AdministerDO::getAccountName, addCompanyDTO.getAccountName()));

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

        CompanyDictDO companyDictDO =new CompanyDictDO();
        BeanUtils.copyProperties(addCompanyDTO,companyDictDO);
        this.baseMapper.insert(companyDictDO);

        AdministerDO administerDO = new AdministerDO();
        administerDO.setAccountName(addCompanyDTO.getAccountName());
        administerDO.setCompanyId(companyDictDO.getId());
        administerDO.setPassword("admin123");
        administerDO.setStatus("1");
        administerDO.setRole(1);
        administerMapper.insert(administerDO);

        RoleAdministerMappingDO roleAdministerMappingDO = new RoleAdministerMappingDO();
        roleAdministerMappingDO.setAdministerId(administerDO.getId());
        roleAdministerMappingDO.setRoleId(2L);
        roleAdministerMappingMapper.insert(roleAdministerMappingDO);

        //添加默认部门  名称为公司名称
        DepartmentDictDO departmentDictDO = new DepartmentDictDO();
        departmentDictDO.setCompanyId(companyDictDO.getId());
        departmentDictDO.setDepartmentName(companyDictDO.getCompanyName());
        //departmentDictDO.setParentId(1L);
        departmentDictMapper.insert(departmentDictDO);

        return ConstantUtils.ADD_SUCCESS;
    }

    public String deleteAdminister(CompanyDictDO companyDictDO) {
        this.baseMapper.deleteById(companyDictDO.getId());

        administerMapper.delete(new QueryWrapper<AdministerDO>()
        .lambda()
        .eq(AdministerDO::getCompanyId,companyDictDO.getId()));

        return ConstantUtils.DELETE_SUCCESS;
    }

    public String updateAdminister(AddCompanyDTO addCompanyDTO) {

        CompanyDictDO companyDictDO = new CompanyDictDO();
        BeanUtils.copyProperties(addCompanyDTO,companyDictDO);
        this.baseMapper.updateById(companyDictDO);

        //查找企业管理员
        AdministerDO administerDO = administerMapper.selectOne(new QueryWrapper<AdministerDO>()
        .lambda()
        .eq(AdministerDO::getCompanyId,addCompanyDTO.getId())
        .eq(AdministerDO::getRole,1));
        if (StringUtils.isNotBlank(addCompanyDTO.getAccountName())){
            administerDO.setAccountName(addCompanyDTO.getAccountName());
        }
        if (StringUtils.isBlank(addCompanyDTO.getPassword())){
            addCompanyDTO.setPassword(addCompanyDTO.getPassword());
        }
        administerMapper.updateById(administerDO);
        return ConstantUtils.SET_SUCCESS;
    }

}