package com.yonge.cooleshow.student.controller.open;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import com.yonge.cooleshow.biz.dal.service.UserMusicService;
import com.yonge.cooleshow.biz.dal.service.UserMusicService;
import com.yonge.cooleshow.biz.dal.wrapper.UserMusicWrapper;
import com.microsvc.toolkit.common.response.paging.PageInfo;
import com.microsvc.toolkit.common.response.paging.QueryInfo;
import com.microsvc.toolkit.common.response.template.R;
import com.microsvc.toolkit.config.jwt.utils.JwtUserInfo;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.exception.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Slf4j
@Validated
@RestController
@RequestMapping("${app-config.url.student:}/open/userMusic")
@Api(tags = "用户曲目作品")
public class OpenUserMusicController {

    @Autowired
    private UserMusicService userMusicService;
    @Autowired
    private SysUserFeignService sysUserFeignService;


    @ApiOperation(value = "详情", notes = "用户曲目作品-根据详情ID查询单条, 传入id")
    @GetMapping("/detail/{id}")
    public HttpResponseResult<UserMusicWrapper.UserMusic> detail(@PathVariable("id") Long id) {
        SysUser user = sysUserFeignService.queryUserInfo();
        JwtUserInfo userInfo = null;
        if (user != null && null != user.getId()) {
            userInfo = JwtUserInfo.builder()
                .clientType(ClientEnum.STUDENT.name())
                .userId(user.getId().toString())
                .build();
        }
        UserMusicWrapper.UserMusicQuery build = UserMusicWrapper.UserMusicQuery.builder().id(id).build();
        IPage<UserMusicWrapper.UserMusic> pages = userMusicService.selectPage(QueryInfo.getPage(build), build
            ,userInfo);
        List<UserMusicWrapper.UserMusic> records = pages.getRecords();
        if (records.isEmpty()) {
            throw new BizException(999,"作品已删除");
        }

        return HttpResponseResult.succeed(records.get(0));
    }


    @ApiOperation(value = "查询分页", notes = "用户曲目作品- 传入 UserMusicWrapper.UserMusicQuery")
    @PostMapping("/page")
    public HttpResponseResult<PageInfo<UserMusicWrapper.UserMusic>> page(@RequestBody UserMusicWrapper.UserMusicQuery query) {
        SysUser user = sysUserFeignService.queryUserInfo();
        JwtUserInfo userInfo = null;
        if (user != null && null != user.getId()) {
            userInfo = JwtUserInfo.builder()
                .clientType(ClientEnum.STUDENT.name())
                .userId(user.getId().toString())
                .build();
        }
        IPage<UserMusicWrapper.UserMusic> pages = userMusicService.selectPage(QueryInfo.getPage(query), query
            ,userInfo);

        return HttpResponseResult.succeed(QueryInfo.pageInfo(pages));
    }
}
