var util = require('util');
var Service = require('Service');
var utils = require('utils');
var AttachDao = require('../../dao/app_attach');

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

util.inherits(GetUserHead, Service);

module.exports = GetUserHead;

GetUserHead.prototype.process =  function(req, res) {
  if(!req.user_id){
    this.onLogicError(1, "请提供用户编号！");
    return;
  }
  if(!utils.isInt(req.user_id)) {
    this.onLogicError(2, '请提供正确的用户编号！');
    return;
  }
  var self = this;
  req.session.cache.get('user', parseInt(req.user_id), function(err, user) {
    if(err) {
      self.onLogicError(3, '请提供正确的用户编号！');
      return;
    }
    var daoAttach = new AttachDao(self);
    daoAttach.query({fields:'code,name',where:'id='+user.head_id}, function(rows) {
      if(rows.length==0) {
        self.onLogicError(4, '没有找到有效的头像！');
        return;
      }
      res.data = {};
      res.data.head = {
        code: rows[0].code,
        name: rows[0].name
      }
      self.end(res);
    });
  });
};