package com.yonge.cooleshow.website.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.dto.search.MusicAlbumDetailSearch;
import com.yonge.cooleshow.biz.dal.dto.search.MusicAlbumSearch;
import com.yonge.cooleshow.biz.dal.service.MusicAlbumService;
import com.yonge.cooleshow.common.enums.YesOrNoEnum;
import com.yonge.toolset.mybatis.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.base.page.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;

import javax.validation.Valid;

/**
 * 专辑表 web 控制层
 * @author yzp
 * @date 2022-03-26 00:21:46
 * @version v1.0
 **/
@RestController
@RequestMapping("/open/music/album")
@Api(tags = "开放专辑接口")
public class OpenMusicAlbumController 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) {

        // 检查app版本
        query.setAuditVersion(YesOrNoEnum.NO);
        query.setAlbumStatus(YesOrNoEnum.YES);
        query.setSortBy(1);
        IPage<MusicAlbumVo> iPage = musicAlbumService.selectStudentPage(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();
        YesOrNoEnum favorite = YesOrNoEnum.NO;
        if (sysUser != null && sysUser.getId() != null) {
            favorite = musicAlbumService.checkFavorite(sysUser.getId(),query.getId() );
        }
        query.setType(2);
        query.setState(YesOrNoEnum.YES);
        AlbumDetailVo albumDetailVo = musicAlbumService.detail(PageUtil.getPage(query), query);
        albumDetailVo.setFavorite(favorite);

        // 热门专辑
        MusicAlbumSearch musicAlbumSearch = new MusicAlbumSearch();
        musicAlbumSearch.setAuditVersion(YesOrNoEnum.NO);
        musicAlbumSearch.setAlbumStatus(YesOrNoEnum.YES);
        musicAlbumSearch.setSortBy(1);
        musicAlbumSearch.setPage(1);
        musicAlbumSearch.setRows(4);
        IPage<MusicAlbumVo> hotMusicAlbum = musicAlbumService.selectStudentPage(PageUtil.getPage(musicAlbumSearch),musicAlbumSearch);
        albumDetailVo.setHotMusicAlbum(hotMusicAlbum.getRecords());

        // 相关专辑
        musicAlbumSearch.setAlbumTagIds(albumDetailVo.getAlbumTag());
        IPage<MusicAlbumVo> relatedMusicAlbum = musicAlbumService.selectStudentPage(PageUtil.getPage(musicAlbumSearch),musicAlbumSearch);
        albumDetailVo.setRelatedMusicAlbum(relatedMusicAlbum.getRecords());
        return succeed(albumDetailVo);
    }

}
