package com.ym.mec.teacher.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.entity.SysMusicScoreAccompaniment;
import com.ym.mec.biz.dal.entity.Teacher;
import com.ym.mec.biz.dal.entity.TenantConfig;
import com.ym.mec.biz.dal.enums.ClientTypeEnum;
import com.ym.mec.biz.dal.page.SysExamSongQueryInfo;
import com.ym.mec.biz.service.SysMusicScoreAccompanimentService;
import com.ym.mec.biz.service.TeacherService;
import com.ym.mec.biz.service.TenantConfigService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.page.PageInfo;

@RequestMapping("sysMusicScoreAccompaniment")
@Api(tags = "曲库伴奏服务")
@RestController
public class SysMusicScoreAccompanimentController extends BaseController {

    @Autowired
    private SysMusicScoreAccompanimentService sysMusicScoreAccompanimentService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private TeacherService teacherService;
    
    @Autowired
    private TenantConfigService tenantConfigService;

    @ApiOperation(value = "修改")
    @PostMapping("/update")
    public Object update(@RequestBody SysMusicScoreAccompaniment sysMusicScoreAccompaniment) {
        sysMusicScoreAccompanimentService.updateAcc(sysMusicScoreAccompaniment);
        return succeed();
    }

    @ApiOperation(value = "删除")
    @PostMapping("/del")
    public Object del(Integer id) {
        sysMusicScoreAccompanimentService.delete(id);
        return succeed();
    }

    @ApiOperation(value = "获取伴奏声部列表")
    @GetMapping("/querySubjectIds")
    public Object querySubjectIds(Integer categoriesId) {
        return succeed(sysMusicScoreAccompanimentService.querySubjectIds(categoriesId));
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/queryPage")
    public Object queryPage(SysExamSongQueryInfo queryInfo) {
        String type = queryInfo.getType();
        if(StringUtils.isEmpty(type)){
            queryInfo.setType("ALL");
        }
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null){
            throw new BizException("请登录");
        }
        if(queryInfo.getClientType() == null){
            queryInfo.setClientType(ClientTypeEnum.NETWORK_ROOM);
        }
        queryInfo.setCreateUserId(sysUser.getId());
        queryInfo.setShowFlag(1);
        return succeed(sysMusicScoreAccompanimentService.queryAccPage(queryInfo));
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/queryPageLimit")
    public Object queryPageLimit(SysExamSongQueryInfo queryInfo) {
        String type = queryInfo.getType();
        if(StringUtils.isEmpty(type)){
            queryInfo.setType("ALL");
        }
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null){
            throw new BizException("请登录");
        }
        Teacher teacher = teacherService.get(sysUser.getId());
        if (org.apache.commons.lang3.StringUtils.isEmpty(queryInfo.getOrganId())) {
            queryInfo.setOrganId(teacher.getTeacherOrganId() + "");
        }
        if(org.apache.commons.lang3.StringUtils.isNotBlank(teacher.getFlowOrganRange())){
            queryInfo.setOrganId(queryInfo.getOrganId() + "," + teacher.getFlowOrganRange());
        }
        if(queryInfo.getClientType() == null){
            queryInfo.setClientType(ClientTypeEnum.SMART_PRACTICE);
        }
        queryInfo.setCreateUserId(sysUser.getId());
        queryInfo.setShowFlag(1);
        queryInfo.setEnable(true);
        
        TenantConfig tenantConfig = tenantConfigService.getOne(new QueryWrapper<TenantConfig>().eq("tenant_id_", sysUser.getTenantId()));
        
        queryInfo.setCategoriesIds(tenantConfig.getTeachingMaterialId());
        
        //过滤合奏和考级
        queryInfo.setFilterCategoriesIds("40,41,42");
        
        PageInfo<SysMusicScoreAccompaniment> sysMusicScoreAccompanimentList = sysMusicScoreAccompanimentService.queryScorePage(queryInfo);
       
        String url = null,metronomeUrl = null;
        for(SysMusicScoreAccompaniment smsa : sysMusicScoreAccompanimentList.getRows()){
        	
        	url = smsa.getUrl();
        	metronomeUrl = smsa.getMetronomeUrl();
        	
        	smsa.setUrl(smsa.getMetronomeMp3Url());
        	smsa.setMetronomeUrl(smsa.getMp3Url());
        	smsa.setMp3Url(metronomeUrl);
        	smsa.setMetronomeMp3Url(url);
        }
        
        return succeed(sysMusicScoreAccompanimentList);
    }
}
