package com.sqx.modules.artificer.controller.app;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.artificer.dao.ArtificerDao;
import com.sqx.modules.artificer.entity.Artificer;
import com.sqx.modules.artificer.service.AppArtificerIntegralService;
import com.sqx.modules.artificer.vo.HomeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/app/artificer/integral/")
@Api(value = "技师积分", tags = {"技师积分"})
@AllArgsConstructor
public class AppArtificerIntegralController {

    private final AppArtificerIntegralService appArtificerIntegralService;
    private final ArtificerDao artificerDao;
    @GetMapping("/home")
    @ApiOperation("技师首页查询")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = HomeVO.class),})
    public Result home(@RequestParam Long artificerId) {
        return appArtificerIntegralService.home(artificerId);
    }

    @GetMapping("/upgrade")
    @ApiOperation("技师升级定时任务手动入口（精油）")
    @Deprecated
    public Result upgrade(String date) {
        return appArtificerIntegralService.upgrade(date);
    }

    @GetMapping("/upgrade/zy")
    @ApiOperation("技师升级定时任务手动入口（中医）")
    @Deprecated
    public Result upgradeZy(String date) {
        return appArtificerIntegralService.upgradeZy(date);
    }

    @GetMapping("/stop")
    @ApiOperation("技师停牌定时任务手动入口")
    public Result upgrade() {
        return appArtificerIntegralService.line();
    }

    @Login
    @GetMapping("/time")
    @ApiOperation("前端获取多长时间积一分")
    public Result time() {
        return appArtificerIntegralService.time();
    }

    /**
     * 每个月11号 21号和月末 在线时间不对问题（更新所有技师本周期在线时间为00:00:00）
     */
    public void updArtificerTime() {
        artificerDao.update(null, Wrappers.<Artificer>lambdaUpdate().set(Artificer::getUpTime, DateUtil.format(new Date(), "yyyy-MM-dd")+" 00:00:00"));
    }


    @Login
    @GetMapping("/getArtificerLevels")
    @ApiOperation("获取技师等级说明")
    public Result getArtificerLevels(Integer page, Integer limit, Long artificerId) {
        return appArtificerIntegralService.getArtificerLevels(page, limit, artificerId);
    }

    /**
     * 每个月11号 21号执行一次 定时任务
     *
     * @return
     */
//    @Scheduled(cron = "0 0 0 11,21 * ?")
    @ApiOperation("技师停牌处理+升级处理")
    public Result line() {
        //每个月11号 21号和月末 在线时间不对问题（更新所有技师本周期在线时间为00:00:00）
        updArtificerTime();
        //精油升级
        appArtificerIntegralService.upgrade("");
        //中医升级
        appArtificerIntegralService.upgradeZy("");
        //停牌
//        appArtificerIntegralService.line();
        return Result.success();
    }

    /**
     * 每月最后一日触发 定时任务
     *
     * @return
     */
//    @Scheduled(cron = "0 0 0 L * ?")
    @ApiOperation("技师停牌处理+升级处理")
    public Result lines() {
        //每个月11号 21号和月末在线离线时间不对问题（更新所有技师在线时间为00:00:00）
        updArtificerTime();
        //精油升级
        appArtificerIntegralService.upgrade("");
        //中医升级
        appArtificerIntegralService.upgradeZy("");
        //停牌
//        appArtificerIntegralService.line();
        return Result.success();
    }

}
