package com.jkcredit.invoice.hub.controller.web.record;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jkcredit.invoice.hub.controller.base.WebResource;
import com.jkcredit.invoice.hub.controller.web.record.fb.BillRecordFB;
import com.jkcredit.invoice.hub.controller.web.record.fb.ConsumptionStatisticsFB;
import com.jkcredit.invoice.hub.controller.web.record.fb.InvoiceRecordFB;
import com.jkcredit.invoice.hub.model.dto.apiCarFree.InvoiceResultDto;
import com.jkcredit.invoice.hub.model.dto.carFreeCarrierBillStart.BillRecordDto;
import com.jkcredit.invoice.hub.model.dto.record.ConsumptionStatisticsDto;
import com.jkcredit.invoice.hub.model.dto.searchInvoiceResult.InvoiceRecordDto;
import com.jkcredit.invoice.hub.model.vo.record.BillRecordVo;
import com.jkcredit.invoice.hub.model.vo.record.InvoiceRecordVo;
import com.jkcredit.invoice.hub.service.carFreeCarrierBillStart.CarFreeCarrierBillStartService;
import com.jkcredit.invoice.hub.service.consumptionStatistics.ConsumptionStatisticsService;
import com.jkcredit.invoice.hub.service.searchInvoiceResult.SearchInvoiceResultService;
import com.jkcredit.invoice.hub.spi.lang.exception.ServiceException;
import com.jkcredit.invoice.hub.spi.web.data.ResponseData;
import com.jkcredit.invoice.hub.util.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @author: xusonglin
 * @create: 2020/1/20 16:31
 * @version: V1.0
 **/
@Slf4j
@RestController
public class BillRecordResource extends WebResource {
    @Autowired
    CarFreeCarrierBillStartService startService;
    @Autowired
    SearchInvoiceResultService resultService;
    @Autowired
    ConsumptionStatisticsService consumptionStatisticsService;

    @PreAuthorize("hasPermission('record','read')")
    @GetMapping("/billRecord")
    public ResponseData billRecord(Page page, BillRecordFB fb) {
        try {
            BillRecordDto dto = new BillRecordDto();
            BeanUtil.copyProperties(dto, fb);
            IPage<BillRecordVo> billRecordVoList = startService.getBillRecord(page, dto);
            return ResponseData.success(billRecordVoList);
        } catch (ServiceException e) {
            log.error("获取运单详情列表失败, 失败原因：{}", e.getMessage());
            return ResponseData.failed("获取运单详情列表失败");
        }
    }

    @PreAuthorize("hasPermission('record','read')")
    @GetMapping("/invoiceRecord")
    public ResponseData invoiceRecord(Page page, InvoiceRecordFB fb) {
        try {
            InvoiceRecordDto dto = new InvoiceRecordDto();
            BeanUtil.copyProperties(dto, fb);
            IPage<InvoiceRecordVo> invoiceRecordVoIPage = resultService.getInvoiceRecords(page, dto);
            return ResponseData.success(invoiceRecordVoIPage);
        } catch (ServiceException e) {
            log.error("获取发票详情列表失败, 失败原因：{}", e.getMessage());
            return ResponseData.failed("获取发票详情列表失败");
        }
    }

    @PreAuthorize("hasPermission('consumption','read')")
    @GetMapping("/consumptionStatistics")
    public ResponseData consumptionStatistics(Page page, ConsumptionStatisticsFB fb) {
        try {
            ConsumptionStatisticsDto dto = new ConsumptionStatisticsDto();
            BeanUtil.copyProperties(dto, fb);
            return ResponseData.success(consumptionStatisticsService.getConsumptionStatistics(page, dto));
        } catch (ServiceException e) {
            log.error("获取消费统计列表失败，失败原因：{}", e.getMessage());
            return ResponseData.failed("获取消费统计列表失败");
        }
    }
}
