package cn.iocoder.yudao.module.course.controller.admin;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.base.vo.DriveSchoolCourseDeductVO;
import cn.iocoder.yudao.module.course.entity.SchoolCommission;
import cn.iocoder.yudao.module.course.service.SchoolCommissionService;
import cn.iocoder.yudao.module.course.vo.CommissionExportVO;
import cn.iocoder.yudao.module.course.vo.CommissionStatisticsVO;
import cn.iocoder.yudao.module.course.vo.FinanceDeductInfoVO;
import cn.iocoder.yudao.module.course.vo.SchoolCommissionVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
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.Arrays;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 提成记录")
@RestController
@RequestMapping("/drive/school-commission")
@Validated
public class SchoolCommissionController {

    @Resource
    private SchoolCommissionService schoolCommissionService;

    /**
     * 分页查询教练提成记录
     *
     * @param pageReqVO {@link SchoolCommissionVO}
     * @param pageNo    分页参数
     * @param pageSize  分页参数
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<com.baomidou.mybatisplus.core.metadata.IPage < ?>>
     * @author PQZ
     * @date 13:59 2025/2/19
     **/
    @GetMapping("/page")
    @Operation(summary = "获得提成记录分页")
    public CommonResult<IPage<?>> getSchoolCommissionPage(SchoolCommissionVO pageReqVO,
                                                          @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SchoolCommissionVO> page = new Page<>(pageNo, pageSize);
        return success(schoolCommissionService.queryPage(page, pageReqVO));
    }

    /**
     * 创建提成记录
     * @param createReqVO
     * @return
     */
    @PostMapping("/create")
    @Operation(summary = "创建提成记录")
    public CommonResult<Boolean> createSchoolCommission(@Valid @RequestBody SchoolCommissionVO createReqVO) {
        return success(schoolCommissionService.createSchoolCommission(createReqVO));
    }


    /**
     * 获取提成记录
     * @param id
     * @return
     */
    @GetMapping("/get")
    @Operation(summary = "获得提成记录")
    public CommonResult<SchoolCommissionVO> getSchoolCommission(@RequestParam("id") String id) {
        return success(schoolCommissionService.getSchoolCommissionById(id));
    }

    /**
     * 出纳审核提成记录
     * @param schoolCommissionVO
     * @return
     */
    @PutMapping("/cashierConfirm")
    public CommonResult<?> cashierConfirm(@RequestBody SchoolCommissionVO schoolCommissionVO) {
        boolean b = schoolCommissionService.cashierConfirm(schoolCommissionVO);
        return success(b);
    }


    /**
     * 教练提成合计列表
     */
    @GetMapping("/getCoachTotalCommission")
    public CommonResult<List<SchoolCommissionVO>> getCoachTotalCommission(SchoolCommissionVO schoolCommissionVO){
        return success(schoolCommissionService.getCoachTotalCommission(schoolCommissionVO));
    }

    /**
     * 教练提成信息列表
     */
    @GetMapping("/getCommissionListByCoachId")
    public CommonResult<IPage<SchoolCommissionVO>> getCommissionListByCoachId(SchoolCommissionVO schoolCommissionVO,
                                                                             @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                             @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize){
        Page<SchoolCommissionVO> page = new Page<>(pageNo, pageSize);
        return success(schoolCommissionService.getCommissionListByCoachId(schoolCommissionVO, page));
    }

    /**
     * 前端获取数据getList()
     * @param commissionExportVO
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/allCommissionPage")
    public CommonResult<IPage<CommissionExportVO>> getAllCommissionPage(CommissionExportVO commissionExportVO,
                                                                        @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize){
        Page<CommissionExportVO> page = new Page<>(pageNo, pageSize);
        return success(schoolCommissionService.getAllCommissionPage(commissionExportVO, page));
    }

    /**
     * 数据统计部分
     * @param commissionExportVO
     * @return
     */
    @GetMapping("/getCommissionStatistics")
    public CommonResult<CommissionStatisticsVO> getCommissionStatistics(CommissionExportVO commissionExportVO){
        return success(schoolCommissionService.getCommissionStatistics(commissionExportVO));
    }


    /**
     * 首页财务应付部分
     * @param financeDeductInfoVO
     * @return
     */
    @GetMapping("/getFinanceDeductList")
    public CommonResult<List<FinanceDeductInfoVO>> getFinanceDeductList(FinanceDeductInfoVO financeDeductInfoVO){
        return success(schoolCommissionService.getFinanceDeductList(financeDeductInfoVO));
    }

    @GetMapping("/getFinanceDeductDetailsListByCoach")
    public CommonResult<IPage<FinanceDeductInfoVO>> getFinanceDeductDetailsListByCoach(FinanceDeductInfoVO financeDeductInfoVO,
                                                                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize){
        Page<FinanceDeductInfoVO> page = new Page<>(pageNo, pageSize);
        return success(schoolCommissionService.getFinanceDeductDetailsListByCoach(financeDeductInfoVO, page));
    }

    /**
     * 导出数据（支持三种模式）
     * @param exportVO 包含导出类型（exportType）、分页参数等
     */
    @GetMapping("/export")
    public void exportCommission(CommissionExportVO exportVO, HttpServletResponse response) throws IOException {
        // 校验导出类型
        if (exportVO.getExportType() == null || !Arrays.asList("current", "all", "range").contains(exportVO.getExportType())) {
            throw new IllegalArgumentException("无效的导出类型: " + exportVO.getExportType());
        }

        // 根据不同类型查询数据
        List<CommissionExportVO> exportData;
        String fileName = "提成记录";

        switch (exportVO.getExportType()) {
            case "current":
                exportData = schoolCommissionService.getCommissionList(exportVO);
                fileName += "_第" + exportVO.getPageNo() + "页";
                break;
            case "all":
                exportData = schoolCommissionService.getAllCommissionList(exportVO);
                fileName += "_全部数据";
                break;
            case "range":
                exportData = schoolCommissionService.getCommissionListByRange(exportVO);
                fileName += "_" + exportVO.getStartPage() + "-" + exportVO.getEndPage() + "页";
                break;
            default:
                throw new IllegalArgumentException("无效的导出类型: " + exportVO.getExportType());
        }

        // 导出Excel
        ExcelUtils.write(response, fileName + ".xlsx", "提成记录", CommissionExportVO.class, exportData);
    }

    /*@GetMapping("/export")
    public void exportCommission(CommissionExportVO exportVO, HttpServletResponse response) throws IOException {
        // 1. 根据不同类型查询数据
        List<CommissionExportVO> exportData;
        String fileName = "提成记录";

        switch (exportVO.getExportType()) {
            case "current": // 导出当前页
                exportData = schoolCommissionService.getCommissionList(exportVO);
                fileName += "_第" + exportVO.getPageNo() + "页";
                break;
            case "all": // 导出全部数据
                exportData = schoolCommissionService.getAllCommissionList(exportVO);
                fileName += "_全部数据";
                break;
            case "range": // 导出自定义范围
                exportData = schoolCommissionService.getCommissionListByRange(exportVO);
                fileName += "_" + exportVO.getStartPage() + "-" + exportVO.getEndPage() + "页";
                break;
            default:
                throw new IllegalArgumentException("无效的导出类型: " + exportVO.getExportType());
        }
        for (int i = 0; i < exportData.size(); i++) {
            exportData.get(i).setSerialNumber(i + 1);
        }
        ExcelUtils.write(response, fileName + ".xlsx", "提成记录", CommissionExportVO.class, exportData);
    }*/
}
