FuGou.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace app\utils;
  3. use app\models\Option;
  4. use app\models\Order;
  5. use app\models\ShareHolder;
  6. use app\models\ShareHolderFugouFrostLog;
  7. use app\models\User;
  8. use app\models\ShareDetail;
  9. // 链动2+1 复购相关
  10. class FuGou
  11. {
  12. public static function getSetting($store_id)
  13. {
  14. $default = \json_encode([
  15. 'fugou_switch' => 0,
  16. 'fugou_type' => 1,
  17. 'fugou_day' => 0,
  18. 'fugou_price' => 0,
  19. 'fugou_where_num' => 0,
  20. 'fugou_where_price' => 0,
  21. 'fugou_goods' => [],
  22. 'fugou_freeze_level' => '0',
  23. 'fugou_freeze_rand' => [1, 1],
  24. ]);
  25. $fugouSetting = Option::get('fugou', $store_id, 'bonus_pool', 'null')['value'];
  26. if (empty($fugouSetting) || $fugouSetting == 'null') {
  27. $fugouSetting = $default;
  28. }
  29. return \json_decode($fugouSetting, true);
  30. }
  31. // 距离复购剩余天数, 0 不需要复购,< 0 需要复购, > 0 复购剩余天数
  32. public static function cycleByDay($user_id, $store_id, $setting = null)
  33. {
  34. if ($setting === null) {
  35. $setting = self::getSetting($store_id);
  36. }
  37. // 查找是否有复购订单
  38. $fugouOrder = Order::find()->where([
  39. 'user_id' => $user_id,
  40. 'store_id' => $store_id,
  41. 'is_fugou' => 1,
  42. 'is_sale' => 1,
  43. ])->orderBy('id DESC')->one();
  44. $startTime = null;
  45. if ($fugouOrder) {
  46. $startTime = $fugouOrder->created_at;
  47. }
  48. if (empty($startTime)) {
  49. $shareHolder = ShareHolder::findOne(['user_id' => $user_id, 'store_id' => $store_id, 'is_delete' => 0, 'status' => 1]);
  50. if ($shareHolder) {
  51. $startTime = $shareHolder->created_at;
  52. }
  53. }
  54. if (empty($startTime)) {
  55. return 0;
  56. }
  57. $settingDay = $setting['fugou_day'];
  58. // 计算剩余天数
  59. $day = ceil((time() - $startTime) / 86400);
  60. return $settingDay - $day;
  61. }
  62. // 剩余冻结金额,<=0 需要复购,> 0 不需要复购,剩余金额
  63. public static function cycleByPrice($user_id, $store_id, $setting = null, $frost_price = null)
  64. {
  65. if ($setting === null) {
  66. $setting = self::getSetting($store_id);
  67. }
  68. if ($frost_price === null) {
  69. $frost_price = self::getUserFrostPrice($user_id);
  70. }
  71. return $setting['fugou_price'] > 0 ? $setting['fugou_price'] - ($frost_price ?: 0) : 0;
  72. }
  73. // 是否需要复购
  74. public static function isFugou($user_id, $store_id, $setting = null)
  75. {
  76. if ($setting === null) {
  77. $setting = self::getSetting($store_id);
  78. }
  79. $cycle_day = self::cycleByDay($user_id, $store_id, $setting);
  80. $cycle_price = self::cycleByPrice($user_id, $store_id, $setting);
  81. if ($setting['fugou_type'] == 1 || $setting['fugou_type'] == 3) {
  82. if ($cycle_day < 0) {
  83. return true;
  84. }
  85. }
  86. if ($setting['fugou_type'] == 2 || $setting['fugou_type'] == 3) {
  87. if ($cycle_price <= 0) {
  88. return true;
  89. }
  90. }
  91. return false;
  92. }
  93. // 获取用户冻结金额
  94. public static function getUserFrostPrice($user_id)
  95. {
  96. $price = ShareHolderFugouFrostLog::find()->where(['user_id' => $user_id, 'status' => 0])->sum('amount');
  97. return $price ?: 0;
  98. }
  99. // 发放复购冻结佣金
  100. public static function sendFrostPrice($user_id)
  101. {
  102. $t = \Yii::$app->db->beginTransaction();
  103. try {
  104. $totalPrice = static::getUserFrostPrice($user_id);
  105. if ($totalPrice <= 0) {
  106. throw new \Exception('没有冻结佣金');
  107. }
  108. $user = User::findOne($user_id);
  109. $user->updateCounters(['price' => $totalPrice, 'total_price' => $totalPrice]);
  110. ShareHolderFugouFrostLog::updateAll(['status' => 1, 'updated_at' => time()], ['user_id' => $user_id]);
  111. ShareDetail::updateAll(['is_send' => 1, 'send_time' => time()], ['user_id' => $user_id, 'is_fugou' => 1]);
  112. $t->commit();
  113. return true;
  114. } catch (\Exception $e) {
  115. $t->rollBack();
  116. return false;
  117. }
  118. }
  119. }