package com.casic.PgInterface.patroler.manager;

import com.casic.PgInterface.core.hibernate.HibernateEntityDao;
import com.casic.PgInterface.patroler.domain.PgUser;
import com.casic.PgInterface.patroler.dto.PgUserDto;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yxw on 2017/11/2.
 */
@Service
public class PgUserManager extends HibernateEntityDao<PgUser> {

    //获取当前人员列表
    //TODO LIST List<PgPatrolerDto> getAll()
    public List<PgUserDto> getAllUser() {

        Criteria criteria = createCriteria(PgUser.class);
        criteria.add(Restrictions.eq("active",1));
        criteria.addOrder(Order.desc("id"));

        List<PgUser> pgUserList=criteria.list();
        List<PgUserDto> pgUserDtoList = new ArrayList<PgUserDto>();
        for (PgUser pgUser : pgUserList){
            pgUserDtoList.add(new PgUserDto(pgUser));
        }

        return pgUserDtoList;
    }

    //查询人员是否存在
    public boolean userNameIsExits(String userName) {
        Criteria criteria = getSession().createCriteria(PgUser.class);
        criteria.add(Restrictions.eq("active", 1));
        criteria.add(Restrictions.eq("userName", userName));
        if (getCount(criteria) > 0) {
            return true;
        }
        return false;
    }

    public PgUser getPatrolerByuserName(String userName) {
        Criteria criteria = getSession().createCriteria(PgUser.class);
        criteria.add(Restrictions.eq("userName", userName));
        criteria.add(Restrictions.eq("active",1));
        List<PgUser> pgUserList = criteria.list();

        //TODO LIST:为空判断 patrolers==null?
        if (pgUserList != null && pgUserList.size() > 0)
            return pgUserList.get(0);
        else
            return null;
    }

    public boolean signIn(String userName,String passWord){

        String sql="select PASSWORD from PG_USER where USERNAME = '"+userName+"' and active = 1";

        Object object=this.getSession().createSQLQuery(sql).list();

        if(String.valueOf(((List) object).get(0)).equals(passWord)) return true;
        else return false;
    }

}
