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

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.EmployeeDao;
import com.ym.mec.biz.dal.dao.TeacherDao;
import com.ym.mec.biz.dal.entity.Employee;
import com.ym.mec.biz.dal.entity.School;
import com.ym.mec.biz.dal.entity.Teacher;
import com.ym.mec.biz.dal.page.SchoolQueryInfo;
import com.ym.mec.biz.service.SchoolService;
import com.ym.mec.common.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@RequestMapping("eduSchool")
@Api(tags = "学校(教学点)服务")
@RestController
public class EduSchoolController extends BaseController {

    @Autowired
    private SchoolService schoolService;
    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private SysUserFeignService sysUserFeignService;

    @ApiOperation(value = "获取教师vip课教学点")
    @GetMapping("/findVipSchoolByTeacher")
    public Object findVipSchoolByTeacher(@ApiParam(value = "教师编号", required = true) Integer userId,String search){
        Teacher teacher = teacherDao.get(userId);
        if (teacher == null) {
            return failed("用户信息获取失败");
        }
        String organId = new String();
        if(Objects.nonNull(teacher.getTeacherOrganId())){
            organId = teacher.getTeacherOrganId().toString();
        }
        if(StringUtils.isEmpty(teacher.getFlowOrganRange())){
            organId += "," + teacher.getFlowOrganRange();
        }
        return succeed(schoolService.findVipSchoolByTeacher(userId,organId,null,search));
    }

    @ApiOperation(value = "获取教师vip课教学点")
    @GetMapping("/findVipSchoolByTeacher2")
    public Object findVipSchoolByTeacher2(Integer userId,Integer isDefault,String search){
        Teacher teacher = null;
        if(userId != null){
            teacher = teacherDao.get(userId);
        }else {
            SysUser sysUser = sysUserFeignService.queryUserInfo();
            if(sysUser == null){
                return failed(HttpStatus.FORBIDDEN,"请登录");
            }
            teacher = teacherDao.get(sysUser.getId());
        }
        if (teacher == null) {
            return failed("用户信息获取失败");
        }
        String organIdList = teacher.getTeacherOrganId().toString();
        if (StringUtils.isNoneBlank(teacher.getFlowOrganRange())) {
            organIdList += "," + teacher.getFlowOrganRange();
        }
        return succeed(schoolService.findVipSchoolByTeacher(teacher.getId(),organIdList,isDefault,search));
    }
}
