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


import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.train.entity.DriveSchoolCoachClock;
import cn.iocoder.yudao.module.train.service.IDriveSchoolCoachClockService;
import cn.iocoder.yudao.module.train.vo.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

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

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

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

/**
 * 教练打卡表
 *
 * @author author
 * @since 2025-07-16
 */
@RestController
@RequestMapping("/drive-school-coach-clock")
public class DriveSchoolCoachClockController {

    @Resource
    private IDriveSchoolCoachClockService coachClockService;

    @GetMapping("/page")
    @Operation(summary = "获得教练打卡记录分页")
    public CommonResult<IPage<?>> getPage(DriveSchoolCoachClockVO pageReqVO,
                                          @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<DriveSchoolCoachClockVO> page = new Page<>(pageNo, pageSize);
        return success(coachClockService.queryListPage(pageReqVO, page));
    }

    /**
     * 查询今天未离场的打卡记录
     */
    @GetMapping("selectTodayClockRecord")
    public CommonResult<DriveSchoolCoachClock> selectTodayClockRecord(String carNo) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        DriveSchoolCoachClock driveSchoolCoachClock = coachClockService.selectTodayClockRecord(userId,carNo);
        return success(driveSchoolCoachClock);
    }


    @PostMapping("/create")
    @Operation(summary = "教练到场打卡")
    public CommonResult<String> createObj(@RequestBody DriveSchoolCoachClockVO coachClockVO) {
        return success(coachClockService.createObj(coachClockVO));
    }

    @PutMapping("/update")
    @Operation(summary = "教练离场打卡")
    public CommonResult<?> updateObj(@RequestBody DriveSchoolCoachClockVO coachClockVO) {
        coachClockService.updateObj(coachClockVO);
        return success(true);
    }

    /**
     * 根据id获取打卡详情
     */
    @GetMapping("/getDetailById")
    public CommonResult<DriveSchoolCoachClock> getDetailById(String id) {
        DriveSchoolCoachClock driveSchoolCoachClock = coachClockService.getById(id);
        return success(driveSchoolCoachClock);
    }

    /**
     * 统计训练车辆台次
     */
    @GetMapping("/countTrainCar")
    public CommonResult<TrainCarCountVO> countTrainCar(TrainCarCountVO trainCarCountVO) {
        return success(coachClockService.countTrainCar(trainCarCountVO));
    }

    /**
     * 车辆打卡数据列表
     *
     * @param trainCarCountVO
     * @return
     */
    @GetMapping("/trainClockByCar")
    public CommonResult<List<TrainCarCountVO>> trainClockByCar(TrainCarCountVO trainCarCountVO) {
        return success(coachClockService.trainClockByCar(trainCarCountVO));
    }

    /**
     * 车辆打卡训练详细信息，包含打卡基本信息和学员信息
     */
    @GetMapping("/trainClockByCarDetail")
    public CommonResult<List<TrainDetailsByCarVO>> trainClockByCarDetail(TrainDetailsByCarVO trainDetailsByCarVO) {
        return success(coachClockService.trainClockByCarDetail(trainDetailsByCarVO));
    }

    /**
     * 每天晚上7点提醒未进行离场打卡的教练进行打卡
     */
    @TenantIgnore
    @Scheduled(cron = "0 0 19 * * ?")
    public void noClockInRemind() {
        coachClockService.noClockInRemind();
    }

    /**
     * PC端打卡统计数据获取
     */
    @GetMapping("getCloakAndTrainCount")
    public CommonResult<CloakAndTrainCountVO> getCloakAndTrainCount(CloakAndTrainCountVO cloakAndTrainCountVO) {
        return success(coachClockService.getCloakAndTrainCount(cloakAndTrainCountVO));
    }


    /**
     * 导出数据获取
     */
    @GetMapping("coachClockAll")
    public CommonResult<IPage<CoachClockExportVO>> coachClockAll(CoachClockExportVO coachClockExportVO,
                                                                 @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<CoachClockExportVO> page = new Page<>(pageNo, pageSize);
        return success(coachClockService.coachClockAll(coachClockExportVO, page));
    }


    /**
     * 导出
     *
     * @param exportVO
     * @param response
     * @throws IOException
     */
    @GetMapping("/coachClockRecordExport")
    public void exportCommission(CoachClockExportVO exportVO, HttpServletResponse response) throws IOException {
        // 校验导出类型
        if (exportVO.getExportType() == null || !Arrays.asList("current", "all", "range").contains(exportVO.getExportType())) {
            throw new IllegalArgumentException("无效的导出类型: " + exportVO.getExportType());
        }

        // 根据不同类型查询数据
        List<CoachClockExportVO> exportData;
        String fileName = "教练打卡记录";

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


        // 导出Excel
        ExcelUtils.write(response, fileName + ".xlsx", "教练打卡记录", CoachClockExportVO.class, exportData);
    }

    /**
     * 获取当日实时训练情况统计
     */
    @GetMapping("/getTodayTrainSituation")
    public CommonResult<CloakAndTrainCountVO> getTodayTrainSituation() {
        return success(coachClockService.getTodayTrainSituation());
    }

    /**
     * 获取正在训练的车辆信息列表
     */
    @GetMapping("/getTodayCarTrainSituation")
    public CommonResult<List<DriveSchoolCoachClockVO>> getTodayCarTrainSituation(DriveSchoolCoachClock driveSchoolCoachClock) {
        return success(coachClockService.getTodayCarTrainSituation(driveSchoolCoachClock));
    }

    /**
     * 根据教练id查询当日打卡记录的车牌号列表
     */
    @GetMapping("/getClockCarNoByUserId")
    public CommonResult<List<String>> getClockCarNoByUserId() {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        return success(coachClockService.getClockCarNoByUserId(userId));
    }

    /**
     * 根据教练id查询当日打卡记录的车牌号列表
     */
    @GetMapping("/getClockCarInfoByUserId")
    public CommonResult<DriveSchoolCoachClock> getClockCarInfoByUserId(String carNo) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        return success(coachClockService.getClockCarInfoByUserId(userId, carNo));
    }

    @GetMapping("/errorPage")
    @Operation(summary = "获得教练异常打卡记录分页")
    public CommonResult<IPage<?>> queryErrorClockListPage(DriveSchoolCoachClockVO pageReqVO,
                                          @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<DriveSchoolCoachClockVO> page = new Page<>(pageNo, pageSize);
        return success(coachClockService.queryErrorClockListPage(pageReqVO, page));
    }

}
