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

import cn.hutool.core.bean.BeanUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.course.entity.SchoolCourseOrder;
import cn.iocoder.yudao.module.course.service.SchoolCourseOrderService;
import cn.iocoder.yudao.module.course.vo.SchoolCommissionVO;
import cn.iocoder.yudao.module.course.vo.SchoolCourseOrderExportVO;
import cn.iocoder.yudao.module.course.vo.SchoolCourseOrderVO;
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.security.access.prepost.PreAuthorize;
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.Date;
import java.util.List;

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

@Tag(name = "管理后台 - 驾照报名订单")
@RestController
@RequestMapping("/drive/school-course-order")
@Validated
public class SchoolCourseOrderController {

    @Resource
    private SchoolCourseOrderService schoolCourseOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建驾照报名订单")
    public CommonResult<String> createSchoolCourseOrder(@Valid @RequestBody SchoolCourseOrderVO createReqVO) {
        return success(schoolCourseOrderService.createSchoolCourseOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新驾照报名订单")
    public CommonResult<Boolean> updateSchoolCourseOrder(@Valid @RequestBody SchoolCourseOrderVO updateReqVO) {
        schoolCourseOrderService.updateSchoolCourseOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除驾照报名订单")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteSchoolCourseOrder(@RequestParam("id") String id) {
        schoolCourseOrderService.deleteSchoolCourseOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得驾照报名订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<SchoolCourseOrderVO> getSchoolCourseOrder(@RequestParam("id") String id) {
        SchoolCourseOrder schoolCourseOrder = schoolCourseOrderService.getSchoolCourseOrder(id);
        return success(BeanUtils.toBean(schoolCourseOrder, SchoolCourseOrderVO.class));
    }


    /**
     * 获得驾照报名订单分页
     *
     * @param pageReqVO {@link SchoolCourseOrderVO}
     * @param pageNo    分页参数
     * @param pageSize  分页参数
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<com.baomidou.mybatisplus.core.metadata.IPage < ?>>
     * @author PQZ
     * @date 10:08 2025/2/25
     **/
    @GetMapping("/page")
    @Operation(summary = "获得驾照报名订单分页")
    public CommonResult<IPage<?>> getSchoolCourseOrderPage(SchoolCourseOrderVO pageReqVO,
                                                           @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SchoolCommissionVO> page = new Page<>(pageNo, pageSize);
        return success(schoolCourseOrderService.queryPage(page, pageReqVO));
    }

    /**
     * 终止订单
     *
     * @param pageReqVO {@link SchoolCourseOrderVO}
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<java.lang.Boolean>
     */
    @PutMapping("/endOrder")
    public CommonResult<?> endOrder(@RequestBody SchoolCourseOrderVO pageReqVO) {
        // 校验订单号和结束时间
        if (pageReqVO.getOrderNo() == null || pageReqVO.getEndReason() == null || pageReqVO.getEndTime() == null) {
            throw new RuntimeException("订单号和结束时间不能为空");
        }
        schoolCourseOrderService.endOrder(pageReqVO.getOrderNo(), pageReqVO.getEndReason(), pageReqVO.getEndTime());
        return success(true);
    }

    /**
     * 导出
     */
    @GetMapping("/export-excel")
    public void export(HttpServletResponse response, SchoolCourseOrderVO pageReqVO) throws IOException {
        Page<SchoolCommissionVO> page = new Page<>(1, 100000);
        IPage<SchoolCourseOrderVO> schoolCourseOrderVOIPage = schoolCourseOrderService.queryPage(page, pageReqVO);
        List<SchoolCourseOrderExportVO> schoolCourseOrderExportVOS = BeanUtil.copyToList(schoolCourseOrderVOIPage.getRecords(), SchoolCourseOrderExportVO.class);
        ExcelUtils.write(response, "订单信息.xls", "订单信息", SchoolCourseOrderExportVO.class, schoolCourseOrderExportVOS);
    }

    /**
     * 快速创建订单
     *
     * @param createReqVO 创建信息
     * @return 订单号
     */
    @PostMapping("/fastCreate")
    @Operation(summary = "快速创建驾照报名订单")
    public CommonResult<String> fastCreateSchoolCourseOrder(@Valid @RequestBody SchoolCourseOrderVO createReqVO) {
        return success(schoolCourseOrderService.fastCreateSchoolCourseOrder(createReqVO));
    }

    /**
     * 教练修改学员毕业时间
     */
    @GetMapping("/updateStudentGradTime")
    public CommonResult<?> updateStudentGraduateTime(String studentId, String gradTime) {
        schoolCourseOrderService.updateStudentGradTime(studentId, gradTime);
        return success(true);
    }

    /**
     * 教练修改学员拿证时间
     */
    @GetMapping("/updateStudentPassTime")
    public CommonResult<?> updateStudentGetLicenseTime(String studentId, String passTime) {
        schoolCourseOrderService.updateStudentPassTime(studentId, passTime);
        return success(true);
    }
}
