package com.yonge.cooleshow.student.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.dto.RealnameAuthReq;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.auth.api.enums.SysUserType;
import com.yonge.cooleshow.biz.dal.dto.search.StudentMusicSheetSearch;
import com.yonge.cooleshow.biz.dal.enums.AuthStatusEnum;
import com.yonge.cooleshow.biz.dal.enums.YesOrNoEnum;
import com.yonge.cooleshow.biz.dal.service.MusicSheetService;
import com.yonge.cooleshow.biz.dal.support.PageUtil;
import com.yonge.cooleshow.biz.dal.vo.CheckVo;
import com.yonge.cooleshow.biz.dal.vo.MusicSheetDetailVo;
import com.yonge.cooleshow.biz.dal.vo.MusicSheetVo;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.mybatis.page.PageInfo;
import com.yonge.toolset.mybatis.page.QueryInfo;
import com.yonge.toolset.utils.idcard.IdcardInfoExtractor;
import com.yonge.toolset.utils.string.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 曲谱表 web 控制层
 * @author yzp
 * @date 2022-03-26 00:21:46
 * @version v1.0
 **/
@RestController
@RequestMapping("/music/sheet")
@Api(tags = "曲谱表 API接口")
public class MusicSheetController extends BaseController {
    @Autowired
    private SysUserFeignService sysUserFeignService;

	@Autowired
	private MusicSheetService musicSheetService;


    /**
     * 查询单条
     */
    @GetMapping("/detail/{id}")
    @ApiOperation(value = "详情", notes = "传入musicTag")
    public HttpResponseResult<MusicSheetDetailVo> detail(@ApiParam(value = "曲谱编号", required = true) @PathVariable("id") Long id) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }
        return succeed(musicSheetService.detail(id, sysUser, SysUserType.STUDENT));
    }

    @ApiOperation(value = "分页查询", httpMethod="POST", consumes="application/json", produces="application/json")
    @PostMapping(value="/list", consumes="application/json", produces="application/json")
    public HttpResponseResult<PageInfo<MusicSheetVo>> list(@RequestBody StudentMusicSheetSearch query) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }

        // 学生 只能看通过审核 并且 启用的 曲目
        query.setState(YesOrNoEnum.YES);
        query.setAuditStatus(AuthStatusEnum.PASS);
        query.setStudentId(sysUser.getId());
        IPage<MusicSheetVo> musicSheetVoIPage = musicSheetService.selectStudentPage(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(musicSheetVoIPage));
    }

    /**
     * 曲目收藏
     */
    @PostMapping("/favorite/{id}")
    @ApiOperation(value = "曲目收藏/取消收藏")
    public HttpResponseResult<Boolean> favorite(@ApiParam(value = "曲目编号", required = true) @PathVariable("id") Long id) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }
        return succeed(musicSheetService.setFavorite(sysUser.getId(),id));
    }



    /**
     * 我的单曲，收藏单曲
     */
    @GetMapping("/my")
    @ApiOperation(value = "我的单曲")
    public HttpResponseResult<PageInfo<MusicSheetVo>> myMusic(QueryInfo query) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }
        StudentMusicSheetSearch search = new StudentMusicSheetSearch();
        search.setStudentId(sysUser.getId());
        search.setState(YesOrNoEnum.YES);
        search.setAuditStatus(AuthStatusEnum.PASS);
        IPage<MusicSheetVo> sheetVoIPage = musicSheetService.myMusic(PageUtil.getPage(query),search);
        return succeed(PageUtil.pageInfo(sheetVoIPage));
    }


    /**
     * 我的单曲，收藏单曲
     */
    @GetMapping("/favorite")
    @ApiOperation(value = "收藏单曲")
    public HttpResponseResult<PageInfo<MusicSheetVo>> favoriteMusic(QueryInfo query) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }
        StudentMusicSheetSearch search = new StudentMusicSheetSearch();
        search.setStudentId(sysUser.getId());
        search.setState(YesOrNoEnum.YES);
        search.setAuditStatus(AuthStatusEnum.PASS);
        IPage<MusicSheetVo> sheetVoIPage = musicSheetService.favoriteMusic(PageUtil.getPage(query),search);
        return succeed(PageUtil.pageInfo(sheetVoIPage));
    }


    /**
     * 最近练习
     */
    @GetMapping("/practice")
    @ApiOperation(value = "最近练习")
    public HttpResponseResult<PageInfo<MusicSheetVo>> practiceMusic(QueryInfo query) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }
        StudentMusicSheetSearch search = new StudentMusicSheetSearch();
        search.setStudentId(sysUser.getId());
        search.setState(YesOrNoEnum.YES);
        search.setAuditStatus(AuthStatusEnum.PASS);
        IPage<MusicSheetVo> sheetVoIPage = musicSheetService.practiceMusic(PageUtil.getPage(query),search);
        return succeed(PageUtil.pageInfo(sheetVoIPage));
    }


    /**
     * 检查实名认证
     */
    @GetMapping("/realNameAuth/check")
    @ApiOperation(value = "检查实名认证")
    public HttpResponseResult<CheckVo> realNameAuthCheck() {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }

        CheckVo checkVo = new CheckVo();
        if (StringUtil.isEmpty(sysUser.getIdCardNo())) {
            checkVo.setCheck(YesOrNoEnum.NO);
        }  else {
            checkVo.setCheck(YesOrNoEnum.YES);
        }
        return succeed(checkVo);
    }


    /**
     * 实名认证
     */
    @PostMapping(value = "/realNameAuth" ,consumes="application/json", produces="application/json")
    @ApiOperation(value = "实名认证", httpMethod="POST",consumes="application/json", produces="application/json")
    public HttpResponseResult<IdcardInfoExtractor> realNameAuth(@Valid @RequestBody RealnameAuthReq realNameAuthDto) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }

        return sysUserFeignService.realNameAuth(realNameAuthDto);
    }



}
