CompanyServiceImpl.java 9.83 KB
package com.huaheng.pc.general.company.service;

import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.support.Convert;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.general.company.domain.Company;
import com.huaheng.pc.general.company.domain.CompanyWu;
import com.huaheng.pc.general.company.domain.WarehouseCompany;
import com.huaheng.pc.general.company.mapper.CompanyMapper;
import com.huaheng.pc.general.company.mapper.CompanyMapperAuto;
import com.huaheng.pc.general.company.mapper.WarehouseCompanyMapperAuto;
import com.huaheng.pc.general.warehouse.domain.Warehouse;
import com.huaheng.pc.general.warehouse.service.IWarehouseService;
import com.huaheng.pc.inventory.inventory.domain.Inventory;
import com.huaheng.pc.inventory.inventory.service.IInventoryService;
import com.huaheng.pc.system.user.domain.UserCompany;
import com.huaheng.pc.system.user.mapper.UserCompanyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service("company")
public class CompanyServiceImpl implements ICompanyService {

    @Resource
    private UserCompanyMapper userCompanyMapper;
    @Resource
    private CompanyMapperAuto aotuMapper;
    @Resource
    private CompanyMapper companyMapper;
    @Autowired
    private IWarehouseService warehouseService;
    @Resource
    private WarehouseCompanyMapperAuto warehouseCompanyMapperAuto;
    @Resource
    private IInventoryService inventoryService;

    @Override
    public List<Company> selectListEntityByLike(Company condition) {
        return aotuMapper.selectListEntityByLike(condition);
    }

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

    @Override
    public Company selectFirstEntity(Company condition) {
        Company item = aotuMapper.selectFirstEntity(condition);
        return item;
    }

    @Override
    public Company selectEntityById(Integer id) {
        return aotuMapper.selectEntityById(id);
    }

    @Override
    public List<Map<String, Object>> selectListMapByEqual(String columns, Company condition) {
        return aotuMapper.selectListMapByEqual(columns, condition);
    }

    @Override
    public Map<String, Object> selectFirstMap(String columns, Company condition) {
        Map<String, Object> item = aotuMapper.selectFirstMap(columns, condition);
        return item;
    }

    @Override
    public int insert(Company record) {
        return aotuMapper.insert(record);
    }

    @Override
    public int updateByModel(Company record) {
        return aotuMapper.updateByModel(record);
    }

    @Override
    public int updateByCondition(Company record, Company condition) {
        return aotuMapper.updateByCondition(record, condition);
    }

    @Override
    public int deleteById(Integer id) {
        return aotuMapper.deleteById(id);
    }

    @Override
    public int deleteByCondition(Company condition) {
        return aotuMapper.deleteByCondition(condition);
    }

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

    @Override
    public List<Company> getEnableCompanyList() {
        Company condition = new Company();
        condition.setDeleted(false);
        condition.setEnable(true);
        condition.setWarehouseId(ShiroUtils.getWarehouseId());
        return companyMapper.getCurrentCompanyList(condition);
    }


    /**
     * 查询用户对应的货主
     *
     * @return 结果
     */
    @Override
    public List<Company> selectCompanyByCurrentUserId()
    {
        List<Company> list = companyMapper.selectCompanyByUserId(ShiroUtils.getUserId(), ShiroUtils.getWarehouseId());
        return list;
    }

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

    @Override
    public List<Map<String, Object>> getCompanyParentId() {
        Company company=new Company();
        company.setDeleted(false);
        company.setEnable(true);
//        company.setType("holdingCompany");
        List<Map<String, Object>> result = companyMapper.CompanyParent(company);
        return result;
    }

    @Override
    public List<Map<String, Object>> getWarehouseList(Integer id) {
        List<Map<String, Object>> warehouseList = warehouseService.selectListMapByEqual("id,code,name,enable", new Warehouse());
        WarehouseCompany warehouseCompany = new WarehouseCompany();
        warehouseCompany.setCompanyId(id);
        List<Map<String, Object>> warehouseIds = warehouseCompanyMapperAuto.selectListMapByEqual("warehouseId", warehouseCompany);
        for (Map<String, Object> item:warehouseList)        {
           item.put("flag",false);
           item.put("value", item.get("id").toString() + "," + item.get("code").toString());
           for (Map<String, Object> warehouseId:warehouseIds)           {
               if (item.get("id").toString().equals(warehouseId.get("warehouseId").toString()))               {
                   item.put("flag",true);
                   break;
               }
           }
        }
        return warehouseList;
    }

    @Override
    public AjaxResult deletedCompany(String ids) {
        if (StringUtils.isEmpty(ids))
            return AjaxResult.error("id不能为空");
        for (Integer id : Convert.toIntArray(ids))
        {
            Company company = this.selectEntityById(id);
            Inventory inventory = new Inventory();
            inventory.setWarehouseCode(ShiroUtils.getWarehouseCode());
            inventory.setCompanyCode(company.getCode());
            Map<String, Object> map = inventoryService.selectFirstMap("id", inventory);
            if (map != null) {
                return AjaxResult.error("货主编码(" + company.getCode() +")还有库存,不能删除!");
            }
            company.setDeleted(true);
            company.setLastUpdatedBy(ShiroUtils.getLoginName());
            this.updateByModel(company);

            WarehouseCompany condition = new WarehouseCompany();
            condition.setCompanyId(id);
            warehouseCompanyMapperAuto.deleteByCondition(condition);
        }
        return AjaxResult.success("删除成功!");
    }

    @Override
    public AjaxResult updateCompany(Company company) {
        if (company.getId() == null)
            return AjaxResult.error("id为空!");
        String[] warehouse = company.getWarehouseCode().split(",");
        //删除货主仓库关联
        WarehouseCompany condition = new WarehouseCompany();
        condition.setCompanyId(company.getId());
        warehouseCompanyMapperAuto.deleteByCondition(condition);
        //添加货主仓库关联
        WarehouseCompany record = new WarehouseCompany();
        for (int i=0; i<warehouse.length-1; i=i+2)
        {
            record.setCompanyId(company.getId());
            record.setCompanyCode(company.getCode());
            record.setWarehouseId(Integer.valueOf(warehouse[i]));
            record.setWarehouseCode(warehouse[i+1]);
            warehouseCompanyMapperAuto.insert(record);
        }
        //更新货主
        company.setLastUpdatedBy(ShiroUtils.getLoginName());
        this.updateByModel(company);
        return AjaxResult.success("更新成功!");
    }

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

    @Override
    public AjaxResult checkwarehouseCompany(String code) {
        WarehouseCompany warehouseCompany = new WarehouseCompany();
        warehouseCompany.setCompanyCode(code);
        List<WarehouseCompany> list = warehouseCompanyMapperAuto.selectListEntityByEqual(warehouseCompany);
        if (list == null || list.size() == 0){
           throw new ServiceException("系统没有该货主");
        }else {
            return AjaxResult.success("请继续");
        }
    }

    @Override
    public WarehouseCompany selcetIWCBycode(WarehouseCompany code) {
        return companyMapper.selcetIWCBycode(code);
    }

    @Override
    public CompanyWu selectCompanyWu(CompanyWu companyWu) {
        return companyMapper.selectCompanyWu(companyWu);
    }


}