package com.ruoyi.wms.service;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.wms.po.OrderInfo;
import com.ruoyi.wms.repository.OrderInfoRepository;
import com.ruoyi.wms.util.HttpRequest;
import com.ruoyi.wms.vo.OrderQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 李俊辉
 * @version 1.0
 * date: 2023/6/26
 */
@Slf4j
@Service
public class OrderService {
    @Autowired
    private OrderInfoRepository orderInfoRepository;

    /**
     * 获取甲方任务单列表
     * @param orderQueryVo 请求参数
     */
    public List<OrderInfo> findByOrderQueryVo(OrderQueryVo orderQueryVo) {
        String newBillCode = orderQueryVo.getBillCode().concat("scrq_znbyfb");
        final String token = string2MD5(newBillCode);
        orderQueryVo.setToken(token);
        List<OrderInfo> orderInfoList = getSeOrder(JSONObject.toJSONString(orderQueryVo));
        return orderInfoList;
    }

    /**
     * 调用甲方查找任务单接口
     * @param param 请求参数
     */
    public List<OrderInfo> getSeOrder(String param) {
        final String result = HttpRequest.doPost("http://192.168.1.3:9528/getSeOrder", param);
        //log.info(result);
        List<OrderInfo> orderInfoList = new ArrayList<>();
        if (JSONObject.parseObject(result).getInteger("code") == 0) {
            for (int i = 0; i < JSONObject.parseObject(result).getJSONArray("data").size(); i++) {
                String productionTask = JSONObject.parseObject(JSONObject.toJSONString(JSONObject.parseObject(result)
                        .getJSONArray("data").get(i))).getString("生产任务单");
                Integer planQty = Integer.parseInt(JSONObject.parseObject(JSONObject.toJSONString(JSONObject.parseObject(result)
                        .getJSONArray("data").get(i))).getString("数量"));
                String materialSpec = JSONObject.parseObject(JSONObject.toJSONString(JSONObject.parseObject(result)
                        .getJSONArray("data").get(i))).getString("状态");
                String materialCode = JSONObject.parseObject(JSONObject.toJSONString(JSONObject.parseObject(result)
                        .getJSONArray("data").get(i))).getString("型号");
                String materialName = JSONObject.parseObject(JSONObject.toJSONString(JSONObject.parseObject(result)
                        .getJSONArray("data").get(i))).getString("物料名称");
                String saleOrder = JSONObject.parseObject(JSONObject.toJSONString(JSONObject.parseObject(result)
                        .getJSONArray("data").get(i))).getString("销售订单");
                String plannedCompletionDate = JSONObject.parseObject(JSONObject.toJSONString(JSONObject.parseObject(result)
                        .getJSONArray("data").get(i))).getString("计划完工日期");
                OrderInfo orderInfo = new OrderInfo();
                orderInfo.setProductionTask(productionTask);
                orderInfo.setPlanQty(planQty);
                orderInfo.setMaterialCode(materialCode);
                orderInfo.setMaterialName(materialName);
                orderInfo.setMaterialSpec(materialSpec);
                orderInfo.setSaleOrder(saleOrder);
                orderInfo.setPlannedCompletionDate(plannedCompletionDate);
                orderInfoList.add(orderInfo);
            }
        }
        return orderInfoList;
    }

    /***
     * MD5加码 生成32位md5码
     */
    public static String string2MD5(String inStr) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            log.error(e.toString());
            e.printStackTrace();
            return "";
        }
        char[] charArray = inStr.toCharArray();
        byte[] byteArray = new byte[charArray.length];

        for (int i = 0; i < charArray.length; i++) {
            byteArray[i] = (byte) charArray[i];
        }
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
    }

}
