package com.sckw.payment.controller.app;

import com.sckw.core.model.vo.TableTop;
import com.sckw.core.web.response.HttpResult;
import com.sckw.payment.model.constant.LedgerEnum;
import com.sckw.payment.model.constant.LogisticsUnitType;
import com.sckw.payment.model.dto.Id;
import com.sckw.payment.model.vo.req.*;
import com.sckw.payment.service.KwpLedgerLogisticsService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 对账-物流订单
 *
 * @author xucaiqin
 */
@Validated
@RestController
@RequestMapping("/appKwpLedgerLogistics")
public class AppKwpLedgerLogisticsController {
    @Resource
    private KwpLedgerLogisticsService kwpLedgerLogisticsService;

    /**
     * 物流对账单-发起对账(保存草稿)（承运方）
     *
     * @param logisticsReq 保存物流对账单参数
     * @return
     */
    @PostMapping("sendDraft")
    public HttpResult sendDraft(@RequestBody @Valid LogisticsSendReq logisticsReq) {
        return HttpResult.ok(kwpLedgerLogisticsService.sendLedgerDraft(logisticsReq));
    }

    /**
     * 物流对账单-发起对账（承运方）
     *
     * @param logisticsReq 保存物流对账单参数
     * @return
     */
    @PostMapping("send")
    public HttpResult send(@RequestBody @Valid LogisticsSendReq logisticsReq) {
        return HttpResult.ok(kwpLedgerLogisticsService.sendLedger(logisticsReq));
    }

    /**
     * 托运方物流对账单列表查询
     *
     * @param logisticsReq 物流订单查询参数
     * @return
     */
    @PostMapping("shipperList")
    public HttpResult shipperList(@RequestBody @Valid LogisticsReq logisticsReq) {
        return HttpResult.ok(kwpLedgerLogisticsService.shipperList(logisticsReq));
    }

    /**
     * 托运方物流对账单统计
     *
     * @param logisticsReq 物流订单查询参数
     * @return 对账单梳理、对账单金额
     */
    @PostMapping("shipperSum")
    public HttpResult shipperSum(@RequestBody @Valid LogisticsReq logisticsReq) {
        return HttpResult.ok(kwpLedgerLogisticsService.shipperSum(logisticsReq));
    }

    /**
     * 托运方对账单关联订单
     *
     * @param ledgerListReq 对账单id
     * @return
     */
    @GetMapping("shipperOrderList")
    public HttpResult shipperOrderList(@Valid LedgerListReq ledgerListReq) {
        return HttpResult.ok(kwpLedgerLogisticsService.queryOrderList(ledgerListReq));
    }

    /**
     * 承运方物流对账单列表查询（物流公司、收钱方）
     *
     * @param logisticsReq 物流订单查询参数
     * @return
     */
    @PostMapping("carrierList")
    public HttpResult carrierList(@RequestBody @Valid LogisticsReq logisticsReq) {
        return HttpResult.ok(kwpLedgerLogisticsService.carrierList(logisticsReq));
    }


    /**
     * 承运方物流对账单统计
     *
     * @param logisticsReq
     * @return 对账应收金额、对账单数量
     */
    @PostMapping("carrierSum")
    public HttpResult carrierSum(@RequestBody @Valid LogisticsReq logisticsReq) {
        return HttpResult.ok(kwpLedgerLogisticsService.carrierSumCount(logisticsReq));
    }

    /**
     * 承运方对账单关联订单
     *
     * @param ledgerListReq 对账单id
     * @return
     */
    @GetMapping("carrierOrderList")
    public HttpResult carrierOrderList(@Valid LedgerListReq ledgerListReq) {
        return HttpResult.ok(kwpLedgerLogisticsService.queryOrderList(ledgerListReq));
    }

    /**
     * 承运方-查询物流对账单详情
     *
     * @param id 物流对账单id
     * @return 物流对账单详情
     */
    @GetMapping("queryCarrierDetail")
    public HttpResult queryCarrierDetail(@RequestParam("id") @NotBlank(message = "物流对账单id不能为空") String id) {
        return HttpResult.ok(kwpLedgerLogisticsService.queryDetail(Long.valueOf(id), LogisticsUnitType.SHIPPER));
    }

    /**
     * 托运方-查询物流对账单详情
     *
     * @param id 物流对账单id
     * @return 物流对账单详情
     */
    @GetMapping("queryShipperDetail")
    public HttpResult queryDetail(@RequestParam("id") @NotBlank(message = "物流对账单id不能为空") String id) {
        return HttpResult.ok(kwpLedgerLogisticsService.queryDetail(Long.valueOf(id), LogisticsUnitType.CARRIER));
    }

    /**
     * 修改物流对账单详情（修改物流对账单接口使用） 仅发起对账方支持修改
     *
     * @param id 物流对账单id
     * @return 物流对账单详情
     */
    @GetMapping("queryUpdate")
    public HttpResult queryUpdate(@RequestParam("id") @NotBlank(message = "物流对账单id不能为空") String id) {
        return HttpResult.ok(kwpLedgerLogisticsService.queryUpdate(Long.valueOf(id)));
    }

    /**
     * 托运方
     *
     * @param logisticsReq 物流订单查询参数
     * @return 物流对账单统计数据
     */
    @PostMapping("shipperCount")
    public HttpResult shipperCount(@RequestBody @Valid LogisticsReq logisticsReq) {
        logisticsReq.setUnitType(LogisticsUnitType.CARRIER);
        logisticsReq.setUnitTypeTwo(LogisticsUnitType.SHIPPER);
        List<TableTop> tableTops = kwpLedgerLogisticsService.orderCount(logisticsReq);
        List<Integer> re = new ArrayList<>() {{
            add(LedgerEnum.SAVE.getStatus());
            add(LedgerEnum.REVOCATION.getStatus());
        }};
        return HttpResult.ok(tableTops.stream().filter(a -> !re.contains(a.getValue())).collect(Collectors.toList()));
    }

    /**
     * 承运方
     *
     * @param logisticsReq 物流订单查询参数
     * @return 物流对账单统计数据
     */
    @PostMapping("carrierCount")
    public HttpResult carrierCount(@RequestBody @Valid LogisticsReq logisticsReq) {
        logisticsReq.setUnitType(LogisticsUnitType.SHIPPER);
        logisticsReq.setUnitTypeTwo(LogisticsUnitType.CARRIER);
        return HttpResult.ok(kwpLedgerLogisticsService.orderCount(logisticsReq));
    }

    /**
     * 删除对账单
     * 释放关联的订单（只有删除操作才释放订单）
     *
     * @param id 物流对账单id
     * @return
     */
    @DeleteMapping("delete")
    public HttpResult delete(@RequestBody @Valid Id id) {
        return HttpResult.ok(kwpLedgerLogisticsService.remove(id.getIdLong()));
    }

    /**
     * 物流对账单-撤回对账
     *
     * @param id 物流对账单id
     * @return
     */
    @PostMapping("back")
    public HttpResult back(@RequestParam("id") @NotBlank(message = "物流对账单id不能为空") String id) {
        return HttpResult.ok(kwpLedgerLogisticsService.backOrder(Long.valueOf(id)));
    }

    /**
     * 物流对账单-对账确认
     *
     * @param confirmReq
     * @return
     */
    @PostMapping("confirm")
    public HttpResult confirm(@RequestBody @Valid LedgerConfirmReq confirmReq) {
        return HttpResult.ok(kwpLedgerLogisticsService.confirmOrder(confirmReq));
    }

    /**
     * 物流对账单-驳回查询
     *
     * @param id 物流对账单id
     * @return
     */
    @GetMapping("queryBack")
    public HttpResult queryBack(@RequestParam("id") @NotBlank(message = "物流对账单id不能为空") String id) {
        return HttpResult.ok(kwpLedgerLogisticsService.queryBack(Long.valueOf(id)));
    }

    /**
     * 物流对账单-驳回
     *
     * @param ledgerReq 驳回参数
     * @return
     */
    @PostMapping("doBack")
    public HttpResult doBack(@RequestBody @Valid LedgerReq ledgerReq) {
        return HttpResult.ok(kwpLedgerLogisticsService.doBack(ledgerReq));
    }

    /**
     * 物流对账单-对账完成
     * <p>
     * 生成结算单
     * </p>
     *
     * @param ledgerReq 对账完成参数
     * @return
     */
    @PostMapping("success")
    public HttpResult orderSuccess(@RequestBody @Valid LedgerSuccessReq ledgerReq) {
        return HttpResult.ok(kwpLedgerLogisticsService.orderSuccess(ledgerReq));
    }
}
