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

import com.google.common.collect.Lists;
import com.prolog.cs.biz.inbound.constant.ValidatorGroup.validAdd;
import com.prolog.cs.biz.inbound.dto.rqc.RcvLineResult;
import com.prolog.cs.biz.inbound.dto.rqc.RqcLinesDTO;
import com.prolog.cs.biz.inbound.service.rqc.RqcLinesService;
import com.prolog.cs.biz.inbound.vo.RqcDQueryVO;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.utils.AssertUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * @Description // TODO
 * @Date 2021-03-24
 */

@Api(tags = "质检明细行")
@RestController
@RequestMapping("/rqcLines")
public class RqcLinesController {

	@Resource
	private RqcLinesService rqcLinesService;

	/**
	 * // TODO
	 */
	@ApiOperation("添加")
	@PostMapping("/add")
	public RestMessage<Object> add(@Validated(validAdd.class) @RequestBody RqcLinesDTO rqcLinesDTO) {
		return RestMessage.newInstance(true, "addSuccess", rqcLinesService.add(rqcLinesDTO));
	}

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

	/**
	 * // TODO
	 */
	@ApiOperation("条件分页")
	@GetMapping("/page")
	public RestMessage<Page<RqcLinesDTO>> list(RqcDQueryVO rqcDQueryVO) throws Exception {
		return RestMessage.newInstance(true, "pageSuccess", rqcLinesService.findList(rqcDQueryVO));
	}

	/**
	 * // TODO
	 */
	@ApiOperation("批量添加")
	@PostMapping("/batchAdd")
	public RestMessage<Object> batchAdd(@RequestBody List<RqcLinesDTO> rqcLinesDTOList) {
		return RestMessage.newInstance(true, "batchAddSuccess", rqcLinesService.batchAdd(rqcLinesDTOList));
	}

	/**
	 * UP202207190001LIUTAO
	 * 依据企业+仓+货主+单据类型+数据来源为（1：质检）+质量状态获取入库库别策略,获取库别
	 */
	@ApiOperation("依据企业+仓+货主+单据类型+数据来源为（1：质检）+质量状态获取入库库别策略,获取库别")
	@PostMapping("/recommendRoom")
	public RestMessage<List<RcvLineResult>> recommendRoom(@RequestBody RqcLinesDTO dto){
		try{
			Map<RqcLinesDTO,List<RcvLineResult>> result=rqcLinesService.recommendRoom(Collections.singletonList(dto));
			return RestMessage.newInstance(true,"",result.get(dto));
		}catch (Exception e){
			return RestMessage.newInstance(true,e.getMessage(), Lists.newArrayList());
		}
	}
	/**
	 * UP202207190001LIUTAO
	 * 依据企业+仓+货主+单据类型+数据来源为（1：质检）+质量状态+是否异型+收货数+收货件数 获取入库库别策略,获取库别
	 */
	@ApiOperation("依据企业+仓+货主+单据类型+数据来源为（1：质检）+质量状态+是否异型+收货数+收货件数 获取入库库别策略,获取库别")
	@PostMapping("/recommendRoomRf")
	public RestMessage<List<RcvLineResult>> recommendRoomRf(@RequestBody RqcLinesDTO dto){
		try{
			//校验参数
			rqcLinesService.checkRfRqcQty(Collections.singletonList(dto));
			Map<RqcLinesDTO,List<RcvLineResult>> result=rqcLinesService.recommendRoom(Collections.singletonList(dto));
			return RestMessage.newInstance(true,"",result.get(dto));
		}catch (Exception e){
			return RestMessage.newInstance(false,e.getMessage(), Lists.newArrayList());
		}
	}
}
