package com.service;

import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.model.User;
import com.utils.CommonUtils;

import java.util.*;

/**
 * 用户信息
 * 2018-10-18
 * zj
 */
public class UserService<psvn> {
    public static final UserService me = new UserService();
    private User dao = new User().dao();
    public User findById(int id){
        return dao.findById(id);
    }

    public User findByLoginName(String loginname){
        return me.dao.findFirst("select * from t_user where loginname=?",loginname);
    }
    public Page<User> paginate(int pageNumber,int pagesize ,User user ){
        String select ="select u.*,o.orgname";

        StringBuffer where =new StringBuffer(" from  t_user u left join t_org o on o.id=u.orgid  where 1=1 ");

        if (user.getCode()!=null && !user.getCode().equals("")) {
            where.append(" and code='").append(user.getCode()).append("' ");
        }
        if (user.getRealname()!=null && !user.getRealname().equals("")) {
            where.append(" and realname like'").append(user.getRealname()).append("%' ");
        }
        if (user.getLoginname()!=null && !user.getLoginname().equals("")) {
            where.append(" and loginname='").append(user.getLoginname()).append("' ");
        }
        if (user.getPhone()!=null && !user.getPhone().equals("")) {
            where.append(" and phone='").append(user.getPhone()).append("' ");
        }
        if (user.getStatus()!=null && !user.getStatus().equals("")) {
            where.append(" and u.status=").append(user.getStatus()).append(" ");
        }
        if (user.getOrgid()!=null && !user.getOrgid().equals("")) {
            where.append(" and orgid=").append(user.getOrgid()).append(" ");
        }
        return dao.paginate(pageNumber,pagesize, select,where.toString());
    }

    public User findByNamePassword(String loginname, String password, String type){
        //if(type.equals("school")) {
            return dao.findFirst("select u.*,o.orgname,o.rffectivetime,o.facedevicetype,o.orgtypeid,o.orglogo,o.isclose,o.status orgstatus from t_user u left join t_org o on o.id=u.orgid where loginname=? and password=?  ",loginname,password);
        //} else {
            //return dao.findFirst("select u.*,o.orgname,o.rffectivetime,o.facedevicetype,o.orgtypeid,o.orglogo,o.isclose,o.status orgstatus from t_user u left join t_org o on o.id=u.orgid where loginname=? and password=? and orgtypeid != 5 ",loginname,password);
        ///}
    }
    public User findByNamePassword(String loginname, String type){
        if(type.equals("school")) {
            return dao.findFirst("select u.*,o.orgname,o.orgtypeid,o.orglogo,o.isclose,o.status orgstatus from t_user u left join t_org o on o.id=u.orgid where loginname=? and orgtypeid = 5",loginname);
        } else {
            return dao.findFirst("select u.*,o.orgname,o.orgtypeid,o.orglogo,o.isclose,o.status orgstatus from t_user u left join t_org o on o.id=u.orgid where loginname=? and orgtypeid != 5 ",loginname);
        }
    }
    public void deleteById(int id) {
        Db.update("update t_user set status=? where id=? ",0,id);
    }


    public void addOrUpdate(User bean, int operatorid){
        if(bean.getId()== null){
            if(CommonUtils.isNotNullOrNotEmpty(bean.getPassword())) {
                bean.setPassword(CommonUtils.getMD5String(CommonUtils.getMD5String(bean.getPassword())));
            } else {
                bean.setPassword(CommonUtils.getMD5String(CommonUtils.getMD5String(PropKit.get("initPassword"))));
            }
            bean.setStatus(1).setIsadmin(0).setCreatetime(new Date()).setOperatorid(operatorid).save();
        }else{
            User old = dao.findById(bean.getId());
            bean.setCreatetime(old.getCreatetime()).setOperatorid(old.getOperatorid()).setUpdatetime(new Date()).setUpdateuserid(operatorid).update();
        }
    }

    public User verifyLoginCode(String code,Integer orgid){
        return dao.findFirst("select * from t_user where code=? and orgid=?",code,orgid);
    }

    public User verifyLoginName(String loginname,Integer orgid){
        return dao.findFirst("select * from t_user where loginname=? ",loginname);
    }

    public Map selectType(){
        Map map = new HashMap<String,List>();
        List areatype = Db.find("select id,areatype from t_areatype");
        map.put("areatype",areatype);
        return map;
    }


}
