@ -22,6 +22,7 @@ use app\common\repositories\system\groupData\GroupDataRepository;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Cache;
use think\facade\Log;
/**
@ -52,7 +53,10 @@ class UserAssetsLogRepository extends BaseRepository
const STATUS_WITHDRAW = 3; // 提现
public function __construct(UserAssetsLogDao $dao, UserAssetsRepository $userAssetsRepository, ProductRepository $productRepository, GroupDataRepository $groupRepository)
public function __construct(UserAssetsLogDao $dao, UserAssetsRepository $userAssetsRepository,
ProductRepository $productRepository, GroupDataRepository $groupRepository,
UserRepository $userRepository
)
{
$this->dao = $dao;
/**
@ -64,6 +68,11 @@ class UserAssetsLogRepository extends BaseRepository
*/
$this->productRepository = $productRepository;
$this->groupDataRepository = $groupRepository;
/**
* @var UserRepository $userRepository
*/
$this->userRepository = $userRepository;
}
/**
@ -133,18 +142,22 @@ class UserAssetsLogRepository extends BaseRepository
$orderRepository = app(StoreOrderRepository::class);
/**
* @var StoreOrderHotRepository
* @var StoreOrderHotRepository $hotRepository
*/
$hotRepository = app(StoreOrderHotRepository::class);
$order = $orderRepository->getDetail($orderItem['order_id']);
// Log::info("order is".json_encode($order));
foreach ($order['orderProduct'] as $productItem){
$product = $this->productRepository->get($productItem['product_id']);
Log::info("product is". json_encode($product));
if($product['is_hot']){
if(empty($product['is_hot'])){
continue;
}
// 先创建一条记录
$hotData = array(
'uid' => $order['uid'],
'order_id' => $order['order_id'],
@ -152,9 +165,34 @@ class UserAssetsLogRepository extends BaseRepository
'create_time' => date('Y-m-d H:i:s')
);
$hotRepository->create($hotData);
// 计算每个购买当前爆单商品的积分数据
$profit = $productItem['total_price'] - $productItem['cost'];
$list = $hotRepository->getList(['status' => 0, 'product_id' => $productItem['product_id']]);
$max = $product['hot_integral_total'];
$logList = array();
foreach ($list as $item){
$tmp = array('integral' => $this->_getValue($profit * $product['hot_integral_rate'] / 100 / count($list)));
if($max - $item['total'] < $tmp['integral']){
$tmp['integral'] = $max - $item['total'];
$item['status'] = 1;
}
$this->userAssetsRepository->updateAssets($item['uid'], $tmp);
$item['total'] += $tmp['integral'];
$item->save();
$logList[] = array(
'uid' => $item['uid'],
'asset_type' => self::ASSET_TYPE_INTEGRAL,
'type' => self::CHANGE_TYPE_RECOMMEND_PRODUCT,
'status' => self::STATUS_GET,
'product_id' => $productItem['product_id'],
'order_id' => $order['order_id'],
'count' => $tmp['integral'],
);
}
$this->addLog($logList);
}
}
@ -173,8 +211,6 @@ class UserAssetsLogRepository extends BaseRepository
$orderRepository = app(StoreOrderRepository::class);
$order = $orderRepository->getDetail($orderId);
// Log::info('order is '.json_encode($order));
$LogList = array();
foreach ($order['orderProduct'] as $productItem) {
$product = $this->productRepository->get($productItem['product_id']);
@ -241,6 +277,24 @@ class UserAssetsLogRepository extends BaseRepository
}
public function getAllSpreadId($uid){
$key = "user:AllSpread:{$uid}";
$ret = Cache::get($key);
if($ret === false){
$uidArr = $this->userRepository->getSubIds($uid);
$childArr = array();
if($uidArr){
foreach ($uidArr as $itemUid){
$childArr = array_merge($this->getAllSpreadId($itemUid), $childArr);
}
}
$ret = array_merge($uidArr, $childArr, array($uid));
Cache::set($key, $ret, 60 * 5);
}
return $ret;
}
private function _getValue($value)
{
return round($value, 2);