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.auth.api.enums.SysUserType;
import com.yonge.cooleshow.biz.dal.dto.MusicSheetDto;
import com.yonge.cooleshow.biz.dal.dto.req.TeacherMusicSheetAuditReq;
import com.yonge.cooleshow.biz.dal.dto.search.MusicSheetSearch;
import com.yonge.cooleshow.biz.dal.dto.search.StudentMusicSheetOrderSearch;
import com.yonge.cooleshow.biz.dal.dto.search.TeacherMusicSheetAuditSearch;
import com.yonge.cooleshow.biz.dal.dto.search.TeacherMusicSheetSearch;
import com.yonge.cooleshow.biz.dal.entity.MusicSheet;
import com.yonge.cooleshow.biz.dal.entity.MusicSheetAccompaniment;
import com.yonge.cooleshow.biz.dal.enums.*;
import com.yonge.cooleshow.biz.dal.service.MusicSheetService;
import com.yonge.cooleshow.biz.dal.service.MusicTagService;
import com.yonge.cooleshow.biz.dal.support.PageUtil;
import com.yonge.cooleshow.biz.dal.vo.*;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.mybatis.page.PageInfo;
import com.yonge.toolset.utils.string.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

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

/**
 * 曲谱表 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 MusicTagService musicTagService;

    /**
     * 查询单条
     */
    @GetMapping("/detail/{id}")
    @ApiOperation(value = "详情", notes = "传入id")
    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, SysUserType.SYSTEM));
    }

	@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 (StringUtil.isEmpty(musicSheetDto.getMusicSubject())) {
            return failed("曲目声部不能为空");
        }

        String musicTag = musicSheetDto.getMusicTag();
        List<Long> musicTagList = StringUtil.toLongList(musicTag);

        //  一级曲谱标签只能选一个二级
        if (!musicTagService.checkParentDifferent(musicTagList)) {
            return failed("每个一级标签只能选一个二级标签");
        }

        musicSheetDto.setUserId(sysUser.getId());
        musicSheetDto.setAuditStatus(AuthStatusEnum.PASS);
        musicSheetDto.setDelFlag(false);
        musicSheetDto.setSourceType(SourceTypeEnum.PLATFORM);

        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("/delete/{id}")
    public Object delete(@PathVariable Long id) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }

        boolean result = musicSheetService.del(id);
        if (result){
            return succeed("删除成功");
        } else {
            return failed("删除失败");
        }
    }

    @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");
        }
        MusicSheet service = musicSheetService.getById(musicSheet.getId());
        if (YesOrNoEnum.YES.getCode().equals(service.getState().getCode())) {
            return failed("启用状态不许修改");
        }

        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 MusicSheetSearch query) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }

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


    /**
     * 启用/停用
     */
    @PostMapping("/state/{id}")
    @ApiOperation(value = "启用/停用", notes = "传入id")
    public HttpResponseResult<Boolean> state(@ApiParam(value = "曲目编号", required = true)  @PathVariable Long id) {
        if (StringUtil.isEmpty(id)) {
            return failed("参数不能为空");
        }
        return status(musicSheetService.state(id));
    }

    /**
     * 学生详情-乐谱
     */
    @PostMapping(value = "/student", consumes="application/json", produces="application/json")
    @ApiOperation(value = "学生详情-乐谱", httpMethod="POST", consumes="application/json", produces="application/json")
    public HttpResponseResult<PageInfo<StudentMusicSheetOrderVo>> student(@Valid @RequestBody StudentMusicSheetOrderSearch query) {
        return succeed(PageUtil.pageInfo(musicSheetService.selectStudentOrderPage(PageUtil.getPage(query),query)));
    }


    /**
     * 老师详情-乐谱
     */
    @PostMapping(value = "/teacher", consumes="application/json", produces="application/json")
    @ApiOperation(value = "老师详情-乐谱", httpMethod="POST", consumes="application/json", produces="application/json")
    public HttpResponseResult<PageInfo<TeacherMusicSheetVo>> teacher(@Valid @RequestBody TeacherMusicSheetSearch query) {
        return succeed(PageUtil.pageInfo(musicSheetService.selectTeacherPage(PageUtil.getPage(query),query)));
    }

    /**
     * 审核中心-乐谱审核列表
     */
    @PostMapping(value = "/audit/list", consumes="application/json", produces="application/json")
    @ApiOperation(value = "审核中心-乐谱审核列表", httpMethod="POST", consumes="application/json", produces="application/json")
    public HttpResponseResult<PageInfo<TeacherMusicSheetVo>> audit(@RequestBody TeacherMusicSheetAuditSearch query) {
        return succeed(PageUtil.pageInfo(musicSheetService.selectAuditPage(PageUtil.getPage(query),query)));
    }

    /**
     * 审核中心-乐谱审核
     */
    @PostMapping(value = "/audit", consumes="application/json", produces="application/json")
    @ApiOperation(value = "审核中心-乐谱审核", httpMethod="POST", consumes="application/json", produces="application/json")
    public HttpResponseResult<Boolean> audit(@Valid @RequestBody TeacherMusicSheetAuditReq param) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }
        return status(musicSheetService.audit(param,sysUser.getId()));
    }




    @PostMapping(value = "/buySendMessage", consumes="application/json", produces="application/json")
    @ApiOperation(value = "发送学生购买老师曲目信息", httpMethod="POST", consumes="application/json", produces="application/json")
    public HttpResponseResult<Boolean> buySendMessage(@Valid @RequestBody MusicSheetStudentByMessage param) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }
        return status(musicSheetService.sendBuyMessage(param.getTeacherId(),param.getStudentId(),param.getMusicSheetId()));
    }
}
