package cn.hmsoft.skill.school.service;

import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.hmsoft.application.web.BrowserAgent;
import cn.hmsoft.frame.constants.FrameStatus;
import cn.hmsoft.frame.util.FrameAssertUtil;
import cn.hmsoft.skill.data.dao.school.SchoolRegDao;
import cn.hmsoft.skill.data.model.school.SchoolLogin;
import cn.hmsoft.skill.data.model.school.SchoolReg;

@Service
public class SchoolLoginService {

	@Autowired
	private SchoolRegDao daoSchool;

	public SchoolReg login(String school_code, String login_pass,BrowserAgent browser) {
		SchoolReg school = this.daoSchool.find("school_code",school_code);
		FrameAssertUtil.isNotNull(school, "未找到学校信息，请重试！");
		FrameAssertUtil.isEqual(login_pass, school.getLogin_pass(), "您的登录密码错误，请重试！");
		FrameAssertUtil.isEqual(FrameStatus.Active.toString(), school.getSchool_status(), "您的账号尚未开通，请联系我公司对接人员！");
		// 记录登录日志
		if (school != null) {
			SchoolLogin log = new SchoolLogin();
			log.setBrowser_type(browser.getBrowserType());
			log.setBrowser_version(browser.getBrowserVersion());
			log.setLogin_addr(browser.getUserAddr());
			log.setLogin_time(LocalDateTime.now());
			log.setPlatform_type(browser.getPlatformType());
			log.setPlatform_series(browser.getPlatformSeries());
			log.setPlatform_version(browser.getPlatformVersion());
			log.setSchool_id(school.getSchool_id());
			this.daoSchool.insert(log);
			school.setLogin_pass(null);
			school.setSocket_param(null);
		}
		
		return school;
	}

}
