You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProductRelated.php 3.2KB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace app\model\product;
  3. use app\model\product\product\StoreProduct;
  4. use app\model\user\User;
  5. use app\model\user\UserMoney;
  6. use crmeb\basic\BaseModel;
  7. use crmeb\services\CacheService;
  8. use crmeb\traits\ModelTrait;
  9. /**
  10. *
  11. * Class product_related
  12. * @package app\model\product
  13. */
  14. class ProductRelated extends BaseModel
  15. {
  16. use ModelTrait;
  17. /**
  18. * 数据表主键
  19. * @var string
  20. */
  21. protected $pk = 'id';
  22. /**
  23. * 模型名称
  24. * @var string
  25. */
  26. protected $name = 'product_related';
  27. public function getProductOneData()
  28. {
  29. return $this->hasOne(StoreProduct::class, 'id', 'product_id')->bind(['image' => 'image', 'store_name' => 'store_name']);
  30. }
  31. public function giveMoney($product_id, $uid)
  32. {
  33. try {
  34. $product = new StoreProduct();
  35. $ProductRelated = new ProductRelated();
  36. $benefit_money = $product->where('id', $product_id)->value('benefit_money');
  37. if (!empty($benefit_money)) {
  38. $result = $ProductRelated->where('product_id', $product_id)->where('is_delete', 0)->where("uid != " . $uid)->field('uid')->with(['getUserOneData'])->select()->toArray();
  39. if ($result) {
  40. $time = $_SERVER['REQUEST_TIME'];
  41. $User = new User();
  42. $UserMoney = new UserMoney();
  43. $UserMoney_data = [];
  44. $User->startTrans();
  45. $cx = 1;
  46. foreach ($result as $k => $v) {
  47. $UserMoney_data[$k]['uid'] = $v['uid'];
  48. $UserMoney_data[$k]['link_id'] = $product_id;//关联商品id
  49. $UserMoney_data[$k]['type'] = 'benefit_get';
  50. $UserMoney_data[$k]['title'] = '分润商品获得';
  51. $UserMoney_data[$k]['number'] = $benefit_money;
  52. $UserMoney_data[$k]['balance'] = $v['now_money'] + $benefit_money;
  53. $UserMoney_data[$k]['pm'] = 1;
  54. $UserMoney_data[$k]['mark'] = '分润商品获得' . $benefit_money . '余额';
  55. $UserMoney_data[$k]['status'] = 1;
  56. $UserMoney_data[$k]['add_time'] = $time;
  57. $User->where('uid', $v['uid'])->update(['now_money' => $UserMoney_data[$k]['balance']]);
  58. }
  59. $UserMoney->insertAll($UserMoney_data);
  60. $User->commit();
  61. }
  62. }
  63. return true;
  64. } catch (\Exception $e) {
  65. if (isset($cx)) {
  66. $User->rollback();
  67. }
  68. //失败的话,记录下来
  69. CacheService::set('benefit_fail:' . $product_id . '_' . time(), $product_id);
  70. return false;
  71. } catch (\Throwable $e) {
  72. if (isset($cx)) {
  73. $User->rollback();
  74. }
  75. //失败的话,记录下来
  76. CacheService::set('benefit_fail:' . $product_id . '_' . time(), $product_id);
  77. return false;
  78. }
  79. }
  80. public function getUserOneData()
  81. {
  82. return $this->hasOne(User::class, 'uid', 'uid')->bind(['now_money' => 'now_money']);
  83. }
  84. }