package com.prolog.cs.biz.outbound.controller;

import com.prolog.cs.basic.common.model.BaseCommonDict;
import com.prolog.cs.biz.outbound.model.OmsDoBillHDO;
import com.prolog.cs.biz.outbound.model.dto.*;
import com.prolog.cs.biz.outbound.model.entity.OmsDoBillDDO;
import com.prolog.cs.biz.outbound.model.vo.CargoOwnerChangeVO;
import com.prolog.cs.biz.outbound.model.vo.WaveVO;
import com.prolog.cs.biz.outbound.model.vo.check.WavePendingBillVO;
import com.prolog.cs.biz.outbound.resource.CommonClient;
import com.prolog.cs.biz.outbound.service.WavesService;
import com.prolog.cs.biz.outbound.service.WavesSpecialService;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.lock.annotation.RepeatOperationLock;
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 java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags = "特殊出库")
@RestController
@RequestMapping("/special-wave")
public class WavesSpecialController {

    @Autowired
    private WavesService wavesService;

    @Autowired
    private WavesSpecialService wavesSpecialService;

    @Autowired
    private CommonClient commonClient;

    @UserLog
    @ApiOperation(value = "查询OMS特殊订单列表")
    @PostMapping("/billhpage")
    public RestMessage<Page<OmsDoBillHDO>> getBillHList(@RequestBody BillQueryDTO billQueryDTO) {
        RestMessage<List<BaseCommonDict>> billTypeList = commonClient.getDictBasicListByCodes("special_bill_type", null);
        //排除掉直通出库
        List<String> billTypes = billTypeList.getData().stream().filter(item -> !"6".equals(item.getDictCode())).map(BaseCommonDict::getDictCode).collect(Collectors.toList());
        if(billTypes == null || billTypes.size() <= 0){
            return RestMessage.success("操作成功", null);
        }
        billQueryDTO.setBilltypes(billTypes);
        return RestMessage.success("操作成功", wavesService.getBillHList(billQueryDTO));
    }

    @UserLog
    @ApiOperation(value = "查询OMS特殊订单商品明细列表")
    @PostMapping("/billdbybillid")
    public RestMessage<List<OmsDoBillDDO>> getBillDByBillId(@RequestBody ItemQueryDTO itemQueryDTO) {
        return RestMessage.success("操作成功", wavesSpecialService.getBillDByBillId(itemQueryDTO));
    }

    @UserLog
    @ApiOperation(value = "波次生成并下发")
    @PostMapping("/create")
    @RepeatOperationLock(timeOut = 500)
    public RestMessage<?> wavesCreate(@RequestBody WavesCreateDTO wavesCreateVO) {
        wavesSpecialService.wavesCreateAndSend(wavesCreateVO);
        return RestMessage.success("操作成功", null);
    }

    @UserLog
    @ApiOperation(value = "查询代管/待检出库订单")
    @PostMapping("/billh-page")
    public RestMessage<Page<OmsDoBillHDO>> getSpecialOmsBillList(@RequestBody BillQueryDTO billQueryDTO) {
        return RestMessage.success("操作成功", wavesSpecialService.getSpecialOmsBillList(billQueryDTO));
    }

    @UserLog
    @ApiOperation(value = "查询代管/待检出库订单明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "billno", value = "订单编号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "branchid", value = "仓库id", required = true, dataType = "String")
    })
    @GetMapping("/billd-page")
    public RestMessage<Page<PickTaskDDTO>> getSpecialBillDList(String billno, String branchid, Integer pageNum, Integer pageSize) {
        return RestMessage.success("操作成功", wavesSpecialService.getSpecialBillDList(billno,branchid,pageNum,pageSize));
    }

    @UserLog
    @ApiOperation(value = "代管/待检出库确认")
    @ApiImplicitParam(name = "params", required = true, value = "{\"operatorCode\":\"工号\",\"billNoList\":\"订单编号列表\",\"warehouseId\":\"仓id\"}")
    @PostMapping("/confirm")
    public RestMessage<Page<OmsDoBillHDO>> specialBillConfirm(@RequestBody Map<String ,Object> params) {
        List<String> billNoList = (List<String>)params.get("billNoList");
        String operatorCode = (String)params.get("operatorCode");
        String warehouseId = (String)params.get("warehouseId");
        wavesSpecialService.specialBillConfirm(billNoList,operatorCode,warehouseId);
        return RestMessage.success("操作成功", null);
    }

    @UserLog
    @ApiOperation(value = "货主转换")
    @PostMapping("/cargoowner-change")
    public RestMessage<?> cargoOwnerChange(@RequestBody List<CargoOwnerChangeVO> list) {
        wavesSpecialService.cargoOwnerChange(list);
        return RestMessage.success("操作成功", null);
    }

    @UserLog
    @ApiOperation(value = "查询oms出库单明细")
    @PostMapping("/omsbilldate")
    public RestMessage<List<BillHDTO>> getOmsBillDate(@RequestBody List<String> billIds) {
        return RestMessage.success("操作成功", wavesSpecialService.getOmsBillDate(billIds));
    }

    @UserLog
    @ApiOperation(value = "波次列表查询")
    @GetMapping("/")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "waveId", value = "波次id"), @ApiImplicitParam(name = "billCode", value = "订单编码"),
            @ApiImplicitParam(name = "warehouseId", value = "仓库id", required = true), @ApiImplicitParam(name = "cargoOwnerId", value = "货主id"),
            @ApiImplicitParam(name = "status", value = "status"),
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = true), @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true)
    })
    public RestMessage<Page<WaveVO>> getWaves(String warehouseId, String waveId, String billCode, String cargoOwnerId, String status, Integer pageNum, Integer pageSize) {
        return RestMessage.success("操作成功", wavesService.getWaves(pageNum, pageSize, warehouseId, waveId, billCode, cargoOwnerId, status, "1"));
    }

    @UserLog
    @ApiOperation(value = "波次挂起订单查询")
    @GetMapping("/bill/query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "warehouseId", value = "仓库id", required = true),
            @ApiImplicitParam(name = "billCode", value = "仓库id", required = true),
            @ApiImplicitParam(name = "consignorId", value = "货主id", required = true),
            @ApiImplicitParam(name = "waveId", value = "波次id", required = true),
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true)
    })
    public RestMessage<Page<WavePendingBillVO>> getPendingBills(String warehouseId, String waveId, String billCode, String consignorId, Integer pageNum, Integer pageSize) {
        return RestMessage.success("操作成功", wavesService.getPendingBills("1",warehouseId,waveId,billCode,consignorId,pageNum, pageSize));
    }

}
