自动化立体仓库 - WMS系统
zwl
9 天以前 57f9a55581343a805cd589bbdc00e4d28a28aa97
src/main/java/com/zy/asrs/service/impl/CheckDetlServiceImpl.java
@@ -1,12 +1,131 @@
package com.zy.asrs.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zy.asrs.entity.CheckDetl;
import com.zy.asrs.entity.OrderDetl;
import com.zy.asrs.mapper.CheckDetlMapper;
import com.zy.asrs.service.ApiLogService;
import com.zy.asrs.service.CheckDetlService;
import com.zy.common.utils.HttpHandler;
import javafx.print.Printer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
@Service
@Slf4j
public class CheckDetlServiceImpl extends ServiceImpl<CheckDetlMapper, CheckDetl> implements CheckDetlService {
    @Value("${erp.address.URL}")
    //端口
    private String URL;
    @Value("${erp.address.outaddressSave}")
    private String uploadAddress;
    @Autowired
    private CheckDetlService checkDetlService;
    @Autowired
    private ApiLogService apiLogService;
    @Override
    @Transactional
    public void upload(List<CheckDetl> list) {
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 构建主单 JSON 数据
        JSONObject requestBody = new JSONObject();
        if (!list.isEmpty()) {
            CheckDetl first = list.get(0);
            requestBody.put("orderNo", first.getOrderNo());
        }
        // 构建物料列表
        JSONArray matList = new JSONArray();
        for(CheckDetl checkDetl : list) {
            JSONObject mat = new JSONObject();
            mat.put("detailId", checkDetl.getDetailId());
            mat.put("matnr", checkDetl.getMatnr());
            mat.put("anfme", checkDetl.getAnfme());
            mat.put("number", checkDetl.getNumber());
            matList.add(mat);
        }
        requestBody.put("data", matList);
        // 发送 POST 请求
        String response = "";
        boolean success = false;
        try {
            response = new HttpHandler.Builder()
                    .setUri(URL)
                    .setPath(uploadAddress) // 设置你的接口路径
                    .setJson(requestBody.toJSONString())
                    .build()
                    .doPost();
            JSONObject data = JSON.parseObject(response);
            Object isSuccess = findValueByKey(data, "IsSuccess");
            String bool = isSuccess != null ? isSuccess.toString() : "false";
            if ("true".equals(bool)) {
                success = true;
            }
        } catch (Exception e) {
            log.error("上报ERP失败", e);
        } finally {
            try {
                apiLogService.save(
                        "盘点上报",
                        URL + uploadAddress,
                        null,
                        "127.0.0.1",
                        requestBody.toJSONString(),
                        response,
                        success
                );
            } catch (Exception e) {
                log.error("日志保存失败", e);
            }
        }
        if(success) {
            for (CheckDetl entity : list){
                checkDetlService.delete(new EntityWrapper<>(entity));
            }
        }
    }
    public static Object findValueByKey(JSONObject json, String key) {
        Set<String> keySet = json.keySet();
        for (String k : keySet) {
            Object v = json.get(k);
            if (k.equals(key)) {
                return v;
            } else if (v instanceof JSONArray) {
                int size = ((JSONArray) v).size();
                for (int i = 0; i <= size - 1; i++) {
                    Object result = findValueByKey((JSONObject) ((JSONArray) v).get(i), key);
                    if (result != null){
                        return result;
                    }
                }
            } else if (v instanceof JSONObject){
                Object result = findValueByKey((JSONObject) v, key);
                if (result != null){
                    return result;
                }
            }
        }
        return null;
    }
}