package com.yonge.cooleshow.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.dayaedu.cbs.openfeign.wrapper.music.CbsMusicSheetApplicationExtendClientWrapper;
import com.microsvc.toolkit.common.response.template.R;
import com.yonge.cooleshow.biz.dal.dto.AdjustModel;
import com.yonge.cooleshow.biz.dal.dto.ReasonDto;
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.TeacherMusicSheetSearch;
import com.yonge.cooleshow.biz.dal.entity.MusicSheet;
import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import com.yonge.cooleshow.biz.dal.enums.OrderTypeEnum;
import com.yonge.cooleshow.biz.dal.service.MusicSheetService;
import com.yonge.cooleshow.biz.dal.service.SysUserService;
import com.yonge.cooleshow.biz.dal.service.cbs.CbsMusicScoreService;
import com.yonge.cooleshow.biz.dal.vo.*;
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.util.StringUtil;
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.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("${app-config.url.admin:}/music/sheet")
@Api(tags = "曲谱表 API接口")
public class MusicSheetController extends BaseController {
    @Resource
    private SysUserService sysUserService;
	@Resource
	private MusicSheetService musicSheetService;
	@Resource
	private CbsMusicScoreService cbsMusicScoreService;

    @GetMapping("/detail/{id}")
    @ApiOperation(value = "详情", notes = "传入id")
    @PreAuthorize("@pcs.hasPermissions('music/sheet/detail')")
    public HttpResponseResult<MusicSheetDetailVo> detail(@ApiParam(value = "曲谱编号", required = true) @PathVariable("id") Long id) {
        return succeed(musicSheetService.detail(id, sysUserService.getUser(), ClientEnum.SYSTEM));
    }

    @ApiOperation(value = "分页查询", httpMethod="POST", consumes="application/json", produces="application/json")
    @PostMapping(value="/list", consumes="application/json", produces="application/json")
    @PreAuthorize("@pcs.hasPermissions('music/sheet/list')")
    public HttpResponseResult<PageInfo<MusicSheetVo>> list(@RequestBody MusicSheetSearch query) {
        return succeed(PageUtil.pageInfo(musicSheetService.selectCbsPage(PageUtil.getPage(query), query)));
    }

    @ApiOperation(value = "修改应用扩展信息")
    @PreAuthorize("@pcs.hasPermissions('music/sheet/update', {'BACKEND'})")
    @PostMapping("/update")
    public R<JSONObject> update(@RequestBody CbsMusicSheetApplicationExtendClientWrapper.AddMusicSheetApplicationExtend addMusicSheetApplicationExtend) {
        cbsMusicScoreService.update(addMusicSheetApplicationExtend);
        return R.defaultR();
    }

    @PostMapping("/state/{id}")
    @ApiOperation(value = "启用/停用", notes = "传入id")
    @PreAuthorize("@pcs.hasPermissions('music/sheet/state')")
    public HttpResponseResult<Boolean> state(@ApiParam(value = "曲目编号", required = true)  @PathVariable Long id, @RequestBody ReasonDto reasonDto) {
        if (StringUtil.isEmpty(id)) {
            return failed("参数不能为空");
        }
        return status(musicSheetService.state(id,reasonDto.getReason(),ClientEnum.SYSTEM));
    }


    /**
     * 批量启用/停用
     */
    @PostMapping("/stateList")
    @ApiOperation(value = "批量启用/停用")
    @PreAuthorize("@pcs.hasPermissions('music/sheet/stateList')")
    public HttpResponseResult<Boolean> stateList(@RequestBody ReasonDto reasonDto) {
        if (StringUtil.isEmpty(reasonDto.getMusicSheetIds())) {
            return failed("参数不能为空");
        }
        if (reasonDto.getState() == null) {
            return failed("启用/禁用状态不能为空");
        }
        if (reasonDto.getState().equals(YesOrNoEnum.NO) && StringUtil.isEmpty(reasonDto.getReason())) {
            return failed("停用原因不能为空");
        }
        return status(musicSheetService.stateList(reasonDto));
    }

    @PostMapping(value = "/student")
    @ApiOperation(value = "学生详情-乐谱")
    @PreAuthorize("@pcs.hasPermissions('music/sheet/student')")
    public HttpResponseResult<PageInfo<StudentMusicSheetOrderVo>> student(@Valid @RequestBody StudentMusicSheetOrderSearch query) {
        return succeed(PageUtil.pageInfo(musicSheetService.selectStudentOrderPage(PageUtil.getPage(query),query)));
    }

    @PostMapping(value = "/teacher")
    @ApiOperation(value = "老师详情-乐谱")
    @PreAuthorize("@pcs.hasPermissions('music/sheet/teacher')")
    public HttpResponseResult<PageInfo<TeacherMusicSheetVo>> teacher(@Valid @RequestBody TeacherMusicSheetSearch query) {
        return succeed(PageUtil.pageInfo(musicSheetService.selectTeacherPage(PageUtil.getPage(query),query)));
    }

    @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) {
        return status(musicSheetService.sendBuyMessage(param.getTeacherId(), param.getStudentId(), param.getMusicSheetId(),
                                                       ClientEnum.STUDENT, OrderTypeEnum.MUSIC));
    }


    /**
     * 置顶
     */
    @PostMapping("/top/{id}")
    @ApiOperation(value = "置顶曲目", notes = "传入id")
    @PreAuthorize("@pcs.hasPermissions('music/sheet/top')")
    public HttpResponseResult<Boolean> top(@ApiParam(value = "曲目编号", required = true)  @PathVariable Long id) {
        if (StringUtil.isEmpty(id)) {
            return failed("参数不能为空");
        }
        return status(musicSheetService.top(id));
    }


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

        MusicSheet musicSheet = new MusicSheet();
        musicSheet.setId(model.getId());
        musicSheet.setSortNumber(model.getSort());
        musicSheet.setExquisiteFlag(model.getExquisiteFlag());

        musicSheetService.updateById(musicSheet);

        return succeed();
    }
}
