package com.ym.mec.web.controller.education;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.dto.MusicScoreQueryInfo;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.EmployeeDao;
import com.ym.mec.biz.dal.entity.Employee;
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.SysMusicScoreCategoriesService;
import com.ym.mec.biz.service.TenantConfigService;
import com.ym.mec.common.controller.BaseController;

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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RequestMapping("eduMusicScore")
@Api(tags = "智能陪练")
@RestController
public class EduMusicScoreController extends BaseController {
    @Autowired
    private SysMusicScoreCategoriesService sysMusicScoreCategoriesService;
    @Autowired
    private SysMusicScoreAccompanimentService sysMusicScoreAccompanimentService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private EmployeeDao employeeDao;
    
    @Autowired
    private TenantConfigService tenantConfigService;

    @ApiOperation(value = "树状列表")
    @GetMapping("/queryTree")
    public Object queryTree(MusicScoreQueryInfo queryInfo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        
        TenantConfig tenantConfig = tenantConfigService.getOne(new QueryWrapper<TenantConfig>().eq("tenant_id_", sysUser.getTenantId()));
        
        if(tenantConfig == null){
        	return failed("当前用户所在机构信息查询失败");
        }
        
        return succeed(sysMusicScoreCategoriesService.queryCategoriesTreeList(tenantConfig.getTeachingMaterialId(), queryInfo.getEnable()));
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/queryPage")
    public Object queryPage(SysExamSongQueryInfo queryInfo) {
        queryInfo.setShowFlag(1);
        return succeed(sysMusicScoreAccompanimentService.queryAll(queryInfo));
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/queryPageLimit")
    public Object queryPageLimit(SysExamSongQueryInfo queryInfo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        String type = queryInfo.getType();
        if(StringUtils.isEmpty(type)){
            queryInfo.setType("ALL");
        }
        if(queryInfo.getClientType() == null){
            queryInfo.setClientType(ClientTypeEnum.SMART_PRACTICE);
        }
        queryInfo.setShowFlag(1);
        Employee employee = employeeDao.get(sysUser.getId());
        if (org.apache.commons.lang3.StringUtils.isEmpty(queryInfo.getOrganId())) {
            queryInfo.setOrganId(employee.getOrganIdList());
        }else if(org.apache.commons.lang3.StringUtils.isEmpty(employee.getOrganIdList())){
            return failed("用户所在分部异常");
        }else {
            List<String> list = Arrays.asList(employee.getOrganIdList().split(","));
            if(!list.containsAll(Arrays.asList(queryInfo.getOrganId().split(",")))){
                return failed("非法请求");
            }
        }
        queryInfo.setEnable(true);
        return succeed(sysMusicScoreAccompanimentService.queryScorePage(queryInfo));
    }

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