package com.ym.mec.web.controller;

import com.ym.mec.biz.dal.dto.MusicScoreDto;
import com.ym.mec.biz.dal.entity.SysMusicScore;
import com.ym.mec.biz.dal.entity.SysMusicScoreCategories;
import com.ym.mec.biz.dal.enums.ClientTypeEnum;
import com.ym.mec.biz.dal.page.SysExamSongQueryInfo;
import com.ym.mec.biz.service.OrganizationService;
import com.ym.mec.biz.service.SysMusicScoreCategoriesService;
import com.ym.mec.biz.service.SysMusicScoreService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.exception.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("sysMusicScore")
@Api(tags = "曲库")
@RestController
public class SysMusicScoreController extends BaseController {

    @Autowired
    private SysMusicScoreService sysMusicScoreService;

    @ApiOperation(value = "修改")
    @PostMapping("/update")
    @PreAuthorize("@pcs.hasPermissions('sysMusicScore/update')")
    public Object update(@RequestBody MusicScoreDto musicScoreDto) {
        sysMusicScoreService.updateMusicScore(musicScoreDto);
        return succeed();
    }

    @ApiOperation(value = "启用/停用")
    @PostMapping("/show")
    @PreAuthorize("@pcs.hasPermissions('sysMusicScore/show')")
    public Object update(Integer sysMusicScoreId,Integer showFlag) {
        SysMusicScore sysMusicScore = sysMusicScoreService.get(sysMusicScoreId);
        if(sysMusicScore == null){
            throw new BizException("曲目信息不存在");
        }
        sysMusicScore.setShowFlag(showFlag);
        sysMusicScoreService.update(sysMusicScore);
        return succeed();
    }

    @ApiOperation(value = "新增")
    @PostMapping("/add")
    @PreAuthorize("@pcs.hasPermissions('sysMusicScore/add')")
    public Object add(@RequestBody MusicScoreDto musicScoreDto) {
        sysMusicScoreService.add(musicScoreDto);
        return succeed();
    }

    @ApiOperation(value = "删除")
    @PostMapping("/del")
    @PreAuthorize("@pcs.hasPermissions('sysMusicScore/del')")
    public Object del(Integer id) {
        sysMusicScoreService.del(id);
        return succeed();
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/queryPage")
    @PreAuthorize("@pcs.hasPermissions('sysMusicScore/queryPage')")
    public Object queryPage(SysExamSongQueryInfo queryInfo) {
        return succeed(sysMusicScoreService.queryMusicScorePage(queryInfo));
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/queryPage2")
    public Object queryPage2(SysExamSongQueryInfo queryInfo) {
        queryInfo.setType("COMMON");
        queryInfo.setShowFlag(1);
        queryInfo.setClientType(ClientTypeEnum.SMART_PRACTICE);
        return succeed(sysMusicScoreService.queryMusicScorePageInfo(queryInfo));
    }
    
    private void getAllCategoryIdList(List<Integer> categoryIdList, List<SysMusicScoreCategories> sysMusicScoreCategoriesList){
    	
    	if(sysMusicScoreCategoriesList != null && sysMusicScoreCategoriesList.size() > 0){
    		for(SysMusicScoreCategories sysMusicScoreCategories : sysMusicScoreCategoriesList){
    			categoryIdList.add(sysMusicScoreCategories.getId());
    			getAllCategoryIdList(categoryIdList, sysMusicScoreCategories.getSysMusicScoreCategoriesList());
    		}
    	}
    }

    @ApiOperation(value = "转码")
    @PostMapping("/transcod")
    @PreAuthorize("@pcs.hasPermissions('sysMusicScore/transcod')")
    public Object transcod(Integer sysMusicScoreId) {
        sysMusicScoreService.transcod(sysMusicScoreId);
        return succeed();
    }
}
