112 lines
4.2 KiB
PHP
112 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace models;
|
|
|
|
use mt;
|
|
use phpcommon\SqlHelper;
|
|
|
|
class HashRateShopBuyRecord extends BaseModel
|
|
{
|
|
|
|
public static function find($goodsId)
|
|
{
|
|
$row = SqlHelper::ormSelectOne(
|
|
myself()->_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 + 3600 * 24)) {
|
|
$row['this_day_buy_times'] = 0;
|
|
}
|
|
$mondaySeconds = myself()->_getMondaySeconds();
|
|
if (!($row['last_buy_time'] >= $mondaySeconds &&
|
|
$row['last_buy_time'] <= $mondaySeconds + 3600 * 24 * 7)) {
|
|
$row['this_week_buy_times'] = 0;
|
|
}
|
|
$monthFirstDaySeconds = myself()->_getMonthFirstDaySeconds();
|
|
$nextMonthFirstDaySeconds = myself()->_getNextMonthFirstDaySeconds();
|
|
if (!($row['last_buy_time'] >= $monthFirstDaySeconds &&
|
|
$row['last_buy_time'] <= $nextMonthFirstDaySeconds)) {
|
|
$row['this_month_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($goodsId, $goodsNum, $itemId)
|
|
{
|
|
$goodsNum = intval($goodsNum);
|
|
SqlHelper::upsert(
|
|
myself()->_getSelfMysql(),
|
|
't_hashrate_shop_buy_record',
|
|
array(
|
|
'account_id' => myself()->_getAccountId(),
|
|
'goods_id' => $goodsId,
|
|
),
|
|
array(
|
|
'this_day_buy_times' => function () use($goodsNum) {
|
|
$nowDaySeconds = myself()->_getNowDaySeconds();
|
|
$cond = " last_buy_time>=${nowDaySeconds} AND last_buy_time<=${nowDaySeconds} + 3600 * 24 ";
|
|
return "CASE WHEN (${cond}) THEN this_day_buy_times + ${goodsNum} ELSE ${goodsNum} END";
|
|
},
|
|
'this_week_buy_times' => function () use($goodsNum) {
|
|
$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 + ${goodsNum} ELSE ${goodsNum} END";
|
|
},
|
|
'this_month_buy_times' => function () use($goodsNum) {
|
|
$monthFirstDaySeconds = myself()->_getMonthFirstDaySeconds();
|
|
$cond = " last_buy_time>=${monthFirstDaySeconds} ";
|
|
return "CASE WHEN (${cond}) THEN this_month_buy_times + ${goodsNum} ELSE ${goodsNum} END";
|
|
},
|
|
'total_buy_times' => function () use($goodsNum) {
|
|
return "total_buy_times + ${goodsNum}";
|
|
},
|
|
'last_buy_time' => myself()->_getNowTime(),
|
|
'modifytime' => myself()->_getNowTime(),
|
|
),
|
|
array(
|
|
'account_id' => myself()->_getAccountId(),
|
|
'goods_id' => $goodsId,
|
|
'item_id' => $itemId,
|
|
'this_day_buy_times' => $goodsNum,
|
|
'this_week_buy_times' => $goodsNum,
|
|
'this_month_buy_times' => $goodsNum,
|
|
'total_buy_times' => $goodsNum,
|
|
'last_buy_time' => myself()->_getNowTime(),
|
|
'createtime' => myself()->_getNowTime(),
|
|
'modifytime' => myself()->_getNowTime(),
|
|
)
|
|
);
|
|
}
|
|
|
|
}
|