package com.yiqi.admin.controller.settlement;

import com.yiqi.common.constant.UrlConstants;
import com.yiqi.common.core.controller.BaseController;
import com.yiqi.common.core.domain.AjaxResult;
import com.yiqi.common.core.page.TableDataInfo;
import com.yiqi.common.utils.DateUtils;
import com.yiqi.common.utils.poi.ExcelUtil;
import com.yiqi.core.domain.SettlementStoreStatistics;
import com.yiqi.core.service.ISettlementFactoryStatisticsService;
import com.yiqi.core.service.ISettlementStoreClothStatisticsService;
import com.yiqi.core.service.ISettlementStoreStatisticsService;
import com.yiqi.order.domain.dto.SettlementFactoryStatisticsDto;
import com.yiqi.settlement.domain.dto.StoreChargeStatistics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 平台统计报表
 */
@RestController
@RequestMapping(UrlConstants.managerApi + "/platform/statistics")
public class PlatformStatisticsController extends BaseController {

    @Autowired
    private ISettlementFactoryStatisticsService settlementFactoryStatisticsService;

    @Autowired
    private ISettlementStoreClothStatisticsService settlementStoreClothStatisticsService;

    @Autowired
    private ISettlementStoreStatisticsService settlementStoreStatisticsService;

    /**
     * 获取工厂端首页总收益数据
     */
    @GetMapping("/dashborad")
    public AjaxResult dashboard() {
        // 获取订单统计数据
        SettlementStoreStatistics orderStats = settlementStoreStatisticsService.orderStatistics(null, null, null);
        Map<String, Object> result = new HashMap<>();
        result.put("orderStats", orderStats);
        return AjaxResult.success(result);
    }

    /**
     * 获取门店经营日报趋势数据
     */
    @GetMapping("/trend")
    public AjaxResult getDailyTrend(@RequestParam("startDate") String startDate,
                                    @RequestParam("endDate") String endDate) {
        Date start = DateUtils.parseDate(startDate);
        Date end = DateUtils.parseDate(endDate);
        // 获取订单统计趋势
        SettlementStoreStatistics orderStats = settlementStoreStatisticsService.orderStatistics(null, DateUtils.parseDate(startDate), DateUtils.parseDate(endDate));
        Map<String, Object> result = new HashMap<>();
        result.put("orderTrend", orderStats);
        return AjaxResult.success(result);
    }

    /**
     * 获取每日交易统计列表
     */
    @GetMapping("/order/list")
    public AjaxResult list(@RequestParam(required = false) String startDate,
                           @RequestParam(required = false) String endDate,
                           @RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "15") Integer pageSize) {

        Date start = DateUtils.parseDate(startDate);
        Date end = DateUtils.parseDate(endDate);
        // 获取订单统计趋势
        List<SettlementStoreStatistics> orderTrend = settlementStoreStatisticsService.generatePlatformStatisticsByDate(start, end);
        return AjaxResult.success(orderTrend);
    }

    /**
     * 获取门店每日订单统计列表
     */
    @GetMapping("/order/bystore")
    public TableDataInfo generateStatisticsByStore(@RequestParam(required = false) String startDate,
                                                   @RequestParam(required = false) String endDate,
                                                   @RequestParam(required = false) Long storeId,
                                                   @RequestParam(defaultValue = "1") Integer pageNum,
                                                   @RequestParam(defaultValue = "15") Integer pageSize) {
        Date start = startDate != null ? DateUtils.parseDate(startDate) : DateUtils.addDays(new Date(), -14);
        Date end = endDate != null ? DateUtils.parseDate(endDate) : new Date();
        startPage();
        List<SettlementStoreStatistics> settlementStoreStatistics = settlementStoreStatisticsService.generatePlatformStatisticsByStore(start, end);
        return getDataTable(settlementStoreStatistics);
    }

    /**
     * 获取门店每日订单统计列表
     */
    @GetMapping("/order/byday")
    public TableDataInfo generateStatisticsByDay(@RequestParam(required = false) String startDate,
                                                 @RequestParam(required = false) String endDate,
                                                 @RequestParam(required = false) Long storeId,
                                                 @RequestParam(defaultValue = "1") Integer pageNum,
                                                 @RequestParam(defaultValue = "15") Integer pageSize) {
        Date start = startDate != null ? DateUtils.parseDate(startDate) : DateUtils.addDays(new Date(), -14);
        Date end = endDate != null ? DateUtils.parseDate(endDate) : new Date();
        List<SettlementStoreStatistics> statisticsList = settlementStoreStatisticsService.generateStatisticsByDay(storeId, start, end);
        Long totalCount = (long) statisticsList.size();
        // 4. 处理分页
        if (pageNum != null && pageSize != null && pageNum > 0 && pageSize > 0) {
            int startIndex = (pageNum - 1) * pageSize;
            int endIndex = Math.min(startIndex + pageSize, statisticsList.size());
            if (startIndex >= statisticsList.size()) {
                statisticsList = Collections.emptyList();
            }
            statisticsList = statisticsList.subList(startIndex, endIndex);
        }
        return getDataTable(statisticsList, totalCount);
    }

    /**
     * 获取工厂每日统计
     */
    @GetMapping("/order/byfactory")
    public TableDataInfo generateStatisticsByFactory(@RequestParam(required = false) String startDate,
                                                     @RequestParam(required = false) String endDate,
                                                     @RequestParam(required = false) Long factoryId,
                                                     @RequestParam(defaultValue = "1") Integer pageNum,
                                                     @RequestParam(defaultValue = "15") Integer pageSize) {
        Date start = startDate != null ? DateUtils.parseDate(startDate) : DateUtils.addDays(new Date(), -14);
        Date end = endDate != null ? DateUtils.parseDate(endDate) : new Date();
        List<SettlementFactoryStatisticsDto> statisticsList = settlementStoreStatisticsService.generateStatisticsByFactory(factoryId, start, end);
        Long totalCount = (long) statisticsList.size();
        // 4. 处理分页
        if (pageNum != null && pageSize != null && pageNum > 0 && pageSize > 0) {
            int startIndex = (pageNum - 1) * pageSize;
            int endIndex = Math.min(startIndex + pageSize, statisticsList.size());
            if (startIndex >= statisticsList.size()) {
                statisticsList = Collections.emptyList();
            }
            statisticsList = statisticsList.subList(startIndex, endIndex);
        }
        return getDataTable(statisticsList, totalCount);
    }

    /**
     *
     */
    @PostMapping("/order/byday/export")
    public void dayExport(HttpServletResponse response,
                              @RequestParam(required = false) String startDate,
                              @RequestParam(required = false) String endDate,
                              @RequestParam(required = false) Long storeId) {
        Date start = startDate != null ? DateUtils.parseDate(startDate) : DateUtils.addDays(new Date(), -14);
        Date end = endDate != null ? DateUtils.parseDate(endDate) : new Date();
        List<SettlementStoreStatistics> statisticsList = settlementStoreStatisticsService.generateStatisticsByDay(storeId, start, end);
        ExcelUtil<SettlementStoreStatistics> util = new ExcelUtil<>(SettlementStoreStatistics.class);
        util.hideColumn("id", "storeId", "storeName");
        util.exportExcel(response, statisticsList, "按日期报表统计");
    }


    /**
     *
     */
    @PostMapping("/order/byfactory/export")
    public void factoryExport(HttpServletResponse response,
                              @RequestParam(required = false) String startDate,
                              @RequestParam(required = false) String endDate,
                              @RequestParam(required = false) Long factoryId) {
        Date start = startDate != null ? DateUtils.parseDate(startDate) : DateUtils.addDays(new Date(), -14);
        Date end = endDate != null ? DateUtils.parseDate(endDate) : new Date();
        List<SettlementFactoryStatisticsDto> statisticsList = settlementStoreStatisticsService.generateStatisticsByFactory(factoryId, start, end);
        ExcelUtil<SettlementFactoryStatisticsDto> util = new ExcelUtil<>(SettlementFactoryStatisticsDto.class);
        util.hideColumn("id", "storeId", "storeName", "factoryId", "statisticsDate");
        util.exportExcel(response, statisticsList, "工厂报表统计");
    }
}