diff --git a/app/common/repositories/store/order/StoreOrderHotRepository.php b/app/common/repositories/store/order/StoreOrderHotRepository.php index 93098ac..fa24be4 100644 --- a/app/common/repositories/store/order/StoreOrderHotRepository.php +++ b/app/common/repositories/store/order/StoreOrderHotRepository.php @@ -36,4 +36,9 @@ class StoreOrderHotRepository extends BaseRepository $this->dao->create($data); } + + public function getList($where){ + return $this->dao->selectWhere($where); + } + } diff --git a/app/common/repositories/user/UserAssetsLogRepository.php b/app/common/repositories/user/UserAssetsLogRepository.php index 26c5a26..bf8e154 100644 --- a/app/common/repositories/user/UserAssetsLogRepository.php +++ b/app/common/repositories/user/UserAssetsLogRepository.php @@ -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,28 +142,57 @@ 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']){ - $hotData = array( - 'uid' => $order['uid'], - 'order_id' => $order['order_id'], + + if(empty($product['is_hot'])){ + continue; + } + + // 先创建一条记录 + $hotData = array( + 'uid' => $order['uid'], + 'order_id' => $order['order_id'], + 'product_id' => $productItem['product_id'], + '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'], - 'create_time' => date('Y-m-d H:i:s') + 'order_id' => $order['order_id'], + 'count' => $tmp['integral'], ); - $hotRepository->create($hotData); } + $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); diff --git a/app/controller/api/Auth.php b/app/controller/api/Auth.php index c16208d..83c3ce6 100755 --- a/app/controller/api/Auth.php +++ b/app/controller/api/Auth.php @@ -73,8 +73,8 @@ class Auth extends BaseController // app()->make(WechatTemplateMessageService::class)->subscribeSendTemplate($data); // } - app(UserAssetsLogRepository::class)->shareAward(1); - +// app(UserAssetsLogRepository::class)->shareAward(1); + event('order.take', array('order' => array('order_id' => 1))); return app('json')->success(); }