package com.sckw.payment.controller.operate;

import com.sckw.core.model.page.PageRes;
import com.sckw.core.web.response.HttpResult;
import com.sckw.excel.utils.ExcelUtil;
import com.sckw.payment.model.vo.LedgerSell;
import com.sckw.payment.model.vo.excel.LedgerTradeExport;
import com.sckw.payment.model.vo.req.LedgerListReq;
import com.sckw.payment.model.vo.req.TradeReq;
import com.sckw.payment.service.operateService.LedgerTradeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * 对账-交易订单
 *
 * @author xucaiqin
 */
@Tag(name = "运营交易对账管理", description = "提供运营端交易对账相关操作接口")
@RestController
@RequestMapping("/ledgerTrade")
@Validated
public class LedgerTradeController {
    @Resource
    private LedgerTradeService ledgerTradeService;


    /**
     * 查询交易对账单列表
     *
     * @param tradeReq 查询参数
     * @return 对账单列表
     */
    @Operation(summary = "交易对账单列表", description = "查询交易对账单列表")
    @PostMapping("list")
    public HttpResult list(@RequestBody TradeReq tradeReq) {
        return HttpResult.ok(ledgerTradeService.list(tradeReq));
    }

    /**
     * 对账单关联订单
     *
     * @param ledgerListReq 对账单id
     * @return 关联订单列表
     */
    @Operation(summary = "对账单关联订单", description = "查询对账单关联的订单列表")
    @GetMapping("orderList")
    public HttpResult orderList(@Valid LedgerListReq ledgerListReq) {
        return HttpResult.ok(ledgerTradeService.orderList(ledgerListReq));
    }


    /**
     * 查询交易对账单详情
     *
     * @param id 对账单id
     * @return 对账单详情
     */
    @Operation(summary = "交易对账单详情", description = "根据ID查询交易对账单详情")
    @Parameter(name = "id", description = "交易对账单ID", required = true)
    @GetMapping("queryDetail")
    public HttpResult queryDetail(@RequestParam("id") String id) {
        return HttpResult.ok(ledgerTradeService.queryDetail(Long.valueOf(id)));
    }

    /**
     * 贸易订单数分类统计
     *
     * @param tradeReq 查询参数
     * @return 统计数据
     */
    @Operation(summary = "交易对账单统计", description = "贸易订单数分类统计")
    @PostMapping("count")
    public HttpResult count(@RequestBody TradeReq tradeReq) {
        return HttpResult.ok(ledgerTradeService.orderCount(tradeReq));
    }


    /**
     * 导出交易对账单
     *
     * @param response HTTP响应
     * @param tradeReq 查询参数
     * @return 操作结果
     */
    @Operation(summary = "导出交易对账单", description = "导出交易对账单数据")
    @PostMapping("export")
    public HttpResult export(HttpServletResponse response, @RequestBody TradeReq tradeReq) {
        List<LedgerSell> list;

        if (CollectionUtils.isEmpty(tradeReq.getIdList())) {
            tradeReq.setPage(0);
            tradeReq.setPageSize(0);
            PageRes<LedgerSell> pageResult = ledgerTradeService.list(tradeReq);
            list = pageResult.getList();
        } else {
            list = ledgerTradeService.queryIds(tradeReq.getIdList());
        }

        if (CollectionUtils.isEmpty(list)) {
            return HttpResult.error("暂无数据，请确认");
        }
        List<LedgerTradeExport> collect = list.stream().map(a -> {
            LedgerTradeExport ledgerTradeVo = new LedgerTradeExport();
            BeanUtils.copyProperties(a, ledgerTradeVo);
            return ledgerTradeVo;
        }).collect(Collectors.toList());
        ExcelUtil.downData(response, LedgerTradeExport.class, collect);
        return null;
    }


}