package com.yonge.cooleshow.admin.controller.music;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.cooleshow.admin.io.request.music.MusicCompareVo;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.service.MusicCompareRecordStatService;
import com.yonge.cooleshow.biz.dal.service.SysMusicCompareRecordService;
import com.yonge.cooleshow.biz.dal.wrapper.music.MusicCompareWrapper;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.exception.BizException;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.mybatis.support.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 曲目练习统计表 前端控制器
 * </p>
 *
 * @author Eric
 * @since 2022-10-17
 */
@RestController
@RequestMapping("${app-config.url.admin:}/music/compare")
@Api(tags = "曲目练习统计 ")
public class MusicCompareRecordStatController extends BaseController {

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

    /**
     * 查询曲目统计汇总信息
     */
    @ApiOperation(value = "曲目统计汇总信息", notes = "")
    @PostMapping(value = "/record/stat", consumes="application/json", produces="application/json")
    public HttpResponseResult<MusicCompareVo.RecordStat> musicCompareRecordStatInfo(@RequestBody MusicCompareVo.StatQueryRequest request) {

        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }

        if (request.invalidParam()) {
            return failed("无效的请求参数");
        }

        MusicCompareWrapper.RecordStat records = musicCompareRecordStatService.findMusicCompareRecordStatInfo(sysUser.getId(),
                MusicCompareWrapper.StatQueryInfo.from(request.jsonString()));

        return succeed(MusicCompareVo.RecordStat.from(records.jsonString()));
    }

    /**
     * 查询曲目统计汇总信息
     */
    @ApiOperation(value = "曲目统计信息查询", notes = "")
    @PostMapping(value = "/record/page", consumes="application/json", produces="application/json")
    public HttpResponseResult<PageInfo<MusicCompareVo.RecordInfo>> musicCompareRecordPageInfo(@RequestBody MusicCompareVo.StatQueryRequest request) {

        if (request.invalidParam()) {
            return failed("无效的请求参数");
        }

        // 曲目统计信息
        IPage<MusicCompareWrapper.RecordInfo> wrapper = musicCompareRecordStatService.findMusicCompareRecordPage(PageUtil.getPage(request),
                MusicCompareWrapper.StatQueryInfo.from(request.jsonString()));

        // 数据转换
        List<MusicCompareVo.RecordInfo> pageInfos = JSON.parseArray(JSON.toJSONString(wrapper.getRecords()),
                MusicCompareVo.RecordInfo.class);

        return succeed(PageUtil.getPageInfo(wrapper, pageInfos));
    }

    /**
     * 查询曲目统计汇总信息
     */
    @ApiOperation(value = "曲目评测信息查询")
    @PostMapping(value = "/evaluate/page", consumes="application/json", produces="application/json")
    public HttpResponseResult<PageInfo<MusicCompareWrapper.RecordEvaluate>> musicCompareEvaluatePageInfo(@RequestBody MusicCompareVo.EvaluateQueryInfo request) {

        if (request.invalidParam()) {
            return failed("无效的请求参数");
        }

        // 曲目统计信息
        IPage<MusicCompareWrapper.RecordEvaluate> wrapper = musicCompareRecordStatService.findMusicCompareRecordEvaluatePage(PageUtil.getPage(request),
                MusicCompareWrapper.EvaluteQueryInfo.from(request.jsonString()));

        return succeed(PageUtil.pageInfo(wrapper));
    }

    /**
     * 用户最近一次评测报告
     * @param recordId 评测记录ID
     * @return HttpResponseResult<Object>
     */
    @ApiOperation(value = "用户最后一次评测数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recordId", value = "评测记录ID", required = true, dataType = "Integer")
    })
    @GetMapping("evaluate/report")
    public HttpResponseResult<Object> getLastEvaluationMusicalNotesPlayStats(@RequestParam("recordId") Long recordId){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null){
            throw new BizException("请登录");
        }
        return succeed(sysMusicCompareRecordService.getLastEvaluationMusicalNotesPlayStats(sysUser.getId(), recordId));
    }
}
