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

import com.prolog.cs.biz.inbound.dao.rcv.RcvHeadersMapper;
import com.prolog.cs.biz.inbound.dto.PageParam;
import com.prolog.cs.biz.inbound.dto.ReceiveAsnDTO;
import com.prolog.cs.biz.inbound.dto.rcv.RcvHDTO;
import com.prolog.cs.biz.inbound.dto.rcv.RcvLinesPicDTO;
import com.prolog.cs.biz.inbound.dto.rqc.RqcDTO;
import com.prolog.cs.biz.inbound.dto.rqc.RqcHeadersDTO;
import com.prolog.cs.biz.inbound.dto.rqc.RqcLinesDTO;
import com.prolog.cs.biz.inbound.dto.task.TaskQcDetailDTO;
import com.prolog.cs.biz.inbound.model.SupervisionCode;
import com.prolog.cs.biz.inbound.service.impl.rqc.RqcHeadersServiceImpl;
import com.prolog.cs.biz.inbound.service.rqc.RqcHeadersService;
import com.prolog.cs.biz.inbound.utils.UploadCircubillUtil;
import com.prolog.cs.biz.inbound.vo.*;
import com.prolog.cs.biz.inbound.vo.task.TaskQcDetailQueryVO;
import com.prolog.framework.authority.user.UserTemplate;
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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

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

/**
 * @Description 质检单主表
 * @Date 2021-03-24
 */

@Api(tags = "质检单")
@RestController
@RequestMapping("/rqcHeaders")
public class RqcHeadersController {

	@Resource
	private RqcHeadersService rqcHeadersService;

	@Resource
	private UserTemplate userTemplate;

	@Resource
	private RcvHeadersMapper rcvMapper;

	private static Logger logger = LoggerFactory.getLogger(RqcHeadersServiceImpl.class);


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

	/**
	 * 提取质检池中的任务
	 */
	// tianhl
	@ApiOperation("提取质检池中的任务(条件分页)")
	@GetMapping("/extractRqcTask")
	public RestMessage<Page<TaskQcDetailDTO>> extractRqcTask(TaskQcDetailQueryVO taskQcDetailQueryVO) {
		String status = taskQcDetailQueryVO.getStatus();
		if(StringUtils.isBlank(status)) {
			taskQcDetailQueryVO.setStatus("1");  // pc默认提取状态为未完成的任务
		}
		return RestMessage.newInstance(true, "extractRqcTask", rqcHeadersService.extractRqcTask(taskQcDetailQueryVO));
	}

	/**
	 * 找寻待质检的rcv头表
	 */
	// tianhl
	@ApiOperation("提取待质检的rcv头表")
	@GetMapping("/extractRcvH")
	public RestMessage<Page<RcvHDTO>> extractRcvH(RcvQueryVO rcvQueryVO, PageParam pageParam) {
		return RestMessage.newInstance(true, "extractRcvH", rqcHeadersService.extractRcvHAndPro(rcvQueryVO, pageParam));
	}

	/**
	 * 极联新增
	 * UP202207120002
	 *若有选择库别且为立体库，则校验当前容器上存放的同商品不可混批，支持最大混品4种；
	 *若有选择库别，零散数大于0，必须为零货库
	 *当库别有值时，依据企业+仓+货主+数据来源（1）+订单类型+质量状态+收货数(0整/1全散/2整散)+收货件数+是否异型(0否，1是)，调取入库库别选择策略，获取库别list。校验选择的库别是否在库别list里。不存在报错“数量或质量状态变化，需要重新选择库别”
	 *若没有选择库别，质检状态不为待处理，则依据上架策略获取推荐库别，再调取货位分配策略，生成上架任务；若有选择库别，质检状态不为待处理，则调取货位分配策略，生成上架任务；若质量状态为待处理，则生成复检任务。
	 *取消抽检校验
	 */
	// tianhl
	@ApiOperation("新增(级联关联表)")
	@PostMapping("/addWithRelated")
	public RestMessage<Object> addWithRelated(@RequestBody RqcDTO rqcDTO) {
		return RestMessage.newInstance(true, "addSuccess", rqcHeadersService.addWithRelated(rqcDTO));
	}

	/**
	 * 条件分页
	 */
	@ApiOperation("条件分页")
	@GetMapping("/page")
	public RestMessage<Page<RqcHeadersDTO>> list(RqcHQueryVO rqcHQueryVO, PageParam pageParam) {
		return RestMessage.newInstance(true, "pageSuccess", rqcHeadersService.findList(rqcHQueryVO, pageParam));
	}

	/**
	 * 质检任务拆分
	 */
	@ApiOperation("质检任务拆分")
	@PostMapping("/taskSplit")
	public RestMessage<Object> splitTask(@RequestBody List<TaskQcDetailDTO> taskQcDetailDTOList) {
		AssertUtils.isNotEmpty(taskQcDetailDTOList, "数据集合为空!");
		return RestMessage.newInstance(true, "splitTask", rqcHeadersService.splitTask(taskQcDetailDTOList));
	}

	/**
	 * 质检任务合并(同品同批合并) TODO
	 */
	@ApiOperation("质检任务合并(同品同批次)")
	@PostMapping("/taskMerge")
	public RestMessage<Object> taskMerge(@RequestBody List<TaskQcDetailDTO> taskQcDetailDTOList) {
		AssertUtils.isNotEmpty(taskQcDetailDTOList, "数据集合为空!");
		return RestMessage.newInstance(true, "splitTask", rqcHeadersService.taskMerge(taskQcDetailDTOList));
	}

	/**
	 * 质检异常处理
	 */
	@ApiOperation("质检异常处理")
	@PostMapping("/doException")
	public RestMessage<Object> doException(@RequestBody List<RqcLinesDTO> rqcLinesDTOList) {
		AssertUtils.isNotEmpty(rqcLinesDTOList, "数据集合为空!");
		return RestMessage.newInstance(true, "splitTask", rqcHeadersService.doException(rqcLinesDTOList));
	}

	/**
	 * 测试打印获取上架标签数据
	 */
	@ApiOperation("测试打印获取上架标签数据")
	@PostMapping("/testGetInstockPrintData")
	public RestMessage<Object> testGetInstockPrintData(@RequestBody RqcHeadersDTO rqcHeadersDTO) {
		return RestMessage.newInstance(true, "testGetInstockPrintDataSuccess",
				rqcHeadersService.excuteAssembleInstockPrintData(null, rqcHeadersDTO, null, false));
	}

	/**
	 * 质检明细打印数据
	 */
	@ApiOperation("质检明细打印数据")
	@PostMapping("/getRqcDetailsPrintData")
	public RestMessage<Object> getRqcDetailsPrintData(@RequestBody RqcDQueryVO queryVO) {
		return RestMessage.newInstance(true, "getRqcDetailsPrintDataSuccess",
				rqcHeadersService.getRqcDetailsPrintData(queryVO));
	}

	/**
	 * 质检汇总打印数据 数据模板不确定,暂未实现
	 */
	@ApiOperation("质检汇总打印数据(未实现,数据不确定)")
	@PostMapping("/getRqcGatherPrintData")
	public RestMessage<Object> getRqcGatherPrintData(@RequestBody RqcHQueryVO queryVO) {
		return RestMessage.newInstance(true, "getRqcGatherPrintDataSuccess",
				rqcHeadersService.getRqcGatherPrintData(queryVO));
	}

	/**
	 * 提取确认时校验任务是否已经被领取
	 */
	@ApiOperation("提取确认时校验任务是否已经被领取")
	@PostMapping("/checkTaskExcute")
	public RestMessage<Object> checkTaskExcute(@RequestBody List<String> taskIds) {
		return RestMessage.newInstance(true, "checkTaskExcuteSuccess", rqcHeadersService.checkTaskExcute(taskIds));
	}

	/**
	 * 查看收货时提交的图片信息
	 */
	@ApiOperation("查看收货时提交的图片信息")
	@GetMapping("/showRcvLinePic")
	public RestMessage<List<RcvLinesPicDTO>> showRcvLinePic(String rcvLineId) {
		return RestMessage.newInstance(true, "showRcvLinePicSuccess", rqcHeadersService.showRcvLinePic(rcvLineId));
	}

	/**
	 * pda获取待质检收货单
	 */
	@ApiOperation("pad获取待质检的收货单")
	@GetMapping("pad/extractRcvH")
	public RestMessage<Page<RcvHDTO>> extractRcvHByPad(RcvQueryVO rcvQueryVO, PageParam pageParam) {
		// 此处可能pad传的货主id为空,需要控制
		return RestMessage.newInstance(true, "extractRcvHByPad",
				rqcHeadersService.extractRcvHByPad(rcvQueryVO, pageParam));
	}

	/**
	 * pad获取待质检明细
	 * 
	 * @param taskQcDetailQueryVO
	 * @return RestMessage RestMessage
	 */
	@ApiOperation("pad获取待质检明细列表")
	@GetMapping("pad/extractRqcTask")
	public RestMessage<Page<TaskQcDetailDTO>> extractRqcTaskByPad(TaskQcDetailQueryVO taskQcDetailQueryVO) {
		return RestMessage.newInstance(true, "extractRqcTaskByPad",
				rqcHeadersService.extractRqcTaskByPad(taskQcDetailQueryVO));
	}

	/**
	 * pad验收明细查看直接查看list
	 */
	@ApiOperation("pad获取待质检明细详情")
	@GetMapping("pad/getRqcTask")
	public RestMessage<TaskQcDetailDTO> getRqcTask(String taskRqcId) {
		return RestMessage.newInstance(true, "getRqcTask", rqcHeadersService.getRqcTask(taskRqcId));
	}

	/**
	 * pad极联新增
	 */
	@ApiOperation("pad级联新增")
	@PostMapping("pad/addWithRelated")
	public RestMessage<Object> addWithRelatedByPad(@RequestBody RqcDTO rqcDTO) {
		return RestMessage.newInstance(true, "addWithRelatedByPadSuccess",
				rqcHeadersService.addWithRelatedByPad(rqcDTO));
	}

	/**
	 * pad 任务拆分逻辑
	 */
	@ApiOperation("pad质检任务拆分")
	@PostMapping("pad/taskSplit")
	public RestMessage<Object> splitTaskByPad(@RequestBody List<TaskQcDetailDTO> taskQcDetailDTOList) {
		AssertUtils.isNotEmpty(taskQcDetailDTOList, "数据集合为空!");
		return RestMessage.newInstance(true, "splitTask", rqcHeadersService.splitTaskByPad(taskQcDetailDTOList));
	}

	/**
	 * 根据收货id获取验收单(一条)
	 */
	@ApiOperation("根据收货id获取验收单(一条)")
	@GetMapping("/getRqcHeaderByRcvId")
	public RestMessage<RqcHeadersDTO> getRqcHeaderByRcvId(@RequestParam(required = true) String rcvHeaderId) {
		return RestMessage.newInstance(true, "getRqcHeaderByRcvId", rqcHeadersService.getRqcHeaderByRcvId(rcvHeaderId));
	}

	/**
	 * 生产货主待检入库取代管入库的验收结论
	 */
	@ApiOperation("生产货主待检入库取代管入库的验收结论")
	@GetMapping("/getHostRqcResult")
	public RestMessage<List<RqcLinesDTO>> getHostRqcResult(
			@RequestParam(required = true) String itemId, @RequestParam(required = true) String batchId) {
		return RestMessage.newInstance(true, "getHostRqcResult",
				rqcHeadersService.getHostRqcResult( itemId, batchId));
	}

//	@ApiOperation("上传监管码到码上放心平台")
//	@PostMapping("/uploadCircubill")
//	public RestMessage<String> uploadCircubill(@RequestBody UploadCircubParamVO vo) {
//		String uploadFlag = "0";
//		String message ="上传失败";
//		//更新对应的监管码 重传次数为+1，最后上传时间
//		SupervisionCode supervisionCode = vo.getSupervisionCode();
//		rcvMapper.updateUploadTimes(supervisionCode.getRefBillId(),supervisionCode.getRefBillLineId(),supervisionCode.getItemId());
//		boolean flag = UploadCircubillUtil.uploadCircubillNew(vo.getSupervisionCode(),vo.getBillCode(),vo.getInType(),vo.getOutType(),vo.getUserName());
//		if(flag){
//			uploadFlag = "1";
//			//更新对应的监管码状态为 已上传成功
//			rcvMapper.updateUploadFlag(supervisionCode.getRefBillId(),supervisionCode.getRefBillLineId(),supervisionCode.getItemId(),uploadFlag);
//			message ="上传成功";
//		}
//		//返回成功，不影响出库单的流程执行
//		return RestMessage.newInstance(true, message, null);
//	}

//	/**
//	 * 质检汇总打印数据 数据模板不确定,暂未实现
//	 */
//	@ApiOperation("质检汇总打印数据(未实现,数据不确定)")
//	@PostMapping("/getRqcGatherPrintData")
//	public RestMessage<Object> getRqcGatherPrintData(@RequestBody RqcDQueryVO  queryVO) {
//		return RestMessage.newInstance(true, "getRqcGatherPrintDataSuccess",
//				rqcHeadersService.getRqcGatherPrintData(queryVO));
//	}


	@ApiOperation("上传监管码到码上放心平台")
	@PostMapping("/outBoundUploadCircubill")
	public RestMessage<String> outBoundUploadCircubill(@RequestBody OutBoundUploadCircubParamVO vo) {
		try {
			rqcHeadersService.outBoundUploadCircubill(vo);
		} catch (Exception e) {
			logger.info("上传监管码到码上放心平台失败" +e.getMessage());
		}
		//返回成功，不影响出库单的流程执行
		return RestMessage.newInstance(true, "成功", null);
	}

	@ApiOperation("定时任务上传监管码，接口平台配置定时任务调用")
	@PostMapping("/uploadCircubillTimer")
	public RestMessage<Object> uploadCircubillTimer() {
		rqcHeadersService.uploadCircubillTimer();
		return RestMessage.newInstance(true, "上传成功", null);
	}


	@ApiOperation("定时任务 保存监管码小码信息表，接口平台配置定时任务调用")
	@PostMapping("/saveSupervisionCodeSmallTimer")
	public RestMessage<Object> saveSupervisionCodeSmallTimer() {
		rqcHeadersService.saveSupervisionCodeSmallTimer();
		return RestMessage.newInstance(true, "保存成功", null);
	}

	@ApiOperation("定时任务 定时清理监管码小码信息表，接口平台配置定时任务调用")
	@PostMapping("/toHisSupervisionCodeSmallTimer")
	public RestMessage<Object> toHisSupervisionCodeSmallTimer() {
		rqcHeadersService.toHisSupervisionCodeSmallTimer();
		return RestMessage.newInstance(true, "成功", null);
	}

	@ApiOperation("定时任务 定时查询客户码上放心平台ID")
	@PostMapping("/saveCusEntIdTimer")
	public RestMessage<Object> saveCusEntIdTimer() {
		rqcHeadersService.saveCusEntIdTimer();
		return RestMessage.newInstance(true, "成功", null);
	}

}
