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 + "/settlement/factory/statistics")
public class SettlementFactoryStatisticsController extends BaseController {

    @Autowired
    private ISettlementFactoryStatisticsService settlementFactoryStatisticsService;

    @Autowired
    private ISettlementStoreClothStatisticsService settlementStoreClothStatisticsService;

    /**
     * 获取工厂端首页总收益数据
     */
    @GetMapping("/dashborad")
    public AjaxResult dashboard() {
        SettlementFactoryStatisticsDto settlementFactoryStatisticsDto = settlementStoreClothStatisticsService.totalByCondition(getFactoryId(), null, null, null);
        return AjaxResult.success(settlementFactoryStatisticsDto);
    }

    /**
     * 获取门店每日交易统计列表
     */
    @GetMapping("/list/bydate")
    public TableDataInfo generateFactoryStatisticsByDate(@RequestParam(required = false) String startDate,
                                                         @RequestParam(required = false) String endDate,
                                                         @RequestParam(required = false) Long storeId,
                                                         @RequestParam(defaultValue = "1") Integer pageNum,
                                                         @RequestParam(defaultValue = "10") 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 = settlementFactoryStatisticsService
                .generateFactoryStatisticsByDate(getLoginUser().getOrgId(), storeId, start, end, pageNum, pageSize);
        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("/list/bystore")
    public TableDataInfo generateFactoryStatisticsByStore(@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<SettlementFactoryStatisticsDto> statisticsList = settlementFactoryStatisticsService
                .generateFactoryStatisticsByStoreId(getLoginUser().getOrgId(), 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("/total")
    public AjaxResult generateFactoryStatistics(@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();
        SettlementFactoryStatisticsDto settlementFactoryStatisticsDto = settlementStoreClothStatisticsService
                .totalByCondition(getLoginUser().getFactoryId(), storeId, start, end);
        return AjaxResult.success(settlementFactoryStatisticsDto);
    }

    /**
     *
     */
    @PostMapping("/export/bystore")
    public void chargeExport(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<SettlementFactoryStatisticsDto> list = settlementFactoryStatisticsService
                .generateFactoryStatisticsByStoreId(getLoginUser().getOrgId(), storeId, start, end);
        ExcelUtil<SettlementFactoryStatisticsDto> util = new ExcelUtil<>(SettlementFactoryStatisticsDto.class);
        util.exportExcel(response, list, "工厂门店报表统计");
    }

    /**
     * 导出门店每日交易统计
     */
    @PostMapping("/export/bydate")
    public void export(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<SettlementFactoryStatisticsDto> list = settlementFactoryStatisticsService
                .generateFactoryStatisticsByDate(getLoginUser().getOrgId(), storeId, start, end, 1, 1000);
        ExcelUtil<SettlementFactoryStatisticsDto> util = new ExcelUtil<>(SettlementFactoryStatisticsDto.class);
        util.exportExcel(response, list, "工厂每日报表统计");
    }

    /**
     * 同步每日数据
     */
    @GetMapping("/sync")
    public AjaxResult orderSync() {
        Date queryDate = DateUtils.getNowDate();
        Long storeId = getStoreId();
        if (storeId != null) {
        } else {
            settlementFactoryStatisticsService.generateDayStatistics(queryDate);
        }
        return AjaxResult.success();
    }
}