CompanyServiceImpl.java 5 KB
package com.huaheng.pc.config.company.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.company.domain.Company;
import com.huaheng.pc.config.company.mapper.CompanyMapper;
import com.huaheng.pc.config.warehouseCompany.domain.WarehouseCompany;
import com.huaheng.pc.config.warehouseCompany.service.WarehouseCompanyService;
import com.huaheng.pc.system.user.domain.UserCompany;
import com.huaheng.pc.system.user.mapper.UserCompanyMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("companyService")
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService {

    @Resource
    private CompanyMapper companyMapper;
    @Resource
    private UserCompanyMapper userCompanyMapper;
    @Resource
    private WarehouseCompanyService warehouseCompanyService;



    @Override
    public List<Company> selectCompanyByCurrentUserId() {
        return companyMapper.selectCompanyByUserId(ShiroUtils.getUserId(), ShiroUtils.getWarehouseCode());
    }

    /**
     *  查询出仓库所有的货主,并且根据用户id查询对应的货主,包含的就将flag赋为true
     * @param
     * @return
     */
    @Override
    public List<Company> selectCompanyByUserId(Integer id) {
        if (id == 0) {
            id = ShiroUtils.getUserId();
        }
        UserCompany condition = new UserCompany();
        condition.setUserId(id);
        List<UserCompany> userCompanys = userCompanyMapper.selectListEntityByEqual(condition);
        Company company = new Company();
        company.setWarehouseCode(ShiroUtils.getWarehouseCode());
        List<Company> companys = companyMapper.selectByWarehouseCode(company);
        for (Company companyItem : companys)
        {
            for (UserCompany userCompanyItem : userCompanys)
            {
                if (companyItem.getId().equals(userCompanyItem.getCompanyId()))
                {
                    companyItem.setFlag(true);
                    break;
                }
            }
        }
        return companys;
    }

    /**
     * 新增货主
     * */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult addCompany(Company company) {
        /*货主新增时只针对当前登录仓库*/
        company.setDeleted(false);
        //添加货主及仓库关联
        WarehouseCompany record = new WarehouseCompany();
        LambdaQueryWrapper<Company> companyLambdaQueryWrapper = Wrappers.lambdaQuery();
        companyLambdaQueryWrapper.eq(Company::getCode,company.getCode());
        companyLambdaQueryWrapper.eq(Company::getWarehouseCode,company.getWarehouseCode());
        Company tmp = this.getOne(companyLambdaQueryWrapper);
        if(!StringUtils.isNull(tmp)){
            return AjaxResult.error("货主编码重复,请重新输入编码!");
        }
        int c = companyMapper.insertReturnId(company);
        if(c < 1){
            throw new ServiceException("插入货主失败!");
        }
        record.setCompanyId(company.getId());
        record.setCompanyCode(company.getCode());
        record.setWarehouseCode(company.getWarehouseCode());
        Boolean w = warehouseCompanyService.save(record);
        if(!w){
            throw new ServiceException("插入货主仓库关联失败!");
        }
        return AjaxResult.success("新增货主成功!");
    }

    /**
     * 修改货主
     * */
    @Override
    public AjaxResult updateCompany(Company company) {
        //无法修改货主仓库,当前货主新增仓库时,新增该仓库的货主
        if (company.getId() == null) {
            return AjaxResult.error("货主ID为空!");
        }
        //更新货主
        company.setLastUpdatedBy(ShiroUtils.getLoginName());
        company.setLastUpdated(new Date());
        this.updateById(company);
        return AjaxResult.success("修改成功!");
    }

    @Override
    public List<Map<String, Object>> getCode(){
        LambdaQueryWrapper<Company> lambda = Wrappers.lambdaQuery();
        lambda.select(Company::getCode, Company::getId, Company::getName)
                .eq(Company::getWarehouseCode, ShiroUtils.getWarehouseCode());
        return this.listMaps(lambda);
    }

    @Override
    public List<Company> selectListEntityByEqual(Company condition) {
        return companyMapper.selectListEntityByEqual(condition);
    }

    @Override
    public List<Company> loginGetCompany(Integer userId, String warehouseCode) {
        return companyMapper.loginGetCompany(userId,warehouseCode);
    }
}