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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
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.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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;

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

@Tag(name = "小程序 - 驾照报名订单")
@RestController
@RequestMapping("/small/drive/school-course-order")
@Validated
public class SchoolCourseOrderSmallProgramController {

    @Resource
    private SchoolCourseOrderService schoolCourseOrderService;

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

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

    @GetMapping("/get")
    @TenantIgnore
    @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")
    @TenantIgnore
    @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.queryPageNew(page, pageReqVO));
    }

    /**
     * 获取用户已报名的课程
     *
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<java.util.List<cn.iocoder.yudao.module.course.vo.SchoolCourseOrderVO>>
     */
    @TenantIgnore
    @GetMapping("/getCourseByLoginUser")
    public CommonResult<?> getCourseByLoginUser() {
        //获取当前登陆人
        return success(schoolCourseOrderService.getCourseByLoginUser(SecurityFrameworkUtils.getLoginUserId()));
    }

    /**
     * 报名之后发送对应消息
     */
    @GetMapping("/sendMessage")
    @TenantIgnore
    public CommonResult<?> sendMessage(String studentName, String courseName,Long coachId) {
        return success(schoolCourseOrderService.sendOrderMessage(studentName, courseName, coachId));
    }
}
