package com.yonge.cooleshow.admin.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.MusicAlbumSheetDto;
import com.yonge.cooleshow.biz.dal.dto.MusicAlbumSheetSortDto;
import com.yonge.cooleshow.biz.dal.dto.AdjustModel;
import com.yonge.cooleshow.biz.dal.dto.search.MusicAlbumDetailSearch;
import com.yonge.cooleshow.biz.dal.dto.search.MusicAlbumSearch;
import com.yonge.cooleshow.biz.dal.entity.MusicAlbum;
import com.yonge.cooleshow.biz.dal.entity.MusicSheet;
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 com.yonge.toolset.base.util.StringUtil;
import io.swagger.annotations.*;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
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/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="/create", consumes="application/json", produces="application/json")
    @PreAuthorize("@pcs.hasPermissions('music/album/create')")
    public HttpResponseResult<Object> create(@Valid @RequestBody MusicAlbum musicAlbum) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }

        musicAlbum.setCreateBy(sysUser.getId());
        musicAlbum.setCreateTime(new Date());
        musicAlbum.setUpdateBy(sysUser.getId());
        musicAlbum.setUpdateTime(new Date());
        musicAlbum.setAlbumStatus(YesOrNoEnum.NO);
        return succeed(musicAlbumService.createMusicAlbum(musicAlbum));
    }

    @ApiOperation(value = "删除", httpMethod="POST", consumes="application/json", produces="application/json")
    @PostMapping("/delete/{id}")
    @PreAuthorize("@pcs.hasPermissions('music/album/delete')")
    public Object delete(@PathVariable Long id) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }

        return succeed(musicAlbumService.del(id));
    }

    @ApiOperation(value = "修改", httpMethod="POST", consumes="application/json", produces="application/json")
    @PostMapping(value="/update", consumes="application/json", produces="application/json")
    @PreAuthorize("@pcs.hasPermissions('music/album/update')")
    public HttpResponseResult<Object> update(@Valid @RequestBody MusicAlbum musicAlbum) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }
        if (musicAlbum.getId()==null){
            return failed("缺少ID");
        }
        MusicAlbum service = musicAlbumService.getById(musicAlbum.getId());
        if (YesOrNoEnum.YES.getCode().equals(service.getAlbumStatus().getCode())) {
            return failed("启用状态不许修改");
        }
        musicAlbum.setUpdateBy(sysUser.getId());
        musicAlbum.setUpdateTime(new Date());
        return succeed(musicAlbumService.updateMusicAlbum(musicAlbum));
    }

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

    /**
     * 启用/停用
     */
    @PostMapping("/state/{id}")
    @ApiOperation(value = "启用/停用", notes = "传入id")
    @PreAuthorize("@pcs.hasPermissions('music/album/state')")
    public HttpResponseResult<Boolean> state(@ApiParam(value = "专辑编号", required = true)  @PathVariable Long id) {
        if (StringUtil.isEmpty(id)) {
            return failed("参数不能为空");
        }
        return status(musicAlbumService.state(id));
    }


    /**
     * 专辑详情
     */
    @PostMapping(value = "/detail", consumes="application/json", produces="application/json")
    @ApiOperation(value = "专辑详情",  httpMethod="POST", consumes="application/json", produces="application/json")
    @PreAuthorize("@pcs.hasPermissions('music/album/detail')")
    public HttpResponseResult<AlbumDetailVo> detail(@Valid @RequestBody MusicAlbumDetailSearch query) {

        query.setType(2);
        AlbumDetailVo albumDetailVo = musicAlbumService.detail(PageUtil.getPage(query), query, null, null);
        return succeed(albumDetailVo);
    }


    /**
     * 专辑详情能新增的曲目列表
     */
    @PostMapping(value = "/detail/canAddMusicSheet", consumes="application/json", produces="application/json")
    @ApiOperation(value = "专辑详情能新增的曲目列表",  httpMethod="POST", consumes="application/json", produces="application/json")
    @PreAuthorize("@pcs.hasPermissions('music/album/detail/canAddMusicSheet')")
    public HttpResponseResult<AlbumDetailVo> detailAddMusicSheet(@Valid @RequestBody MusicAlbumDetailSearch query) {

        query.setType(1);
        query.setState(YesOrNoEnum.YES);
        AlbumDetailVo albumDetailVo = musicAlbumService.detail(PageUtil.getPage(query), query, null, null);
        return succeed(albumDetailVo);
    }


    /**
     * 专辑详情删除曲目列表
     */
    @PostMapping(value = "/detail/delMusicSheet" ,consumes="application/json", produces="application/json")
    @ApiOperation(value = "专辑详情删除曲目列表",  httpMethod="POST", consumes="application/json", produces="application/json")
    @PreAuthorize("@pcs.hasPermissions('music/album/detail/delMusicSheet')")
    public HttpResponseResult<Boolean> detailDelMusicSheet(@Valid @RequestBody MusicAlbumSheetDto query) {
        if (StringUtil.isEmpty(query.getAlbumId())) {
            return failed("专辑编号ID不能为空");
        }
        if (StringUtil.isEmpty(query.getMusicSheetIds())) {
            return failed("曲目编号不能为空");
        }
        return status(musicAlbumService.delMusicSheet(query.getAlbumId(),query.getMusicSheetIds()));
    }


    /**
     * 修改曲目的排序值
     */
    @PostMapping(value = "/detail/updateMusicSheet" ,consumes="application/json", produces="application/json")
    @ApiOperation(value = "专辑详情修改曲目排序",  httpMethod="POST", consumes="application/json", produces="application/json")
    @PreAuthorize("@pcs.hasPermissions('music/album/detail/updateMusicSheet')")
    public HttpResponseResult<Boolean> updateMusicSheet(@Valid @RequestBody MusicAlbumSheetSortDto query) {
        if (StringUtil.isEmpty(query.getAlbumId())) {
            return failed("专辑编号ID不能为空");
        }
        if (StringUtil.isEmpty(query.getMusicSheetId())) {
            return failed("曲目编号不能为空");
        }
        return succeed(musicAlbumService.updateMusicSheet(query));
    }



    /**
     * 专辑详情添加曲目列表
     */
    @PostMapping(value = "/detail/addMusicSheet",consumes="application/json", produces="application/json")
    @ApiOperation(value = "专辑详情添加曲目列表",  httpMethod="POST", consumes="application/json", produces="application/json")
    @PreAuthorize("@pcs.hasPermissions('music/album/detail/addMusicSheet')")
    public HttpResponseResult<Boolean> detailAddMusicSheet(@Valid @RequestBody MusicAlbumSheetDto query) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }
        if (StringUtil.isEmpty(query.getAlbumId())) {
            return failed("专辑编号ID不能为空");
        }
        if (CollectionUtils.isEmpty(query.getAlbumMusicList())) {
            return failed("曲目不能为空");
        }
        return status(musicAlbumService.addMusicSheet(query.getAlbumId(),query.getAlbumMusicList(),sysUser.getId()));
    }



    /**
     * 设置排序值 和加精
     */
    @PostMapping("/adjust")
    @ApiOperation(value = "专辑调整设置")
    public HttpResponseResult<Boolean> adjust(@RequestBody @Valid AdjustModel model) {



        MusicAlbum musicAlbum = new MusicAlbum();
        musicAlbum.setId(model.getId());
        musicAlbum.setSortNumber(model.getSort());

        musicAlbumService.updateById(musicAlbum);

        return succeed();
    }



}
