text: /**
   * Created by hadoop on 2015/6/18.
   */
  var async = require('async');
  var http = require('http');
  var moment = require('moment')
  
  var querystring = require('querystring');
  var dbpool = require('../core/dbutil');
  var config = require('../core/config')
  var logger = require('../core/logger')
  var resUtils = require('../core/resUtils');
  var timeUtils = require('../core/timeUtils');
  var crypto = require('../core/cryptoUtils');
  var redisClient = require('../core/redisClient');
  var RiceError = require('../core/RiceError')
  var httpRequest = require('../core/httpRequest')
  var common = require('./common.js')
  
  var task = {};
  
  var UC_PID = config.UC_PID;
  var UC_SECRET = config.UC_SECRET;
  var USER_VCODE_RATE_PREFIX = 'STR_WIFI_VCODE_RATE_';
  var USER_VCODE_DAY_PREFIX = 'STR_SCHOOL_WIFI_VCODE_';
  var CRYPTO_SALT = "www.anzhuoshangdian.com";
  var IMG_FILE_SERVER = 'http://imgs.anzhuoshangdian.com';
  
  var PLATFORM = 1;
  var BUSI_SKIP = new Error();
  var BUSI_EXCEPTION = new Error();
  var BREAK_FLAG = BUSI_EXCEPTION;
  
  var endResonse = resUtils.endResonse;
  var returnjsondata = resUtils.returnjsondata;
  
  
  var getConnection = function (context, callback) {
      dbpool.getConnection(function (err, connection) {
          context.conn = connection;
          if (err) {
              logger.getDebugLogger().error("数据库连接异常：" + err);
              return callback(err);
          }
          connection.beginTransaction(function (err) {
              if (err) {
                  logger.getDebugLogger().error("数据库连接异常：" + err);
                  return callback(err);
              }
              return callback(null);
          })
      })
  }
  
  var getConnectionExt = function (callback) {
      getConnection(null, callback)
  }
  
  var commit = function (connection, endBusi, endBusiWithException) {
      if (connection) {
          connection.commit(function (err) {
              if (err) {
                  connection.rollback(function () {
                      connection.release();
                      if (endBusiWithException) {
                          endBusiWithException(err);
                      }
                  });
              } else {
                  connection.release();
                  if (endBusi) {
                      endBusi();
                  }
              }
          });
      } else {
          if (endBusi) {
              endBusi();
          }
      }
  }
  
  var rollback = function (connection) {
      if (connection) {
          connection.rollback(function () {
              connection.release();
          });
      }
  }
  
  /**
   * 终端获取任务列表
   */
  task.getTaskList = function(context,res){
  
      dbpool.getConnection(function (err, connection) {
          if (err) {
              logger.getDebugLogger().error("" + err);
              endResonse('500', '获取数据库连接失败', res);
              if(connection)
              {
                  connection.release();
              }
              return;
          }
          var sql = '';
          var param;
          if(context.userinfo)
          {
              param = [context.userinfo.uid,context.userinfo.uid,context.platform];
          }
          else
          {
              //未登录
              param = ['a','a',context.platform];
          }
  
          sql = "select t1.id,t2.id utaskid,t1.name,type,IFNULL(t2.status,0) status,content,description,target,coins,icon,case when t1.type = 2 then downurl else CONCAT(downurl,'/%3Fuid=',?,'&taskid=',t1.id) " +
              " end downurl,sharecontent,t2.complete,CONCAT(t2.complete,'/',t1.target) extra from mk_activity_task t1 LEFT JOIN (select * from mk_activity_taskrecord t3 where uid=?) t2 ON t1.id=t2.taskid and t1.platform = t2.platform " +
              " where IFNULL(t2. STATUS,0) != 3 and t1.platform = ? and (t1.status = 1 or t2.complete is not NULL) order by rank desc";
          connection.query(sql, param, function (err, rows) {
              if (err) {
                      logger.getDebugLogger().error("查询数据库失败" + err);
                      endResonse('500', '查询数据库失败', res);
                      connection.release();
                      return;
              }
              else
              {
                  if(rows.length > 0)
                  {
                      for(var i = 0;i < rows.length; i++)
                      {
                          rows[i].downurl = rows[i].downurl.toString();
                          if(rows[i].extra != null)
                          {
                              rows[i].extra = rows[i].extra.toString();
                          }
                      }
                  }
                  connection.release();
                  returnjsondata(res,rows);
                  return;
              }
          });
      });
  
  }
  
  /**
   *
   * @type {res|*}
   */
  task.sendTaskInfo = function(context,res){
      dbpool.getConnection(function (err, connection) {
          if (err) {
              logger.getDebugLogger().error("" + err);
              endResonse('500', '获取数据库连接失败', res);
              return;
          }
  
          connection.query("select count(*) count from mk_activity_taskrecord where uid = ? and taskid = ? and platform = ?", [context.uid,context.taskid,context.platform], function (err, rows) {
              if (err) {
                  connection.rollback(function () {
                      logger.getDebugLogger().error("查询数据库似失败" + err);
                      endResonse('500', '查询数据库似失败', res);
                      connection.release();
                      return;
                  });
              }
              else
              {
                  if(rows.length > 0 && rows[0].count > 0)
                  {
                      res.end('2');
                      return;
                  }
                  else
                  {
                      var param = [context.uid,context.taskid,1,new moment().format('YYYY-MM-DD HH-mm-ss'),context.platform];
                      connection.query("insert into mk_activity_taskrecord (uid,taskid,status,time,platform) values (?,?,?,?,?)", param, function (err, ret) {
                          if (err) {
                              connection.rollback(function () {
                                  logger.getDebugLogger().error("插入数据库失败" + err);
                                  endResonse('500', '插入数据库失败', res);
                                  connection.release();
                              });
                          }
                          else
                          {
                              if(ret && ret.affectedRows == 1)
                              {
                                  res.end('0');
                              }
                              else
                              {
                                  res.end('1');
                              }
                              connection.commit(function(err){
                                  connection.release();
                              });
                              return;
                          }
                      });
                  }
              }
          });
  });
  };
  
  /**
   * 完成任务
   * @param context
   * @param res
   */
  task.commitTask = function(context,res)
  {
      async.waterfall([
          function(callback)
          {
              getConnection(context,callback);
          },
          function(callback)
          {
              context.conn.query('select t1.uid,t2.coins integral,t1.taskid,t2.name from mk_activity_taskrecord t1,mk_activity_task t2 where t1.platform = ? and t1.taskid = t2.id and t1.id = ? and t1.status = 2', [context.platform, context.utaskid], function (err, rows) {
                  if(err)
                  {
                      logger.getDebugLogger().error("查询数据库失败" + err);
                      endResonse('500', '查询数据库失败', res);
                      context.conn.release();
                      return;
                  }
                  else
                  {
                      if(rows && rows.length > 0)
                      {
                          var uid = rows[0].uid;
                          var addtime = Math.floor(new Date().getTime()/1000);
                          var title = '完成'+rows[0].name+'任务赠送金米'
                          var integral = rows[0].integral;
                          var inte_type = 0;
                          var taskid = rows[0].taskid;
                          var tasktype = 2;
                          var param = [uid,addtime,title,integral,inte_type,taskid,tasktype,context.platform];
                          context.taskid = taskid;
                          context.conn.query('insert into mk_integral (uid,addtime,title,integral,inte_type,taskid,tasktype,platform) values (?,?,?,?,?,?,?,?)',param,function(err,rows){
                              if(err)
                              {
                                  logger.getDebugLogger().error("插入数据库失败" + err);
                                  endResonse('500', '插入数据库失败', res);
                                  context.conn.release();
                                  return;
                              }
                              else
                              {
                                  context.conn.query('UPDATE mk_member SET integral=integral + ? WHERE uid = ?',[integral,uid],function(err,rows){
                                      if(err)
                                      {
                                          logger.getDebugLogger().error("更新数据库失败" + err);
                                          endResonse('500', '更新数据库失败', res);
                                          context.conn.release();
                                          return;
                                      }
                                      else
                                      {
                                          context.conn.query('update mk_activity_taskrecord set status = 3,time = ? where taskid = ? ',[new moment().format('YYYY-MM-DD HH:mm:ss'),context.taskid],function(err,rows){
                                              if(err)
                                              {
                                                  logger.getDebugLogger().error("更新数据库失败" + err);
                                                  endResonse('500', '更新数据库失败', res);
                                                  context.conn.release();
                                                  return;
                                              }
                                              else
                                              {
                                                  context.conn.query('select integral from mk_member where uid = ? and platform = ? LIMIT 1',[uid,context.platform],function(err,rows){
                                                      if(err)
                                                      {
                                                          logger.getDebugLogger().error("查询数据库失败" + err);
                                                          var result = {'code':1,'msg':'更新任务失败'};
                                                          returnjsondata(res,result);
                                                          rollback(context.conn.release());
                                                          return;
                                                      }
                                                      else
                                                      {
                                                          if(rows && rows.length > 0)
                                                          {
                                                              var result = {'code':0,'msg':'领取金米成功','balance':rows[0].integral};
                                                              returnjsondata(res,result);
                                                              commit(context.conn);
                                                          }
                                                          return;
                                                      }
                                                  });
  
                                              }
                                          });
                                      }
                                  });
                              }
                          });
                      }
                      else
                      {
                          var result = {'code':1,'msg':'没有查到对应的记录'};
                          returnjsondata(res,result);
                          return;
                      }
                  }
              });
          }
      ]);
  }
  
  /**
   * 领取
   */
  task.completedTask = function(context,res){
      dbpool.getConnection(function (err, connection) {
          if (err) {
              logger.getDebugLogger().error("" + err);
              endResonse('500', '获取数据库连接失败', res);
              return;
          }
  
          connection.query("update mk_activity_taskrecord set status = 2 , time = ? where taskid = ? and uid = ?", [new moment().format('YYYY-MM-DD HH:mm:ss'),context.taskid,context.uid], function (err, rows) {
              if (err) {
                  connection.rollback(function () {
                      logger.getDebugLogger().error("查询数据库失败" + err);
                      endResonse('500', '查询数据库失败', res);
                      connection.release();
                      return;
                  });
              }
              else
              {
                  if(rows.affectedRows == 1)
                  {
                      res.end('0');
                      connection.commit(function(err){
                          connection.release();
                      });
                  }
                  else
                  {
                      res.end('1');
                      connection.rollback(function(err){
                          connection.release();
                      });
                  }
  
                  return;
              }
          });
      });
  }
  
  
  module.exports = task;