var Service = require('../../../../common/Service');
var utils = require('../../../../common/utils');
var MsgDao = require('../../dao/vmsg');
var util = require('util');

/**
 * 申请加为好友
 */
var GetFriendApplyList = function () {
  Service.call(this);
  this.checkAuthority = false;
};

util.inherits(GetFriendApplyList, Service);

module.exports = GetFriendApplyList;

/**
 * 处理服务请求
 * @param {Object} req 服务请求对象
 * @param {Object} res 服务响应对象
 */
GetFriendApplyList.prototype.process = function (req, res) {
  var self = this;
  var dao = new MsgDao(this);
  dao.execute('select a.id,a.sender_id,d.name as sender_name,' +
    'IFNULL((SELECT id FROM sys_app_attach WHERE org_id=d.org_id ' +
    'AND app_code="sys.userHead" AND uploader_id=d.id ORDER BY id DESC LIMIT 1),0) AS head_id,' +
    'd.sex as sender_sex,a.send_time,a.content from sys_msg a,' +
    'sys_msg_app b,sys_msg_receive c,sys_user d where a.id=b.id ' +
    'and a.id=c.id and a.org_id=d.org_id and a.sender_id=d.id and ' +
    'b.app_code="sys" and b.app_data_type="user/applyAddFriend" and c.receiver_id=' +
    req.session.userId + ' and a.org_id=' + req.session.orgId, null,
    function (rows) {
      res.rows = rows;
      for (var i = 0; i < res.rows.length; i++) {
        res.rows[i].send_time = utils.formatDate(res.rows[i].send_time, 'yyyy-MM-dd hh:mm:ss');
      }
      self.end(res);
    });
};