diff --git a/webapp/models/HashRateShopBuyRecord.php b/webapp/models/HashRateShopBuyRecord.php new file mode 100644 index 00000000..488c738d --- /dev/null +++ b/webapp/models/HashRateShopBuyRecord.php @@ -0,0 +1,148 @@ +_getSelfMysql(), + 't_hashrate_shop_buy_record', + array( + 'account_id' => myself()->_getAccountId(), + 'goods_id' => $goodsId, + ) + ); + return $row; + } + + public static function all() + { + $rows = SqlHelper::ormSelect( + myself()->_getSelfMysql(), + 't_hashrate_shop_buy_record', + array( + 'account_id' => myself()->_getAccountId(), + ) + ); + return array_map(function ($row) { + $nowDaySeconds = myself()->_getNowDaySeconds(); + if (!($row['last_buy_time'] >= $nowDaySeconds && $row['last_buy_time'] <= $nowDaySeconds)) { + $row['this_day_buy_times'] = 0; + } + $mondaySeconds = myself()->_getMondaySeconds(); + if (!($row['last_buy_time'] >= $mondaySeconds && $row['last_buy_time'] <= $mondaySeconds)) { + $row['this_week_buy_times'] = 0; + } + return $row; + }, $rows); + } + + public static function allToHash() + { + $rows = self::all(); + $buyRecordHash = array(); + array_walk($rows, function ($row) use (&$buyRecordHash) { + $buyRecordHash[$row['goods_id']] = $row; + }); + return $buyRecordHash; + } + + public static function add($itemId, $itemNum) + { + SqlHelper::upsert( + myself()->_getSelfMysql(), + 't_shop_buy_record', + array( + 'account_id' => myself()->_getAccountId(), + 'item_id' => $itemId, + ), + array( + 'this_day_buy_times' => function () { + $nowDaySeconds = myself()->_getNowDaySeconds(); + $cond = " last_buy_time>=${nowDaySeconds} AND last_buy_time<=${nowDaySeconds} + 3600 * 24 "; + return "CASE WHEN (${cond}) THEN this_day_buy_times + 1 ELSE 0 END"; + }, + 'this_week_buy_times' => function () { + $mondaySeconds = myself()->_getMondaySeconds(); + $cond = " last_buy_time>=${mondaySeconds} AND last_buy_time<=${mondaySeconds} + 3600 * 24 * 7 "; + return "CASE WHEN (${cond}) THEN this_week_buy_times + 1 ELSE 0 END"; + }, + 'total_buy_times' => function () { + return 'total_buy_times + 1'; + }, + 'last_buy_time' => myself()->_getNowTime(), + 'modifytime' => myself()->_getNowTime(), + ), + array( + 'account_id' => myself()->_getAccountId(), + 'item_id' => $itemId, + 'this_day_buy_times' => 1, + 'this_week_buy_times' => 1, + 'total_buy_times' => 1, + 'last_buy_time' => myself()->_getNowTime(), + 'createtime' => myself()->_getNowTime(), + 'modifytime' => myself()->_getNowTime(), + ) + ); + } + + public static function addWithAddress($address, $itemId, $itemNum) + { + $account_id = ShopBuyRecord::getAccountId($address); + SqlHelper::upsert( + myself()->_getMysql($address), + 't_shop_buy_record', + array( + 'account_id' => $account_id, + 'item_id' => $itemId, + ), + array( + 'this_day_buy_times' => function () { + $nowDaySeconds = myself()->_getNowDaySeconds(); + $cond = " last_buy_time>=${nowDaySeconds} AND last_buy_time<=${nowDaySeconds} + 3600 * 24 "; + return "CASE WHEN (${cond}) THEN this_day_buy_times + 1 ELSE 0 END"; + }, + 'this_week_buy_times' => function () { + $mondaySeconds = myself()->_getMondaySeconds(); + $cond = " last_buy_time>=${mondaySeconds} AND last_buy_time<=${mondaySeconds} + 3600 * 24 * 7 "; + return "CASE WHEN (${cond}) THEN this_week_buy_times + 1 ELSE 0 END"; + }, + 'total_buy_times' => function () { + return 'total_buy_times + 1'; + }, + 'last_buy_time' => myself()->_getNowTime(), + 'modifytime' => myself()->_getNowTime(), + ), + array( + 'account_id' => $account_id, + 'item_id' => $itemId, + 'this_day_buy_times' => 1, + 'this_week_buy_times' => 1, + 'total_buy_times' => 1, + 'last_buy_time' => myself()->_getNowTime(), + 'createtime' => myself()->_getNowTime(), + 'modifytime' => myself()->_getNowTime(), + ) + ); + } + + private static function getAccountId($address) + { + $row = SqlHelper::ormSelectOne( + myself()->_getMysql($address), + 't_user', + array( + 'address' => $address + ) + ); + + return $row['account_id']; + } + +}