package com.yonge.cooleshow.website.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.StudentMusicAlbumSearch;
import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import com.yonge.cooleshow.biz.dal.enums.OrderStatusEnum;
import com.yonge.cooleshow.biz.dal.enums.album.PurchaseRecordTypeEnum;
import com.yonge.cooleshow.biz.dal.service.MusicAlbumService;
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.cooleshow.common.enums.YesOrNoEnum;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.base.page.QueryInfo;
import com.yonge.toolset.mybatis.support.PageUtil;
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.*;


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

	@Autowired
	private MusicAlbumService musicAlbumService;

    /**
     * 专辑收藏
     */
    @PostMapping("/favorite/{id}")
    @ApiOperation(value = "专辑收藏/取消收藏")
    public HttpResponseResult<Boolean> favorite(@ApiParam(value = "专辑编号", required = true) @PathVariable("id") Long id,
                                                @ApiParam(value = "客户端类型") @RequestParam(value = "clientType", required = false, defaultValue = "STUDENT") String clientType) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }

        if (ClientEnum.invalid(clientType)) {
            return failed("无效的客户端类型");
        }

        return succeed(musicAlbumService.setFavorite(sysUser.getId(), id, ClientEnum.valueOf(clientType)));
    }


    /**
     * 专辑收藏
     */
    @GetMapping("/favorite")
    @ApiOperation(value = "学生-收藏专辑列表")
    public HttpResponseResult<PageInfo<MusicAlbumVo>> favoriteAlbum(@ApiParam(value = "客户端类型") @RequestParam(value = "clientType", required = false, defaultValue = "STUDENT") String clientType,
                                                                    QueryInfo query) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }

        if (ClientEnum.invalid(clientType)) {
            return failed("无效的客户端类型");
        }

        StudentMusicAlbumSearch search = new StudentMusicAlbumSearch();
        search.studentId(sysUser.getId())
                .albumFavorite(true)
                .setAlbumStatus(YesOrNoEnum.YES);

        IPage<MusicAlbumVo> albumVoIPage = musicAlbumService.favoriteAlbum(PageUtil.getPage(query), search, ClientEnum.valueOf(clientType));
        return succeed(PageUtil.pageInfo(albumVoIPage));
    }

    /**
     * 已购买专辑信息
     * @return HttpResponseResult<PageInfo<MusicAlbumVo>>
     */
    @ApiOperation(value = "已购买专辑信息")
    @GetMapping("/purchased")
    public HttpResponseResult<PageInfo<MusicAlbumVo>> purchasedAlbumInfo(@ApiParam(value = "客户端类型") @RequestParam(value = "clientType", required = false, defaultValue = "STUDENT") String clientType,
                                                                         QueryInfo query) {

        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }
        StudentMusicAlbumSearch search = new StudentMusicAlbumSearch();
        search.studentId(sysUser.getId())
                .albumPurchased(true)
                .purchaseType(PurchaseRecordTypeEnum.ALBUM)
                .orderStatus(OrderStatusEnum.PAID)
                .clientType(ClientEnum.valueOf(clientType));

        IPage<MusicAlbumVo> albumVoIPage = musicAlbumService.favoriteAlbum(PageUtil.getPage(query), search, ClientEnum.valueOf(clientType));
        return succeed(PageUtil.pageInfo(albumVoIPage));
    }
}
