WarehouseU8ServiceImpl.java 8.81 KB
package com.huaheng.pc.config.warehouse.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.common.redis.service.IRedisService;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.Wrappers;
import com.huaheng.common.utils.http.HttpUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.address.domain.Address;
import com.huaheng.pc.config.address.service.AddressService;
import com.huaheng.pc.config.warehouse.domain.WarehouseU8;
import com.huaheng.pc.config.warehouse.domain.WorkOrderComponent;
import com.huaheng.pc.config.warehouse.domain.WorkOrderList;
import com.huaheng.pc.config.warehouse.mapper.WarehouseU8Mapper;
import com.huaheng.pc.config.warehouse.mapper.WorkOrderListMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("warehouseWu")
public class WarehouseU8ServiceImpl extends ServiceImpl<WarehouseU8Mapper, WarehouseU8> implements WarehouseU8Service {
    @Resource
    private AddressService addressService;
    @Resource
    private IRedisService iRedisService;
    @Resource
    private WorkOrderListMapper workOrderListMapper;




    @Override
    public AjaxResult GetCurrentStock(WorkOrderComponent data, String type) throws Exception {
        Address address = getU8Address();
        if (address == null){
            throw new Exception("未配置U8 API地址");
        }
        String url = address.getUrl() + "GetMoallocateMes";
        StringBuilder param = new StringBuilder();
        param.append("cCompanyCode=").append(data.getCCompanyCode())
//        param.append("cCompanyCode=").append("UFDATA_807_2018")
                .append("&Mocode=").append(data.getMocode())
                .append("&Moseq=").append(data.getMoseq())
                .append("&CInvcode=").append(data.getCInvcode())
                .append("&MaterialCode=").append(data.getMaterialCode());
        String result = HttpUtils.sendPost(url, param.toString());
        if (StringUtils.isEmpty(result)){
            throw new Exception("未查询到工单子件数据!");
        }
        JSONObject json = JSONObject.parseObject(result);
        JSONArray array = json.getJSONArray("data");
        if (array == null || array.size() == 0){
            throw new Exception(json.getString("msg"));
        }
        List<WorkOrderList> list = this.dataFiltering(array, type);
        final int i = (int) (Math.random()* 10);
        if(type.equals("1")&&i==7){
            WorkOrderList w=new WorkOrderList();
            w.setWorkOrderNumber(list.get(0).getWorkOrderNumber());
            List<WorkOrderList> workOrderLists = workOrderListMapper.selectByOption(w);
            if(workOrderLists!=null&&workOrderLists.size()>0){
                workOrderListMapper.deleteList(workOrderLists.get(0).getWorkOrderNumber());
            }
            for(WorkOrderList  workOrderList:list){
                workOrderListMapper.insert(workOrderList);
            }
        }
        List<WorkOrderList> workOrderLists = this.optionData(list, data.getOption());
        if (list.size() == 0){
            throw new Exception("未查询到工单子件数据!");
        }
        AjaxResult ajax = AjaxResult.success();
        ajax.setData(workOrderLists);
        iRedisService.put("CS_work_order_list", list, -1);
        return ajax;
    }

    @Override
    public Address  getU8Address() {
        Address address = addressService.getOne(new LambdaQueryWrapper<Address>()
                .eq(Address::getParam, "QITAO")
                .eq(Address::getWarehouseCode, "KS0001"));
        return address;
    }

    @Override
    public List dataFiltering(JSONArray array, String type) {
        List<WorkOrderList> list = new ArrayList<>();
        for (int i = 0; i< array.size(); i++){
            BigDecimal needQuantity = null;
            if (array.getJSONObject(i).containsKey("应领数量")){
                needQuantity = array.getJSONObject(i).getBigDecimal("应领数量");
            }
            BigDecimal workOrderQuantity = null;
            if (array.getJSONObject(i).containsKey("工单数量")){
                workOrderQuantity = array.getJSONObject(i).getBigDecimal("工单数量");
            }
            BigDecimal receiveQuantity = null;
            if (array.getJSONObject(i).containsKey("已领数量")){
                receiveQuantity = array.getJSONObject(i).getBigDecimal("已领数量");
            }
            if ("0".equals(type) && needQuantity !=null && receiveQuantity != null && needQuantity.compareTo(receiveQuantity) == 0){
                continue;
            }
            String materialCode = array.getJSONObject(i).getString("材料编码");
            if (StringUtils.isEmpty(materialCode)){
                continue;
            }
            WorkOrderList workOrderList = new WorkOrderList();
            workOrderList.setMaterialName(array.getJSONObject(i).getString("材料名称"));
            workOrderList.setOrderDate(array.getJSONObject(i).getString("订单日期"));
            workOrderList.setOrderBomCode(array.getJSONObject(i).getString("订单物料编码"));
            workOrderList.setNeedQuantity(needQuantity);
            workOrderList.setMaterialCode(materialCode);
            workOrderList.setSpecification1(array.getJSONObject(i).getString("规格1"));
            workOrderList.setName(array.getJSONObject(i).getString("名称"));
            workOrderList.setSpecification(array.getJSONObject(i).getString("规格"));
            workOrderList.setLineNumber(array.getJSONObject(i).getString("行号"));
            workOrderList.setWorkOrderQuantity(workOrderQuantity);
            workOrderList.setWorkOrderNumber(array.getJSONObject(i).getString("工单号"));
            workOrderList.setWorkOrderId(array.getJSONObject(i).getInteger("工单ID"));
            workOrderList.setWorkOrderSonId(array.getJSONObject(i).getInteger("工单子件行ID"));
            workOrderList.setReceiveQuantity(receiveQuantity);
            list.add(workOrderList);
        }
        return list;
    }

    private List<WorkOrderList> optionData(List<WorkOrderList> list,String option){
        Iterator<WorkOrderList> iterator = list.iterator();
        if(iterator.hasNext()){
            WorkOrderList next = iterator.next();
            WorkOrderList workOrderList1=new WorkOrderList();
            workOrderList1.setWorkOrderNumber(next.getWorkOrderNumber());
            workOrderList1.setMaterialCode(next.getMaterialCode());
            workOrderList1.setUserDef1(option);
            List<WorkOrderList> workOrderLists = workOrderListMapper.selectByOption(workOrderList1);
            if(workOrderLists!=null&& workOrderLists.size()>1){
                for(WorkOrderList workOrderList:workOrderLists){
                    final String materialCode = workOrderList.getMaterialCode();
                    if(materialCode.equals(next.getMaterialCode())){
                       iterator.remove();
                    }
                }
            }
        }
        return list;
    }
    @Override
    public List<Map<String, Object>> getName() {
        LambdaQueryWrapper<WarehouseU8> lambda = Wrappers.lambdaQuery();
        lambda.select(WarehouseU8::getUWarehouseCode, WarehouseU8::getId, WarehouseU8::getUWarehouseName)
                .eq(WarehouseU8::getWarehouseCode, ShiroUtils.getWarehouseCode());
        return this.listMaps(lambda);
    }

    @Override
    public List<WarehouseU8> selectList() {
        return this.list(new LambdaQueryWrapper<WarehouseU8>().eq(WarehouseU8::getWarehouseCode,ShiroUtils.getWarehouseCode()));
    }

    @Override
    public List<WarehouseU8> getByDomain(WarehouseU8 warehouseU8) {
        LambdaQueryWrapper<WarehouseU8> lambda = Wrappers.lambdaQuery();
        lambda.eq(StringUtils.isNotEmpty(warehouseU8.getWarehouseCode()),WarehouseU8::getWarehouseCode, warehouseU8.getWarehouseCode());
        lambda.eq(StringUtils.isNotEmpty(warehouseU8.getUCompanyCode()),WarehouseU8::getUCompanyCode, warehouseU8.getUCompanyCode());
        lambda.eq(StringUtils.isNotEmpty(warehouseU8.getUWarehouseCode()),WarehouseU8::getUWarehouseCode, warehouseU8.getUWarehouseCode());
        return this.list(lambda);
    }
    @Override
    public Map<String, List<WarehouseU8>> getAllWarehouseWu(){
        LambdaQueryWrapper<WarehouseU8> lambda = Wrappers.lambdaQuery();
        List<WarehouseU8> list = list(lambda);
        Map<String, List<WarehouseU8>> map = list.stream().collect(Collectors.groupingBy(WarehouseU8::getUCompanyCode));

        return map;
    }
}