package com.zhongdao.jlr.business.dao.impl;

import com.dubh.common.dao.hibernate.BaseDaoHibernate;
import com.util.UtilTools;
import com.util.Vo;
import com.zhongdao.jlr.business.dao.UserInfoDao;
import com.zhongdao.jlr.business.vo.LoginUserVo;
import com.zhongdao.jlr.business.vo.PageInfo;
import com.zhongdao.jlr.pojo.EnterpriseJlr;
import com.zhongdao.jlr.pojo.JLRRole;
import com.zhongdao.jlr.pojo.JLRUser;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Repository
public class UserInfoDaoImpl extends BaseDaoHibernate implements UserInfoDao {
    @Autowired
    HttpServletRequest request;
    @Override
    public List<JLRRole> getRoleInfo() {
        //获取sessions用户信息
        LoginUserVo logininfo=(LoginUserVo)request.getSession().getAttribute("logininfo");
        List<JLRRole> baseRole=null;
        try {
            //获取角色信息
            baseRole=(List<JLRRole>)getRsByHql("from JLRRole where delete_flag != 1 and id != "+logininfo.getRole_id());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return baseRole;
    }

    @Override
    public List<EnterpriseJlr> getEnterpriseInfo() {
        //获取sessions用户信息
        LoginUserVo logininfo=(LoginUserVo)request.getSession().getAttribute("logininfo");
        List<EnterpriseJlr> enterpriseJlrs=null;
        try {
            //获取用户信息
            enterpriseJlrs=(List<EnterpriseJlr>)getRsByHql("from EnterpriseJlr");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return enterpriseJlrs;
    }

    @Override
    public Vo getUserInfo(HttpServletRequest request, String name, String phone,String role,String username) {
        Vo vo=new Vo();
        //声明一个分页对象
        PageInfo pageInfo=new PageInfo();
        //获取页数和一页显示多少行并将其放入对象中
        pageInfo.setPage(UtilTools.getPage(request));
        pageInfo.setRows(UtilTools.getRows(request));
        //sql动态拼接
        StringBuffer sql=new StringBuffer("select j.login_name,j.id,j.name,j.phone,j.email,r.name roleName,j.update_time " +
                "from jlr_user j " +
                "left join jlr_role r " +
                "on r.id=j.role_id " +
                "where j.role_id != 1 and j.delete_flag != 1");
        if (StringUtils.isNotBlank(name)) {
            sql.append("  and j.name like '%" + name + "%'");
        }
        if (StringUtils.isNotBlank(phone)) {
            sql.append("  and j.phone like '%" + phone + "%'");
        }
        if (StringUtils.isNotBlank(role)) {
            sql.append("  and j.role_id  = '"+ role +"'");
        }
        if (StringUtils.isNotBlank(username)) {
            sql.append("  and j.login_name  = '"+ username +"'");
        }
        String countSql=sql.toString();
        String info=sql.append(" order by j.update_time desc limit "+pageInfo.getCurrentIndex()+","+pageInfo.getRows()).toString();
        try {
            //获取用户信息总数
            List lists=getRsBySql(countSql);
            //分页获取用户信息
            List list = getRsBySql(info);
            if (list != null && list.size() > 0) {
                //将用户信息放入vo对象中
                vo.setRows(UtilTools.listTojsonArray(list, new String[]{"LOGINNAME","ID","NAME","PHONE","EMAIL","ROLENAME","UPDATETIME"}));
            }
            if(lists != null && lists.size() > 0) {
                vo.setTotal(lists.size());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return vo;
    }

    @Override
    public boolean updateUserInfo(JLRUser user) {
        try {
            //添加或更新用户信息
            saveOrUpdate(user);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public JLRUser getUser(String id) {
        JLRUser jlrUser=null;
        try {
            //获取单个用户对象信息
            jlrUser=(JLRUser) getObjectHql("from JLRUser where id ="+id);
        } catch (Exception e) {
            e.printStackTrace();
            return  null;
        }
        return jlrUser;
    }

    @Override
    public boolean deleteUser(JLRUser user) {
        try {
            if(user != null){
                user.setDelete_flag(1);
            }
            //添加或更新用户信息
            saveOrUpdate(user);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
