package com.controller;

import com.config.BaseController;
import com.config.ControllerInterface;
import com.jfinal.captcha.CaptchaRender;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.model.User;
import com.service.OrgService;
import com.service.UserService;
import com.tools.JwtUtils;
import com.utils.CommonUtils;

import java.util.HashMap;
import java.util.Map;

@ControllerInterface(path ="/moniyonghu")
public class MoniyonghuController extends BaseController {
    private static OrgService orgService = OrgService.me;
    private static UserService userService = UserService.me;
    public void index(){
        render("/view/userrole/moniyonghu.html");
    }
    public void moni(){
        String loginname = this.getPara("loginname");
        String type = this.getPara("type");
        User user = userService.findByNamePassword(loginname,type);
        if(user == null) {
            success(1,"用户名密码错误！");
            return;
        } else if(user.getStatus() == 0){
            success(1,"该用户已注销！");
            return;
        } else if(user.getStatus() == 2){
            success(1,"该用户已停用！");
            return;
        } else if (user.getInt("orgstatus") == 2){
            success(1,"该用户审核不通过！");
            return;
        } else if (user.getInt("orgstatus") == 0){
            success(1,"该用户未审核！");
            return;
        }

        user.setPassword(null);
        //登录成功，生成jwt的token
        Map info = new HashMap();
        info.put("orgid",user.getOrgid());
        info.put("orgname",user.get("orgname"));
        info.put("userid",user.get("id"));
        String token = JwtUtils.createJWT(user.getId()+"",info,1000*60*500);
        user.remove("password").put("token",token);
        user.put("view_path",VIEW_PATH);
        String schoolids = orgService.getSchoolForSql(user.getId());
        this.setSessionAttr("schoolids", schoolids);
        this.setSessionAttr("userInfo",user);
        user.setAddress(PropKit.get("channelType"));
        //加载当前机构下面的学校
        success(user);
    }
}
