|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- <?php
-
- namespace app\model\product;
-
- use app\model\product\product\StoreProduct;
- use app\model\user\User;
- use app\model\user\UserMoney;
- use crmeb\basic\BaseModel;
- use crmeb\services\CacheService;
- use crmeb\traits\ModelTrait;
-
- /**
- *
- * Class product_related
- * @package app\model\product
- */
- class ProductRelated extends BaseModel
- {
-
- use ModelTrait;
-
- /**
- * 数据表主键
- * @var string
- */
- protected $pk = 'id';
-
- /**
- * 模型名称
- * @var string
- */
- protected $name = 'product_related';
-
- public function getProductOneData()
- {
- return $this->hasOne(StoreProduct::class, 'id', 'product_id')->bind(['image' => 'image', 'store_name' => 'store_name']);
- }
-
- public function giveMoney($product_id, $uid)
- {
- try {
- $product = new StoreProduct();
- $ProductRelated = new ProductRelated();
- $benefit_money = $product->where('id', $product_id)->value('benefit_money');
- if (!empty($benefit_money)) {
- $result = $ProductRelated->where('product_id', $product_id)->where('is_delete', 0)->where("uid != " . $uid)->field('uid')->with(['getUserOneData'])->select()->toArray();
- if ($result) {
- $time = $_SERVER['REQUEST_TIME'];
- $User = new User();
- $UserMoney = new UserMoney();
- $UserMoney_data = [];
- $User->startTrans();
- $cx = 1;
- foreach ($result as $k => $v) {
- $UserMoney_data[$k]['uid'] = $v['uid'];
- $UserMoney_data[$k]['link_id'] = $product_id;//关联商品id
- $UserMoney_data[$k]['type'] = 'benefit_get';
- $UserMoney_data[$k]['title'] = '分润商品获得';
- $UserMoney_data[$k]['number'] = $benefit_money;
- $UserMoney_data[$k]['balance'] = $v['now_money'] + $benefit_money;
- $UserMoney_data[$k]['pm'] = 1;
- $UserMoney_data[$k]['mark'] = '分润商品获得' . $benefit_money . '余额';
- $UserMoney_data[$k]['status'] = 1;
- $UserMoney_data[$k]['add_time'] = $time;
- $User->where('uid', $v['uid'])->update(['now_money' => $UserMoney_data[$k]['balance']]);
- }
- $UserMoney->insertAll($UserMoney_data);
- $User->commit();
- }
- }
- return true;
- } catch (\Exception $e) {
- if (isset($cx)) {
- $User->rollback();
- }
- //失败的话,记录下来
- CacheService::set('benefit_fail:' . $product_id . '_' . time(), $product_id);
- return false;
- } catch (\Throwable $e) {
- if (isset($cx)) {
- $User->rollback();
- }
- //失败的话,记录下来
- CacheService::set('benefit_fail:' . $product_id . '_' . time(), $product_id);
- return false;
- }
- }
-
- public function getUserOneData()
- {
- return $this->hasOne(User::class, 'uid', 'uid')->bind(['now_money' => 'now_money']);
- }
-
- }
|