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

import com.prolog.cs.biz.inbound.dto.OmsAsnBillSaveByOmsDTO;
import com.prolog.cs.biz.inbound.dto.OmsAsnBillSaveDTO;
import com.prolog.cs.biz.inbound.remote.dto.OmsAsnBillHQueryVO;
import com.prolog.cs.biz.inbound.service.OmsAsnBillDService;
import com.prolog.cs.biz.inbound.vo.CancelOrderOmsVO;
import com.prolog.cs.biz.inbound.vo.OmsAsnBillDVO;
import com.prolog.cs.biz.inbound.vo.OmsAsnBillHVO;
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.web.bind.annotation.*;

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

@Api(tags = "oms-ASN订单")
@RestController
@RequestMapping("/asn/v1.0")
public class OmsAsnBillHController {

	@Resource
	private OmsAsnBillDService asnService;

	/**
	 * 清理asn单
	 */
	@UserLog("清理asn单")
	@ApiOperation("清理asn单")
	@PostMapping("/clear")
	public RestMessage<Object> clear(@RequestBody List<String> ids) {
		return RestMessage.doSuccess(asnService.clear(ids));
	}

	/**
	 * 手动回传入库订单
	 */
	@UserLog("手动回传入库订单")
	@ApiOperation("手动回传入库订单")
	@GetMapping("/call2erp")
	public RestMessage<Object> call2erp(String cargoOwnerId, String billNo, String rcvNo, String[] rcvIds) {
		return RestMessage.doSuccess(asnService.call2erp(cargoOwnerId, billNo, rcvNo, rcvIds));
	}

	/**
	 * 自动清理asn单
	 */
	@ApiOperation("自动清理asn单")
	@GetMapping("/autoClear")
	public RestMessage<Object> autoClear(Integer clearDays, String enterpriseId, String warehouseId,
			String cargoOwnerId) {
		return RestMessage.doSuccess(asnService.autoClear(clearDays, enterpriseId, warehouseId, cargoOwnerId));
	}

	/**
	 * 采购订单异常处理
	 */
	@ApiOperation("采购订单异常处理")
	@PostMapping("/errorHandling")
	public RestMessage<Object> errorHandling(@RequestBody List<Map<String, Object>> errorMap) {
		return RestMessage.doSuccess(asnService.errorHandling(errorMap));
	}

    @ApiOperation("分页查询ASN主单信息")
    @GetMapping("/getAsnList")
    public RestMessage<Page<OmsAsnBillHVO>> getAsnList(OmsAsnBillHQueryVO query) {
        return RestMessage.doSuccess(asnService.queryOmsAsnBillH(query));
    }

    @ApiOperation("分页依据主单BillId查询ASN明细数据")
    @ApiImplicitParams({
      @ApiImplicitParam(name = "billId", required = true, dataType = "String"),
      @ApiImplicitParam(name = "pageSize", dataType = "Integer", defaultValue = "10"),
      @ApiImplicitParam(name = "pageNum", dataType = "Integer", defaultValue = "1")
    })
    @GetMapping("/getAsnDetailsList")
    public RestMessage<Page<OmsAsnBillDVO>> getAsnDetailsList(@RequestParam String billId,
                                                              @RequestParam Integer pageSize,
                                                              @RequestParam Integer pageNum) {
        return RestMessage.doSuccess(asnService.queryOmsAsnBillDByBillid(billId, pageSize, pageNum));
    }

    @ApiOperation("保存ASN单据主表及明细表信息")
    @PostMapping("/saveAsnBill")
    public RestMessage<Object> saveAsnBill(@RequestBody List<OmsAsnBillSaveDTO> saveDTOs) {
        asnService.saveOmsAsnBill(saveDTOs);

        return RestMessage.newInstance(true, "保存成功", null);
    }

    @ApiOperation("依据主单BillId查询ASN主表数据")
    @ApiImplicitParams({
      @ApiImplicitParam(name = "billId", required = true, dataType = "String")
    })
    @GetMapping("/getAsnBill")
    public RestMessage<OmsAsnBillHVO> getAsnBill(@RequestParam String billId) {
        return RestMessage.doSuccess(asnService.queryOmsAsnBillHById(billId));
    }

	@ApiOperation("拉取ERP中间库订单")
	@GetMapping("/pull-asn")
	public RestMessage<String> pullAsn() {
		asnService.pullAsn();
		return RestMessage.newInstance(true, "保存成功", null);
	}

	@ApiOperation("提供OMS调用保存ASN单以及明细接口")
	@PostMapping("/saveAsnBillByOms")
	public RestMessage<Object> saveAsnBillByOms(@RequestBody List<OmsAsnBillSaveByOmsDTO> saveDTOs) {
		asnService.saveAsnBillByOms(saveDTOs);
		return RestMessage.newInstance(true, "保存成功", null);
	}

	/**
	 * 取消asn单
	 */
	@UserLog("提供OMS调用取消订单")
	@ApiOperation("提供OMS调用取消订单")
	@PostMapping("/cancelOrderOms")
	public RestMessage<List<CancelOrderOmsVO>> cancelOrderOms(@RequestBody List<CancelOrderOmsVO> cancelList) {
		return RestMessage.doSuccess(asnService.cancelOrderOms(cancelList));
	}
}
