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.LedgerLogistics;
import com.sckw.payment.model.vo.excel.LedgerLogisticsExport;
import com.sckw.payment.model.vo.req.LedgerListReq;
import com.sckw.payment.model.vo.req.LogisticsReq;
import com.sckw.payment.service.operate.LedgerLogisticsService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
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
 */
@Validated
@RestController
@RequestMapping("/ledgerLogistics")
public class LedgerLogisticsController {
    @Resource
    private LedgerLogisticsService ledgerLogisticsService;


    /**
     * @param logisticsReq 物流订单查询参数
     * @return
     */
    @PostMapping("list")
    public HttpResult list(@RequestBody LogisticsReq logisticsReq) {
        return HttpResult.ok(ledgerLogisticsService.list(logisticsReq, false));
    }

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


    /**
     * 查询物流对账单详情
     *
     * @param id 物流对账单id
     * @return 物流对账单详情
     */
    @GetMapping("queryDetail")
    public HttpResult queryDetail(@RequestParam("id") @NotBlank(message = "物流对账单id不能为空") String id) {
        return HttpResult.ok(ledgerLogisticsService.queryDetail(Long.valueOf(id)));
    }

    /**
     * @param logisticsReq 物流订单查询参数
     * @return 物流对账单统计数据
     */
    @PostMapping("count")
    public HttpResult count(@RequestBody LogisticsReq logisticsReq) {
        return HttpResult.ok(ledgerLogisticsService.orderCount(logisticsReq));
    }


    /**
     * 导出
     *
     * @param response
     * @param logisticsReq
     * @return
     */
    @PostMapping("export")
    public HttpResult export(HttpServletResponse response, @RequestBody LogisticsReq logisticsReq) {
        List<LedgerLogistics> list;
        if (CollectionUtils.isEmpty(logisticsReq.getIdList())) {
            PageRes<LedgerLogistics> pageResult = ledgerLogisticsService.list(logisticsReq, true);
            list = pageResult.getList();
        } else {
            list = ledgerLogisticsService.selectList(logisticsReq.getIdList());
        }
        if (CollectionUtils.isEmpty(list)) {
            return HttpResult.error("暂无数据，请确认");
        }
        List<LedgerLogisticsExport> collect = list.stream().map(a -> {
            LedgerLogisticsExport ledgerLogisticsVo = new LedgerLogisticsExport();
            BeanUtils.copyProperties(a, ledgerLogisticsVo);
            return ledgerLogisticsVo;
        }).collect(Collectors.toList());

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