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

import cn.hutool.core.date.DateUtil;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.base.entity.DlDriveSchoolStudent;
import cn.iocoder.yudao.module.base.service.DlDriveSchoolStudentService;
import cn.iocoder.yudao.module.base.vo.DlDriveSchoolStudentVO;
import cn.iocoder.yudao.module.base.vo.DriveSchoolStudentExportVo;
import cn.iocoder.yudao.module.inspection.query.InspectionStaffQuery;
import cn.iocoder.yudao.module.inspection.vo.InspectionStaffExportVo;
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.apache.commons.lang3.StringUtils;
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 cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 驾校学员")
@RestController
@RequestMapping("/base/dl-drive-school-student")
@Validated
public class DlDriveSchoolStudentController {

    @Resource
    private DlDriveSchoolStudentService schoolStudentService;

    @PostMapping("/create")
    @Operation(summary = "创建驾校学员")
    public CommonResult<String> createDlDriveSchoolStudent(@Valid @RequestBody DlDriveSchoolStudentVO createReqVO) {
        return success(schoolStudentService.createDlDriveSchoolStudent(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新驾校学员")
    public CommonResult<Boolean> updateDlDriveSchoolStudent(@Valid @RequestBody DlDriveSchoolStudentVO updateReqVO) {
        schoolStudentService.updateDlDriveSchoolStudent(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除驾校学员")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteDlDriveSchoolStudent(@RequestParam("id") String id) {
        schoolStudentService.deleteDlDriveSchoolStudent(id);
        return success(true);
    }

    /**
     * 获取学员详细信息
     *
     * @param id 学员id
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<cn.iocoder.yudao.module.base.vo.DlDriveSchoolStudentVO>
     * @author PQZ
     * @date 9:58 2025/2/10
     **/
    @GetMapping("/get")
    @Operation(summary = "获得驾校学员")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<DlDriveSchoolStudentVO> getDlDriveSchoolStudent(@RequestParam("id") String id) {
        return success(schoolStudentService.queryStudentById(id));
    }

    @GetMapping("/getByUserId")
    @Operation(summary = "获得驾校学员")
    public CommonResult<DlDriveSchoolStudent> getDlDriveSchoolStudent(@RequestParam("id") Long id) {
        return success(schoolStudentService.getStudentByUserId(id));
    }

    /**
     * 分页获取学员列表
     *
     * @param pageReqVO {@link DlDriveSchoolStudentVO}
     * @param pageNo    分页参数
     * @param pageSize  分页参数
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<com.baomidou.mybatisplus.core.metadata.IPage < ?>>
     * @author PQZ
     * @date 10:46 2025/1/18
     **/
    @GetMapping("/page")
    @Operation(summary = "获得驾校学员分页")
    public CommonResult<IPage<?>> getDlDriveSchoolStudentPage(@Valid DlDriveSchoolStudentVO pageReqVO,
                                                              @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<DlDriveSchoolStudentVO> page = new Page<>(pageNo, pageSize);
        return success(schoolStudentService.queryListPage(pageReqVO, page));
    }

    /**
     * 获取可以考试的学员列表
     *
     * @param courseId 课程ID
     * @param subject  科目
     * @return List<DlDriveSchoolStudent>
     * @author vinjor-M
     * @date 23:16 2025/1/20
     **/
    @GetMapping("/getCanExamStudentList")
    @Operation(summary = "获取可以考试的学员列表")
    public CommonResult<?> getCanExamStudentList(String courseId, Integer subject, String userName) {
        return success(schoolStudentService.getCanExamStudentList(courseId, subject, userName));
    }

    /**
     * 教练查自己所有学生列表
     *
     * @author vinjor-M
     * @date 17:20 2025/2/12
     **/
    @GetMapping("/myStudentCoachPage")
    @Operation(summary = "教练查自己所有学生列表")
    public CommonResult<IPage<?>> myStudentCoachPage(@Valid DlDriveSchoolStudentVO pageReqVO,
                                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<DlDriveSchoolStudentVO> page = new Page<>(pageNo, pageSize);
        return success(schoolStudentService.queryCoachListPage(pageReqVO, page));
    }

    /**
     * 业务经理查自己招生列表
     *
     * @author vinjor-M
     * @date 17:20 2025/2/12
     **/
    @GetMapping("/myStudentBusinessPage")
    @Operation(summary = "业务经理查自己招生列表")
    public CommonResult<IPage<?>> myStudentBusinessPage(@Valid DlDriveSchoolStudentVO pageReqVO,
                                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<DlDriveSchoolStudentVO> page = new Page<>(pageNo, pageSize);
        return success(schoolStudentService.queryBusinessListPage(pageReqVO, page));
    }

    /**
     * @param type      时间查询类型（01驾校统招，02教练自招，03自来客户）
     * @param timeType  时间查询类型（all-全部|day-当日|month-当月|more-自定义）
     * @param coachId   教练id
     * @param startTime 查询时间范围--开始
     * @param endTime   查询时间范围--结束
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<java.util.List < ?>>
     * @author PQZ
     * @date 16:30 2025/2/19
     **/
    @GetMapping("/indexStuentList")
    @Operation(summary = "首页数据统计查询接口")
    public CommonResult<List<?>> indexStuentList(@RequestParam(value = "type") String type,
                                                   @RequestParam(value = "timeType") String timeType,
                                                   @RequestParam(value = "coachId", required = false) Long coachId,
                                                   @RequestParam(value = "startTime", required = false) String startTime,
                                                   @RequestParam(value = "endTime", required = false) String endTime) {
        //默认查全部数据
        String startTimeStr = "";
        String endTimeStr = "";
        if("more".equals(timeType)){
            if(StringUtils.isNotEmpty(startTime)){
                startTimeStr = startTime+" 00:00:01";
            }
            if(StringUtils.isNotEmpty(endTime)) {
                endTimeStr = endTime + " 23:59:59";
            }
        }else if("month".equals(timeType)){
            //当月
            startTimeStr = DateUtil.format(DateUtil.beginOfMonth(DateUtil.date()),"yyyy-MM-dd")+" 00:00:01";
            endTimeStr = DateUtil.format(DateUtil.endOfMonth(DateUtil.date()),"yyyy-MM-dd")+" 23:59:59";
        }else if("day".equals(timeType)){
            //当天
            startTimeStr = DateUtil.formatDate(DateUtil.date())+" 00:00:01";
            endTimeStr = DateUtil.formatDate(DateUtil.date())+" 23:59:59";
        }
        return success(schoolStudentService.indexGetTrainList(type,coachId,startTimeStr,endTimeStr));
    }

    /**
     * 查询自来学生列表
     * @author PQZ
     * @date 11:41 2025/2/20
     * @param timeType  时间查询类型（all-全部|day-当日|month-当月|more-自定义）
     * @param startTime 查询时间范围--开始
     * @param endTime   查询时间范围--结束
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<java.util.List<?>>
    **/
    @GetMapping("/indexGetFormList")
    @Operation(summary = "首页数据统计查询接口")
    public CommonResult<List<?>> indexGetFormList(@RequestParam(value = "timeType") String timeType,
                                                  @RequestParam(value = "startTime", required = false) String startTime,
                                                  @RequestParam(value = "endTime", required = false) String endTime){
        //默认查全部数据
        String startTimeStr = "";
        String endTimeStr = "";
        if("more".equals(timeType)){
            if(StringUtils.isNotEmpty(startTime)){
                startTimeStr = startTime+" 00:00:01";
            }
            if(StringUtils.isNotEmpty(endTime)) {
                endTimeStr = endTime + " 23:59:59";
            }
        }else if("month".equals(timeType)){
            //当月
            startTimeStr = DateUtil.format(DateUtil.beginOfMonth(DateUtil.date()),"yyyy-MM-dd")+" 00:00:01";
            endTimeStr = DateUtil.format(DateUtil.endOfMonth(DateUtil.date()),"yyyy-MM-dd")+" 23:59:59";
        }else if("day".equals(timeType)){
            //当天
            startTimeStr = DateUtil.formatDate(DateUtil.date())+" 00:00:01";
            endTimeStr = DateUtil.formatDate(DateUtil.date())+" 23:59:59";
        }
        return success(schoolStudentService.indexGetFormList(startTimeStr,endTimeStr));
    }

    /**
     * 获取学员详细信息
     *
     * @param uniqueCode 学员id
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<cn.iocoder.yudao.module.base.vo.DlDriveSchoolStudentVO>
     * @author PQZ
     * @date 9:58 2025/2/10
     **/
    @GetMapping("/getByUniqueCode")
    @Operation(summary = "获得驾校学员")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<DlDriveSchoolStudentVO> getDlDriveSchoolStudentByUniqueCode(@RequestParam("uniqueCode") String uniqueCode) {
        return success(schoolStudentService.queryStudentByUniqueCode(uniqueCode));
    }


    /**
     * 新增文件夹
     * @param userId 用户id
     * @return
     */
    @PostMapping("/addFolder")
    public CommonResult<?> addFolder(@RequestBody Long userId) {
        return success(schoolStudentService.addFolder(userId));
    }


    @GetMapping("/export")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserList(DlDriveSchoolStudent query,
                               HttpServletResponse response) throws IOException {
        List<DriveSchoolStudentExportVo> list = schoolStudentService.getAll(query);
        // 输出 Excel
        ExcelUtils.write(response, "学员数据.xls", "数据", DriveSchoolStudentExportVo.class,
                list);
    }

}
