package com.zy.asrs.service.impl; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.zy.asrs.mapper.ErpTokenMapper; import com.zy.asrs.entity.ErpToken; import com.zy.asrs.service.ErpTokenService; import com.baomidou.mybatisplus.service.impl.ServiceImpl; import com.core.common.Cools; import com.zy.common.utils.HttpHandler; import com.zy.system.entity.Config; import com.zy.system.service.ConfigService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.net.URI; import java.util.Date; import java.util.HashMap; import java.util.Map; @Service("erpTokenService") public class ErpTokenServiceImpl extends ServiceImpl implements ErpTokenService { @Autowired private ConfigService configService; @Override public JSONObject refreshToken() { String appId = getConfigValue("ErpAppId", "jxlk"); String appSecrect = getConfigValue("ErpAppSecrect", "jxlk123@jxlk"); String url = getConfigValue("ErpTokenUrl", "http://127.0.0.1:8080/thb-cloud/rest"); JSONObject result = callRest(url, buildTokenParams(appId, appSecrect)); if (result == null) { return error("ERP token获取失败:空响应"); } if (!Boolean.TRUE.equals(result.getBoolean("success"))) { return result; } JSONObject data = result.getJSONObject("data"); String token = data == null ? null : data.getString("access_token"); if (Cools.isEmpty(token)) { return error("ERP token获取失败:响应未包含access_token"); } Date now = new Date(); this.baseMapper.upsertToken(appId, appSecrect, token, now); return result; } @Override public String getAccessToken() { String appId = getConfigValue("ErpAppId", "jxlk"); ErpToken one = this.selectOne(new EntityWrapper().eq("app_id", appId)); if (one != null && !Cools.isEmpty(one.getToken())) { return one.getToken(); } JSONObject refreshed = refreshToken(); if (refreshed == null || !Boolean.TRUE.equals(refreshed.getBoolean("success"))) { return null; } ErpToken again = this.selectOne(new EntityWrapper().eq("app_id", appId)); return again == null ? null : again.getToken(); } @Override public JSONObject stockIn(String labelno) { return callWareApi("stockIn", params -> { params.put("labelno", labelno); }); } @Override public JSONObject stockOut(String labelno, String tlocation) { return callWareApi("stockOut", params -> { params.put("labelno", labelno); params.put("tlocation", Cools.isEmpty(tlocation) ? "W001" : tlocation); }); } @Override public JSONObject stockBack(String labelno, Double qty, String tlocation) { return callWareApi("stockBack", params -> { params.put("labelno", labelno); params.put("qty", qty); params.put("tlocation", Cools.isEmpty(tlocation) ? "RW" : tlocation); }); } private JSONObject callWareApi(String eventcode, ParamAppender appender) { String url = getConfigValue("ErpTokenUrl", "http://127.0.0.1:8080/thb-cloud/rest"); String appId = getConfigValue("ErpAppId", "jxlk"); String token = getAccessToken(); if (Cools.isEmpty(token)) { return error("ERP access_token为空"); } Map params = new HashMap<>(); params.put("funid", "ware_api"); params.put("eventcode", eventcode); params.put("user_code", appId); params.put("access_token", token); appender.append(params); JSONObject result = callRest(url, params); if (result != null) { return result; } return error("ERP接口调用失败:空响应"); } private Map buildTokenParams(String appId, String appSecrect) { Map params = new HashMap<>(); params.put("funid", "rest_app"); params.put("eventcode", "get_token"); params.put("user_code", appId); params.put("appid", appId); params.put("appsecrect", appSecrect); return params; } private JSONObject callRest(String url, Map params) { try { URI uri = URI.create(url); String host = uri.getHost(); int port = uri.getPort(); String scheme = uri.getScheme(); String path = uri.getRawPath(); if (Cools.isEmpty(host) || Cools.isEmpty(scheme) || Cools.isEmpty(path)) { return error("ERP接口地址配置错误:" + url); } String base = host + (port > 0 ? ":" + port : ""); String resp = new HttpHandler.Builder() .setUri(base) .setHttps("https".equalsIgnoreCase(scheme)) .setPath(path) .setParams(params) .build() .doPost(); if (Cools.isEmpty(resp)) { return null; } return JSONObject.parseObject(resp); } catch (Exception e) { return error("ERP接口调用异常:" + e.getMessage()); } } private String getConfigValue(String code, String defaultValue) { try { Config config = configService.selectConfigByCode(code); if (config != null && !Cools.isEmpty(config.getValue())) { return String.valueOf(config.getValue()).trim(); } } catch (Exception ignored) { } return defaultValue; } private JSONObject error(String msg) { JSONObject obj = new JSONObject(); obj.put("success", false); obj.put("message", msg); return obj; } private interface ParamAppender { void append(Map params); } }