package com.prolog.cs.base.gsp.controller;


import com.prolog.cs.base.gsp.enums.GspUnqualifiedStatus;
import com.prolog.cs.base.gsp.model.GspUnqualifiedDestroy;
import com.prolog.cs.base.gsp.service.GspUnqualifiedDestroyService;
import com.prolog.cs.base.gsp.vo.GspUnqualifiedDestroyDVO;
import com.prolog.cs.data.exchange.client.ExchangeSender;
import com.prolog.cs.data.exchange.client.core.EtlMessage;
import com.prolog.cs.data.exchange.client.core.EtlTypeConstants;
import com.prolog.framework.bz.core.util.ObjectUtils;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.log.aop.annotation.UserLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * @Description 不合格品销毁管理
 * @Date 2021-05-08 
 */

@Api(tags = "不合格品销毁管理")
@RestController
@RequestMapping("/gspUnqualifiedDestroy")
public class GspUnqualifiedDestroyController {

	@Autowired
	private GspUnqualifiedDestroyService service;

	@Autowired
	private ExchangeSender exchangeSender;
	
	/**
	 * 分页查询不合格品销毁提取列表
	 */
	@ApiOperation("分页查询不合格品销毁提取列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "warehouseId", required = true, dataType = "String"),
			@ApiImplicitParam(name = "cargoOwnerId", required = true, dataType = "String"),
			@ApiImplicitParam(name = "lossCode", required = false, dataType = "String"),
			@ApiImplicitParam(name = "itemId", required = false, dataType = "String"),
			@ApiImplicitParam(name = "pageSize", dataType = "Integer", defaultValue = "0"),
			@ApiImplicitParam(name = "pageNum", dataType = "Integer", defaultValue = "10")
	})
	@GetMapping("/listPagePick")
	public RestMessage<Page<GspUnqualifiedDestroyDVO>> listPagePick(@RequestParam String warehouseId,
															 @RequestParam String cargoOwnerId,
															 @RequestParam(required = false) String lossCode,
															 @RequestParam(required = false) String itemId,
															 @RequestParam Integer pageSize,
															 @RequestParam Integer pageNum) throws Exception {
		return RestMessage.newInstance(true, "success", service.listPagePick(warehouseId,cargoOwnerId,lossCode,itemId,pageSize,pageNum));
	}
	
	/**
	 * 不合格品销毁提取保存
	 */
	@ApiOperation("不合格品销毁提取保存")
	@PostMapping("/add")
	public RestMessage<Object> add(@RequestBody GspUnqualifiedDestroy gspUnqualifiedDestroy){
		service.add(gspUnqualifiedDestroy);
		return RestMessage.newInstance(true, "success", null);
	}

	/**
	 * 批量删除待审核销毁申请
	 */
	@ApiOperation("批量删除待审核销毁申请")
	@PostMapping("/delIds")
	public RestMessage<Object> delIds(@NotEmpty(message = "请选择数据操作") @RequestBody List<String> pkIdList) {
		service.delIds(pkIdList);
		return RestMessage.newInstance(true, "success", "");
	}

	/**
	 * 分页查询不合格品销毁列表
	 */
	@ApiOperation("分页查询不合格品销毁列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "warehouseId", required = true, dataType = "String"),
			@ApiImplicitParam(name = "cargoOwnerId", required = true, dataType = "String"),
			@ApiImplicitParam(name = "status", required = false, dataType = "String"),
			@ApiImplicitParam(name = "pageSize", dataType = "Integer", defaultValue = "0"),
			@ApiImplicitParam(name = "pageNum", dataType = "Integer", defaultValue = "10")
	})
	@GetMapping("/listPage")
	public RestMessage<Page<GspUnqualifiedDestroy>> listPage(@RequestParam String warehouseId,
														  @RequestParam String cargoOwnerId,
														  @RequestParam String status,
														  @RequestParam Integer pageSize,
														  @RequestParam Integer pageNum) throws Exception {
		return RestMessage.newInstance(true, "success", service.listPage(warehouseId,cargoOwnerId,status,pageSize,pageNum));
	}

	/**
	 * 主表id查询销毁商品列表
	 */
	@ApiOperation("主表id查询销毁商品列表")
	@GetMapping("/findDetailByMstId")
	public RestMessage<List<GspUnqualifiedDestroyDVO>> findDetailByMstId(@RequestParam(required=false) String mstId){
		return RestMessage.newInstance(true, "success", service.findDetailByMstId(mstId));
	}

	/**
	 * 不合格品销毁批量审核通过接口
	 */
	@ApiOperation("不合格品销毁批量审核通过接口")
	@PostMapping("/batchAuditYes")
	public RestMessage<Object> batchAuditYes(
			@NotEmpty(message = "collection is empty")
			@RequestBody List<GspUnqualifiedDestroy> gspUnqualifiedDestroyDTOList) throws Exception {
		service.batchUpdate(gspUnqualifiedDestroyDTOList, GspUnqualifiedStatus.WMS_AUDIT_YES.getStatus());
		return RestMessage.newInstance(true, "success", "");
	}


	/**
	 * 不合格品销毁批量审核驳回接口
	 */
	@ApiOperation("不合格品销毁批量审核驳回接口")
	@PostMapping("/batchAuditNo")
	public RestMessage<Object> batchAuditNo(@NotEmpty(message = "collection is empty")@RequestBody List<GspUnqualifiedDestroy> 
														gspUnqualifiedDestroyDTOList) throws Exception {
		service.batchUpdate(gspUnqualifiedDestroyDTOList, GspUnqualifiedStatus.WMS_AUDIT_NO.getStatus());
		return RestMessage.newInstance(true, "success", "");
	}

	@UserLog
	@ApiOperation(value = "获取详情（含销毁）")
	@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String")
	@GetMapping("/getById")
	public RestMessage<GspUnqualifiedDestroy> getById(String id) throws Exception
	{
		return RestMessage.newInstance(true, "查询成功", service.getById(id));
	}

	/**
	 * 批量录入销毁信息
	 */
	@ApiOperation("批量录入销毁信息")
	@PostMapping("/batchDestroy")
	public RestMessage<Object> batchDestroy(@NotEmpty(message = "collection is empty")@RequestBody List<GspUnqualifiedDestroy> gspUnqualifiedDestroyDTOList) throws Exception {
		for (GspUnqualifiedDestroy gspUnqualifiedDestroy : gspUnqualifiedDestroyDTOList) {
			if (ObjectUtils.isNull(gspUnqualifiedDestroy.getDestroySituation())) {
				return RestMessage.newInstance(false, "销毁后现场情况不能为空！", "");
			}
			if (ObjectUtils.isNull(gspUnqualifiedDestroy.getDestroyExecutor())) {
				return RestMessage.newInstance(false, "销毁执行人不能为空！", "");
			}
			if (ObjectUtils.isNull(gspUnqualifiedDestroy.getDestroySupervisor())) {
				return RestMessage.newInstance(false, "销毁监督人不能为空！", "");
			}
//			if (ObjectUtils.isNull(gspUnqualifiedDestroy.getNmpaOfficer())) {
//				return RestMessage.newInstance(false, "药监部门人员不能为空！", "");
//			}
			if (ObjectUtils.isNull(gspUnqualifiedDestroy.getDestroyPic())) {
				return RestMessage.newInstance(false, "销毁图片不能为空！", "");
			}
		}
		service.batchUpdate(gspUnqualifiedDestroyDTOList, GspUnqualifiedStatus.DESTROY.getStatus());
		String[] ids = gspUnqualifiedDestroyDTOList.stream().map(GspUnqualifiedDestroy::getId).toArray(String[]::new);
		EtlMessage em = new EtlMessage();
		em.setId(String.join(",",ids));
		em.setEtlType(EtlTypeConstants.gsp_destroy_audit_w2o);
		exchangeSender.send(em);
		return RestMessage.newInstance(true, "success", "");
	}
	
}
