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

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.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.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;

    @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> getCurrentCompanyList() {
        Company condition = new Company();
        condition.setDeleted(false);
        condition.setWarehouseId(ShiroUtils.getWarehouseId());
        return companyMapper.getCurrentCompanyList(condition);
    }

    @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 record = new Company();
            record.setId(id);
            record.setDeleted(true);
            record.setLastUpdatedBy(ShiroUtils.getLoginName());
            this.updateByModel(record);

            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("更新成功!");
    }



}