package com.prolog.cs.biz.inbound.controller.rqc;

import com.prolog.cs.biz.inbound.dao.task.TaskQcDetailMapper;
import com.prolog.cs.biz.inbound.dto.PageParam;
import com.prolog.cs.biz.inbound.dto.rcv.RcvHDTO;
import com.prolog.cs.biz.inbound.dto.rqc.RqcReDTO;
import com.prolog.cs.biz.inbound.dto.rqc.RqcReHeadersDTO;
import com.prolog.cs.biz.inbound.dto.task.TaskQcRecheckDetailDTO;
import com.prolog.cs.biz.inbound.dto.task.TaskQcRecheckDetailRFDTO;
import com.prolog.cs.biz.inbound.service.rqc.RqcReHeadersService;
import com.prolog.cs.biz.inbound.utils.UserUtils;
import com.prolog.cs.biz.inbound.vo.RcvQueryVO;
import com.prolog.cs.biz.inbound.vo.RqcHQueryVO;
import com.prolog.cs.biz.inbound.vo.task.TaskQcDetailQueryVO;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.utils.AssertUtils;
import com.prolog.framework.utils.StringUtils;
import com.prolog.wms.base.util.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.compress.utils.Lists;
import org.springframework.web.bind.annotation.*;

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

/**
 * @Description
 * @Date 2021-04-02
 */

@Api(tags = "复检单")
@RestController
@RequestMapping("/rqcReHeadersRF")
public class RqcReHeadersRFController {

	@Resource
	private RqcReHeadersService rqcReHeadersService;
	@Resource
	private UserUtils userUtils;
	@Resource
	private TaskQcDetailMapper taskQcDetailMapper;
	/**
	 * 条件分页
	 */
	@ApiOperation("条件分页")
	@GetMapping("/page")
	public RestMessage<Page<RqcReHeadersDTO>> list(RqcHQueryVO rqcHQueryVO, PageParam pageParam) {
		return RestMessage.newInstance(true, "pageSuccess", rqcReHeadersService.findList(rqcHQueryVO, pageParam));
	}

	/**
	 * 详情
	 */
	@ApiOperation("详情(主键id)")
	@GetMapping("/findByPkId")
	public RestMessage<RqcReHeadersDTO> findByPkId(String pkId) {
		AssertUtils.isNotBlank(pkId, "主键id不可为空");
		return RestMessage.newInstance(true, "findByPkIdSuccess", rqcReHeadersService.findByPkId(pkId));
	}

	@ApiOperation("新增(级联关联表)")
	@PostMapping("/addWithRelatedRf")
	public RestMessage<Object> addWithRelated(@RequestBody RqcReDTO rqcReDTO) {
		rqcReHeadersService.addWithRelated(rqcReDTO);
		return RestMessage.newInstance(true, "addWithRelated", "");
	}

	/**
	 * 提取复检池中的任务
	 */
	@ApiOperation("提取复检池中的任务(条件分页)")
	@GetMapping("/extractRqcReTask")
	public RestMessage<Page<TaskQcRecheckDetailRFDTO>> extractRqcTask(TaskQcDetailQueryVO taskQcDetailQueryVO) {
		taskQcDetailQueryVO.setPageNum(1);
		taskQcDetailQueryVO.setPageSize(100000);
		Page<TaskQcRecheckDetailDTO> page=rqcReHeadersService.extractRqcReTask(taskQcDetailQueryVO);
		Page<TaskQcRecheckDetailRFDTO>  pageRF= PageUtil.newPage(page, TaskQcRecheckDetailRFDTO.class);
		if(pageRF.getList()==null){
			pageRF.setList(Lists.newArrayList());
		}
		for(TaskQcRecheckDetailRFDTO dto : pageRF.getList()){
			dto.setOddCaseQty(dto.getCaseQty());
			String productionDate=dto.getShowProductionDate();
			if(!StringUtils.isBlank(productionDate)){
				dto.setBatchProductionDate(productionDate.substring(0,4).concat("-").concat(productionDate.substring(4,6).concat("-").concat(productionDate.substring(6,8))));
			}
			String expiredDate=dto.getShowExpiredDate();
			if(!StringUtils.isBlank(expiredDate)){
				dto.setBatchExpiredDate(expiredDate.substring(0,4).concat("-").concat(expiredDate.substring(4,6).concat("-").concat(expiredDate.substring(6,8))));
			}
		}
		return RestMessage.newInstance(true, "extractRqcTask",pageRF);
	}

	/**
	 * 找寻待复质检的rcv头表
	 */
	@ApiOperation("提取待复质检的rcv头表")
	@GetMapping("/extractRcvH")
	public RestMessage<Page<RcvHDTO>> extractRcvH(RcvQueryVO rcvQueryVO, PageParam pageParam) {
//		UserDTO userDTO = userUtils.getUserDTO();
//		UserBizModel userBizModel = userDTO.getUserBizModel();
//		Set<String> consignors = userBizModel.getConsignors();
//		if (!ObjectUtils.isNull(consignors)) {
//			List<String> list = new ArrayList<>(consignors);
//			String cid = list.get(0);
//			rcvQueryVO.setMtlOwnerId(cid);
//		}
		Page<RcvHDTO> page=rqcReHeadersService.extractRcvHAndPro(rcvQueryVO, pageParam);
		if(page!=null && page.getList()!=null && page.getList().size()>0){
			for(RcvHDTO rcvHDTO : page.getList()){
				List<String> distCodes= Lists.newArrayList();
				distCodes.add("oms_asn_bill_type");
				List<Map<String,String>> distInfos =taskQcDetailMapper.queryDictInfo(distCodes);
				if(distInfos!=null && distInfos.size()>0){
					List<Map<String,String>> billTypeList=distInfos.stream().filter(dist-> Objects.equals(dist.get("type_code"),"oms_asn_bill_type")).collect(Collectors.toList());
					for(Map<String,String> map : billTypeList){
						if(Objects.equals(map.get("dict_code"),rcvHDTO.getRcvType())){
							rcvHDTO.setRcvTypeName(map.get("dict_name"));
							break;
						}
					}
				}
			}
		}
		return RestMessage.newInstance(true, "extractRcvH",page);
	}

}
