package com.sckw.payment.controller.operate;

import com.sckw.core.web.response.HttpResult;
import com.sckw.excel.utils.ExcelUtil;
import com.sckw.payment.model.constant.TradeUnitType;
import com.sckw.payment.model.vo.SettlementTrade;
import com.sckw.payment.model.vo.excel.SettlementTradeExportVo;
import com.sckw.payment.model.vo.req.SettlementTradeReq;
import com.sckw.payment.service.operate.SettlementTradeService;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 结算-交易订单
 *
 * @author xucaiqin
 */
@RestController
@RequestMapping( "/settlementTrade")
public class SettlementTradeController {

    @Resource
    private SettlementTradeService settlementTradeService;

    /**
     * 销售分页列表
     *
     * @param settlementReq
     * @return
     */
    @PostMapping( "list")
    public HttpResult list(@RequestBody @Valid SettlementTradeReq settlementReq) {
        settlementReq.check();
        return HttpResult.ok(settlementTradeService.list(settlementReq));
    }

    /**
     * 待结算、部分结算、全部结算
     *
     * @param settlementReq
     * @return
     */
    @PostMapping( "count")
    public HttpResult getCount(@RequestBody @Valid SettlementTradeReq settlementReq) {
        settlementReq.check();
        return HttpResult.ok(settlementTradeService.getCount(settlementReq));
    }

    /**
     * 表尾金额结算数据
     *
     * @param settlementReq
     * @return
     */
    @PostMapping( "sum")
    public HttpResult sum(@RequestBody @Valid SettlementTradeReq settlementReq) {
        settlementReq.check();
        return HttpResult.ok(settlementTradeService.sumMoney(settlementReq));
    }

    @PostMapping("export")
    public HttpResult exportCollection(HttpServletResponse response, @RequestBody @Valid SettlementTradeReq settlementReq) {
        settlementReq.check();
        settlementReq.setUnitType(TradeUnitType.SELL);
        settlementReq.setUnitTypeTwo(TradeUnitType.PURCHASE);
        List<SettlementTrade> list = settlementTradeService.exportList(settlementReq,settlementReq.getIdList());
        if (CollectionUtils.isEmpty(list)) {
            return HttpResult.error("暂无数据，请确认");
        }
        List<SettlementTradeExportVo> collect = list.stream().map(a -> {
            SettlementTradeExportVo settlementTradeExportVo = new SettlementTradeExportVo();
            BeanUtils.copyProperties(a, settlementTradeExportVo);
            return settlementTradeExportVo;
        }).collect(Collectors.toList());
        ExcelUtil.downData(response, SettlementTradeExportVo.class, collect);
        return null;
    }
}
