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


import com.alibaba.fastjson.JSON;
import com.prolog.cs.base.gsp.enums.GspUnqualifiedStatus;
import com.prolog.cs.base.gsp.model.GspUnqualifiedLoss;
import com.prolog.cs.base.gsp.service.GspUnqualifiedLossService;
import com.prolog.cs.base.gsp.vo.ErpUnqualifiedLossVo;
import com.prolog.cs.base.gsp.vo.GspUnqualifiedLossDVO;
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.common.message.RestMessage;
import com.prolog.framework.core.pojo.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
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
 */

@Slf4j
@Api(tags = "不合格品破损管理")
@RestController
@RequestMapping("/gspUnqualifiedLoss")
public class GspUnqualifiedLossController {

	@Autowired
	private GspUnqualifiedLossService gspUnqualifiedLossService;

	@Autowired
	private ExchangeSender exchangeSender;

	/**
	 * 不合格品破损提取保存
	 */
	@ApiOperation("不合格品破损提取保存")
	@PostMapping("/add")
    public RestMessage<Object> add(@RequestBody GspUnqualifiedLoss gspUnqualifiedLoss) throws Exception {
        Boolean needErpAudit = gspUnqualifiedLossService.add(gspUnqualifiedLoss);

        if (needErpAudit) {
            // 如果需要 ERP 审核
            EtlMessage em = new EtlMessage();
            em.setId(gspUnqualifiedLoss.getId());
            em.setEtlType(EtlTypeConstants.op_pl_w2o);
            exchangeSender.send(em);
        }
        return RestMessage.newInstance(true, "success", null);
    }

	/**
	 * 批量删除待审核破损申请
	 */
	@ApiOperation("批量删除待审核破损申请")
	@PostMapping("/delIds")
	public RestMessage<Object> delIds(@NotEmpty(message = "请选择数据操作") @RequestBody List<String> pkIdList) {
		gspUnqualifiedLossService.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<GspUnqualifiedLoss>> listPage(
			@RequestParam String warehouseId,
			@RequestParam String cargoOwnerId,
			@RequestParam String status,
			@RequestParam Integer pageSize,
			@RequestParam Integer pageNum
	) throws Exception {
		return RestMessage.newInstance(true, "success", gspUnqualifiedLossService.listPage(warehouseId,cargoOwnerId,status,pageSize,pageNum));
	}

	/**
	 * 主表id查询破损商品列表
	 */
	@ApiOperation("主表id查询破损商品列表")
	@GetMapping("/findDetailByMstId")
	public RestMessage<List<GspUnqualifiedLossDVO>> findDetailByMstId(@NotEmpty(message = "mstId is empty") @RequestParam String mstId){
		return RestMessage.newInstance(true, "success", gspUnqualifiedLossService.findDetailByMstId(mstId));
	}

	/**
	 * 不合格品破损批量审核通过
	 */
	@ApiOperation("不合格品破损批量审核通过")
	@PostMapping("/batchAuditYes")
	public RestMessage<Object> batchAuditYes(@NotEmpty(message = "collection is empty")@RequestBody List<GspUnqualifiedLoss> list) throws Exception {
		gspUnqualifiedLossService.batchAudit(list, GspUnqualifiedStatus.WMS_AUDIT_YES.getStatus());
		EtlMessage em = new EtlMessage();
		String[] arr= list.stream().map(GspUnqualifiedLoss::getId).toArray(String[]::new);
		log.info("调用EtlTypeConstants.gsp_pl_audit_w2o,入参为：" + JSON.toJSONString(arr));
		em.setId(String.join(",",list.stream().map(GspUnqualifiedLoss::getId).toArray(String[]::new)));
		em.setEtlType(EtlTypeConstants.gsp_pl_audit_w2o);
		exchangeSender.send(em);
		return RestMessage.newInstance(true, "success", "");
	}

	/**
	 * 不合格品破损批量审核驳回
	 */
	@ApiOperation("不合格品破损批量审核驳回")
	@PostMapping("/batchAuditNo")
	public RestMessage<Object> batchAuditNo(@NotEmpty(message = "success")@RequestBody List<GspUnqualifiedLoss> list) throws Exception {
		gspUnqualifiedLossService.batchAudit(list, GspUnqualifiedStatus.WMS_AUDIT_NO.getStatus());
		return RestMessage.newInstance(true, "batchEditSuccess", "");
	}


	/**
	 * 新奇康的库内不合格品报损采用ERP下发模式
	 * WMS收到下发后直接记录数据，自动审核通过并扣减库存
	 */
	@ApiOperation("不合格品报损接收保存自动审核")
	@PostMapping("/erp/add")
	public RestMessage<Object> erpAdd(@RequestBody ErpUnqualifiedLossVo vo) throws Exception {
		log.info("收到来自ERP的不合格品报损下传信息：{}", vo.toString());
		gspUnqualifiedLossService.erpAdd(vo);
		return RestMessage.newInstance(true, "success", null);
	}

}
