var util = require('util');
var Service = require('Service');
var utils = require('utils');
var Dao = require('../../dao/sys_session');

var getListLogin = function() {
  Service.call(this);
};

util.inherits(getListLogin, Service);

module.exports = getListLogin;

getListLogin.prototype.process =  function(req, res) {
  var self = this;
  var dao = new Dao(this);
  var fields = 'id,code,create_time,user_id,end_time,last_address,'
    +'client_type,client_version,client_os,client_os_version,device_model';
  var where = '';
  if(req.name){
    where += ' and (user_id in (select id from sys_user where name like "%'+req.name+'%")or user_id like "%'+req.name+'%")';
  }
  if(req.start){
    where += ' and create_time >"'+req.start+' 00:00:00"';
  }
  if(req.end){
    where += ' and create_time <"'+req.end+' 23:59:00"';
  }
  if(req.keyword) {
    where += '`code` like "%'+req.keyword+'%" or `last_address` like "%'+req.keyword
      +'%" or client_type like "%'+req.keyword+'%" or client_os like "%'+req.keyword
      +'%" or device_model like "%'+req.keyword+'%"';
  }
  fields += ',(select name from sys_user where id = sys_session.user_id) as user_name';
  dao.queryDataPage(req, {fields:fields, where:'org_id='+req.session.orgId+where+' order by id desc'}, function(dataPage) {
    res.dataPage = dataPage;
    self.end(res);
  });
};