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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.base.constant.SchoolBaseConstants;
import cn.iocoder.yudao.module.base.service.DlDriveSchoolCoachService;
import cn.iocoder.yudao.module.base.service.SchoolNotifyMessageSendService;
import cn.iocoder.yudao.module.base.vo.DlDriveSchoolCoachRespVO;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.train.entity.ReservationCourse;
import cn.iocoder.yudao.module.train.service.ReservationCourseService;
import cn.iocoder.yudao.module.train.vo.ReservationCourseVO;
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.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.module.base.utils.DriveSchoolBaseUtil.getSubjectStr;

@Tag(name = "小程序 - 预约练车")
@RestController
@RequestMapping("/reservation-course")
@Validated
public class AppReservationCourseController {

    @Resource
    private ReservationCourseService reservationCourseService;

    @Resource
    private SchoolNotifyMessageSendService schoolNotifyMessageSendService;


    @GetMapping("/page")
    @Operation(summary = "获得预约练车分页")
    @TenantIgnore
    public CommonResult<IPage<?>> getPage(ReservationCourseVO pageReqVO,
                                          @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<ReservationCourseVO> page = new Page<>(pageNo, pageSize);
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        pageReqVO.setUserId(userId);
        return success(reservationCourseService.queryListPage(pageReqVO, page));
    }

    @PutMapping("/create")
    @Operation(summary = "预约练车")
    @TenantIgnore
    @Transactional
    public CommonResult<Boolean> createReservationCourse(@RequestBody @Validated ReservationCourse createObj) {
        reservationCourseService.save(createObj);

        // 准备消息内容
        String message = String.format(SchoolBaseConstants.SCHOOL_NOTIFY_MESSAGE_TEMPLATE_MEMBER_RESERVE_TRAIN, createObj.getUserName(), createObj.getReservDay(), createObj.getReservTime(), createObj.getCourseName(), getSubjectStr(createObj.getSubject()));

        // 发送消息
        schoolNotifyMessageSendService.sendMessage(createObj.getCoachId(), message, SchoolBaseConstants.SCHOOL_NOTIFY_MESSAGE_TYPE_ADMIN, createObj.getTenantId());
        return success(true);
    }

    @PutMapping("/cancel")
    @Operation(summary = "取消预约练车")
    public CommonResult<Boolean> updateReservationCourse(String id) {
        ReservationCourse updateObj = new ReservationCourse();
        updateObj.setId(id);
        updateObj.setIfCancel(true);
        reservationCourseService.updateById(updateObj);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除预约练车")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteReservationCourse(@RequestParam("id") String id) {
        reservationCourseService.removeById(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得预约练车")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ReservationCourse> getReservationCourse(@RequestParam("id") String id) {
        return success(reservationCourseService.getById(id));
    }

}