package com.zy.asrs.openapi.task; 
 | 
  
 | 
import com.alibaba.fastjson.JSON; 
 | 
import com.alibaba.fastjson.JSONObject; 
 | 
import com.alibaba.fastjson.serializer.SerializerFeature; 
 | 
import com.alibaba.fastjson.support.config.FastJsonConfig; 
 | 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; 
 | 
import com.zy.asrs.common.domain.dto.ReportStockDto; 
 | 
import com.zy.asrs.common.domain.enums.ApiType; 
 | 
import com.zy.asrs.common.domain.enums.OrderSettleType; 
 | 
import com.zy.asrs.common.openapi.entity.ApiList; 
 | 
import com.zy.asrs.common.openapi.entity.HostKey; 
 | 
import com.zy.asrs.common.openapi.service.ApiListService; 
 | 
import com.zy.asrs.common.openapi.service.HostKeyService; 
 | 
import com.zy.asrs.common.utils.HttpHandler; 
 | 
import com.zy.asrs.common.wms.entity.Order; 
 | 
import com.zy.asrs.common.wms.entity.OrderDetl; 
 | 
import com.zy.asrs.common.wms.service.LocDetlService; 
 | 
import com.zy.asrs.common.wms.service.OrderDetlService; 
 | 
import com.zy.asrs.common.wms.service.OrderService; 
 | 
import org.apache.commons.codec.digest.DigestUtils; 
 | 
import org.springframework.beans.factory.annotation.Autowired; 
 | 
import org.springframework.scheduling.annotation.Scheduled; 
 | 
import org.springframework.stereotype.Component; 
 | 
  
 | 
import java.util.Date; 
 | 
import java.util.HashMap; 
 | 
import java.util.List; 
 | 
import java.util.Map; 
 | 
  
 | 
@Component 
 | 
public class ReportApiScheduler { 
 | 
  
 | 
    @Autowired 
 | 
    private ApiListService apiListService; 
 | 
    @Autowired 
 | 
    private OrderService orderService; 
 | 
    @Autowired 
 | 
    private OrderDetlService orderDetlService; 
 | 
    @Autowired 
 | 
    private HostKeyService hostKeyService; 
 | 
    @Autowired 
 | 
    private LocDetlService locDetlService; 
 | 
  
 | 
    @Scheduled(cron = "0/3 * * * * ? ") 
 | 
    public void orderComplete() { 
 | 
        //订单完成上报 
 | 
  
 | 
        //获取已完成订单 
 | 
        List<Order> list = orderService.list(new LambdaQueryWrapper<Order>() 
 | 
                .eq(Order::getSettle, OrderSettleType.COMPLETE.id)); 
 | 
        for (Order order : list) { 
 | 
            //查询是否需要上报 
 | 
            ApiList apiList = apiListService.getOne(new LambdaQueryWrapper<ApiList>() 
 | 
                    .eq(ApiList::getType, ApiType.ORDER_COMPLETE.type) 
 | 
                    .eq(ApiList::getStatus, 1) 
 | 
                    .eq(ApiList::getHostId,order.getHostId())); 
 | 
            if (apiList == null) { 
 | 
                continue; 
 | 
            } 
 | 
  
 | 
            //获取订单明细 
 | 
            List<OrderDetl> orderDetls = orderDetlService.list(new LambdaQueryWrapper<OrderDetl>() 
 | 
                    .eq(OrderDetl::getOrderId, order.getId())); 
 | 
  
 | 
            //封装数据 
 | 
            HashMap<String, Object> data = new HashMap<>(); 
 | 
            data.put("order", order); 
 | 
            data.put("orderDetls", orderDetls); 
 | 
  
 | 
            getSign(data, order.getHostId());//获取签名 
 | 
  
 | 
            try { 
 | 
                String response = new HttpHandler.Builder() 
 | 
                        .setUri(apiList.getUrl()) 
 | 
                        .setJson(JSON.toJSONString(data)) 
 | 
                        .build() 
 | 
                        .doPost(); 
 | 
                JSONObject result = JSON.parseObject(response); 
 | 
                if (result.getOrDefault("code", 0).equals(200)) { 
 | 
                    order.setSettle(OrderSettleType.REPORT_COMPLETE.id.longValue()); 
 | 
                    order.setUpdateTime(new Date()); 
 | 
                    orderService.updateById(order);//更新订单状态 
 | 
                } 
 | 
            } catch (Exception e) { 
 | 
                e.printStackTrace(); 
 | 
            } 
 | 
        } 
 | 
    } 
 | 
  
 | 
    @Scheduled(cron = "0/3 * * * * ? ") 
 | 
    public void orderCancel() { 
 | 
        //订单取消上报 
 | 
  
 | 
        //获取已取消订单 
 | 
        List<Order> list = orderService.list(new LambdaQueryWrapper<Order>() 
 | 
                .eq(Order::getSettle, OrderSettleType.CANCEL.id)); 
 | 
        for (Order order : list) { 
 | 
            //查询是否需要上报 
 | 
            ApiList apiList = apiListService.getOne(new LambdaQueryWrapper<ApiList>() 
 | 
                    .eq(ApiList::getType, ApiType.ORDER_CANCEL.type) 
 | 
                    .eq(ApiList::getStatus, 1) 
 | 
                    .eq(ApiList::getHostId,order.getHostId())); 
 | 
            if (apiList == null) { 
 | 
                continue; 
 | 
            } 
 | 
  
 | 
            //获取订单明细 
 | 
            List<OrderDetl> orderDetls = orderDetlService.list(new LambdaQueryWrapper<OrderDetl>() 
 | 
                    .eq(OrderDetl::getOrderId, order.getId())); 
 | 
  
 | 
            //封装数据 
 | 
            HashMap<String, Object> data = new HashMap<>(); 
 | 
            data.put("order", order); 
 | 
            data.put("orderDetls", orderDetls); 
 | 
  
 | 
            getSign(data, order.getHostId());//获取签名 
 | 
  
 | 
            try { 
 | 
                String response = new HttpHandler.Builder() 
 | 
                        .setUri(apiList.getUrl()) 
 | 
                        .setJson(JSON.toJSONString(data)) 
 | 
                        .build() 
 | 
                        .doPost(); 
 | 
                JSONObject result = JSON.parseObject(response); 
 | 
                if (result.getOrDefault("code", 0).equals(200)) { 
 | 
                    order.setSettle(OrderSettleType.REPORT_COMPLETE.id.longValue()); 
 | 
                    order.setUpdateTime(new Date()); 
 | 
                    orderService.updateById(order);//更新订单状态 
 | 
                } 
 | 
            } catch (Exception e) { 
 | 
                e.printStackTrace(); 
 | 
            } 
 | 
        } 
 | 
    } 
 | 
  
 | 
    @Scheduled(cron = "* 30 23 * * ? ") 
 | 
    public void reportStock() { 
 | 
        //库存上报 
 | 
        //每天晚上11点半执行 
 | 
  
 | 
        //查询是否需要上报 
 | 
        List<ApiList> list = apiListService.list(new LambdaQueryWrapper<ApiList>() 
 | 
                .eq(ApiList::getType, ApiType.REPORT_STOCK.type) 
 | 
                .eq(ApiList::getStatus, 1)); 
 | 
        for (ApiList apiList : list) { 
 | 
            List<ReportStockDto> reportStockDto = locDetlService.getReportStockDto(apiList.getHostId()); 
 | 
            //封装数据 
 | 
            HashMap<String, Object> data = new HashMap<>(); 
 | 
            data.put("stock", reportStockDto); 
 | 
            getSign(data, apiList.getHostId());//获取签名 
 | 
  
 | 
            //保留null值 
 | 
            FastJsonConfig fastJsonConfig = new FastJsonConfig(); 
 | 
            fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty); 
 | 
            String paramData = JSON.toJSONString(data, fastJsonConfig.getSerializeConfig(), fastJsonConfig.getSerializerFeatures()); 
 | 
            try { 
 | 
                String response = new HttpHandler.Builder() 
 | 
                        .setUri(apiList.getUrl()) 
 | 
                        .setJson(JSON.toJSONString(paramData)) 
 | 
                        .build() 
 | 
                        .doPost(); 
 | 
                JSONObject result = JSON.parseObject(response); 
 | 
                if (result.getOrDefault("code", 0).equals(200)) { 
 | 
  
 | 
                } 
 | 
            } catch (Exception e) { 
 | 
                e.printStackTrace(); 
 | 
            } 
 | 
        } 
 | 
  
 | 
    } 
 | 
  
 | 
    //获取签名 
 | 
    public String getSign(HashMap<String, Object> data, Long hostId) { 
 | 
        HostKey hostKey = hostKeyService.getOne(new LambdaQueryWrapper<HostKey>() 
 | 
                .eq(HostKey::getHostId, hostId)); 
 | 
        if (hostKey == null) { 
 | 
            return null; 
 | 
        } 
 | 
  
 | 
        if (hostKey.getSignStatus() == 0) {//没有开启签名 
 | 
            return null; 
 | 
        } 
 | 
  
 | 
        //创建自然排序map 
 | 
        JSONObject param = new JSONObject(true); 
 | 
        for (Map.Entry<String, Object> entry : data.entrySet()) { 
 | 
            if (!entry.getKey().equals("sign")) {//剔除sign 
 | 
                param.put(entry.getKey(), entry.getValue()); 
 | 
            } 
 | 
        } 
 | 
        param.put("signKey", hostKey.getSignKey()); 
 | 
        String md5Hex = DigestUtils.md5Hex(param.toJSONString()); 
 | 
        data.put("sign", md5Hex); 
 | 
        return md5Hex; 
 | 
    } 
 | 
  
 | 
} 
 |