package com.prolog.cs.biz.outbound.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import com.prolog.cs.biz.outbound.core.SpringUtils;
import com.prolog.cs.biz.outbound.dao.BarcodeDMapper;
import com.prolog.cs.biz.outbound.dao.BarcodeHMapper;
import com.prolog.cs.biz.outbound.dao.OmsDoBillHMapper;
import com.prolog.cs.biz.outbound.dao.PickTaskDMapper;
import com.prolog.cs.biz.outbound.model.PickTaskD;
import com.prolog.cs.biz.outbound.model.vo.ReceiveDoBillVO;
import com.prolog.cs.biz.outbound.model.vo.check.OuterTaskVO;
import com.prolog.cs.biz.outbound.service.IOmsDoBillHService;
import com.prolog.cs.biz.outbound.service.MqOutBoundConsumer;
import com.prolog.framework.authority.core.dto.UserBizModel;
import com.prolog.framework.authority.core.dto.UserDTO;
import com.prolog.framework.authority.util.HystrixVarUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.core.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class TmsTest {

    @Autowired
    private PickTaskServiceImpl pickTaskService;

    @Resource
    private PickTaskDMapper pickTaskDMapper;
    @Resource
    private MqOutBoundConsumer consumer;
    @Resource
    private OmsDoBillHMapper omsDoBillHMapper;
    @Resource
    private BarcodeHMapper barcodeHMapper;
    @Resource
    private BarcodeDMapper barcodeDMapper;

    @Resource
    private PickTaskAppServiceImpl pickTaskAppService;

    @Autowired
    private IOmsDoBillHService iOmsDoBillHService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Before
    public void before() throws Exception {
        String userName = "yy";
        HystrixRequestContext.initializeContext();
        UserDTO userDTO = new UserDTO();
        userDTO.setUsername(userName);
        userDTO.setNickname(userName);
        userDTO.setId(userName);
        UserBizModel userBizModel = new UserBizModel();
        userBizModel.setEmployeeName(userName);
        userBizModel.setEmployeeNo(userName);
        userDTO.setUserBizModel(userBizModel);
        HystrixVarUtil.setUserDTO(JSONObject.toJSONString(userDTO));
    }

    @After
    public void after() throws Exception {
    }

    public void testPrintWholeLabels() throws Exception {
        List<String> pickTaskDIds = new ArrayList<>();
        //pickTaskDIds.add("704316706332078080");
        pickTaskDIds.add("731236934801821696");
        pickTaskDIds.add("731236934801821697");
        pickTaskDIds.add("731236934801821698");
        pickTaskDIds.add("731236934801821699");
        pickTaskDIds.add("731236934801821700");
        pickTaskDIds.add("731236934801821701");
        pickTaskDIds.add("731236934801821702");
        pickTaskDIds.add("731236934801821703");
        pickTaskDIds.add("731236934801821704");
        pickTaskDIds.add("731236934801821705");
        List<String> barcodeIds = new ArrayList<>();
        //pickTaskDIds.add("704316706332078080");
        //barcodeIds.add("729066700984684544");
        //{"warehouseId":"726788113300975616","type":0,"pickTaskDIds":["729064261980131328"],"barcodeIds":["729066700984684544","729066700867244032","729066700925964288"]}
        //PickTaskServiceImpl.printWholeLabels(pickTaskDIds,"599581702716788736",0,"thl");
        System.out.println(JSON.toJSONString(pickTaskService.printWholeLabels(pickTaskDIds, barcodeIds,
                "726788113300975616", 0, "thl")));
    }

    @Test
    public void testQueryOutboundDataToOms() throws Exception {
        List<String> omsBillIds = new ArrayList<>();
        omsBillIds.add("ZDCTEST1012001");
        System.out.println(JSON.toJSONString(pickTaskService.queryOutboundDataToOms(omsBillIds)));
    }

    @Test
    public void testOutboundTaskCallback() {
        String msg = prepareOutboundTaskCallback();
        Message message = new Message(msg.getBytes(StandardCharsets.UTF_8), null);

        consumer.outboundTaskCallback(message, null);
    }

    private String prepareOutboundTaskCallback() {
        String msg = "{\n" +
                "        \"containerNo\": \"100011\",\n" +
                "        \"msgId\": \"7e808ec5-86ff-4edc-8352-e281f7cc5732\",\n" +
                "        \"weight\": null,\n" +
                "        \"wholeFlag\": null,\n" +
                "        \"subList\": [\n" +
                "            {\n" +
                "                \"taskId\": \"904360184343351296\",\n" +
                "                \"taskLineId\": \"904360184355360768\",\n" +
                "                \"workType\": \"20\",\n" +
                "                \"businessType\": \"DO_SO\",\n" +
                "                \"eisTaskId\": \"C26570D01CD6480FAB881EAA04D25A05\",\n" +
                "                \"itemId\": \"2155036\",\n" +
                "                \"lotId\": \"904352619110207488\",\n" +
                "                \"packId\": \"UOM00053\",\n" +
                "                \"ownerId\": \"748963439141064888\",\n" +
                "                \"qty\": 10,\n" +
                "                \"enterpriseId\": \"891063172566487040\",\n" +
                "                \"warehouseId\": \"891420072483098624\",\n" +
                "                \"billNo\": \"904347633483649024\",\n" +
                "                \"wmsLocationCode\": \"MJZ\",\n" +
                "                \"originalArea\": null,\n" +
                "                \"locNo\": null,\n" +
                "                \"targetArea\": \"\",\n" +
                "                \"targetLocation\": \"\"\n" +
                "            }\n" +
                "        ]\n" +
                "    }";
        return msg;
    }

    @Test
    public void testIsAllDone() {
        PickTaskServiceImpl service = SpringUtils.getBeansByType(PickTaskServiceImpl.class);
        List<PickTaskD> list = pickTaskDMapper.getPickTaskDListByPickTaskIds(new String[]{"904360184343351296"});
        list.remove(7);
        List<PickTaskD> list2 = pickTaskDMapper.getPickTaskDListByPickTaskIds(new String[]{"904360184343351296"});
        System.out.println(Arrays.toString(service.isAllDone(list, list2)));
    }

    @Test
    public void testReceiveDoBill() {

        String msg = prepareReceiveDoBill();
        ReceiveDoBillVO vo = JSONObject.parseObject(msg, ReceiveDoBillVO.class);
        vo.setBillNo(IdUtil.fastSimpleUUID());
        iOmsDoBillHService.receiveDoBill(Collections.singletonList(vo));
    }

    private String prepareReceiveDoBill() {
//        String msg = "{\n" +
//                "        \"billNo\": \"OUT11006\",\n" +
//                "        \"billType\": \"1\",\n" +
//                "        \"billDate\": \"2022-09-13 00:00:00\",\n" +
//                "        \"billState\": \"1\",\n" +
//                "        \"planDeliveryDate\": \"2022-09-13 00:00:00\",\n" +
//                "        \"deliveryName\": \"1\",\n" +
//                "        \"consignorName\": \"青牛货主001\",\n" +
//                "        \"consignorCode\": \"HQN001\",\n" +
//                "        \"shipperCode\": \"XT00001166\",\n" +
//                "        \"shipperName\": \"师宗县老百姓大药房连锁有限公司\",\n" +
//                "        \"branchCode\": \"CQN001\",\n" +
//                "        \"shopCode\": \"1\",\n" +
//                "        \"shopName\": \"1\",\n" +
//                "        \"remark\": \"1\",\n" +
//                "        \"custPersonTel\": \"1\",\n" +
//                "        \"custPerson\": \"1\",\n" +
//                "        \"receiveProvinceName\": \"1\",\n" +
//                "        \"receiveCityName\": \"1\",\n" +
//                "        \"receiveDistrictName\": \"1\",\n" +
//                "        \"receiveTownName\": \"1\",\n" +
//                "        \"receiveAddress\": \"1\",\n" +
//                "        \"receiveCommunityName\": \"1\",\n" +
//                "        \"salesPersonName\": \"1\",\n" +
//                "        \"salesPersonTel\": \"1\",\n" +
//                "        \"preparedBy\": \"1\",\n" +
//                "        \"payTime\": \"2022-09-13 00:00:00\",\n" +
//                "        \"payAmount\": \"1\",\n" +
//                "        \"paySn\": \"1\",\n" +
//                "        \"carrierName\": \"顺丰\",\n" +
//                "        \"carrierCode\": \"SF\",\n" +
//                "        \"freight\": \"1\",\n" +
//                "        \"shipBillNo\": \"1\",\n" +
//                "        \"lineCode\": \"1\",\n" +
//                "        \"lineName\": \"1\",\n" +
//                "        \"priorityLevel\": \"1\",\n" +
//                "        \"arrivalMode\": \"1\",\n" +
//                "        \"transportMode\": \"1\",\n" +
//                "        \"partlyRed\": \"1\",\n" +
//                "        \"srcPlate\": \"1\",\n" +
//                "        \"srcPlateBillno\": \"1\",\n" +
//                "        \"totQty\": \"1\",\n" +
//                "        \"totPcs\": \"1\",\n" +
//                "        \"totamt\": \"1\",\n" +
//                "        \"totVolume\": \"1\",\n" +
//                "        \"totWeight\": \"1\",\n" +
//                "        \"detailList\": [\n" +
//                "            {\n" +
//                "                \"seqNo\": \"1\",\n" +
//                "                \"billNo\": \"OUT00001\",\n" +
//                "                \"itemCode\": \"TCCS1146\",\n" +
//                "                \"itemName\": \"TC测试商品\",\n" +
//                "                \"packUnits\": \"1\",\n" +
//                "                \"itemSpecs\": \"1\",\n" +
//                "                \"packTypeNum\": \"1\",\n" +
//                "                \"itemWeight\": \"1\",\n" +
//                "                \"itemVolume\": \"1\",\n" +
//                "                \"itemQty\": \"100\",\n" +
//                "                \"redQty\": \"0\",\n" +
//                "                \"deliveryQty\": \"100\",\n" +
//                "                \"price\": \"1\",\n" +
//                "                \"amt\": \"1\",\n" +
//                "                \"discountPrice\": \"1\",\n" +
//                "                \"discountAmt\": \"1\",\n" +
//                "                \"batchNo\": \"\",\n" +
//                "                \"lotNo\": \"\",\n" +
//                "                \"sterilizationBatchNo\": \"1\",\n" +
//                "                \"sterilizationTime\": \"2023-08-24 00:00:00\",\n" +
//                "                \"pdateFrom\": \"2023-08-24 00:00:00\",\n" +
//                "                \"validUntil\": \"2024-08-24 00:00:00\"\n" +
//                "            },\n" +
//                "            {\n" +
//                "                \"seqNo\": \"2\",\n" +
//                "                \"billNo\": \"OUT00001\",\n" +
//                "                \"itemCode\": \"TCCS1545\",\n" +
//                "                \"itemName\": \"TC测试商品01\",\n" +
//                "                \"packUnits\": \"1\",\n" +
//                "                \"itemSpecs\": \"1\",\n" +
//                "                \"packTypeNum\": \"1\",\n" +
//                "                \"itemWeight\": \"1\",\n" +
//                "                \"itemVolume\": \"1\",\n" +
//                "                \"itemQty\": \"100\",\n" +
//                "                \"redQty\": \"0\",\n" +
//                "                \"deliveryQty\": \"100\",\n" +
//                "                \"price\": \"1\",\n" +
//                "                \"amt\": \"1\",\n" +
//                "                \"discountPrice\": \"1\",\n" +
//                "                \"discountAmt\": \"1\",\n" +
//                "                \"batchNo\": \"\",\n" +
//                "                \"lotno\": \"\",\n" +
//                "                \"sterilizationBatchNo\": \"1\",\n" +
//                "                \"sterilizationTime\": \"2023-08-24 00:00:00\",\n" +
//                "                \"pdateFrom\": \"2023-08-24 00:00:00\",\n" +
//                "                \"validUntil\": \"2024-08-24 00:00:00\"\n" +
//                "            }\n" +
//                "        ],\n" +
//                "        \"supervisionList\": []\n" +
//                "    }";
        String msg = "[\n" +
                "  {\n" +
                "    \"billNo\": \"TTN23032051468\",\n" +
                "    \"billType\": \"1\",\n" +
                "    \"billDate\": \"2023-10-12 00:00:00\",\n" +
                "    \"billState\": \"10\",\n" +
                "    \"planDeliveryDate\": \"\",\n" +
                "    \"deliveryName\": \"\",\n" +
                "    \"consignorName\": \"\",\n" +
                "    \"consignorCode\": \"HQN001\",\n" +
                "    \"shipperCode\": \"XT00001166\",\n" +
                "    \"shipperName\": \"师宗县老百姓大药房连锁有限公司\",\n" +
                "    \"branchCode\": \"CQN001\",\n" +
                "    \"shopCode\": \"QN\",\n" +
                "    \"shopName\": \"青牛\",\n" +
                "    \"remark\": \"1\",\n" +
                "    \"custPersonTel\": \"18166021234\",\n" +
                "    \"custPerson\": \"曾先生\",\n" +
                "    \"receiveProvinceName\": \"湖北省\",\n" +
                "    \"receiveCityName\": \"仙桃市\",\n" +
                "    \"receiveDistrictName\": \"南城新区\",\n" +
                "    \"receiveTownName\": \"\",\n" +
                "    \"receiveAddress\": \"沔州大道中段29号\",\n" +
                "    \"receiveCommunityName\": \"1\",\n" +
                "    \"salesPersonName\": \"测试业务员\",\n" +
                "    \"salesPersonTel\": \"18166081899\",\n" +
                "    \"preparedBy\": \"1\",\n" +
                "    \"payTime\": \"2023-10-11 01:20:03\",\n" +
                "    \"payAmount\": \"1\",\n" +
                "    \"paySn\": \"1\",\n" +
                "    \"carrierName\": \"顺丰\",\n" +
                "    \"carrierCode\": \"SF\",\n" +
                "    \"freight\": \"1\",\n" +
                "    \"shipBillNo\": \"1\",\n" +
                "    \"lineCode\": \"1\",\n" +
                "    \"lineName\": \"1\",\n" +
                "    \"priorityLevel\": \"1\",\n" +
                "    \"arrivalMode\": \"2\",\n" +
                "    \"transportMode\": \"\",\n" +
                "    \"partlyRed\": \"\",\n" +
                "    \"srcPlate\": \"1\",\n" +
                "    \"srcPlateBillno\": \"1\",\n" +
                "    \"totQty\": \"1\",\n" +
                "    \"totPcs\": \"1\",\n" +
                "    \"totamt\": \"1\",\n" +
                "    \"totVolume\": \"1\",\n" +
                "    \"totWeight\": \"1\",\n" +
                "    \"itemHealthIndustry\": \"QN\",\n" +
                "    \"isSupportZmd\": \"1\",\n" +
                "    \"detailList\": [\n" +
                "      {\n" +
                "        \"seqNo\": \"1\",\n" +
                "        \"billNo\": \"1\",\n" +
                "        \"itemName\": \"医用外科口罩1\",\n" +
                "        \"itemCode\": \"H014733\",\n" +
                "        \"packUnits\": \"1\",\n" +
                "        \"itemSpecs\": \"1\",\n" +
                "        \"packTypeNum\": \"1\",\n" +
                "        \"itemWeight\": \"1\",\n" +
                "        \"itemVolume\": \"1\",\n" +
                "        \"itemQty\": \"1\",\n" +
                "        \"redQty\": \"\",\n" +
                "        \"deliveryQty\": \"1\",\n" +
                "        \"price\": \"1\",\n" +
                "        \"amt\": \"1\",\n" +
                "        \"discountPrice\": \"1\",\n" +
                "        \"discountAmt\": \"1\",\n" +
                "        \"batchNo\": \"1\",\n" +
                "        \"lotNo\": \"1\",\n" +
                "        \"sterilizationBatchNo\": \"1\",\n" +
                "        \"sterilizationTime\": \"2023-10-12 00:00:00\",\n" +
                "        \"pdateFrom\": \"2023-10-12 00:00:00\",\n" +
                "        \"validUntil\": \"2023-10-12 00:00:00\"\n" +
                "      }\n" +
                "    ],\n" +
                "    \"supervisionList\": [\n" +
                "      {\n" +
                "        \"supervisionCode\": \"2302000050001\",\n" +
                "        \"itemCode\": \"23.02.00005\",\n" +
                "        \"lotNo\": \"230501\",\n" +
                "        \"supervisionType\": \"大\"\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "]";
        return msg;
    }


    @Test
    public void outerCheckConfirmV2() {
        String msg = "{\"pickTaskId\":\"957351369076559872\",\"billGroupId\":\"CKD202402201609\",\"barcode\":\"SF7444513876900\",\"type\":\"1\",\"typeName\":\"整件\",\"checkStatus\":\"1\",\"qty\":1}";
        OuterTaskVO vo = JSONObject.parseObject(msg, OuterTaskVO.class);

        pickTaskAppService.outerCheckConfirmV2(vo);
    }
}
