package com.sckw.order.controller;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.sckw.core.annotation.RepeatSubmit;
import com.sckw.core.exception.CustomPromptException;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.core.web.constant.HttpStatus;
import com.sckw.core.web.response.HttpResult;
import com.sckw.excel.utils.ExcelUtil;
import com.sckw.order.api.model.OrderPara;
import com.sckw.order.api.model.TradeOrderPara;
import com.sckw.order.model.vo.KwoTradeOrderArchiveVo;
import com.sckw.order.model.vo.SaleTopVo;
import com.sckw.order.model.vo.req.*;
import com.sckw.order.serivce.KwoTradeOrderService;
import com.sckw.payment.api.model.dto.common.R;
import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * @desc: 贸易订单相关
 * @author: yzc
 * @date: 2023-07-07 14:10
 */
@RestController
@RequestMapping(value = "/kwoTradeOrder")
@RequiredArgsConstructor
@Tag(name = "贸易订单")
public class KwoTradeOrderController {

    private final KwoTradeOrderService kwoTradeOrderService;

    //    /**
//     * @desc: 采购下单草稿
//     * @author: yzc
//     * @date: 2023-07-07 15:29
//     * @Param purchaseOrderParam:
//     * @return: com.sckw.core.web.response.HttpResult
//     */
//    @GlobalTransactional(name = "default_tx_group")
//    @RepeatSubmit(interval = 1000,message ="前方拥堵，请稍后尝试")
//    @PostMapping(value = "/addPurchaseOrderDraft", produces = MediaType.APPLICATION_JSON_VALUE)
//    public HttpResult addPurchaseOrderDraft(@RequestBody @Validated PurchaseOrderDraftParam param) {
//        kwoTradeOrderService.addPurchaseOrderDraft(param);
//        return HttpResult.ok("采购下单草稿保存成功");
//    }
    @RequestMapping(value = "/trade", method = RequestMethod.GET)
    @Operation(summary = "查询贸易订单", description = "查询贸易订单")
    public HttpResult selectAll(@ParameterObject OrderPara orderPara) {
        TradeOrderPara tradeOrderPara = new TradeOrderPara();
        tradeOrderPara.setStartTime(LocalDateTimeUtil.parse(orderPara.getStartTime(), "yyyy-MM-dd HH:mm:ss"));
        tradeOrderPara.setEndTime(LocalDateTimeUtil.parse(orderPara.getEndTime(), "yyyy-MM-dd HH:mm:ss"));
        tradeOrderPara.setUserId(orderPara.getUserId());
        tradeOrderPara.setOrderNo(orderPara.getOrderNo());
        return HttpResult.ok("查询成功", kwoTradeOrderService.queryOrder(tradeOrderPara));
    }

    /**
     * @desc: 采购下单提交
     * @author: yzc
     * @date: 2023-07-07 15:29
     * @Param purchaseOrderParam:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @GlobalTransactional(name = "default_tx_group")
    @RepeatSubmit(interval = 1000, message = "前方拥堵，请稍后尝试")
    @PostMapping(value = "/addPurchaseOrderSubmit", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult addPurchaseOrderSubmit(@RequestBody @Validated PurchaseOrderParam purchaseOrderParam) {
        kwoTradeOrderService.addPurchaseOrderSubmit(purchaseOrderParam);
        return HttpResult.ok("采购下单提交成功");
    }

//    /**
//     * @desc: 代客下单草稿
//     * @author: yzc
//     * @date: 2023-07-07 15:29
//     * @Param valetOrderParam:
//     * @return: com.sckw.core.web.response.HttpResult
//     */
//    @GlobalTransactional(name = "default_tx_group")
//    @RepeatSubmit(interval = 1000,message ="前方拥堵，请稍后尝试")
//    @PostMapping(value = "/addValetOrderDraft", produces = MediaType.APPLICATION_JSON_VALUE)
//    public HttpResult addValetOrderDraft(@RequestBody @Validated ValetOrderDraftParam param) {
//        kwoTradeOrderService.addValetOrderDraft(param);
//        return HttpResult.ok("代客下单草稿保存成功");
//    }
//
//
//    /**
//     * @desc: 代客下单提交
//     * @author: yzc
//     * @date: 2023-07-07 15:30
//     * @Param valetOrderParam:
//     * @return: com.sckw.core.web.response.HttpResult
//     */
//    @GlobalTransactional(name = "default_tx_group")
//    @RepeatSubmit(interval = 1000,message ="前方拥堵，请稍后尝试")
//    @PostMapping(value = "/addValetOrderSubmit", produces = MediaType.APPLICATION_JSON_VALUE)
//    public HttpResult addValetOrderSubmit(@RequestBody @Validated ValetOrderParam valetOrderParam) {
//        kwoTradeOrderService.addValetOrderSubmit(valetOrderParam);
//        return HttpResult.ok("代客下单提交成功");
//    }

    /**
     * 门户贸易订单下单
     */
//    @GlobalTransactional(name = "default_tx_group")
    @RepeatSubmit(interval = 1000, message = "前方拥堵，请稍后尝试")
    @PostMapping(value = "/addOrder", produces = MediaType.APPLICATION_JSON_VALUE)
    @Operation(summary = "门户贸易订单下单", description = "门户贸易订单下单")
    public HttpResult addOrder(@RequestBody @Validated TradeOrderParam tradeOrderParam) {
        kwoTradeOrderService.addTradeOrder(tradeOrderParam);
        return HttpResult.ok("下单提交成功");
    }

    /**
     * todo-xcq
     *
     * @desc: 订单详情
     * @author: yzc
     * @date: 2023-07-07 15:30
     * @Param id:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @GetMapping("/detail")
    @Operation(summary = "贸易订单详情", description = "贸易订单详情")
    public HttpResult detail(@RequestParam Long id) {
        return HttpResult.ok("贸易订单详情查询成功", kwoTradeOrderService.getDetail(id));
    }

    /**
     * 贸易订单详情看板：完成率、今日/本月运单完成量、派车次数
     */
    @GetMapping("/dashboardStatistic")
    @Operation(summary = "贸易订单看板统计", description = "完成率、今日总量、车次、本月总量")
    public HttpResult dashboardStatistic(@RequestParam Long id) {
        return HttpResult.ok("查询成功", kwoTradeOrderService.tradeOrderDashboardStatistic(id));
    }

    @PostMapping("/audit")
    @Operation(summary = "审核贸易订单", description = "审核贸易订单")
//    @GlobalTransactional(name = "default_tx_group")
    @RepeatSubmit(interval = 1000, message = "前方拥堵，请稍后尝试")
    public HttpResult audit(@RequestBody @Validated TradeOrderAuditParam tradeOrderAuditParam) {
        return HttpResult.ok("贸易订单审核成功", kwoTradeOrderService.auditOrder(tradeOrderAuditParam));
    }

    @PostMapping("/settle")
    @Operation(summary = "贸易订单手动完结", description = "贸易订单手动完结")
//    @GlobalTransactional(name = "default_tx_group")
    @RepeatSubmit(interval = 1000, message = "前方拥堵，请稍后尝试")
    public HttpResult settle(@RequestBody @Validated TradeOrderSettleParam tradeOrderSettleParam) {
        return HttpResult.ok("手动完结成功", kwoTradeOrderService.settleOrder(tradeOrderSettleParam));
    }

    /**
     * @desc: 修改订单
     * @author: yzc
     * @date: 2023-07-07 15:34
     * @Param updateOrderParam:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @GlobalTransactional(name = "default_tx_group")
    @RepeatSubmit(interval = 1000, message = "前方拥堵，请稍后尝试")
    @PostMapping(value = "/update", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult update(@RequestBody @Validated UpdateOrderParam updateOrderParam) {
        kwoTradeOrderService.update(updateOrderParam);
        return HttpResult.ok("订单修改成功");
    }

    /**
     * @desc: 订单受理
     * @author: yzc
     * @date: 2023-07-12 11:16
     * @Param acceptanceOrderParam:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @GlobalTransactional(name = "default_tx_group")
    @RepeatSubmit(interval = 1000, message = "前方拥堵，请稍后尝试")
    @PostMapping(value = "/acceptanceOrder", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult acceptanceOrder(@RequestBody @Validated AcceptanceOrderParam acceptanceOrderParam) {
        kwoTradeOrderService.acceptanceOrder(acceptanceOrderParam);
        return HttpResult.ok("订单受理成功");
    }

    /**
     * @desc: 驳回订单
     * @author: yzc
     * @date: 2023-07-12 11:22
     * @Param rejectOrderParam:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @GlobalTransactional(name = "default_tx_group")
    @PostMapping(value = "/rejectOrder", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult rejectOrder(@RequestBody @Validated RejectOrderParam rejectOrderParam) {
        kwoTradeOrderService.rejectOrder(rejectOrderParam);
        return HttpResult.ok("订单驳回成功");
    }

    /**
     * @desc: 驳回订单原因查询
     * @author: yzc
     * @date: 2023-07-12 11:22
     * @Param id:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @GetMapping("/rejectReason")
    public HttpResult rejectReason(@RequestParam Long id) {
        return HttpResult.ok("驳回订单原因查询成功", kwoTradeOrderService.rejectReason(id));
    }

    /**
     * @desc: 完结订单
     * @author: yzc
     * @date: 2023-07-12 17:29
     * @Param completeOrderParam:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @GlobalTransactional(name = "default_tx_group")
    @PostMapping(value = "/completeOrder", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult completeOrder(@RequestBody @Validated CompleteOrderParam completeOrderParam) {
        kwoTradeOrderService.completeOrder(completeOrderParam);
        return HttpResult.ok("订单完结成功");
    }


    /**
     * @desc: 批量删除
     * @author: yzc
     * @date: 2023-07-12 17:34
     * @Param param:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @GlobalTransactional(name = "default_tx_group")
    @PostMapping(value = "/batchDelete", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult batchDelete(@RequestBody @Validated BatchDeleteParam param) {
        kwoTradeOrderService.batchDelete(param);
        return HttpResult.ok("订单批量删除成功");
    }

    /**
     * @desc: 获取贸易订单地址下拉列表
     * @author: yzc
     * @date: 2023-08-02 17:26
     * @Param param:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @GetMapping("/getAddressDropList")
    public HttpResult getAddressDropList() {
        return HttpResult.ok("获取贸易订单地址下拉列表成功", kwoTradeOrderService.getAddressDropList());
    }

    /**
     * @param id 贸易主键ID
     * @desc 查询贸易订单商品关联单位
     * @author zk
     * @date 2023/12/4
     **/
    @GetMapping("/goodsUnit")
    public HttpResult goodsUnit(@RequestParam Long id) {
        return HttpResult.ok(kwoTradeOrderService.goodsUnit(id));
    }


    @GetMapping("/getAddressInfo")
    public HttpResult getAddressInfo(@RequestParam Long id) {
        return HttpResult.ok("贸易订单地址信息查询成功", kwoTradeOrderService.getAddressInfo(id));
    }

    /**
     * @desc: 贸易订单列表分页查询
     * @author: yzc
     * @date: 2023-07-17 10:29
     * @Param params:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @PostMapping(value = "/tradeOrderSelect", produces = MediaType.APPLICATION_JSON_VALUE)
    @Operation(summary = "分页查询贸易订单")
    public HttpResult select(@RequestBody @Validated TradeOrderListSelectParam params) {
        return HttpResult.ok(kwoTradeOrderService.tradeOrderSelect(params));
    }

    /**
     * @desc: 贸易订单列表统计查询
     * @author: yzc
     * @date: 2023-07-17 10:29
     * @Param params:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @PostMapping(value = "/tradeOrderStatistic", produces = MediaType.APPLICATION_JSON_VALUE)
    @Operation(summary = "查询贸易订单统计信息")
    public HttpResult statistic(@RequestBody @Validated TradeOrderListSelectParam params) {
        return HttpResult.ok(kwoTradeOrderService.tradeOrderStatistic(params));
    }

    /**
     * @desc: app主界面贸易订单统计
     * @author: yzc
     * @date: 2023-08-21 17:38
     * @Param params:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @PostMapping(value = "/tradeOrderAppStatistic", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult appStatistic(@RequestBody @Validated TradeOrderAppStatisticParam params) {
        return HttpResult.ok(kwoTradeOrderService.appStatistic(params));
    }

    /**
     * @desc: 贸易订单列表导出
     * @author: yzc
     * @date: 2023-07-17 10:29
     * @Param params:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @GetMapping(value = "/countStatistic", produces = MediaType.APPLICATION_JSON_VALUE)
    @Operation(summary = "统计贸易订单数量")
    public HttpResult countStatistic() {
        return HttpResult.ok("统计贸易订单数量成功", kwoTradeOrderService.countStatistic());
    }

    @PostMapping(value = "/tradeOrderExport", produces = MediaType.APPLICATION_JSON_VALUE)
    public void export(@RequestBody @Validated TradeOrderListExportParam params, HttpServletResponse response) {
        List<TradeOrderListExport> list = kwoTradeOrderService.export(params);
        if (CollectionUtils.isEmpty(list)) {
            throw new CustomPromptException(HttpStatus.SUCCESS_CODE, "暂无数据，请确认");
        }
        ExcelUtil.downData(response, TradeOrderListExport.class, list);
    }

    /**
     * @desc: 贸易订单对账单分页列表查询
     * @author: yzc
     * @date: 2023-07-20 15:03
     * @Param params:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @PostMapping(value = "/tradeOrderStatementList", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult tradeOrderStatementList(@RequestBody @Validated TradeOrderStatementList params) {
        return HttpResult.ok(kwoTradeOrderService.tradeOrderStatementList(params));
    }

    /**
     * @desc: 合同关联订单列表
     * @author: yzc
     * @date: 2023-07-27 16:48
     * @Param params:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @PostMapping(value = "/tradeOrderContractList", produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpResult tradeOrderContractList(@RequestBody @Validated TradeOrderContractParam params) {
        return HttpResult.ok(kwoTradeOrderService.tradeOrderContractList(params));
    }

    @PostMapping("/queryAddContractList")
    @Operation(summary = "增补运力下拉框查询", description = "增补运力下拉框查询")
    public HttpResult queryAddContractList(@RequestBody @Validated ContractLogisticsQueryParam param) {
        return HttpResult.ok("增补运力下拉框查询成功", kwoTradeOrderService.queryAddContractList(param));
    }

    @PostMapping("/addContractList")
    @Operation(summary = "增补运力", description = "增补运力")
    public HttpResult addContractList(@RequestBody @Validated ContractLogisticsAddParam param) {
        kwoTradeOrderService.addContractList(param);
        return HttpResult.ok("增补运力查询成功");
    }

    @GlobalTransactional(name = "default_tx_group")
    @RepeatSubmit(interval = 1000, message = "前方拥堵，请稍后尝试")
    @PostMapping(value = "/cancelOrder", produces = MediaType.APPLICATION_JSON_VALUE)
    @Operation(summary = "买家撤销贸易订单", description = "买家撤销贸易订单")
    public HttpResult cancelOrder(@RequestBody @Validated CancelTradeOrderParam param) {
        kwoTradeOrderService.cancelTradeOrder(param);
        return HttpResult.ok("订单撤销成功");
    }

    /**
     * 客户销售排行
     */
    @GetMapping("/saleTop")
    @Operation(summary = "客户销售排行", description = "客户销售排行")
    public R<List<SaleTopVo>> saleTop() {
        return R.ok(kwoTradeOrderService.saleTop());
    }

    /**
     * 累计交易金额
     *
     * @return
     */
    @GetMapping("/saleSum")
    @Operation(summary = "累计交易金额", description = "累计交易金额")
    public R<BigDecimal> saleSum() {
        return R.ok(kwoTradeOrderService.saleSum());
    }

    @GetMapping("/saleSummary")
    @Operation(summary = "商品销售排行", description = "商品销售排行")
    public R<List<KwoTradeOrderArchiveVo>> saleSummary() {
        return R.ok(kwoTradeOrderService.saleSummary());
    }

}
