package com.yonge.cooleshow.teacher.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.MusicSheetDto;
import com.yonge.cooleshow.biz.dal.dto.search.StudentMusicSheetSearch;
import com.yonge.cooleshow.biz.dal.enums.AuthStatusEnum;
import com.yonge.cooleshow.biz.dal.enums.ChargeTypeEnum;
import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import com.yonge.cooleshow.biz.dal.enums.SourceTypeEnum;
import com.yonge.cooleshow.biz.dal.service.AppVersionInfoService;
import com.yonge.cooleshow.biz.dal.service.MusicSheetService;
import com.yonge.cooleshow.biz.dal.vo.MusicSheetDetailVo;
import com.yonge.cooleshow.biz.dal.vo.MusicSheetShareVo;
import com.yonge.cooleshow.biz.dal.vo.MusicSheetVo;
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.*;

import javax.validation.Valid;
import java.util.Date;

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

	@Autowired
	private MusicSheetService musicSheetService;

    @Autowired
    private AppVersionInfoService appVersionInfoService;

    /**
     * 查询单条
     */
    @GetMapping("/detail/{id}")
    @ApiOperation(value = "详情", notes = "传入musicTag")
    public HttpResponseResult<MusicSheetDetailVo> detail(@ApiParam(value = "曲谱编号", required = true) @PathVariable("id") Long id) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }
        return succeed(musicSheetService.detail(id, sysUser, ClientEnum.TEACHER));
    }

	@ApiOperation(value = "新增", httpMethod="POST", consumes="application/json", produces="application/json")
    @PostMapping(value="/create", consumes="application/json", produces="application/json")
    public HttpResponseResult<Object> create(@Valid @RequestBody MusicSheetDto musicSheetDto) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }

        if (ChargeTypeEnum.CHARGE.getCode().equals(musicSheetDto.getChargeType().getCode())
                && musicSheetDto.getMusicPrice()==null){
            return failed("此曲谱为收费曲谱，需要提供价格");
        }
        if (musicSheetDto.getNotation() == null) {
            musicSheetDto.setNotation(YesOrNoEnum.NO);
        }

        musicSheetDto.setUserId(sysUser.getId());
        musicSheetDto.setAuditStatus(AuthStatusEnum.DOING);
        musicSheetDto.setSubmitAuditTime(new Date());
        musicSheetDto.setSourceType(SourceTypeEnum.TEACHER);

        musicSheetDto.setCreateBy(sysUser.getId());
        musicSheetDto.setCreateTime(new Date());
        musicSheetService.saveMusicSheet(musicSheetDto,sysUser.getId());
        return succeed("新增曲谱成功");
    }


    @ApiOperation(value = "修改", httpMethod="POST", consumes="application/json", produces="application/json")
    @PostMapping(value="/update", consumes="application/json", produces="application/json")
    public HttpResponseResult<Object> update(@Valid @RequestBody MusicSheetDto musicSheet) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }
        if (musicSheet.getId()==null){
            return failed("缺少ID");
        }
        if (musicSheet.getNotation() == null) {
            musicSheet.setNotation(YesOrNoEnum.NO);
        }

        musicSheet.setUpdateBy(sysUser.getId());
        musicSheet.setUpdateTime(new Date());
        musicSheet.setSubmitAuditTime(new Date());
        musicSheet.setAuditStatus(AuthStatusEnum.DOING);
        musicSheet.setUpdateBy(sysUser.getId());
        musicSheet.setUpdateTime(new Date());
        if ( musicSheetService.saveMusicSheet(musicSheet,sysUser.getId())){
            return succeed("修改成功");
        } else {
            return failed("修改失败");
        }
    }

    @ApiOperation(value = "曲目分页查询", httpMethod="POST", consumes="application/json", produces="application/json")
    @PostMapping(value="/list", consumes="application/json", produces="application/json")
    public HttpResponseResult<PageInfo<MusicSheetVo>> list(@RequestBody StudentMusicSheetSearch query) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }
        // 老师课查看所有曲目
        if (query.getMyself() == null || query.getMyself()) {
            // 设置只查老师用户 状态为启用
            query.setCreateBy(sysUser.getId());
            if (query.getAuditStatus() != null && AuthStatusEnum.PASS.getCode().equals(query.getAuditStatus().getCode())) {
                query.setState(YesOrNoEnum.YES);
            }
            query.setSourceType(SourceTypeEnum.TEACHER);
        } else {
            // 检查app版本
            query.setAuditVersion(appVersionInfoService.getAppAuditVersion(query.getPlatform(),query.getVersion()));

            //  只能看通过审核 并且 启用的 曲目
            query.setState(YesOrNoEnum.YES);
            query.setAuditStatus(AuthStatusEnum.PASS);
            query.setStudentId(sysUser.getId());
        }

        IPage<MusicSheetVo> musicSheetVoIPage = musicSheetService.selectPage(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(musicSheetVoIPage));
    }


    /**
     * 我的单曲，收藏单曲
     */
    @GetMapping("/my")
    @ApiOperation(value = "我的单曲")
    public HttpResponseResult<PageInfo<MusicSheetVo>> myMusic(QueryInfo query) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }
        StudentMusicSheetSearch search = new StudentMusicSheetSearch();
        search.setStudentId(sysUser.getId());
        search.setState(YesOrNoEnum.YES);
        search.setAuditStatus(AuthStatusEnum.PASS);
        search.setUserType(ClientEnum.TEACHER);
        IPage<MusicSheetVo> sheetVoIPage = musicSheetService.myMusic(PageUtil.getPage(query),search);
        return succeed(PageUtil.pageInfo(sheetVoIPage));
    }

    @ApiOperation(value = "乐谱分享")
    @GetMapping(value="/share")
    public HttpResponseResult<MusicSheetShareVo> shareMusicSheet() {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }
        return succeed(musicSheetService.shareMusicSheet(sysUser));
    }


    @ApiOperation(value = "我的乐谱数量")
    @GetMapping(value="/count")
    public HttpResponseResult<Integer> countTeacherMusicSheet() {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }
        return succeed(musicSheetService.countTeacherMusicSheet(sysUser));
    }



}
