CompanyDictServiceImpl.java 13.2 KB
package com.laowu.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.laowu.common.exception.HttpException;
import com.laowu.dto.company.AddCompanyDTO;
import com.laowu.dto.company.CompanyDTO;
import com.laowu.dto.company.DeleteCompanyDTO;
import com.laowu.dto.company.StationCompanyDTO;
import com.laowu.mapper.*;
import com.laowu.model.*;
import com.laowu.service.CompanyDictService;
import com.laowu.util.ConstantUtils;
import com.laowu.vo.company.CompanyUpdateHistoryVO;
import com.laowu.vo.company.CompanyVO;
import com.laowu.vo.company.RelateWorkVO;
import com.laowu.vo.team.TotalTeamsVO;
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.List;

/**
 * <p>
 * 企业管理表 服务实现类
 * </p>
 *
 * @author Tuyp
 * @since 2023-02-16
 */
@Service
public class CompanyDictServiceImpl extends ServiceImpl<CompanyDictMapper, CompanyDictDO> implements CompanyDictService {

    @Autowired
    private CompanyPhotoMapper companyPhotoMapper;

    @Autowired
    private CompanyLabelMapper companyLabelMapper;

    @Autowired
    private CompanyRelateWorkMapper companyRelateWorkMapper;

    @Autowired
    private CompanyUpdateHistoryMapper companyUpdateHistoryMapper;

    @Autowired
    private PositionDictMapper positionDictMapper;

    @Autowired
    private WorkstationDictMapper workstationDictMapper;

    public IPage<CompanyVO> company(CompanyDTO companyDTO) {

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

        IPage<CompanyVO> company = this.baseMapper.company(pager, companyDTO.getCompanyName(), companyDTO.getProvince(), companyDTO.getCity(), companyDTO.getCounty());

        List<CompanyVO> records = company.getRecords();
        for (CompanyVO companyVO : records) {

            //图
            List<CompanyPhotoDO> companyPhotoDOS = companyPhotoMapper.selectList(new QueryWrapper<CompanyPhotoDO>()
                    .lambda()
                    .eq(CompanyPhotoDO::getCompanyId, companyVO.getId()));
            companyVO.setCompanyPhotoDOS(companyPhotoDOS);


            //标签
            List<CompanyLabelDO> companyLabelDOS = companyLabelMapper.selectList(new QueryWrapper<CompanyLabelDO>()
                    .lambda()
                    .eq(CompanyLabelDO::getCompanyId, companyVO.getId()));
            companyVO.setCompanyLabelDOS(companyLabelDOS);

            //关联业务
            List<RelateWorkVO> relateWorkDOS = companyRelateWorkMapper.relateWork(companyVO.getId());
            companyVO.setRelateWorkVOS(relateWorkDOS);

            //历史
            List<CompanyUpdateHistoryVO> companyUpdateHistoryVOS = companyUpdateHistoryMapper.companyUpdateHistory(companyVO.getId());
            companyVO.setCompanyUpdateHistoryVOS(companyUpdateHistoryVOS);
        }
        return company;
    }


    @Transactional(rollbackFor = Exception.class)
    public String deleteCompany(DeleteCompanyDTO deleteCompanyDTO) {
        this.baseMapper.deleteById(deleteCompanyDTO.getId());

        companyPhotoMapper.delete(new QueryWrapper<CompanyPhotoDO>()
                .lambda()
                .eq(CompanyPhotoDO::getCompanyId, deleteCompanyDTO.getId()));

        companyLabelMapper.delete(new QueryWrapper<CompanyLabelDO>()
                .lambda()
                .eq(CompanyLabelDO::getCompanyId, deleteCompanyDTO.getId()));

        companyRelateWorkMapper.delete(new QueryWrapper<CompanyRelateWorkDO>()
                .lambda()
                .eq(CompanyRelateWorkDO::getCompanyId, deleteCompanyDTO.getId()));

        CompanyUpdateHistoryDO companyUpdateHistoryDO = new CompanyUpdateHistoryDO();
        companyUpdateHistoryDO.setOpr("删除了企业");
        companyUpdateHistoryDO.setAdministerId(deleteCompanyDTO.getUserId());
        companyUpdateHistoryDO.setModule(deleteCompanyDTO.getModule());
        companyUpdateHistoryMapper.insert(companyUpdateHistoryDO);

        return ConstantUtils.DELETE_SUCCESS;
    }


    @Transactional(rollbackFor = Exception.class)
    public String addCompany(AddCompanyDTO addCompanyDTO) {

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

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

        CompanyDictDO companyDictDO = new CompanyDictDO();
        BeanUtils.copyProperties(addCompanyDTO, companyDictDO);
        if (null != addCompanyDTO.getStationId()) {
            WorkstationDictDO workstationDictDO = workstationDictMapper.selectById(addCompanyDTO.getStationId());
            if (null != workstationDictDO) {
                companyDictDO.setCompanyOrigin(workstationDictDO.getWorkstationName());
            }
        }
        this.baseMapper.insert(companyDictDO);
        List<CompanyPhotoDO> companyPhotoDOS = addCompanyDTO.getCompanyPhotoDOS();
        for (CompanyPhotoDO companyPhotoDO : companyPhotoDOS) {
            companyPhotoDO.setCompanyId(companyDictDO.getId());
            companyPhotoMapper.insert(companyPhotoDO);
        }

        List<CompanyLabelDO> companyLabelDOList = addCompanyDTO.getCompanyLabelDOS();
        for (CompanyLabelDO companyLabelDO : companyLabelDOList) {
            companyLabelDO.setCompanyId(companyDictDO.getId());
            companyLabelMapper.insert(companyLabelDO);
        }

        Long stationId = addCompanyDTO.getStationId();
        if (null != stationId) {
            CompanyRelateWorkDO companyRelateWorkDO = new CompanyRelateWorkDO();
            companyRelateWorkDO.setCompanyId(companyDictDO.getId());
            companyRelateWorkDO.setStationId(stationId);
            companyRelateWorkMapper.insert(companyRelateWorkDO);
        }

        //记录 日志
        CompanyUpdateHistoryDO companyUpdateHistoryDO = new CompanyUpdateHistoryDO();
        companyUpdateHistoryDO.setAdministerId(addCompanyDTO.getUserId());
        companyUpdateHistoryDO.setModule(addCompanyDTO.getModule());
        companyUpdateHistoryDO.setOpr("创建了企业");
        companyUpdateHistoryDO.setCompanyId(companyDictDO.getId());
        companyUpdateHistoryMapper.insert(companyUpdateHistoryDO);

        return ConstantUtils.ADD_SUCCESS;
    }

    @Transactional
    public String updateCompany(AddCompanyDTO addCompanyDTO) {

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

        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("变更了“");


        List<CompanyPhotoDO> companyPhotoDOS = addCompanyDTO.getCompanyPhotoDOS();
        //图片
        if (null != companyPhotoDOS) {
            companyPhotoMapper.delete(new QueryWrapper<CompanyPhotoDO>()
                    .lambda()
                    .eq(CompanyPhotoDO::getCompanyId, addCompanyDTO.getId()));

            for (CompanyPhotoDO companyPhotoDO : companyPhotoDOS) {
                companyPhotoDO.setCompanyId(addCompanyDTO.getId());
                companyPhotoMapper.insert(companyPhotoDO);
            }
            stringBuffer.append("企业图片、");
        }

        //标签
        List<CompanyLabelDO> companyLabelDOS = addCompanyDTO.getCompanyLabelDOS();
        if (null != companyLabelDOS) {
            companyLabelMapper.delete(new QueryWrapper<CompanyLabelDO>()
                    .lambda()
                    .eq(CompanyLabelDO::getCompanyId, addCompanyDTO.getId()));

            for (CompanyLabelDO companyLabelDO : companyLabelDOS) {
                companyLabelDO.setCompanyId(addCompanyDTO.getId());
                companyLabelMapper.insert(companyLabelDO);
            }
            stringBuffer.append("企业标签、");
        }

        if (null != companyDictDO.getCompanyName() && !companyDictDO.getCompanyName().equals(addCompanyDTO.getCompanyName())) {
            stringBuffer.append("企业名称、");
        }
        if (null != companyDictDO.getProvince() && !companyDictDO.getProvince().equals(addCompanyDTO.getProvince())) {
            stringBuffer.append("省、");
        }
        if (null != companyDictDO.getCity() && !companyDictDO.getCity().equals(addCompanyDTO.getCity())) {
            stringBuffer.append("市、");
        }
        if (null != companyDictDO.getCounty() && !companyDictDO.getCounty().equals(addCompanyDTO.getCounty())) {
            stringBuffer.append("县、");
        }
        if (null != companyDictDO.getPositionId() && !companyDictDO.getPositionId().equals(addCompanyDTO.getPositionId())) {
            stringBuffer.append("经营范围、");
        }
        if (null != companyDictDO.getCompanyCode() && !companyDictDO.getCompanyCode().equals(addCompanyDTO.getCompanyCode())) {
            stringBuffer.append("企业代码、");
        }
        if (null != companyDictDO.getCompanyDesc() && !companyDictDO.getCompanyDesc().equals(addCompanyDTO.getCompanyDesc())) {
            stringBuffer.append("企业简介、");
        }

        String opr = stringBuffer.substring(0, stringBuffer.length() - 1) + "”";

        //修改了哪些信息
        CompanyUpdateHistoryDO companyUpdateHistoryDO = new CompanyUpdateHistoryDO();
        companyUpdateHistoryDO.setModule(addCompanyDTO.getModule());
        companyUpdateHistoryDO.setAdministerId(addCompanyDTO.getUserId());
        companyUpdateHistoryDO.setCompanyId(addCompanyDTO.getId());
        companyUpdateHistoryDO.setOpr(opr);
        companyUpdateHistoryMapper.insert(companyUpdateHistoryDO);

        return ConstantUtils.SUCCESS_UPDATE;
    }

    public CompanyVO oneCompany(CompanyDictDO companyDictDO) {

        CompanyVO companyVO = new CompanyVO();

        CompanyDictDO companyDictDO1 = this.baseMapper.selectById(companyDictDO.getId());
        BeanUtils.copyProperties(companyDictDO1, companyVO);

        if (null != companyDictDO1.getPositionId()) {
            PositionDictDO positionDictDO = positionDictMapper.selectById(companyDictDO1.getPositionId());
            companyVO.setPositionType(positionDictDO.getPositionType());
        }

        List<CompanyPhotoDO> companyPhotoDOS = companyPhotoMapper.selectList(new QueryWrapper<CompanyPhotoDO>()
                .lambda()
                .eq(CompanyPhotoDO::getCompanyId, companyDictDO1.getId()));
        companyVO.setCompanyPhotoDOS(companyPhotoDOS);

        List<CompanyLabelDO> companyLabelDOS = companyLabelMapper.selectList(new QueryWrapper<CompanyLabelDO>()
                .lambda()
                .eq(CompanyLabelDO::getCompanyId, companyDictDO1.getId()));
        companyVO.setCompanyLabelDOS(companyLabelDOS);

        //关联业务
        List<RelateWorkVO> relateWorkDOS = companyRelateWorkMapper.relateWork(companyDictDO.getId());
        companyVO.setRelateWorkVOS(relateWorkDOS);

        //历史
        List<CompanyUpdateHistoryVO> companyUpdateHistoryVOS = companyUpdateHistoryMapper.companyUpdateHistory(companyVO.getId());
        companyVO.setCompanyUpdateHistoryVOS(companyUpdateHistoryVOS);

        return companyVO;
    }

    public TotalTeamsVO totalCount(WorkstationDictDO workstationDictDO) {
        TotalTeamsVO totalTeamsVO = new TotalTeamsVO();

        Integer count = this.baseMapper.stationCompanyCount(workstationDictDO.getId());
        totalTeamsVO.setCount(count);

        return totalTeamsVO;
    }

    public IPage stationCompany(StationCompanyDTO stationCompanyDTO) {

        Page pager = new Page(stationCompanyDTO.getPageNum(), stationCompanyDTO.getPageSize());
        IPage<CompanyVO> company = this.baseMapper.stationCompany(pager, stationCompanyDTO.getStationId(), stationCompanyDTO.getCompanyName(), stationCompanyDTO.getProvince(), stationCompanyDTO.getCompanyName(), stationCompanyDTO.getCounty());

        List<CompanyVO> records = company.getRecords();
        for (CompanyVO companyVO : records) {

            //图
            List<CompanyPhotoDO> companyPhotoDOS = companyPhotoMapper.selectList(new QueryWrapper<CompanyPhotoDO>()
                    .lambda()
                    .eq(CompanyPhotoDO::getCompanyId, companyVO.getId()));
            companyVO.setCompanyPhotoDOS(companyPhotoDOS);

            //标签
            List<CompanyLabelDO> companyLabelDOS = companyLabelMapper.selectList(new QueryWrapper<CompanyLabelDO>()
                    .lambda()
                    .eq(CompanyLabelDO::getCompanyId, companyVO.getId()));
            companyVO.setCompanyLabelDOS(companyLabelDOS);

            //关联业务
            List<RelateWorkVO> relateWorkDOS = companyRelateWorkMapper.relateWork(companyVO.getId());
            companyVO.setRelateWorkVOS(relateWorkDOS);

            //历史
            List<CompanyUpdateHistoryVO> companyUpdateHistoryVOS = companyUpdateHistoryMapper.companyUpdateHistory(companyVO.getId());
            companyVO.setCompanyUpdateHistoryVOS(companyUpdateHistoryVOS);
        }
        return company;
    }
}