package com.yonge.cooleshow.student.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
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.RestController;

import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.entity.SysMusicCompareRecord;
import com.yonge.cooleshow.biz.dal.queryInfo.SysMusicCompareRecordQueryInfo;
import com.yonge.cooleshow.biz.dal.service.SysMusicCompareRecordService;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.exception.BizException;

/**
 * @Author Joburgess
 * @Date 2021/8/13 0013
 */
@Api(tags = "小酷Ai记录")
@RequestMapping("sysMusicRecord")
@RestController
public class SysMusicCompareRecordController extends BaseController {

    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private SysMusicCompareRecordService sysMusicCompareRecordService;

    @ApiOperation(value = "添加记录")
    @PostMapping("add")
    public HttpResponseResult add(SysMusicCompareRecord record){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null){
            throw new BizException("请登录");
        }
        if(Objects.isNull(record.getFeature())){
            return failed("请设置功能点");
        }
        record.setUserId(sysUser.getId());
        record.setClientId("student");
        return succeed(sysMusicCompareRecordService.insert(record));
    }

    @ApiOperation(value = "用户最后一次评测数据")
    @GetMapping("getLastEvaluationMusicalNotesPlayStats")
    public HttpResponseResult getLastEvaluationMusicalNotesPlayStats(Long recordId){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null){
            throw new BizException("请登录");
        }
        return succeed(sysMusicCompareRecordService.getLastEvaluationMusicalNotesPlayStats(sysUser.getId(), recordId));
    }

    @ApiOperation("学员训练数据统计")
    @GetMapping("studentTrainData")
    public HttpResponseResult studentTrainData(SysMusicCompareRecordQueryInfo queryInfo){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("获取用户信息失败");
        }
        queryInfo.setUserId(sysUser.getId());
        if (StringUtils.isEmpty(queryInfo.getStartTime())) {
            return failed("时间不能为空");
        }
        LocalDate localDate = LocalDate.parse(queryInfo.getStartTime() + "-01",
                                              DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        queryInfo.setStartTime(localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        LocalDate endDate = localDate.plusDays(localDate.getMonth().length(localDate.isLeapYear()) -1);
        queryInfo.setEndTime(endDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        return succeed(sysMusicCompareRecordService.studentTrainData(queryInfo));
    }

}
