package com.sckw.report.controller;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import com.sckw.contract.api.model.dto.req.ContractAuditPara;
import com.sckw.contract.api.model.dto.res.ContractDetailRespVo;
import com.sckw.contract.api.model.dto.res.LogisticDetailRespVo;
import com.sckw.contract.api.model.vo.ContractVo;
import com.sckw.core.web.response.BaseResult;
import com.sckw.order.api.model.*;
import com.sckw.report.service.KwHomeService;
import com.sckw.report.service.vo.OperationCountVo;
import com.sckw.report.api.model.TodoCountVo;
import com.sckw.transport.api.model.param.LogisticsOrderAuditPara;
import com.sckw.transport.api.model.vo.LogisticsDto;
import com.sckw.transport.api.model.vo.LogisticsVo;
import com.sckw.transport.api.model.vo.WaybillOrderDetailVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


@AllArgsConstructor
@RestController
@RequestMapping("/kwHome")
@Tag(name = "供应商首页", description = "供应商首页")
public class HomeController {

    private final KwHomeService kwHomeService;

    @GetMapping("/operationCount")
    @Operation(summary = "运营-运营统计", description = "运营统计")
    public BaseResult<OperationCountVo> operationCount() {
        return BaseResult.success(kwHomeService.operationCount());
    }

    @GetMapping("/count")
    @Operation(summary = "ai助手待办统计", description = "ai助手待办统计")
    public TodoCountVo todoCount() {
        return kwHomeService.todoCount();
    }

    @GetMapping("/operationTradeContract")
    @Operation(summary = "运营-列表贸易合同", description = "运营数据列表贸易合同")
    public BaseResult<List<ContractVo>> operationTradeContract() {
        return BaseResult.success(kwHomeService.operationTradeContract());
    }

    @GetMapping("/operationLogisticsContract")
    @Operation(summary = "运营-列表物流合同", description = "运营数据列表物流合同")
    public BaseResult<List<ContractVo>> operationLogisticsContract() {
        return BaseResult.success(kwHomeService.operationLogisticsContract());
    }

    @GetMapping("/operationTradeOrder")
    @Operation(summary = "运营-列表贸易订单", description = "运营数据列表贸易订单")
    public BaseResult<List<TradeVo>> operationTradeOrder() {
        return BaseResult.success(kwHomeService.operationTradeOrder());
    }

    @GetMapping("/operationLogisticsWaybill")
    @Operation(summary = "运营-列表物流运单", description = "运营数据列表物流运单")
    public BaseResult<List<LogisticsVo>> operationLogisticsWaybill() {
        return BaseResult.success(kwHomeService.operationLogisticsOrder());
    }

    //连续审核贸易合同
    // kwcSale/queryContractDetail-贸易合同详情
    @PostMapping("/auditTradeContract")
    @Operation(summary = "运营-贸易合同审核", description = "勾选连续审核时，操作通过或拒绝之后，拿着上次的合同id再次调用这个接口。有下一个待审核的合同时，返回详情。如果没有，将返回业务异常。")
    public BaseResult<ContractDetailRespVo> auditTradeContract(@RequestBody @Valid ContractAuditPara contractAuditPara) {
        return BaseResult.success(kwHomeService.auditTradeContract(contractAuditPara));
    }

    // kwcSale/queryContractDetail-贸易合同详情
    @PostMapping("/signTradeContract")
    @Operation(summary = "运营-贸易合同签约", description = "")
    public BaseResult<ContractDetailRespVo> signTradeContract(@RequestBody @Valid ContractAuditPara contractAuditPara) {
        return BaseResult.success(kwHomeService.signTradeContract(contractAuditPara));
    }

    //logisticsContract/queryLogisticsContractDetail
    @PostMapping("/signLogisticsContract")
    @Operation(summary = "运营-物流合同签约", description = "物流合同签约")
    public BaseResult<LogisticDetailRespVo> signLogisticsContract(@RequestBody @Valid ContractAuditPara contractAuditPara) {
        return BaseResult.success(kwHomeService.signLogisticsContract(contractAuditPara));
    }

    //kwoTradeOrder/detail
    @PostMapping("/auditTradeOrder")
    @Operation(summary = "运营-贸易订单审核", description = "贸易订单审核")
    public BaseResult<OrderDetailRes> auditTradeOrder(@RequestBody @Valid TradeOrderAuditPara para) {
        return BaseResult.success(kwHomeService.auditTradeOrder(para));
    }

    //kwtWaybillOrder/queryBillOrderDetail
    @PostMapping("/auditLogisticsTask")
    @Operation(summary = "运营-物流运单审核", description = "物流运单审核")
    public BaseResult<WaybillOrderDetailVo> auditLogisticsTask(@RequestBody @Valid LogisticsOrderAuditPara para) {
        return BaseResult.success(kwHomeService.auditLogisticsTask(para));
    }

    private void fillDefault(CountPara countPara) {
        if (StrUtil.isBlank(countPara.getStartTime()) && StrUtil.isBlank(countPara.getEndTime())) {
            LocalDateTime start = LocalDateTimeUtil.beginOfDay(LocalDateTime.now()).minusDays(7);
            LocalDateTime end = LocalDateTimeUtil.endOfDay(LocalDateTime.now());

            countPara.setStartTime(LocalDateTimeUtil.formatNormal(start));
            countPara.setEndTime(LocalDateTimeUtil.formatNormal(end));
        }
    }

    @PostMapping("/countSale")
    @Operation(summary = "数据统计-销量", description = "数据统计-销量")
    public BaseResult<SaleDto> countSale(@RequestBody CountPara countPara) {
        fillDefault(countPara);
        return BaseResult.success(kwHomeService.countSale(countPara));
    }

    @PostMapping("/countBuy")
    @Operation(summary = "数据统计-采购商", description = "数据统计-采购商")
    public BaseResult<BuyDto> countBuy(@RequestBody CountPara countPara) {
        fillDefault(countPara);
        return BaseResult.success(kwHomeService.countBuy(countPara));
    }

    @PostMapping("/countLogistics")
    @Operation(summary = "数据统计-物流", description = "数据统计-物流")
    public BaseResult countLogistics(@RequestBody CountPara countPara) {
        fillDefault(countPara);
        return BaseResult.success(kwHomeService.countLogistics(countPara));
    }

    @PostMapping("/countGoods")
    @Operation(summary = "数据统计-商品", description = "数据统计-商品")
    public BaseResult<GoodsDto> countGoods(@RequestBody CountPara countPara) {
        fillDefault(countPara);
        return BaseResult.success(kwHomeService.countGoods(countPara));
    }

    //实时销量
    @GetMapping("/timeSale")
    @Operation(summary = "实时销量", description = "实时销量")
    public BaseResult<TimeSaleDto> timeSale() {
        return BaseResult.success(kwHomeService.timeSale());
    }

    //采购量排行
    @GetMapping("/buyRank")
    @Operation(summary = "采购量排行", description = "采购量排行")
    public BaseResult<RankDto> buyRank() {
        return BaseResult.success(kwHomeService.buyRank());
    }

    //物流排行
    @GetMapping("/logisticsRank")
    @Operation(summary = "物流排行", description = "物流排行")
    public BaseResult<LogisticsDto> logisticsRank() {
        return BaseResult.success(kwHomeService.logisticsRank());
    }

}
