function getCredit($userId, $actionId, $schoolId) { $tblCredit = new DB_Udo_CreditAction(); $tblAccount = new DB_Pay_Account(); $tblCreditLog = new DB_Udo_UserCreditLog(); $rand = new Common_Char(); //获取该动作的基础信息 $result = $tblCredit->scalar("creditAmount,name,outputInput,ratio,times,isSynthesized,actionType", "where actionId = {$actionId} and isValid = 1"); if (!$result) { return Common_Error::ERROR_ACTION; } $upTimes = $result['times']; //接下来获取用户当日的同action的操作 $today = strtotime("today"); $tomorrow = strtotime("tomorrow"); $statusSuccess = Common_Config::CREDIT_SUCCESS; $sameTimes = $tblCreditLog->queryCount("where actionId = {$actionId} and userId = {$userId} and createTime between {$today} and {$tomorrow}\r\n and status = {$statusSuccess}"); //如果今日增长已经超出限额 if ($sameTimes >= $upTimes) { return Common_Error::ERROR_OVER_TIMES; } //否则更新账户信息,如果失败再重试三次 $retry = 0; $updateAccount = 0; $id = $tblAccount->scalar("id,amt,score", "where sso_id = {$userId}"); //print_r($id); //$testUpdate = $tblAccount->query("update account set score = 1895 where id = {$id['id']}"); //print_r($testUpdate); while (!$updateAccount && $retry <= 3) { $score = $id['score'] + $result['creditAmount']; //print_r($score); //$updateAccount = $tblAccount->query("update account set score = {$score} where id = {$id['id']}"); $remark = "日常活动"; $random = $rand->getRandChar(8); $sign = md5(Common_Config::PAY_OSID . $userId . $result['creditAmount'] . $remark . $random . Common_Config::PAY_SECRET); //print_r(Common_Config::PAY_OSID.(string)$userId.(string)$score.$remark.$random.Common_Config::PAY_SECRET); $url = Common_Config::CREDIT_UPDATE; $post_data = array("osid" => Common_Config::PAY_OSID, "ssoId" => $userId, "score" => $result['creditAmount'], "remark" => $remark, "random" => $random, "sign" => $sign); $cl = new Common_Curl(); $updateAccount = $cl->request($url, $post_data); $retry++; //print_r($updateAccount); } //账户更新失败返回失败信息 if (!$updateAccount) { return Common_Error::ERROR_UPDATE_BALANCE; } //账户更新成功,插入学分和U币变动日支 $tblCreditLog->insert(array("userId" => $userId, "creditSource" => Common_Config::DAILY_CREDIT_ACTION, "actionId" => $actionId, "info" => $result['name'] . "获得学分", "amt" => $result['creditAmount'], "status" => Common_Config::CREDIT_SUCCESS, "schoolId" => $schoolId, "createTime" => time())); return Common_Error::ERROR_SUCCESS; }
function coinScore($id, $uid) { $tblCoinLog = new DB_Udo_CoinLog(); $tblCreditLog = new DB_Udo_UserCreditLog(); $tblCreditInfo = new DB_Udo_CreditInfo(); $tblAccount = new DB_Pay_Account(); //先找到兑换的汇率和额度 $creditInfo = $tblCreditInfo->scalar("id,amt,price", "where isValid = 1 and id = {$id}"); //首先更新账户信息 //获取账户余额 $id = $tblAccount->scalar("id,amt,score", "where sso_id = {$uid}"); if ($id['amt'] < $creditInfo['price']) { return Common_Error::ERROR_SHORT_BALANCE; } //print_r($id); //更新账户信息,如果失败再重试三次 $retry = 0; $updateAccount = 0; while (!$updateAccount && $retry <= 3) { $amt = $id['amt'] - $creditInfo['price']; $score = $id['score'] + $creditInfo['amt']; $updateAccount = $tblAccount->query("update account set amt = {$amt},score = {$score} where id = {$id['id']}"); //$updateAccount = $tblAccount->update($id['id'],array("amt"=>$id['amt']-$creditInfo['price'],"score"=>$id['score']+$creditInfo['amt'])); $retry++; } //账户更新失败返回失败信息 if (!$updateAccount) { return Common_Error::ERROR_UPDATE_BALANCE; } //账户更新成功,插入学分和U币变动日支 $tblCoinLog->insert(array("userId" => $uid, "amt" => 0 - $creditInfo['price'], "info" => "U币兑换学分", "createTime" => time())); $tblCreditLog->insert(array("userId" => $uid, "creditSource" => Common_Config::CREDIT_COIN_EXCHANGE, "info" => "U币兑换获得学分", "amt" => $creditInfo['amt'], "status" => Common_Config::CREDIT_SUCCESS, "createTime" => time())); return Common_Error::ERROR_SUCCESS; }