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.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dto.search.MusicAlbumDetailSearch;
import com.yonge.cooleshow.biz.dal.dto.search.MusicAlbumSearch;
import com.yonge.cooleshow.biz.dal.dto.search.StudentMusicAlbumSearch;
import com.yonge.cooleshow.biz.dal.enums.YesOrNoEnum;
import com.yonge.cooleshow.biz.dal.service.MusicAlbumService;
import com.yonge.cooleshow.biz.dal.support.PageUtil;
import com.yonge.cooleshow.biz.dal.vo.AlbumDetailVo;
import com.yonge.cooleshow.biz.dal.vo.MusicAlbumVo;
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 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/album")
@Api(tags = "专辑表 API接口")
public class MusicAlbumController extends BaseController {
    @Autowired
    private SysUserFeignService sysUserFeignService;

	@Autowired
	private MusicAlbumService musicAlbumService;


    @ApiOperation(value = "分页查询", httpMethod="POST", consumes="application/json", produces="application/json")
    @PostMapping(value="/list", consumes="application/json", produces="application/json")
    public HttpResponseResult<PageInfo<MusicAlbumVo>> list(@RequestBody MusicAlbumSearch query) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }
        query.setAlbumStatus(YesOrNoEnum.YES);
        query.setSortBy(1);
        IPage<MusicAlbumVo> iPage = musicAlbumService.selectPage(PageUtil.getPage(query),query);
        return succeed(PageUtil.pageInfo(iPage));
    }


    /**
     * 专辑详情
     */
    @PostMapping(value = "/detail",  consumes="application/json", produces="application/json")
    @ApiOperation(value = "专辑详情",httpMethod="POST", consumes="application/json", produces="application/json")
    public HttpResponseResult<AlbumDetailVo> detail(@Valid @RequestBody MusicAlbumDetailSearch query) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }
        query.setType(2);
        query.setState(YesOrNoEnum.YES);
        YesOrNoEnum yesOrNoEnum = musicAlbumService.checkFavorite(sysUser.getId(),query.getId() );
        AlbumDetailVo albumDetailVo = musicAlbumService.detail(PageUtil.getPage(query), query);
        albumDetailVo.setFavorite(yesOrNoEnum);
        return succeed(albumDetailVo);
    }

    /**
     * 专辑收藏
     */
    @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(musicAlbumService.setFavorite(sysUser.getId(),id));
    }


    /**
     * 专辑收藏
     */
    @GetMapping("/favorite")
    @ApiOperation(value = "收藏专辑列表")
    public HttpResponseResult<PageInfo<MusicAlbumVo>> favoriteAlbum(QueryInfo query) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }
        StudentMusicAlbumSearch search = new StudentMusicAlbumSearch();
        search.setStudentId(sysUser.getId());
        search.setAlbumStatus(YesOrNoEnum.YES);
        IPage<MusicAlbumVo> albumVoIPage = musicAlbumService.favoriteAlbum(PageUtil.getPage(query), search);
        return succeed(PageUtil.pageInfo(albumVoIPage));
    }
}
