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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.jx.core.controller.BaseController;
import cn.iocoder.yudao.module.jx.domain.DriveSchoolCoach;
import cn.iocoder.yudao.module.jx.domain.DriveSchoolReservationCourse;
import cn.iocoder.yudao.module.jx.domain.DriveSchoolTeachingJournal;
import cn.iocoder.yudao.module.jx.mapper.DriveSchoolCoachMapper;
import cn.iocoder.yudao.module.jx.service.IDriveSchoolInfoService;
import cn.iocoder.yudao.module.jx.service.IDriveSchoolReservationCourseService;
import cn.iocoder.yudao.module.jx.utils.ExcelUtil;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 预约练车Controller
 *
 * @author ruoyi
 * @date 2024-04-08
 */
@RestController
@RequestMapping("/drivingSchool/system/reservationCourse")
public class DriveSchoolReservationCourseController extends BaseController
{
    @Resource
    private IDriveSchoolReservationCourseService driveSchoolReservationCourseService;
    @Autowired
    private IDriveSchoolInfoService driveSchoolInfoService;

    @Resource
    private AdminUserApi userApi;

    @PostMapping("/updateCourseById")
    public void updateCourseById(@RequestBody DriveSchoolReservationCourse driveSchoolReservationCourse) {
        driveSchoolReservationCourseService.updateCourseById(driveSchoolReservationCourse);
    }


    /**
     * 查询预约练车列表
     */
    @GetMapping("/list")
    public CommonResult<IPage<?>> list(DriveSchoolReservationCourse driveSchoolReservationCourse)
    {
        Page<DriveSchoolReservationCourse> page = new Page<>(driveSchoolReservationCourse.getPageNum(), driveSchoolReservationCourse.getPageSize());
        IPage<DriveSchoolReservationCourse> driveSchoolReservationCourseIPage = driveSchoolReservationCourseService.selectDriveSchoolReservationCourseList(driveSchoolReservationCourse, page);
        return CommonResult.success(driveSchoolReservationCourseIPage);
        //return getDataTable(list);
    }

    /**
     * 查询预约练车列表pc
     */
    @GetMapping("/pclist")
    public CommonResult<IPage<?>> pclist(DriveSchoolReservationCourse driveSchoolReservationCourse)
    {
        Page<DriveSchoolReservationCourse> page = new Page<>(driveSchoolReservationCourse.getPageNum(), driveSchoolReservationCourse.getPageSize());
        IPage<DriveSchoolReservationCourse> driveSchoolReservationCourseIPage = driveSchoolReservationCourseService.selectDriveSchoolReservationCoursePclist(driveSchoolReservationCourse, page);
        return CommonResult.success(driveSchoolReservationCourseIPage);
        //return getDataTable(list);
    }

    @GetMapping("/listUser")
    public CommonResult listUser(DriveSchoolReservationCourse driveSchoolReservationCourse)
    {
        Page<DriveSchoolReservationCourse> page = new Page<>(driveSchoolReservationCourse.getPageNum(), 9999);
        driveSchoolReservationCourse.setUserId(SecurityFrameworkUtils.getLoginUserId());
        IPage<DriveSchoolReservationCourse> driveSchoolReservationCourseIPage = driveSchoolReservationCourseService.selectDriveSchoolReservationCourseList(driveSchoolReservationCourse, page);
        List<DriveSchoolReservationCourse> records = driveSchoolReservationCourseIPage.getRecords();
        return CommonResult.success(records);
    }

    @GetMapping("/getListUser")
    public CommonResult getListUser(DriveSchoolReservationCourse driveSchoolReservationCourse)
    {
        List<DriveSchoolReservationCourse> list = driveSchoolReservationCourseService.selectDriveSchoolReservationGetListUser(driveSchoolReservationCourse);

        return CommonResult.success(list);
    }

    /**
     * 导出预约练车列表
     */
    //@Log(title = "预约练车", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, DriveSchoolReservationCourse driveSchoolReservationCourse)
//    {
//        List<DriveSchoolReservationCourse> list = driveSchoolReservationCourseService.selectDriveSchoolReservationCourseList(driveSchoolReservationCourse);
//        ExcelUtil<DriveSchoolReservationCourse> util = new ExcelUtil<DriveSchoolReservationCourse>(DriveSchoolReservationCourse.class);
//        util.exportExcel(response, list, "预约练车数据");
//    }

    /**
     * 获取预约练车详细信息
     */
    @GetMapping(value = "/{id}")
    public CommonResult getInfo(@PathVariable("id") Long id)
    {
        return success(driveSchoolReservationCourseService.selectDriveSchoolReservationCourseById(id));
    }

    /**
     * 新增预约练车
     */
    //@Log(title = "预约练车", businessType = BusinessType.INSERT)
    @PostMapping
    public CommonResult add(@RequestBody DriveSchoolReservationCourse driveSchoolReservationCourse) throws Exception {
        return CommonResult.success(driveSchoolReservationCourseService.insertDriveSchoolReservationCourse(driveSchoolReservationCourse));
    }

    /**
     * 修改预约练车
     */
    //@Log(title = "预约练车", businessType = BusinessType.UPDATE)
    @PutMapping
    public CommonResult edit(@RequestBody DriveSchoolReservationCourse driveSchoolReservationCourse)
    {
        return toAjax(driveSchoolReservationCourseService.updateDriveSchoolReservationCourse(driveSchoolReservationCourse));
    }

    /**
     * 教练修改预约练车
     */
    //@Log(title = "教练修改预约练车", businessType = BusinessType.UPDATE)
    @PutMapping("editByCoach")
    public CommonResult editByCoach(@RequestBody DriveSchoolReservationCourse driveSchoolReservationCourse)
    {
        return toAjax(driveSchoolReservationCourseService.updateDriveSchoolReservationCourseByCoach(driveSchoolReservationCourse));
    }


    /**
     * app修改预约练车
     */
    /*//@Log(title = "预约练车", businessType = BusinessType.UPDATE)
    @PutMapping()
    public CommonResult edit(@RequestBody DriveSchoolReservationCourse driveSchoolReservationCourse)
    {
        return toAjax(driveSchoolReservationCourseService.updateDriveSchoolReservationCourse(driveSchoolReservationCourse));
    }*/

    /**
     * 删除预约练车
     */
    //@PreAuthorize("@ss.hasPermi('drivingSchool/system:reservationCourse:remove')")
    //@Log(title = "预约练车", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public CommonResult remove(@PathVariable Long[] ids)
    {
        return toAjax(driveSchoolReservationCourseService.deleteDriveSchoolReservationCourseByIds(ids));
    }

    /**
     * 获取首页统计数据
     */
    @GetMapping(value = "/applet/getHomeData")
    public CommonResult getHomeData()
    {
        return success(driveSchoolReservationCourseService.getHomeData());
    }

    /**
     * app获取首页统计数据
     */
    @GetMapping(value = "/applet/getIndexData")
    public CommonResult getIndexData()
    {
        return success(driveSchoolReservationCourseService.getIndexData());
    }

    /**
     * 获取首页预约记录
     */
    @GetMapping(value = "/applet/getReservationCourseList")
    public CommonResult getReservationCourseList(@RequestParam(required = false,defaultValue = "1") Integer pageNum,@RequestParam(required = false,defaultValue = "10") Integer pageSize)
    {   Page page =new Page(pageNum,pageSize);
        return success(driveSchoolReservationCourseService.getReservationCourseList(page));
    }

    /**
     * 小程序获取首页预约记录
     */
    @GetMapping(value = "/applet/getReservationCourseListByUserId")
    public CommonResult getReservationCourseListByUserId()
    {
        return success(driveSchoolReservationCourseService.getReservationCourseListByUserId());
    }

    /**
     * app获取首页预约记录
     */
    @GetMapping(value = "/applet/getAppReservationCourseListByUserId")
    public CommonResult getAppReservationCourseListByUserId(DriveSchoolReservationCourse driveSchoolReservationCourse)
    {
        return success(driveSchoolReservationCourseService.getAppReservationCourseListByUserId(driveSchoolReservationCourse));
    } 

    /**
     * app获取首页预约记录上
     */
    @GetMapping(value = "/applet/getAppReservationCourseListByUserIds")
    public CommonResult getAppReservationCourseListByUserIds(DriveSchoolReservationCourse driveSchoolReservationCourse)
    {
        return success(driveSchoolReservationCourseService.getAppReservationCourseListByUserIds(driveSchoolReservationCourse));
    }

    /**
     * 统计学员的练车时间
     */
    @GetMapping(value = "/applet/getDriveTime")
    public CommonResult getDriveTime()
    {
        return success(driveSchoolReservationCourseService.getDriveTime());
    }

//    @Resource
//    private SysUserMapper sysUserMapper;

    @Resource
    private DriveSchoolCoachMapper driveSchoolCoachMapper;

    /**
     * 近期长时间来练车的学员
     */
    @GetMapping(value = "/applet/getOftenDriveStudents")
    public CommonResult getOftenDriveStudents()
    {

        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserRespDTO sysUser = userApi.getUser(userId);

//        Long userId = SecurityFrameworkUtils.getLoginUserId();
//        SysUser sysUser = sysUserMapper.selectUserById(userId);
        String phonenumber = sysUser.getMobile();
        DriveSchoolCoach driveSchoolCoach = driveSchoolCoachMapper.selectByPhonenumber(phonenumber);
        Long jlId = 0L;
        if (ObjectUtils.isNotEmpty(driveSchoolCoach)){
            jlId = driveSchoolCoach.getId();
        }
        return success(driveSchoolReservationCourseService.getOftenDriveStudents(jlId));
    }
}
