package com.trade.client.trade.salesorder.controller;

import com.github.pagehelper.PageInfo;
import com.trade.client.security.utils.UserUtils;
import com.trade.client.trade.salesorder.service.SalesOrderCService;
import com.trade.service.trade.salescontract.domain.SalesCommodity;
import com.trade.service.trade.salescontract.service.SalesCommodityService;
import com.trade.service.trade.salescontract.service.SalesContractService;
import com.trade.service.trade.salescontract.vo.SalesCommoditySearch;
import com.trade.service.trade.salescontract.vo.SalesContractVo;
import com.trade.service.trade.salesorder.dto.*;
import com.trade.common.core.domain.ResponseResult;
import com.trade.service.trade.salesorder.vo.SalesOrderDtlVO;
import com.trade.service.trade.salesorder.vo.SalesOrderSearch;
import com.trade.service.trade.salesorder.vo.SalesOrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 销售订单表 C端控制器
 * </p>
 *
 * @author zhangchenm
 * @since 2024-09-06
 */
@RestController
@RequestMapping("/salesOrder")
@Api(tags = "销售订单信息", value = "销售订单信息")
public class SalesOrderController {

    @Resource
    private SalesOrderCService salesOrderCService;
    @Resource
    private SalesCommodityService salesCommodityService;
    @Resource
    private SalesContractService salesContractService;

    /**
     * 销售订单-list
     */
    @RequestMapping("/list")
    public ResponseResult<PageInfo<SalesOrderVO>> list(SalesOrderSearch search) {
        search.setCustomerUserId(UserUtils.getCustomerUserId());
        return ResponseResult.success(salesOrderCService.queryListBySearch(search));
    }

    /**
     * 销售订单-保存
     */
    @RequestMapping("/save")
    public ResponseResult<Void> add(@RequestBody SalesOrderDTO salesOrderDTO) {
        salesOrderCService.save(salesOrderDTO, false);
        return ResponseResult.success();
    }

    /**
     * 销售订单-提交
     */
    @RequestMapping("/submit")
    public ResponseResult<Void> submit(@RequestBody @Valid SalesOrderDTO salesOrderDTO) {
        salesOrderCService.submit(salesOrderDTO);
        return ResponseResult.success();
    }

    /**
     * 销售订单-发货
     */
    @RequestMapping("/deliver")
    public ResponseResult<Void> deliver(@RequestBody @Valid SalesOrderDeliverDTO orderDeliverDTO) {
        salesOrderCService.deliver(orderDeliverDTO);
        return ResponseResult.success();
    }

    /**
     * 销售订单-详情
     * 
     * @param id 销售订单ID
     * @param is4Update 是否为更新
     */
    @RequestMapping("/detail")
    public ResponseResult<SalesOrderDtlVO> detail(Long id, Boolean is4Update) {
        return ResponseResult.success(salesOrderCService.getDetail(id, is4Update));
    }

    /**
     * 销售订单-删除
     */
    @RequestMapping("/delete")
    public ResponseResult<Boolean> delete(Long id) {
        return ResponseResult.success(salesOrderCService.delete(id));
    }

    /**
     * 销售订单-获取销售合同下拉选项
     */
    @RequestMapping("/salesContractList")
    public ResponseResult<List<SalesContractVo>> getCommodityList(String importerCertNo) {
        return ResponseResult.success(
                salesContractService.querySalesContractByCustomerUserId(UserUtils.getCustomerUserId(), importerCertNo));
    }

    /**
     * 销售订单-获取待选择商品列表
     */
    @RequestMapping("/commodityList")
    public ResponseResult<PageInfo<SalesCommodity>> getCommodityList(SalesCommoditySearch search) {
        return ResponseResult.success(salesCommodityService.getContractCommodityList(search));
    }

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

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

    /**
     * 处理订单收款列表
     */
    @ApiOperation(value = "处理订单收款列表", notes = "处理订单收款列表")
    @PostMapping("/processOrderReceivePaymentLis")
    public ResponseResult<List<ReceivePaymentExtendDto>> processOrderReceivePaymentList(
            @RequestBody @Valid List<ReceivePaymentExtendDto> receivePaymentExtendDtos) {
        return ResponseResult.success(salesOrderCService.processOrderReceivePaymentList(receivePaymentExtendDtos));
    }

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

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

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