CompanyServiceImpl.java 4.26 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.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 javax.annotation.Resource;
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) {
        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() == userCompanyItem.getCompanyId())
                {
                    companyItem.setFlag(true);
                    break;
                }
            }
        }
        return companys;
    }

    @Override
    public AjaxResult addCompany(Company company) {
        String[] warehouse = company.getWarehouseCode().split(",");
        //更新货主
        company.setCreatedBy(ShiroUtils.getLoginName());
        company.setLastUpdatedBy(ShiroUtils.getLoginName());
        this.save(company);
        //添加货主仓库关联
        WarehouseCompany record = new WarehouseCompany();
        for (int i=0; i<warehouse.length-1; i++)
        {
            record.setCompanyId(company.getId());
            record.setCompanyCode(company.getCode());
            record.setWarehouseCode(warehouse[i]);
            warehouseCompanyService.save(record);
        }
        return AjaxResult.success("更新成功!");
    }

    @Override
    public AjaxResult updateCompany(Company company) {
        if (company.getId() == null)
            return AjaxResult.error("id为空!");
        String[] warehouse = company.getWarehouseCode().split(",");
        LambdaQueryWrapper<WarehouseCompany> lambdaQueryWrapper = Wrappers.lambdaQuery();
        warehouseCompanyService.remove(lambdaQueryWrapper.eq(WarehouseCompany::getCompanyId, company.getId()));
        //添加货主仓库关联
        WarehouseCompany record = new WarehouseCompany();
        for (int i=0; i<warehouse.length-1; i++)
        {
            record.setCompanyId(company.getId());
            record.setCompanyCode(company.getCode());
            record.setWarehouseCode(warehouse[i+1]);
            warehouseCompanyService.save(record);
        }
        //更新货主
        company.setLastUpdatedBy(ShiroUtils.getLoginName());
        this.updateById(company);
        return AjaxResult.success("更新成功!");
    }

    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);
    }
}