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.operate.LedgerTradeService;
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
 */
@RestController
@RequestMapping("/ledgerTrade")
@Validated
public class LedgerTradeController {
    @Resource
    private LedgerTradeService ledgerTradeService;


    /**
     * @param tradeReq
     * @return
     */
    @PostMapping("list")
    public HttpResult list(@RequestBody TradeReq tradeReq) {
        return HttpResult.ok(ledgerTradeService.list(tradeReq));
    }

    /**
     * @param ledgerListReq 对账单id
     * @return
     */
    @GetMapping("orderList")
    public HttpResult orderList(@Valid LedgerListReq ledgerListReq) {
        return HttpResult.ok(ledgerTradeService.orderList(ledgerListReq));
    }


    /**
     * @param id 对账单id
     * @return
     */
    @GetMapping("queryDetail")
    public HttpResult queryDetail(@RequestParam("id") String id) {
        return HttpResult.ok(ledgerTradeService.queryDetail(Long.valueOf(id)));
    }

    /**
     * 贸易订单数分类统计
     *
     * @return
     */
    @PostMapping("count")
    public HttpResult count(@RequestBody TradeReq tradeReq) {
        return HttpResult.ok(ledgerTradeService.orderCount(tradeReq));
    }


    /**
     * 导出 todo
     *
     * @param response
     * @param tradeReq
     * @return
     */
    @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;
    }


}
