package org.dromara.system.controller.common;


import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.secure.BCrypt;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.apache.commons.lang3.ObjectUtils;
import org.dromara.common.core.domain.AjaxResult;
import org.dromara.common.core.domain.R;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.redis.utils.RedisUtils;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.FormalTeacherClass;
import org.dromara.system.domain.SysUser;
import org.dromara.system.service.ISysUserService;
import org.dromara.system.service.Task;
import org.dromara.system.service.common.IAppletService;
import org.dromara.system.utils.SendSmsUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

/**小程序公共接口
 * @Author: tjf
 * @Date: 2023/5/25 11:46
 * @Describe:
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/applet")
public class AppletController extends BaseController {

    private final IAppletService appletService;
    private final ISysUserService sysUserService;

    private final Task task;


    /**
     * 准备下课
     */
    @PostMapping("/xiaKe")
    public R<Void> xiake( @RequestBody FormalTeacherClass formalTeacherClass)
    {
     return    appletService.xiake(formalTeacherClass);
    }

    /**
     * 小程序首页数据
     * 共多少班级
     * 已放学 未放学
     */
    @PostMapping("/index")
    public AjaxResult index(@RequestBody FormalTeacherClass formalTeacherClass)
    {
        return  appletService.index(formalTeacherClass);
    }

    /**
     * app放学状态列表
     */
    @PostMapping("/indexList")
    public AjaxResult indexList(@RequestBody FormalTeacherClass formalTeacherClass)
    {
        return  appletService.indexList(formalTeacherClass);
    }

    /**
     *首页统计
     */
    @GetMapping("/pc/statistics")
    public AjaxResult pcStatistics()
    {
        return  appletService.pcStatistics();
    }

    /**
     *首页统计今日准时放学/延迟放学/本周发布文章
     */
    @PostMapping("/pc/fangXue/statistics")
    public AjaxResult fangXueStatistics()
    {
        return  appletService.fangXueStatistics();
    }

    /**
     *首页班级人数统计
     * 老师返回自己所带的班级，学校返回所有班级
     */
    @GetMapping("/pc/classStudentNum")
    public AjaxResult pcClassStudentNum()
    {
        return  appletService.pcClassStudentNum();
    }

    @SaIgnore
    @GetMapping("/pc/afterClass")
    public AjaxResult afterClass()
    {
        task.afterClass();
        return  AjaxResult.success("成功");
    }
    /**
     *忘记密码发送验证短信
     */
    @SaIgnore
    @RepeatSubmit(interval = 1,timeUnit = TimeUnit.MINUTES)
    @GetMapping("/appForgetPW/{phone}")
    public AjaxResult appForgetPW(@PathVariable String phone)
    {
        String code = SendSmsUtils.getCode(4);
        SendSmsUtils.sendSms(phone,"SMS_219525380","{\"code\":\"" + code + "\"}");
        String key = "SMS_CODE:"+phone;
        RedisUtils.setCacheObject(key,code, Duration.ofMinutes(5));
        return  AjaxResult.success(code);
    }
    /**
     *忘记密码校验验证码,修改密码
     */
    @SaIgnore
    @RepeatSubmit
    @GetMapping("/appCheck")
    public R<Void> appCheck(@RequestParam("code") String code,@RequestParam("phone") String phone,@RequestParam("password") String password)
    {
        String key = "SMS_CODE:"+phone;
        Object cacheObject = RedisUtils.getCacheObject(key);
        if (ObjectUtils.isNotEmpty(cacheObject)){
            if (code.equals(String.valueOf(cacheObject))){
               //修改密码
               return toAjax(sysUserService.resetUserPwdByUserName(phone,BCrypt.hashpw(password)));
            }
        }
        return  R.fail();
    }

}
