package com.trade.admin.trade.salesorder;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import com.trade.service.trade.salescontract.service.SalesContractService;
import com.trade.service.trade.salesorder.dto.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.trade.common.core.domain.ResponseResult;
import com.trade.service.trade.salesorder.service.ISalesOrderService;
import com.trade.service.trade.salesorder.vo.SalesOrderSearch;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "销售订单信息", value = "销售订单信息")
@RestController
@RequestMapping("/salesOrderB")
@Validated
public class SalesOrderBController {

    @Resource
    private ISalesOrderService salesOrderService;
    @Resource
    private SalesContractService salesContractService;

    /**
     * 获取销售订单列表
     */
    @RequestMapping("/queryList")
    @PreAuthorize("@ss.hasPermi('trade:salesOrder:list')")
    public Object queryList(SalesOrderSearch search) {
        search.setPoint(false);
        return ResponseResult.success(salesOrderService.queryListBySearch(search));
    }

    /**
     * 根据订单id获取销售订单详情
     */
    @RequestMapping("/getDetailById")
    @PreAuthorize("@ss.hasPermi('trade:salesOrder:detail')")
    public Object getByEntityId(@NotNull(message = "订单ID不能为空") Long id) {
        return ResponseResult.success(salesOrderService.getDetail(id, false));
    }

    /**
     * 获取销售合同目的港下拉框
     */
    @RequestMapping("/getDestPortList")
    public ResponseResult<List<String>> getDestPortList() {
        return ResponseResult.success(salesContractService.getDestPortList(null));
    }

    /**
     * 商品列表提示收款
     */
    @ApiOperation(value = "商品列表提示收款", notes = "商品列表提示收款")
    @PostMapping("/goodsReceivePayment")
    @PreAuthorize("@ss.hasPermi('trade:salesOrder:goodsReceivePayment')")
    public ResponseResult<Boolean> processGoodsReceivePayment(
            @RequestBody @Valid SalesGoodsReceivePaymentDto salesOrderReceivePayment) {
        return ResponseResult.success(salesOrderService.processGoodsReceivePayment(salesOrderReceivePayment));
    }

    /**
     * 处理订单收款列表
     */
    @ApiOperation(value = "确认收款(处理订单收款列表)", notes = "确认收款(处理订单收款列表)")
    @PostMapping("/processOrderReceivePaymentLis")
    @PreAuthorize("@ss.hasPermi('trade:salesOrder:processOrderReceivePaymentList')")
    public ResponseResult<List<ReceivePaymentExtendDto>> processOrderReceivePaymentList(
            @RequestBody @Valid List<ReceivePaymentExtendDto> receivePaymentExtendDtos) {
        return ResponseResult.success(salesOrderService.processOrderReceivePaymentList(receivePaymentExtendDtos, true));
    }

    /**
     * 订单收款列表
     */
    @ApiOperation(value = "订单收款列表", notes = "订单收款列表")
    @PostMapping("/queryOrderListInfo")
    public ResponseResult<List<OrderListInfoDto>> queryOrderListInfoByOrderIds(
            @RequestBody @Valid List<SalesOrderReceiveExtendDto> salesOrderInfo) {
        return ResponseResult.success(salesOrderService.queryOrderListInfoByOrderIds(salesOrderInfo));
    }

    /**
     * 提交订单收款列表
     */
    @ApiOperation(value = "提交订单收款列表", notes = "提交订单收款列表")
    @PostMapping("/submitOrderListInfo")
    public ResponseResult<OrderPaymentAmountDto> submitOrderListInfo(
            @RequestBody @Valid List<OrderListInfoDto> orderListInfoDtos) {
        return ResponseResult.success(salesOrderService.submitOrderListInfo(orderListInfoDtos, 1));
    }

    /**
     * 处理商品收款
     *
     * @param receivePaymentSubDto
     * @return 勾选商品金额
     */
    @ApiOperation(value = "处理订单收款列表", notes = "处理订单收款列表")
    @PostMapping("/processOrderReceivePayment")
    public ResponseResult<Boolean> processOrderReceivePayment(
            @RequestBody @Valid ReceivePaymentSubDto receivePaymentSubDto) {
        return ResponseResult.success(salesOrderService.processOrderReceivePayment(receivePaymentSubDto, true));
    }
}
