MaterialAPIService.java 6.15 KB
package com.huaheng.api.U8.Service;

import com.huaheng.api.U8.domain.ICSMaterialModel;
import com.huaheng.api.general.service.BasicDataApiService;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.framework.web.service.DictService;
import com.huaheng.pc.general.company.domain.CompanyWu;
import com.huaheng.pc.general.company.domain.WarehouseCompany;
import com.huaheng.pc.general.company.mapper.WarehouseCompanyMapperAuto;
import com.huaheng.pc.general.company.service.ICompanyService;
import com.huaheng.pc.general.material.domain.Material;
import com.huaheng.pc.general.material.service.IMaterialService;
import com.huaheng.pc.system.dict.domain.DictData;
import com.huaheng.pc.system.dict.mapper.DictDataMapper;
import com.huaheng.pc.system.dict.service.IDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

import static java.util.Comparator.comparing;
import static java.util.Comparator.comparingLong;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

@Service
public class MaterialAPIService {

    @Autowired
    BasicDataApiService basicDataApiService;
    @Autowired
    private ICompanyService companyService;
    @Autowired
    IMaterialService materialService;
    @Resource
    private DictDataMapper dictDataMapper;
    @Autowired
    private IDictDataService dictDataService;


    @Resource
    private WarehouseCompanyMapperAuto warehouseCompanyMapperAuto;

    @Transactional
    public AjaxResult ICSInventory(List<ICSMaterialModel> iCSInventorys)    {
        WarehouseCompany warehouseCompany = new WarehouseCompany();
        AjaxResult ajaxResult = new AjaxResult();
        List<Material> materials=new ArrayList<>();
        List<DictData> dictData1s=new ArrayList<>();
        for(ICSMaterialModel iCSInventory:iCSInventorys) {
            CompanyWu companyWu=new CompanyWu();
            companyWu.setuCompanyCode(iCSInventory.getCompanyCode());
            companyWu=companyService.selectCompanyWu(companyWu);
            if (companyWu == null) {
                return AjaxResult.error("货主不存在");
            }
            warehouseCompany.setCompanyCode(companyWu.getCompanyCode());
            List<WarehouseCompany> list = warehouseCompanyMapperAuto.selectListEntityByEqual(warehouseCompany);
            if (list == null || list.size() == 0) {
                return AjaxResult.error("系统中没有该货主:" + warehouseCompany.toString() + "  信息,请先录入货主信息!");
            }
            //先查询货主对应的仓库,有几个仓库,就循环几次来添加。
            for (WarehouseCompany item : list) {
//            DictData dictData = new DictData();
//            dictData.setDictType("materialType");
//            dictData.setWarehouseCode(item.getWarehouseCode());
//            dictData.setDictValue(iCSInventory.getcInvCCode());
//            dictData.setDictLabel(iCSInventory.getcInvCName());
//            dictData.setEnable(true);
                Material material = new Material();
                material.setCode(iCSInventory.getcInvCode());
                material.setBarcode(iCSInventory.getcInvAddCode());
                material.setName(iCSInventory.getcInvName());
                material.setUserDef1(iCSInventory.getcInvAddCode());
                material.setSpecification(iCSInventory.getcInvStd());
                material.setMasterUnit(iCSInventory.getcComUnitName());
                material.setAssistUnit(iCSInventory.getcAssComUnitCode());
                material.setConvertRate(iCSInventory.getfConvertRate());
                material.setType(iCSInventory.getcInvCCode());
                material.setDictLabel(iCSInventory.getcInvCName());
                material.setCompanyId(item.getCompanyId());
                material.setCompanyCode(item.getCompanyCode());
                material.setWarehouseId(item.getWarehouseId());
                material.setWarehouseCode(item.getWarehouseCode());
                material.setLastUpdatedBy(ShiroUtils.getLoginName());
                material.setCreatedBy(ShiroUtils.getLoginName());

                Material mr=new Material();
                mr.setCode(material.getCode());
                mr.setWarehouseCode(material.getWarehouseCode());
                if(materialService.selectFirstEntity(mr)==null) {
                    materials.add(material);
                }

                //字典

                boolean result = dictData1s.stream().anyMatch(x -> x.getDictValue().equals(material.getType()) &&
                                                                    x.getDictType().equals("materialType") &&
                                                                    x.getWarehouseCode().equals(material.getWarehouseCode())) ;
                if (result == false)   {
                    DictData dictData=new DictData();
                    dictData.setHeaderId(17);
                    dictData.setWarehouseId(material.getWarehouseId());
                    dictData.setWarehouseCode(material.getWarehouseCode());
                    dictData.setDictValue(material.getType());
                    dictData.setDictLabel(material.getDictLabel());
                    dictData.setDictType("materialType");
                    dictData.setCreateBy(ShiroUtils.getLoginName());

                    DictData dd=new DictData();
                    dd.setWarehouseCode(dictData.getWarehouseCode());
                    dd.setDictType(dictData.getDictType());
                    dd.setDictValue(dictData.getDictValue());
                    dd=dictDataService.selectModel(dd);
                    if(dd==null) {
                        dictData1s.add(dictData);
                    }
                }
            }
        }
        if(materials.size()>0) {
            materialService.insertList(materials);
        }
        if(dictData1s.size()>0) {
            dictDataMapper.insertDictDatas(dictData1s);
        }
        return AjaxResult.success("成功");
    }



}