package com.ruoyi.web.controller.basic;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.RestResponse;
import com.ruoyi.basic.service.YjAppUserService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.entity.AppUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.course.domain.req.ReqSearchClaTime;
import com.ruoyi.course.domain.time.RespBusinessClaTimeCalendar;
import com.ruoyi.course.service.ScClaTimeService;
import com.ruoyi.im.service.FileService;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;


@RestController
@RequestMapping("/api/my")
public class MyController extends BaseController {


    @Autowired
    private ScClaTimeService scClaTimeService;

    @Autowired
    private YjAppUserService appUserService;

    @Autowired
    private  FileService fileService;

    @GetMapping("/myInformation")
    public RestResponse myInformation(){
        AppUser user= appUserService.getById(SecurityUtils.getAppLoginUser().getAppUserId());
        return new RestResponse().setSuccess(true).setData(user);
    }

    //上传头像
    @PostMapping("/avatarUpload")
    public RestResponse avatarUpload(@RequestParam("file") MultipartFile file,
                                            @RequestParam(defaultValue = "true") Boolean isPermanent){
        String url= fileService.uploadImage(file,"app_avatar",isPermanent).getOriginUrl();
        return new RestResponse().setSuccess(true).setData(url);
    }
    @PostMapping("/modifyMyInformation")
    public RestResponse modifyMyInformation(@RequestBody AppUser user){
        AppUser dbUser= appUserService.getById(SecurityUtils.getAppLoginUser().getAppUserId());
        AppUser modifyUser=new AppUser();
            modifyUser.setId(dbUser.getId());
            if (StrUtil.isNotEmpty(user.getPassword())
                    &&SecurityUtils.matchesPassword(user.getPassword(),dbUser.getPassword())){
                modifyUser.setPassword(SecurityUtils.encryptPassword(user.getNewPassword()));
                return new RestResponse().setSuccess(appUserService.updateById(modifyUser));
            }
            modifyUser.setSex(user.getSex());
            modifyUser.setAvatar(user.getAvatar());
            modifyUser.setNickName(user.getNickName());
            modifyUser.setSignature(user.getSignature());
            appUserService.updateById(modifyUser);
        return new RestResponse().setSuccess(true).setData(modifyUser);
    }

    /**
     * 我的课表-教练
     * @return
     */
    @GetMapping("/getCourseList")
    public RestResponse getCourseList(){
        Long[] roleId=SecurityUtils.getAppLoginUser().getRoles();
        //104 教师  105店长  103 会籍顾问
        if (!ArrayUtil.containsAny(roleId,104l)){
            return new RestResponse().setSuccess(false).setMessage("当前登录人无权限！");
        }
        Long teacherId=SecurityUtils.getAppLoginUser().getManageAccountId();
        ReqSearchClaTime reqSearchClaTime=new ReqSearchClaTime();
        reqSearchClaTime.setTeacherId(teacherId);//teacherId

        RespBusinessClaTimeCalendar c =scClaTimeService.searchListForCalendar(reqSearchClaTime);
        return new RestResponse().setSuccess(true).setMessage("成功").setData(c);
    }

    /**
     * 课程详情（教练）
     * @param map
     * @return
     */
    @PostMapping("/courseTimeDetail")
    public RestResponse courseTimeDetail(@RequestBody Map<String,Long> map){
        Long courseTimeId=map.get("courseTimeId");
        return scClaTimeService.courseTimeDetail(courseTimeId);
    }

    /**
     * 学员预约列表(教师)
     * @param
     * @return
     */
    @GetMapping("/appointmentListForTeacher")
    public RestResponse appointmentListForTeacher(){

        return scClaTimeService.appointmentListForTeacher();
    }
    /**
     * 学员预约列表(店长)
     * @param
     * @return
     */
    @GetMapping("/appointmentListForManager")
    public RestResponse appointmentListForManager(){

        return scClaTimeService.appointmentListForManager();
    }

    /**
     * 确认/拒绝某个学员的 预约（教师/店长）
     * bookId 预约id
     * bookStatus  1->教练确认；2->店长确认/预约成功/待上课  5- >预约失败
     * @param map
     * @return
     */
    @PostMapping("/checkAppointment")
    public RestResponse checkAppointment(@RequestBody Map map){
        Long courseTimeId=Long.parseLong(map.get("bookId").toString());
        int bookStatus=Integer.parseInt(map.get("bookStatus").toString());
        return scClaTimeService.checkAppointment(courseTimeId,bookStatus);
    }


    /**
     * 确认上课(不消课)
     * @param map courseTimeId
     *            startTime 实际上课时间//HH:mm:ss 24小时制
     *            endTime 实际下课时间 //HH:mm:ss 24小时制
     * @return
     */
    @PostMapping("/confirmClass")
    public RestResponse confirmClass(@RequestBody Map map){
        Long courseTimeId=Long.parseLong(map.get("courseTimeId").toString());
        String startTime=map.get("startTime").toString();//HH:mm:ss
        String endTime=map.get("endTime").toString();//HH:mm:ss
        if (StrUtil.hasEmpty(startTime,endTime)){
            throw new RuntimeException("请输入具体上下课时间！");
        }
        return scClaTimeService.confirmClass(courseTimeId,startTime,endTime);
    }

    /**
     * 学员签到(消课)
     * @param map code bookId
     * @return
     */
    @PostMapping("/checkIn")
    public RestResponse checkIn(@RequestBody Map map){
        return scClaTimeService.checkIn(map);
    }


    /**
     * 教练课时统计 今日 昨日 本月 上月
     * @param
     * @return
     * today
     * yesterday
     * thisMonth
     * lastMonth
     */
    @PostMapping("/teacherClaTotal")
    public RestResponse teacherClaTotal(@RequestBody Map<String,String> map){
        String time=map.get("time").toString();
        return scClaTimeService.teacherClaTotal(time);
    }

    /**
     * 教练费用统计 今日 昨日 本月 上月
     * @param
     * @return
     * today
     * yesterday
     * thisMonth
     * lastMonth
     */
    @PostMapping("/teacherFeeTotal")
    public RestResponse teacherFeeTotal(@RequestBody Map<String,String> map){
        String time=map.get("time").toString();
        return scClaTimeService.teacherFeeTotal(time);
    }

    /**
     * 课程顾问-我的客户 报课的客户
     * @return
     */
    @GetMapping("/myClient")
    public RestResponse myClient(){
        return scClaTimeService.myClient();
    }

    /**
     * 提成统计（销售/课程顾问） 今日 昨日 本月 上月
     * @param
     * @return
     * today
     * thisMonth
     * lastMonth
     */
    @PostMapping("/commissionTotal")
    public RestResponse commissionTotal(@RequestBody Map<String,String> map){
        String time=map.get("time").toString();
        return scClaTimeService.commissionTotal(time);
    }

    /**
     * 销售信息（店长） 今日 昨日 本月 上月
     * @param
     * @return
     * today
     * yesterday
     * thisMonth
     * lastMonth
     */
    @PostMapping("/salesInformation")
    public RestResponse salesInformation(@RequestBody Map<String,String> map){
        String time=map.get("time").toString();
        return scClaTimeService.salesInformation(time);
    }

    /**
     * 场地费用（店长） 今日 昨日 本月 上月
     * @param
     * @return
     * today
     * thisMonth
     * lastMonth
     */
    @PostMapping("/claRoomFee")
    public RestResponse claRoomFee(@RequestBody Map<String,String> map){
        String time=map.get("time").toString();
        return scClaTimeService.claRoomFee(time);
    }


}
