PluginPool.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use app\constants\OptionSetting;
  9. use app\plugins\scanCodePay\models\Order as ScanOrder;
  10. use app\models\PluginPoolDetail;
  11. use yii\db\ActiveRecord;
  12. use yii\behaviors\TimestampBehavior;
  13. use Yii;
  14. use yii\helpers\Json;
  15. /**
  16. * This is the model class for table "{{%UnitFounder_pool}}".
  17. *
  18. * @property integer $id
  19. * @property integer $config_id
  20. * @property integer $store_id
  21. * @property integer $start_time
  22. * @property integer $end_time
  23. * @property float $money
  24. * @property float $rl_rate
  25. * @property float $mx_rate
  26. * @property integer $is_send
  27. * @property integer $is_scan
  28. * @property integer $send_time
  29. * @property integer $created_at
  30. * @property integer $updated_at
  31. */
  32. class PluginPool extends \yii\db\ActiveRecord
  33. {
  34. /**
  35. * @inheritdoc
  36. */
  37. public static function tableName()
  38. {
  39. return '{{%plugin_pool}}';
  40. }
  41. public function behaviors()
  42. {
  43. return [
  44. [
  45. 'class' => TimestampBehavior::class,
  46. 'attributes' => [
  47. ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
  48. ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at']
  49. ]
  50. ]
  51. ];
  52. }
  53. /**
  54. * @inheritdoc
  55. */
  56. public function rules()
  57. {
  58. return [
  59. [['is_send', 'send_time', 'start_time', 'config_id', 'end_time', 'store_id', 'is_scan'], 'integer'],
  60. [['money', 'rl_rate', 'mx_rate'], 'number'],
  61. [['created_at', 'updated_at'], 'safe']
  62. ];
  63. }
  64. /**
  65. * 增加合伙人分红池
  66. * @param $order
  67. * @param $money
  68. * @param string $desc
  69. * @return false|void
  70. */
  71. public static function poolPush($config_id, $order, $money, $desc = '', $pay_price = 0, $rl_rate = 0, $mx_rate = 0, $model = 0, $add_money = 0)
  72. {
  73. $is_scan = $order instanceof ScanOrder ? 1 : 0;
  74. if ($is_scan) {
  75. $store_id = $order->store_id;
  76. $bd_admin = Admin::findOne(['store_id' => $order->store_id, 'type' => 'bd_agent', 'is_delete' => 0, 'is_enable' => 1]);
  77. if (empty($bd_admin)) {
  78. $admin_id = Salesman::getBdId($order->store_id);
  79. if ($admin_id) {
  80. $bd_admin = Admin::findOne(['id' => $admin_id, 'type' => 'bd_agent', 'is_delete' => 0, 'is_enable' => 1]);
  81. $store_id = $bd_admin->store_id;
  82. }
  83. }
  84. }else{
  85. $config = PluginPoolConfig::findOne($config_id);
  86. $store_id = $config->store_id;
  87. }
  88. $query = self::find()->where(['and',
  89. [
  90. '<', 'start_time', time()
  91. ],
  92. [
  93. '>=', 'end_time', time()
  94. ],
  95. [
  96. '=', 'config_id', $config_id
  97. ],
  98. [
  99. '=', 'is_send', 0
  100. ],
  101. [
  102. '=', 'store_id', $store_id
  103. ]
  104. ]);
  105. $bd_setting = [];
  106. if ($bd_admin) {
  107. $bd_setting = Option::getDecode(OptionSetting::SHARE_STRING_CODE_SALE_SETTING_BD, $bd_admin->id, OptionSetting::SHARE_GROUP_NAME);
  108. }
  109. $store_setting = PluginPoolConfig::poolSetting($store_id);
  110. $store_award = $store_setting['pool_award_type'] ?? 1;
  111. $bd_award = $bd_setting['pool_award_type'] ?? 1;
  112. if ($store_award != $bd_award) {
  113. $query->andWhere(['=', 'is_scan', $is_scan]);
  114. }
  115. $pool = $query->orderBy(['id' => SORT_DESC])->one();
  116. // 如果没有奖金池,重新创建奖金池
  117. if (!$pool) {
  118. $pool = new self();
  119. [$start_time, $end_time] = self::getTime($config_id);
  120. $pool->start_time = $start_time;
  121. $pool->end_time = $end_time;
  122. $pool->is_send = 0;
  123. $pool->send_time = 0;
  124. $pool->is_scan = $is_scan;
  125. $pool->store_id = $store_id;
  126. $pool->mx_rate = $mx_rate;
  127. $pool->rl_rate = $rl_rate;
  128. $pool->config_id = $config_id;
  129. }
  130. $before_money = $pool->money ? $pool->money : 0;
  131. $pool->money += $money;
  132. if (!$pool->save()) {
  133. throw new \ErrorException("---------------- 合伙人奖金池更新失败 -------------------" . $pool->getErrorSummary(false)[0]);
  134. }
  135. $pool_log = new PluginPoolDetail();
  136. $pool_log->order_id = $order->id;
  137. $pool_log->config_id = $config_id;
  138. $pool_log->order_no = $order->order_no;
  139. $pool_log->pay_price = $is_scan ? $pay_price : $order->pay_price + $order->take_price;
  140. $pool_log->store_id = $order->store_id;
  141. $pool_log->before_money = $before_money;
  142. $pool_log->after_money = $pool->money;
  143. $pool_log->total_pv = $add_money;
  144. $pool_log->rl_rate = $rl_rate;
  145. $pool_log->mx_rate = $mx_rate;
  146. $pool_log->money = $money;
  147. $pool_log->total_price = $order->total_price;
  148. $pool_log->is_scan = $is_scan;
  149. $pool_log->pool_id = $pool->id;
  150. $pool_log->model_type = $model;
  151. $pool_log->desc = $desc ?: '订单号:' . $order->order_no . '追加';
  152. if (!$pool_log->save()) {
  153. throw new \ErrorException("---------------- 奖金池记录保存失败 -------------------" . $pool_log->getErrorSummary(false)[0]);
  154. }
  155. if ($is_scan) {
  156. switch ($model) {
  157. case 0:
  158. ScanOrder::updateAll(['small_model_pv_bonus' => $money], ['id' => $order['id']]);
  159. break;
  160. case 1:
  161. ScanOrder::updateAll(['big_model_pv_bonus' => $money], ['id' => $order['id']]);
  162. break;
  163. default:
  164. break;
  165. }
  166. }
  167. }
  168. private static function getTime($config_id)
  169. {
  170. $config = PluginPoolConfig::findOne($config_id);
  171. if (empty($config) || empty($config->time_value) || empty($config->time_type)) {
  172. $start_time = strtotime(date('Y-m-d'));
  173. //debug_log([__METHOD__, __LINE__, "---------------- -------------------start_time:" . $start_time . "======"], "app_debug.log");
  174. return [$start_time, strtotime('+1 day', $start_time)];
  175. }
  176. // 计算 $start_time
  177. switch ($config->time_type) {
  178. case 'minute':
  179. $start_time = strtotime(date('Y-m-d H:i') . ':00');
  180. break;
  181. case 'hour':
  182. $start_time = strtotime(date('Y-m-d H') . ':00:00');
  183. break;
  184. case 'week':
  185. $start_time = strtotime(date('Y-m-d'), strtotime('monday this week'));
  186. break;
  187. case 'month':
  188. $start_time = strtotime(date('Y-m'));
  189. break;
  190. default:
  191. $start_time = strtotime(date('Y-m-d'));
  192. break;
  193. }
  194. // 计算 $end_time
  195. $end_time = strtotime('+' . $config->time_value . ' ' . $config->time_type, $start_time) - 1;
  196. return [$start_time, $end_time];
  197. }
  198. }