package com.ym.mec.teacher.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.TeacherDao;
import com.ym.mec.biz.dal.entity.School;
import com.ym.mec.biz.dal.entity.Teacher;
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.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @Author Joburgess
 * @Date 2019/11/4
 */

@RequestMapping("school")
@Api(tags = "教师教学点服务")
@RestController
public class TeacherSchoolController extends BaseController {

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

    @ApiOperation(value = "新增学校")
    @PostMapping("/add")
    public Object add(School school){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null){
            return failed(HttpStatus.FORBIDDEN,"请登录");
        }
        Teacher teacher = teacherDao.get(sysUser.getId());
        school.setUserId(sysUser.getId());
        school.setOrganId(teacher.getTeacherOrganId());
        schoolService.insert(school);
        return succeed();
    }

    @ApiOperation(value = "删除学校")
    @PostMapping("/del")
    public Object del(@ApiParam(value = "学校编号", required = true) Integer id){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null){
            return failed(HttpStatus.FORBIDDEN,"请登录");
        }
        schoolService.deleteByTeacherAndId(id,sysUser.getId());
        return succeed();
    }

    @ApiOperation(value = "修改学校")
    @PostMapping("/update")
    public Object update(School school){
        school.setUpdateTime(new Date());
        schoolService.update(school);
        return succeed();
    }

    @ApiOperation(value = "获取教师vip课教学点")
    @GetMapping("/findVipSchoolByTeacher")
    public Object findVipSchoolByTeacher(Integer isDefault){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null){
            return failed(HttpStatus.FORBIDDEN,"请登录");
        }
        Teacher teacher = teacherDao.get(sysUser.getId());
        if(teacher == null){
        	return failed("非法访问");
		}
		String organIdList = teacher.getTeacherOrganId().toString();
		if (StringUtils.isNoneBlank(teacher.getFlowOrganRange())) {
			organIdList = organIdList + "," + teacher.getFlowOrganRange();
		}
        return succeed(schoolService.findVipSchoolByTeacher(sysUser.getId(),organIdList));
    }

    @ApiOperation(value = "获取教师vip课教学点")
    @GetMapping("/findVipSchoolByTeacher2")
    public Object findVipSchoolByTeacher2(Integer isDefault){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null){
            return failed(HttpStatus.FORBIDDEN,"请登录");
        }
        Teacher 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(sysUser.getId(),organIdList,isDefault));
    }
}
