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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson2.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.prolog.cs.biz.outbound.Application;
import com.prolog.cs.biz.outbound.dao.PickTaskHMapper;
import com.prolog.cs.biz.outbound.model.PickTaskH;
import com.prolog.entity.vo.OutBoundRequestItemVO;
import com.prolog.entity.vo.OutBoundRequestVO;
import com.prolog.framework.authority.core.dto.UserDTO;
import com.prolog.framework.utils.SnowFlakeUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@WebAppConfiguration
public class RFPickTaskServiceImplTest {

	@Resource
	private PickTaskHMapper pickTaskHMapper;

	@Resource
	RFPickTaskServiceImpl service;

	@Test
	public void test() {

		String[] arrays = new String[]{"935620111635714048"};
		List<PickTaskH> hList = pickTaskHMapper.getTaskHdrsByReplenish(arrays);
		System.out.println(JSONObject.toJSONString(hList));
	}

	@Test
	public void updateTaskStatusAndPushEisByPassive() {

		String string = "[{\"pickTaskId\":\"935620115167891456\",\"waveId\":\"935620078651707392\"}]";

		List<PickTaskH> pickTaskHS = com.alibaba.fastjson.JSONObject.parseArray(string, PickTaskH.class);
		UserDTO user = new UserDTO();
		user.setId("EIS");
		user.setUsername("EIS");
		String[] ids = new String[]{"935620111635714048"};
//		service.updateTaskStatusAndPushEisByPassive(ids, pickTaskHS, user, DateUtil.date());
	}

	@Test
	public void activatePickTask2Eis() {
		service.activatePickTask2Eis(Sets.newHashSet("937058001389490176"), Sets.newHashSet("CKD2023102311601"));
	}

	public static void main(String[] args) {
		String json = "[{\"groupOrderPolicy\":\"0\",\"msgId\":\"9663a03b860b40109f634b1965953e27\",\"subList\":[{\"billNo\":\"CKD2023102311599\",\"businessType\":\"DO_SO\",\"companyId\":\"XT00001166\",\"companyName\":\"师宗县老百姓大药房连锁有限公司\",\"containerNo\":\"\",\"enterpriseId\":\"891063172566487040\",\"inventoryShortageType\":2,\"isCheck\":\"0\",\"isMaterial\":\"1\",\"isOver\":\"0\",\"itemId\":\"2155036\",\"locNo\":\"AGVL_LOC_LHQ_001\",\"lotId\":\"904352619110207488\",\"originalArea\":\"\",\"ownerId\":\"748963439141064888\",\"packId\":\"UOM00053\",\"priority\":10,\"qty\":\"9.0000\",\"str1\":\"1\",\"str2\":\"1\",\"targetArea\":\"\",\"targetLocation\":\"AGVL\",\"taskId\":\"937021220724592640\",\"taskLineId\":\"937021220724019200\",\"waveNo\":\"937021138540826624\",\"wmsLocationCode\":\"AGVL\",\"workType\":\"20\"},{\"billNo\":\"CKD2023102311600\",\"businessType\":\"DO_SO\",\"companyId\":\"XT00001166\",\"companyName\":\"师宗县老百姓大药房连锁有限公司\",\"containerNo\":\"\",\"enterpriseId\":\"891063172566487040\",\"inventoryShortageType\":2,\"isCheck\":\"0\",\"isMaterial\":\"1\",\"isOver\":\"0\",\"itemId\":\"2155036\",\"locNo\":\"AGVL_LOC_LHQ_001\",\"lotId\":\"904352619110207488\",\"originalArea\":\"\",\"ownerId\":\"748963439141064888\",\"packId\":\"UOM00053\",\"priority\":10,\"qty\":\"9.0000\",\"str1\":\"1\",\"str2\":\"1\",\"targetArea\":\"\",\"targetLocation\":\"AGVL\",\"taskId\":\"937021220728786944\",\"taskLineId\":\"937021220728213505\",\"waveNo\":\"937021138540826624\",\"wmsLocationCode\":\"AGVL\",\"workType\":\"20\"}],\"warehouseId\":\"891420072483098624\"}]";
		List<OutBoundRequestVO> list = com.alibaba.fastjson.JSONObject.parseArray(json, OutBoundRequestVO.class);
		String billNo = "CKD20231023115991";
		List<OutBoundRequestVO> result = Lists.newArrayList();
		for (OutBoundRequestVO vo : list) {
			List<OutBoundRequestItemVO> collect = vo.getSubList().stream().filter(v -> v.getBillNo().equals(billNo)).collect(Collectors.toList());
			if (CollUtil.isEmpty(collect)) {
				continue;
			}
			OutBoundRequestVO obrv = new OutBoundRequestVO();
			BeanUtil.copyProperties(vo, obrv);
			obrv.setMsgId(String.valueOf(SnowFlakeUtils.getId()));
			obrv.setSubList(collect);
			result.add(obrv);
		}
		System.out.println(JSONObject.toJSONString(result));
	}
}
