MOMService.java 7.24 KB
package com.huaheng.api.erp.service;

import com.alibaba.fastjson.JSON;
import com.huaheng.api.erp.domain.mom.MOMMaterial;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.materialGroup.domain.ERPMaterialGroup;
import com.huaheng.api.erp.domain.mom.MOMResult;
import com.huaheng.api.erp.domain.mom.QueryParam;
import com.huaheng.api.general.service.BasicDataApiService;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.http.HttpUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.address.service.AddressService;
import com.huaheng.pc.config.materialGroup.service.MaterialGroupService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Service
public class MOMService {
    @Resource
    private AddressService addressService;
    @Resource
    private BasicDataApiService basicDataApiService;
    @Resource
    private MaterialGroupService materialGroupService;

    public AjaxResult pullMaterialInfo(QueryParam queryParam){
        String filterString = "";
        String groupname = queryParam.getGroupname();
        queryParam.setGroupname("");
        if(queryParam!=null){
            filterString = queryParam.toJson();
        }
//        String url = "http://42.236.3.82:31789/api/MaterielInfo/GetMaterielInfo";
        String url = addressService.selectAddress(QuantityConstant.ERP_PULL_MATERIALINFO, QuantityConstant.DEFAULT_WAREHOUSE, null);
        String result = HttpUtils.sendGet(url, filterString);
        if(StringUtils.isEmpty(result)){
            return AjaxResult.error("返回为空");
        }
        MOMResult MOMResult = JSON.parseObject(result, MOMResult.class);
        if(MOMResult.hasError()){
            return AjaxResult.error(MOMResult.getMessage());
        }
        Object[] rows = MOMResult.getRows();

        if(rows.length==0){
            return AjaxResult.success();
        }
        String s = JSON.toJSONString(rows);
        MOMMaterial[] MOMMaterials = JSON.parseObject(s, MOMMaterial[].class);

        for (MOMMaterial MOMMaterial : MOMMaterials) {
            Material material = new Material();
            material.setWarehouseCode(QuantityConstant.DEFAULT_WAREHOUSE);
            material.setCompanyCode(QuantityConstant.DEFAULT_COMPANY);
            material.setType(MOMMaterial.getMaterialtype());
            material.setCode(MOMMaterial.getErpcode());
            material.setName(groupname + "   " + MOMMaterial.getMaterialname());
            material.setSpec(MOMMaterial.getSpecification());
            material.setUnit(MOMMaterial.getMaterialunit());
            material.setMaterialguid(MOMMaterial.getMaterialguid());
            material.setBrand(MOMMaterial.getMateriallog());
            material.setTexture(MOMMaterial.getTexture());

            basicDataApiService.material(material);
        }

        return AjaxResult.success();
    }

    private ERPMaterialGroup[] pullMaterialGroup(){
        ERPMaterialGroup[] materialGroups = null;
        String url = addressService.selectAddress(QuantityConstant.ERP_PULL_MATERIALGROUP, QuantityConstant.DEFAULT_WAREHOUSE, null);
//        String url = "http://42.236.3.82:31789/api/MaterielGroup/GetMaterielGroup";
        String res = HttpUtils.sendGet(url,null);
//        String res = "{\"success\":true,\"rows\":[{\"iid\":9597,\"createtime\":\"2022-05-21T00:00:00\",\"createuser\":null,\"groupguid\":\"6426a90f-0699-4586-9297-47c0f8646a57\",\"erpguidguid\":\"100623\",\"erppareneguid\":\"0\",\"groupname\":\"标准机械\",\"groupcode\":\"41\",\"isdelete\":0,\"parentguid\":\"0\",\"children\":[{\"iid\":9565,\"createtime\":\"2022-05-21T00:00:00\",\"createuser\":null,\"groupguid\":\"a063dc24-045e-43f0-9c66-67600173258d\",\"erpguidguid\":\"102818\",\"erppareneguid\":\"100623\",\"groupname\":\"过滤器\",\"groupcode\":\"41.10\",\"isdelete\":0,\"parentguid\":\"6426a90f-0699-4586-9297-47c0f8646a57\",\"children\":[{\"iid\":9561,\"createtime\":\"2022-05-21T00:00:00\",\"createuser\":null,\"groupguid\":\"1109c6c9-da25-4e70-a745-d508210444b0\",\"erpguidguid\":\"127116\",\"erppareneguid\":\"102818\",\"groupname\":\"浸没式超滤膜组件\",\"groupcode\":\"41.10.105\",\"isdelete\":0,\"parentguid\":\"a063dc24-045e-43f0-9c66-67600173258d\",\"children\":[]},{\"iid\":9562,\"createtime\":\"2022-05-21T00:00:00\",\"createuser\":null,\"groupguid\":\"d374ba2e-6d82-4a23-82ec-0b46d95dd584\",\"erpguidguid\":\"127126\",\"erppareneguid\":\"102818\",\"groupname\":\"浸没式超滤膜组件\",\"groupcode\":\"41.10.106\",\"isdelete\":0,\"parentguid\":\"a063dc24-045e-43f0-9c66-67600173258d\",\"children\":[]},{\"iid\":9563,\"createtime\":\"2022-05-21T00:00:00\",\"createuser\":null,\"groupguid\":\"8ad69e5d-9040-4a54-8e5e-c137bc709cf6\",\"erpguidguid\":\"127127\",\"erppareneguid\":\"102818\",\"groupname\":\"Y型过滤器 UPVC\",\"groupcode\":\"41.10.451\",\"isdelete\":0,\"parentguid\":\"a063dc24-045e-43f0-9c66-67600173258d\",\"children\":[]},{\"iid\":9564,\"createtime\":\"2022-05-21T00:00:00\",\"createuser\":null,\"groupguid\":\"2d867697-bb11-4471-9386-8db835b56fb5\",\"erpguidguid\":\"130029\",\"erppareneguid\":\"102818\",\"groupname\":\"NF膜组件\",\"groupcode\":\"41.10.211\",\"isdelete\":0,\"parentguid\":\"a063dc24-045e-43f0-9c66-67600173258d\",\"children\":[]}]}]}]}";
        if(StringUtils.isEmpty(res)){
            return materialGroups;
        }
        MOMResult result = JSON.parseObject(res, MOMResult.class);
        Object[] rows = result.getRows();
        String s = JSON.toJSONString(rows);
        materialGroups = JSON.parseObject(s, ERPMaterialGroup[].class);
        return materialGroups;
    }

    public AjaxResult pullMaterialGroups(){

        ERPMaterialGroup[] erpMaterialGroups = pullMaterialGroup();
        if(erpMaterialGroups == null || erpMaterialGroups.length==0){
            return AjaxResult.error();
        }
        List<ERPMaterialGroup> erpMaterialGroups1 = getArrays(Arrays.asList(erpMaterialGroups));
        materialGroupService.truncateTable();
        int i = materialGroupService.insertBatch(erpMaterialGroups1);
        if(i<=0){
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }

    private List<ERPMaterialGroup> getArrays(List<ERPMaterialGroup> erpMaterialGroups){
        List<ERPMaterialGroup> arrayList = new ArrayList<>();
        for (ERPMaterialGroup erpMaterialGroup : erpMaterialGroups) {
            if(erpMaterialGroup.getChildren().size()==0){
                arrayList.add(erpMaterialGroup);
                continue;
            }
            arrayList.addAll(erpMaterialGroup.getChildren());
            erpMaterialGroup.setChildren(Collections.emptyList());
            arrayList.add(erpMaterialGroup);
            arrayList = getArrays(arrayList);
        }
        return arrayList;
    }

    public void sycMaterial(){
        List<ERPMaterialGroup> list = materialGroupService.list();
        if(list==null || list.size()==0){
            return;
        }
        for (ERPMaterialGroup erpMaterialGroup : list) {
            pullMaterialInfo(new QueryParam().setGroupguid(erpMaterialGroup.getGroupguid()).setGroupname(erpMaterialGroup.getGroupname()));
        }

    }


}