package com.sckw.payment.controller;

import com.sckw.core.exception.BusinessException;
import com.sckw.core.model.page.PageRes;
import com.sckw.core.utils.StringUtils;
import com.sckw.core.web.response.HttpResult;
import com.sckw.excel.utils.ExcelUtil;
import com.sckw.payment.model.dto.ILedger;
import com.sckw.payment.model.vo.req.*;
import com.sckw.payment.model.vo.res.LedgerLogisticsVo;
import com.sckw.payment.service.KwpLedgerLogisticsService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

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

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

    private void fill(LogisticsSendReq logisticsSendReq) {
        String ids = logisticsSendReq.getIds();
        List<Long> longs = StringUtils.splitStrToList(ids, Long.class);
        if (CollectionUtils.isEmpty(longs)) {
            throw new BusinessException("物流订单不能为空");
        }
        logisticsSendReq.setOrders(longs);
    }

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

    /**
     * 物流对账单-发起对账
     *
     * @param logisticsReq
     * @return
     */
    @PostMapping("send")
    public HttpResult send(@RequestBody @Valid LogisticsSendReq logisticsReq) {
        fill(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("carrierList")
    public HttpResult carrierList(@RequestBody @Valid LogisticsReq logisticsReq) {
        return HttpResult.ok(kwpLedgerLogisticsService.carrierList(logisticsReq));
    }

    /**
     * 查询物流对账单详情
     *
     * @param id
     * @return
     */
    @GetMapping("queryDetail")
    public HttpResult queryDetail(@RequestParam("id") String id) {
        return HttpResult.ok(kwpLedgerLogisticsService.queryDetail(Long.valueOf(id)));
    }

    /**
     * 修改物流对账单详情（修改物流对账单接口使用）
     *
     * @param id
     * @return
     */
    @GetMapping("queryUpdate")
    public HttpResult queryUpdate(@RequestParam("id") String id) {
        return HttpResult.ok(kwpLedgerLogisticsService.queryUpdate(Long.valueOf(id)));
    }

    /**
     * 托运方
     *
     * @return
     */
    @GetMapping("shipperCount")
    public HttpResult shipperCount() {
        return HttpResult.ok(kwpLedgerLogisticsService.orderCount(1));
    }

    /**
     * 承运方
     *
     * @return
     */
    @GetMapping("carrierCount")
    public HttpResult carrierCount() {
        return HttpResult.ok(kwpLedgerLogisticsService.orderCount(2));
    }

    /**
     * 删除对账单
     * 释放关联的订单（只有删除操作才释放订单）
     *
     * @param id
     * @return
     */
    @DeleteMapping("delete")
    public HttpResult delete(@RequestParam("id") String id) {
        return HttpResult.ok(kwpLedgerLogisticsService.remove(Long.valueOf(id)));
    }

    /**
     * 物流对账单-撤回对账
     *
     * @param id
     * @return
     */
    @PostMapping("back")
    public HttpResult back(@RequestParam("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
     * @return
     */
    @GetMapping("queryBack")
    public HttpResult queryBack(@RequestParam("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));
    }

    /**
     * 托运方-导出
     *
     * @param response
     * @param logisticsReq
     * @return
     */
    @PostMapping("exportShipper")
    public HttpResult exportShipper(HttpServletResponse response, @RequestBody LogisticsReq logisticsReq) {
        String ids = logisticsReq.getIds();
        List<Long> longs = StringUtils.splitStrToList(ids, Long.class);
        List<ILedger> list;
        if (CollectionUtils.isEmpty(longs)) {
            PageRes<ILedger> pageResult = kwpLedgerLogisticsService.shipperList(logisticsReq);
            list = pageResult.getList();
        } else {
            list = kwpLedgerLogisticsService.selectShipperList(longs);
        }
        if (CollectionUtils.isEmpty(list)) {
            return HttpResult.error("没有可导出的数据");
        }
        List<LedgerLogisticsVo> collect = list.stream().map(a -> {
            LedgerLogisticsVo ledgerLogisticsVo = new LedgerLogisticsVo();
            BeanUtils.copyProperties(a, ledgerLogisticsVo);
            return ledgerLogisticsVo;
        }).collect(Collectors.toList());

        ExcelUtil.downData(response, LedgerLogisticsVo.class, collect);
        return null;
    }

    /**
     * 承运方
     *
     * @param response
     * @param logisticsReq
     * @return
     */
    @PostMapping("exportCarrier")
    public HttpResult exportCarrier(HttpServletResponse response, @RequestBody LogisticsReq logisticsReq) {
        String ids = logisticsReq.getIds();
        List<Long> longs = StringUtils.splitStrToList(ids, Long.class);
        List<ILedger> list;
        if (CollectionUtils.isEmpty(longs)) {
            PageRes<ILedger> pageResult = kwpLedgerLogisticsService.carrierList(logisticsReq);
            list = pageResult.getList();

        } else {
            list = kwpLedgerLogisticsService.selectCarrierList(longs);
        }
        if (CollectionUtils.isEmpty(list)) {
            return HttpResult.error("没有可导出的数据");
        }
        List<LedgerLogisticsVo> collect = list.stream().map(a -> {
            LedgerLogisticsVo ledgerLogisticsVo = new LedgerLogisticsVo();
            BeanUtils.copyProperties(a, ledgerLogisticsVo);
            return ledgerLogisticsVo;
        }).collect(Collectors.toList());

        ExcelUtil.downData(response, LedgerLogisticsVo.class, collect);
        return null;
    }
}
