package com.yihu.hos.system.dao;

import com.yihu.hos.system.model.SystemUser;
import com.yihu.hos.system.model.SystemUserRole;
import com.yihu.hos.web.framework.dao.SQLGeneralDAO;
import com.yihu.hos.web.framework.model.Result;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * Created by chenweida on 2016/1/19.
 */
@Repository("userDao")
public class UserDao extends SQLGeneralDAO {

    public static final String BEAN_ID = "userDao";

  
    public Result getUserList(Map<String, Object> params) throws Exception {
        StringBuilder sb = new StringBuilder("from SystemUser t where 1=1  and t.activated != '"+SystemUser.ACTIVATED_DELETE+"'");
        if (!StringUtils.isEmpty(params.get("name"))) {
            sb.append(" and t.loginCode like '%" + params.get("name") + "%' ");
        }
        return super.getDataGridResult(sb.toString(), Integer.valueOf(params.get("page").toString()), Integer.valueOf(params.get("rows").toString()));
    }

  
    public void activityUser(String useid, String flag) throws Exception {
        String sql = "update System_user set activated='" + flag + "' where Id='" + useid + "'";
        super.execute(sql);
    }

  
    public SystemUser getUserByLoginCode(String loginCode) throws Exception {
        List<SystemUser> users = (List<SystemUser>) hibernateTemplate.find("from SystemUser s where s.loginCode =? and s.activated!=?", loginCode,SystemUser.ACTIVATED_DELETE);
        if (users != null && users.size() > 0) {
            return users.get(0);
        }
        return null;
    }

    public void deleteUserRoleByUserId(String userId) throws Exception
    {
        String sql = "delete from system_role_user where user_id ='"+ userId + "'";
        super.execute(sql);
    }

    public List<SystemUserRole> getUserRoleByUserId(String userId) throws Exception
    {
        List<SystemUserRole> userRolse = (List<SystemUserRole>) hibernateTemplate.find("from SystemUserRole s where s.userId =? ", userId);
        if (userRolse != null && userRolse.size() > 0) {
            return userRolse;
        }
        return null;
    }

}
