package com.yonge.cooleshow.teacher.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.microsvc.toolkit.common.response.paging.PageInfo;
import com.microsvc.toolkit.common.response.paging.QueryInfo;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.entity.Student;
import com.yonge.cooleshow.biz.dal.entity.Subject;
import com.yonge.cooleshow.biz.dal.entity.Teacher;
import com.yonge.cooleshow.biz.dal.entity.UserMusicStar;
import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import com.yonge.cooleshow.biz.dal.service.*;
import com.yonge.cooleshow.biz.dal.wrapper.UserMusicStarWrapper;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
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 java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.yonge.cooleshow.common.entity.HttpResponseResult.failed;

@Slf4j
@Validated
@RestController
@RequestMapping("${app-config.url.teacher:}/userMusicStar")
@Api(tags = "用户曲目作品点赞记录")
public class UserMusicStarController {

    @Autowired
    private UserMusicStarService userMusicStarService;

    @Autowired
    private SysUserFeignService sysUserFeignService;

    @Autowired
    private SysUserService sysUserService;

    @ApiOperation(value = "查询分页", notes = "用户曲目作品点赞记录- 传入 UserMusicStarWrapper.UserMusicStarQuery")
    @PostMapping("/page")
    public HttpResponseResult<PageInfo<UserMusicStarWrapper.UserMusicStar>> page(@RequestBody @Validated UserMusicStarWrapper.UserMusicStarQuery query) {

        IPage<UserMusicStarWrapper.UserMusicStar> pages = userMusicStarService.selectPage(QueryInfo.getPage(query), query);


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


    @ApiOperation(value = "点赞/取消点赞")
    @PostMapping("/star")
    public HttpResponseResult<JSONObject> star(@Validated @RequestBody UserMusicStarWrapper.Star star) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        star.setUserId(user.getId());
        star.setClientType(ClientEnum.TEACHER);
        // 新增数据
        userMusicStarService.star(star);

        return HttpResponseResult.succeed();
    }
}
