package com.loan.system.controller.wechat;

import com.loan.system.domain.pojo.Result;
import com.loan.system.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/wechat/statistics")
@Api("数据统计接口")
public class StatisticsController {

    @GetMapping("/dailyReport")
    @ApiOperation("获取日报表")
    public Result dailyReport(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                              @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {

        //TODO:统计echart数据
//        Map<String, Object> stats = new HashMap<>();
//
//        // 示例数据 - 需要替换为真实数据库查询
//        stats.put("roles", Map.of(
//                "student", userService.countByRole("student"),
//                "school_admin", userService.countByRole("school_admin"),
//                "admin", userService.countByRole("admin")
//        ));
//
//        stats.put("status", Map.of(
//                "active", userService.countByStatus("active"),
//                "inactive", userService.countByStatus("inactive")
//        ));

        return ResultUtil.success();
    }

    @GetMapping("/financeEfficiency")
    @ApiOperation("获取资金效率表")
    public Result financeEfficiency(@DateTimeFormat(pattern = "yyyy-MM") LocalDate begin,
                                    @DateTimeFormat(pattern = "yyyy-MM") LocalDate end) {
        return ResultUtil.success();
    }

    @GetMapping("/financeBalance")
    @ApiOperation("获取资金余额")
    public Result financeBalance(@DateTimeFormat(pattern = "yyyy-MM") LocalDate begin,
                                 @DateTimeFormat(pattern = "yyyy-MM") LocalDate end) {
        return ResultUtil.success();
    }

    @GetMapping("/schedule")
    @ApiOperation("获取业务安排表")
    public Result schedule(@RequestParam Long caseId) {
        return ResultUtil.success();
    }

    //到处数据
    public void exportBusinessData(HttpServletResponse response) {
        LocalDate begin = LocalDate.now().minusDays(30);
        LocalDate end = LocalDate.now().minusDays(1);

        //查询概览运营数据，提供给Excel模板文件
        //BusinessDataVO businessData = workspaceService.getBusinessData(LocalDateTime.of(begin, LocalTime.MIN), LocalDateTime.of(end, LocalTime.MAX));

        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("template/运营数据报表模板.xlsx");
        try {
            //基于提供好的模板文件创建一个新的Excel表格对象
            XSSFWorkbook excel = new XSSFWorkbook(inputStream);

            //获得Excel文件中的一个Sheet页
            XSSFSheet sheet = excel.getSheet("Sheet1");
            sheet.getRow(1).getCell(1).setCellValue(begin + "至" + end);

            //获得第4行
            XSSFRow row = sheet.getRow(3);
            //获取单元格
//            row.getCell(2).setCellValue(businessData.getTurnover());
//            row.getCell(4).setCellValue(businessData.getOrderCompletionRate());
//            row.getCell(6).setCellValue(businessData.getNewUsers());
//
//            row = sheet.getRow(4);
//            row.getCell(2).setCellValue(businessData.getValidOrderCount());
//            row.getCell(4).setCellValue(businessData.getUnitPrice());
//
//            for (int i = 0; i < 30; i++) {
//                LocalDate date = begin.plusDays(i);
//                //准备明细数据
//                businessData = workspaceService.getBusinessData(LocalDateTime.of(date, LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX));
//                row = sheet.getRow(7 + i);
//                row.getCell(1).setCellValue(date.toString());
//                row.getCell(2).setCellValue(businessData.getTurnover());
//                row.getCell(3).setCellValue(businessData.getValidOrderCount());
//                row.getCell(4).setCellValue(businessData.getOrderCompletionRate());
//                row.getCell(5).setCellValue(businessData.getUnitPrice());
//                row.getCell(6).setCellValue(businessData.getNewUsers());
//            }
            //通过输出流将文件下载到客户端浏览器中
            ServletOutputStream out = response.getOutputStream();
            excel.write(out);

            //关闭资源
            out.flush();
            out.close();
            excel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
