package com.vincent.rsf.openApi.service.impl; 
 | 
  
 | 
import com.alibaba.fastjson.JSONArray; 
 | 
import com.alibaba.fastjson.JSONObject; 
 | 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; 
 | 
import com.fasterxml.jackson.core.JsonProcessingException; 
 | 
import com.fasterxml.jackson.databind.ObjectMapper; 
 | 
import com.fasterxml.jackson.databind.cfg.CoercionAction; 
 | 
import com.fasterxml.jackson.databind.cfg.CoercionInputShape; 
 | 
import com.vincent.rsf.framework.common.R; 
 | 
import com.vincent.rsf.framework.exception.CoolException; 
 | 
import com.vincent.rsf.openApi.config.PlatformProperties; 
 | 
import com.vincent.rsf.openApi.entity.Loc; 
 | 
import com.vincent.rsf.openApi.entity.constant.WmsConstant; 
 | 
import com.vincent.rsf.openApi.entity.dto.CommonResponse; 
 | 
import com.vincent.rsf.openApi.entity.constant.RcsConstant; 
 | 
import com.vincent.rsf.openApi.entity.dto.SyncLocsDto; 
 | 
import com.vincent.rsf.openApi.entity.params.ExMsgCallbackParams; 
 | 
import com.vincent.rsf.openApi.entity.params.RcsPubTaskParams; 
 | 
import com.vincent.rsf.openApi.entity.params.SyncRcsLocsParam; 
 | 
import com.vincent.rsf.openApi.mapper.LocMapper; 
 | 
import com.vincent.rsf.openApi.service.WmsRcsService; 
 | 
import lombok.extern.slf4j.Slf4j; 
 | 
import org.springframework.beans.factory.annotation.Autowired; 
 | 
import org.springframework.http.HttpEntity; 
 | 
import org.springframework.http.HttpHeaders; 
 | 
import org.springframework.http.HttpMethod; 
 | 
import org.springframework.http.ResponseEntity; 
 | 
import org.springframework.stereotype.Service; 
 | 
import org.springframework.transaction.annotation.Transactional; 
 | 
import org.springframework.web.client.RestTemplate; 
 | 
  
 | 
import java.util.List; 
 | 
import java.util.Map; 
 | 
import java.util.Objects; 
 | 
  
 | 
  
 | 
@Slf4j 
 | 
@Service("wmsRcsService") 
 | 
public class WmsRcsServiceImpl extends ServiceImpl<LocMapper, Loc> implements WmsRcsService { 
 | 
  
 | 
    @Autowired 
 | 
    private PlatformProperties rcsApi; 
 | 
  
 | 
    @Autowired 
 | 
    private PlatformProperties.WmsApi wmsApi; 
 | 
  
 | 
    @Autowired 
 | 
    private RestTemplate restTemplate; 
 | 
  
 | 
  
 | 
    /** 
 | 
     * @author Ryan 
 | 
     * @date 2025/8/27 
 | 
     * @description: 任务下发 
 | 
     * @version 1.0 
 | 
     */ 
 | 
    @Override 
 | 
    @Transactional(rollbackFor = Exception.class) 
 | 
    public CommonResponse pubTasks(RcsPubTaskParams params)  { 
 | 
        /**RCS基础配置链接*/ 
 | 
        String rcsUrl =  rcsApi.getHost() + ":" + rcsApi.getPort() + RcsConstant.pubTask; 
 | 
        log.info("任务下发,请求地址: {}, 请求参数: {}", rcsUrl , JSONObject.toJSONString(params)); 
 | 
        HttpHeaders headers = new HttpHeaders(); 
 | 
        headers.add("Content-Type", "application/json"); 
 | 
        headers.add("api-version", "v2.0"); 
 | 
        HttpEntity httpEntity = new HttpEntity(params, headers); 
 | 
        ResponseEntity<String> exchange = restTemplate.exchange(rcsUrl, HttpMethod.POST, httpEntity, String.class); 
 | 
        log.info("任务下发后,响应结果: {}", exchange); 
 | 
        if (Objects.isNull(exchange.getBody())) { 
 | 
            throw new CoolException("任务下发失败!!"); 
 | 
        } else { 
 | 
            ObjectMapper objectMapper = new ObjectMapper(); 
 | 
            objectMapper.coercionConfigDefaults() 
 | 
                    .setCoercion(CoercionInputShape.EmptyString, CoercionAction.AsEmpty); 
 | 
            try { 
 | 
                CommonResponse result = objectMapper.readValue(exchange.getBody(), CommonResponse.class); 
 | 
                if (result.getCode() == 200) { 
 | 
                    return result; 
 | 
                } else { 
 | 
                    throw new CoolException("任务下发失败!!"); 
 | 
                } 
 | 
            } catch (JsonProcessingException e) { 
 | 
                throw new CoolException(e.getMessage()); 
 | 
            } 
 | 
        } 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * @author Ryan 
 | 
     * @date 2025/8/27 
 | 
     * @description: 取消任务 
 | 
     * @version 1.0 
 | 
     */ 
 | 
    @Override 
 | 
    public CommonResponse cancelTasks(Map<String, Object> params) { 
 | 
        return  new CommonResponse(); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * @author Ryan 
 | 
     * @date 2025/8/27 
 | 
     * @description: RCS状态回写 
 | 
     * @version 1.0 
 | 
     */ 
 | 
    @Override 
 | 
    public CommonResponse callBackEvent(ExMsgCallbackParams params) { 
 | 
        String callUrl =  wmsApi.getHost() + ":" + wmsApi.getPort() + WmsConstant.callBack; 
 | 
        /**WMS基础配置链接*/ 
 | 
        log.info("任务执行状态上报,请求地址: {}, 请求参数: {}", callUrl , JSONObject.toJSONString(params)); 
 | 
        HttpHeaders headers = new HttpHeaders(); 
 | 
        headers.add("Content-Type", "application/json"); 
 | 
        headers.add("api-version", "v2.0"); 
 | 
        HttpEntity httpEntity = new HttpEntity(params, headers); 
 | 
        ResponseEntity<String> exchange = restTemplate.exchange(callUrl, HttpMethod.POST, httpEntity, String.class); 
 | 
        log.info("任务执行状态上报,响应结果: {}", exchange); 
 | 
        if (Objects.isNull(exchange.getBody())) { 
 | 
            return new CommonResponse(); 
 | 
        } else { 
 | 
            ObjectMapper objectMapper = new ObjectMapper(); 
 | 
            objectMapper.coercionConfigDefaults() 
 | 
                    .setCoercion(CoercionInputShape.EmptyString, CoercionAction.AsEmpty); 
 | 
            try { 
 | 
                CommonResponse result = objectMapper.readValue(exchange.getBody(), CommonResponse.class); 
 | 
                if (result.getCode() == 200) { 
 | 
                    return result; 
 | 
                } else { 
 | 
                    return result; 
 | 
//                    throw new CoolException("任务执行状态上报失败!!"); 
 | 
                } 
 | 
            } catch (JsonProcessingException e) { 
 | 
                throw new CoolException(e.getMessage()); 
 | 
            } 
 | 
        } 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * @author Ryan 
 | 
     * @date 2025/8/27 
 | 
     * @description: 库位信息同步 
 | 
     * @version 1.0 
 | 
     */ 
 | 
    @Override 
 | 
    public List<SyncLocsDto> syncLocs(SyncRcsLocsParam params) { 
 | 
         return syncRcsLocs(params); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * @author Ryan 
 | 
     * @date 2025/8/28 
 | 
     * @description: 拉取RCS库位,同步至本地 
 | 
     * @version 1.0 
 | 
     */ 
 | 
    @Transactional(rollbackFor = Exception.class) 
 | 
    public List<SyncLocsDto> syncRcsLocs(SyncRcsLocsParam  params) { 
 | 
        /**RCS基础配置链接*/ 
 | 
        String rcsUrl =  rcsApi.getHost() + ":" + rcsApi.getPort() + RcsConstant.syncLocs; 
 | 
        log.info("任务下发,请求地址: {}, 请求参数: {}", rcsUrl , JSONObject.toJSONString(params)); 
 | 
        HttpHeaders headers = new HttpHeaders(); 
 | 
        headers.add("Content-Type", "application/json"); 
 | 
        headers.add("api-version", "v2.0"); 
 | 
        HttpEntity httpEntity = new HttpEntity(params, headers); 
 | 
        ResponseEntity<String> exchange = restTemplate.exchange(rcsUrl, HttpMethod.POST, httpEntity, String.class); 
 | 
        log.info("任务下发后,响应结果: {}", exchange); 
 | 
        if (Objects.isNull(exchange.getBody())) { 
 | 
            throw new CoolException("任务下发失败!!"); 
 | 
        } else { 
 | 
            ObjectMapper objectMapper = new ObjectMapper(); 
 | 
            objectMapper.coercionConfigDefaults() 
 | 
                    .setCoercion(CoercionInputShape.EmptyString, CoercionAction.AsEmpty); 
 | 
            try { 
 | 
                CommonResponse result = objectMapper.readValue(exchange.getBody(), CommonResponse.class); 
 | 
                if (result.getCode() == 200) { 
 | 
                    JSONObject jsonObject = JSONObject.parseObject(exchange.getBody()).getJSONObject("data"); 
 | 
                    List<SyncLocsDto> array =  JSONArray.parseArray(jsonObject.getJSONArray("records").toJSONString(), SyncLocsDto.class); 
 | 
                    log.info("records结果: {}",  array); 
 | 
                    return array; 
 | 
                } else { 
 | 
                    throw new CoolException("任务下发失败!!"); 
 | 
                } 
 | 
            } catch (JsonProcessingException e) { 
 | 
                throw new CoolException(e.getMessage()); 
 | 
            } 
 | 
        } 
 | 
    } 
 | 
} 
 |