package com.ym.mec.web.controller;

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.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

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

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

    @ApiOperation(value = "新增学校")
    @PostMapping("/add")
    @PreAuthorize("@pcs.hasPermissions('school/add')")
    public Object add(School school){
        schoolService.insert(school);
        return succeed();
    }

    @ApiOperation(value = "删除学校")
    @PostMapping("/del/{id}")
    @PreAuthorize("@pcs.hasPermissions('school/del')")
    public Object del(@ApiParam(value = "学校编号", required = true) @PathVariable("id") Integer id){
        schoolService.delete(id);
        return succeed();
    }

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

    @ApiOperation(value = "根据学校编号查询学校")
    @GetMapping("/get/{id}")
    @PreAuthorize("@pcs.hasPermissions('school/get')")
    public Object get(@ApiParam(value = "学校编号", required = true) @PathVariable("id") Integer id){
        return succeed(schoolService.get(id));
    }

    @ApiOperation(value = "分页查询学校列表")
    @GetMapping("/queryPage")
    @PreAuthorize("@pcs.hasPermissions('school/queryPage')")
    public Object queryPage(SchoolQueryInfo queryInfo){
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			return failed("用户信息获取失败");
		}
		if(!sysUser.getIsSuperAdmin()){
			Employee employee = employeeDao.get(sysUser.getId());
			if (StringUtils.isEmpty(queryInfo.getOrganId())) {
				queryInfo.setOrganId(employee.getOrganIdList());
			}else if(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("非法请求");
				}
			}
		}
        return succeed(schoolService.queryPageDetail(queryInfo));
    }

    @ApiOperation(value = "根据机构编号获取学校列表")
    @GetMapping("/queryByOrganId")
    @PreAuthorize("@pcs.hasPermissions('school/queryByOrganId')")
    public Object queryByOrganId(String organId){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        if(!sysUser.getIsSuperAdmin()){
            Employee employee = employeeDao.get(sysUser.getId());
            if (StringUtils.isEmpty(organId)) {
                organId = employee.getOrganIdList();
            }else if(StringUtils.isEmpty(employee.getOrganIdList())){
                return failed("用户所在分部异常");
            }else {
                List<String> list = Arrays.asList(employee.getOrganIdList().split(","));
                if(!list.containsAll(Arrays.asList(organId.split(",")))){
                    return failed("非法请求");
                }
            }
        }
        return succeed(schoolService.queryByOrganId(organId));
    }

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