package cn.hmsoft.skill.school.control;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.hmsoft.application.web.Ajax;
import cn.hmsoft.application.web.AjaxControl;
import cn.hmsoft.application.web.BrowserAgent;
import cn.hmsoft.frame.data.model.FrameOptr;
import cn.hmsoft.frame.exception.BusinessException;
import cn.hmsoft.frame.util.FrameAgentUtil;
import cn.hmsoft.frame.util.FrameAssertUtil;
import cn.hmsoft.frame.util.FrameRandomCodeUtil;
import cn.hmsoft.helper.StringHelper;
import cn.hmsoft.skill.data.dao.school.SchoolRegDao;
import cn.hmsoft.skill.data.model.school.SchoolReg;
import cn.hmsoft.skill.helper.SkillParamHelper;
import cn.hmsoft.skill.school.service.SchoolLoginService;

@RestController
public class SchoolLoginControl  extends AjaxControl {
	@Autowired
	private SchoolLoginService sSchool;
	@Autowired
	private SchoolRegDao daoSchool;
	
	@RequestMapping("/school/school/login")
	public Ajax login(String school_code, String login_pass, String valid_code) {
		FrameAssertUtil.checkParam(school_code, login_pass);

		BrowserAgent browser = FrameAgentUtil.getBrowserAgent(this.getRequest());

		// 判断是否开启验登录验证
		if (SkillParamHelper.SchoolLoginWithVaildCode) {
			// 为了后续注册，这里不要清除验证码信息
			boolean flag = FrameRandomCodeUtil.validRandomCode(this.getRequest(), valid_code, false);
			if (!flag) {
				throw new BusinessException("请求验证失败，请重试！");
			}
		}
		
		//判断是否系统管理员  admin
		if(SkillParamHelper.ADMIN.equals(school_code)) {
			FrameOptr optr = this.daoSchool.findBySql(FrameOptr.class, "select * from frame_optr where org_id=0");
			this.getSession().setAttribute(SkillParamHelper.SessionName_FrameOptr, optr);
			return new Ajax();
		}else {
			SchoolReg school = this.sSchool.login(school_code, login_pass, browser);
			if (school != null) {
				this.getSession().setAttribute(SkillParamHelper.SessionName_SchoolReg, school);
				/*FrameOptr optr = BeanHelper.copy(FrameOptr.class, SkillParamHelper.GobalSchoolOptr);
				// optr.setResMap(SkillParamHelper.GobalSchoolOptr.getResMap());
				optr.setOptr_name(StringHelper.isEmpty(school.getSchool_short_name()) ? school.getSchool_name() : school.getSchool_short_name());
				*/
				FrameOptr optr = this.daoSchool.findBySql(FrameOptr.class, "select * from frame_optr where org_id=?", school.getSchool_id());
				if (optr == null) {
					throw new BusinessException("找不到学校代码[" + school.getSchool_id() + "]对应的操作员FrameOptr!");
				}
				//optr.setHome_page(SkillParamHelper.GobalSchoolRole.getHome_page());   TODO...
				if (StringHelper.isNotEmpty(school.getSchool_short_img()))
					optr.setOptr_image("./assets/img/school/" + school.getSchool_short_img());
				
				this.getSession().setAttribute(SkillParamHelper.SessionName_FrameOptr, optr);
				// this.getSession().setAttribute(SkillParamHelper.SessionName_FrameOptr, );
			}
			return new Ajax(school);
		}

	}

	@RequestMapping("/school/school/logout")
	public Ajax logout() {
		this.getSession().invalidate();
		return new Ajax();
	}
}
