package com.yonge.cooleshow.website.controller.open;

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

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.entity.Student;
import com.yonge.cooleshow.biz.dal.entity.Subject;
import com.yonge.cooleshow.biz.dal.entity.Teacher;
import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import com.yonge.cooleshow.biz.dal.service.StudentService;
import com.yonge.cooleshow.biz.dal.service.SubjectService;
import com.yonge.cooleshow.biz.dal.service.TeacherService;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;

@RequestMapping("subject")
@Api(tags = "声部服务")
@RestController
public class SubjectController extends BaseController {

	@Autowired
	private SubjectService subjectService;
	
    @Autowired
    private SysUserFeignService sysUserFeignService;
    
    @Autowired
    private TeacherService teacherService;
    
    @Autowired
    private StudentService studentService;

	@ApiOperation(value = "根据声部编号查询声部")
	@GetMapping("/get/{id}")
	public Object get(@ApiParam(value = "声部编号", required = true) @PathVariable("id") Long id) {
		return succeed(subjectService.get(id));
	}

	@ApiOperation(value = "获取声部")
	@GetMapping("/subjectSelect")
    public HttpResponseResult<List<Subject>> subjectSelect(
			@ApiParam(value = "类型 PRACTICE 陪练课 LIVE 直播课 VIDEO 视频课 PINAO_ROOM 琴房 MUSIC 曲目 ") String type, @ApiParam(value = "客户端 TEACHER/STUDENT") String clientId){

		String userExtSubjectIds = null;
        
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser != null && sysUser.getId() != null) {
			if (StringUtils.equals(ClientEnum.STUDENT.getCode(), clientId)) {
				Student student = studentService.getById(sysUser.getId());
	        	if(student != null){
	        		userExtSubjectIds = student.getSubjectId();
	        	}
			} else if (StringUtils.equals(ClientEnum.TEACHER.getCode(), clientId)) {
				Teacher teacher = teacherService.getById(sysUser.getId());
	        	if(teacher != null){
	        		userExtSubjectIds = teacher.getSubjectId();
	        	}
			}
        }
        
		List<Subject> subjectSelect = subjectService.subjectSelect(type, userExtSubjectIds);
        return succeed(subjectSelect);
    }
}
