package com.yc.ship.module.trade.controller.admin.business;

import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.excel.core.util.ExcelUtils;
import com.yc.ship.framework.operatelog.core.annotations.OperateLog;
import com.yc.ship.module.trade.controller.admin.business.vo.DailyBusinessStatisticsReqVO;
import com.yc.ship.module.trade.controller.admin.business.vo.DailyBusinessStatisticsRespVO;
import com.yc.ship.module.trade.service.business.DailyBusinessStatisticsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.io.IOUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import java.io.IOException;
import java.util.List;

import static com.yc.ship.framework.common.pojo.CommonResult.success;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 营业统计日报")
@RestController
@RequestMapping("/trade/business")
@Validated
public class DailyBusinessStatisticsController {
    @Resource
    private DailyBusinessStatisticsService dailyBusinessStatisticsService;

    @PostMapping("/dailyBusinessStatistics/page")
    @Operation(summary = "获得营业统计日报分页")
    public CommonResult<PageResult<DailyBusinessStatisticsRespVO>> getDailyBusinessStatisticsPage(@RequestBody DailyBusinessStatisticsReqVO pageReqVO) {
        PageResult<DailyBusinessStatisticsRespVO> pageResult = dailyBusinessStatisticsService.getDailyBusinessStatisticsPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/dailyBusinessStatistics/export-excel")
    @Operation(summary = "导出营业统计日报 Excel")
    @OperateLog(type = EXPORT,enable = false)
    public void exportDailyBusinessStatisticsList(@Valid DailyBusinessStatisticsReqVO pageReqVO, HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DailyBusinessStatisticsRespVO> list = dailyBusinessStatisticsService.getDailyBusinessStatisticsPage(pageReqVO).getList();
        // 计算合计
        DailyBusinessStatisticsRespVO total = new DailyBusinessStatisticsRespVO();
        total.setSourceName("合计");
        int totalAdultCount = 0, totalChildCount = 0, totalInfantCount = 0, totalWithCount = 0;
        int totalLeaderCount = 0, totalFreeCount = 0, totalFreeNum = 0, totalTotalCount = 0;
        int totalActualAmount = 0, totalValidCount = 0;

        for (DailyBusinessStatisticsRespVO item : list) {
            totalAdultCount += item.getAdultCount() == null ? 0 : item.getAdultCount();
            totalChildCount += item.getChildCount() == null ? 0 : item.getChildCount();
            totalInfantCount += item.getInfantCount() == null ? 0 : item.getInfantCount();
            totalWithCount += item.getWithCount() == null ? 0 : item.getWithCount();
            totalLeaderCount += item.getLeaderCount() == null ? 0 : item.getLeaderCount();
            totalFreeCount += item.getFreeCount() == null ? 0 : item.getFreeCount();
            totalFreeNum += item.getFreeNum() == null ? 0 : item.getFreeNum();
            totalTotalCount += item.getTotalCount() == null ? 0 : item.getTotalCount();
            totalActualAmount += item.getActualAmount() == null ? 0 : item.getActualAmount();
            totalValidCount += item.getValidCount() == null ? 0 : item.getValidCount();
        }

        total.setAdultCount(totalAdultCount);
        total.setChildCount(totalChildCount);
        total.setInfantCount(totalInfantCount);
        total.setWithCount(totalWithCount);
        total.setLeaderCount(totalLeaderCount);
        total.setFreeCount(totalFreeCount);
        total.setFreeNum(totalFreeNum);
        total.setTotalCount(totalTotalCount);
        total.setActualAmount(totalActualAmount);
        total.setValidCount(totalValidCount);

        list.add(total);

        // 导出 Excel
        ExcelUtils.write(response, "营业统计日报.xls", "数据", DailyBusinessStatisticsRespVO.class,
                BeanUtils.toBean(list, DailyBusinessStatisticsRespVO.class));
    }

}
