package com.yonge.cooleshow.admin.task;

import com.yonge.cooleshow.biz.dal.service.*;
import com.yonge.cooleshow.common.constant.SysConfigConstant;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;

/**
 * @Author: liweifan
 * @Data: 2022/3/28 15:50
 */
@RestController
@RequestMapping("/task")
@ApiIgnore
public class TaskController extends BaseController {
    @Autowired
    private UserOrderService userOrderService;
    @Autowired
    private UserBindingTeacherService userBindingTeacherService;
    @Autowired
    private CourseScheduleService courseScheduleService;
    @Autowired
    private UserAccountRecordService userAccountRecordService;
    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private PlatformCashAccountRecordService platformCashAccountRecordService;
    @Autowired
    private ActivityPlanService activityPlanService;
    @Autowired
    private ActivityPlanEvaluationService activityPlanEvaluationService;

    @Value("${app.blacklist:}")
    private String blacklistFilePath;

    /***
     * 轮询用户订单
     * @author liweifan
     * @updateTime 2022/4/29 19:12
     * @return: com.yonge.cooleshow.common.entity.HttpResponseResult
     */
    @GetMapping("/pollingOrder")
    public HttpResponseResult pollingOrder() {
        userOrderService.pollingOrder();
        return succeed();
    }

    /***
     * 一日一次，凌晨执行
     * @author liweifan
     * @updateTime 2022/4/29 19:12
     * @return: com.yonge.cooleshow.common.entity.HttpResponseResult<java.lang.Boolean>
     */
    @GetMapping("/daysTask")
    public HttpResponseResult<Boolean> daysTask() {
        //超过天数无琴房课的解除绑定关系
        userBindingTeacherService.unbindTask();
        //平台老师结算
        platformCashAccountRecordService.updateWaitRecord();
        return succeed(true);
    }

    // 老师作业布置
    @PostMapping(value = "/teacherSend")
    public HttpResponseResult<Object> sendTodayNotRepliedAndNotDecorateHomework() {
        courseScheduleService.sendTodayNotRepliedAndNotDecorateHomework();
        return HttpResponseResult.succeed();
    }

    //老师课酬
    @PostMapping(value = "/teacherEarning")
    public HttpResponseResult<Object> sendTeacherEarning() {
        userAccountRecordService.sendTeacherEarning();
        return HttpResponseResult.succeed();
    }

    //刷新黑名单
    @PostMapping(value = "/refreshBlackList")
    public HttpResponseResult refreshBlackList() {

        if (StringUtils.isEmpty(blacklistFilePath)) {
            blacklistFilePath = SysConfigConstant.BLACK_LIST_FILE_PATH;
        }

        RBucket<Long> bucket = redissonClient.getBucket(SysConfigConstant.BLACK_LIST_UPDATE_TIME);
        File file = new File(blacklistFilePath);
        bucket.set(file.lastModified());
        return HttpResponseResult.succeed();
    }

    //营销活动
    @PostMapping(value = "/activityState")
    public HttpResponseResult<Object> activityState() {
        activityPlanService.activityState();
        return HttpResponseResult.succeed();
    }

    //活动中定时轮询
    @PostMapping(value = "/activityIng")
    public HttpResponseResult<Object> activityIng() {
        activityPlanService.activityIng();
        return HttpResponseResult.succeed();
    }

    /**
     * 活动测试
     * @param activityId 活动ID
     * @return HttpResponseResult<Object>
     */
    @GetMapping("/test")
    public HttpResponseResult<Object> test(@RequestParam("activityId") Long activityId) {

        activityPlanEvaluationService.successActivity(activityId);
        return HttpResponseResult.succeed();
    }
}
