PluginPoolConfig.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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 ScanCodePayOrder;
  10. use app\plugins\scanCodePay\models\Order as ScanOrder;
  11. use app\utils\OrderNo;
  12. use app\utils\yunst\src\OrderYunst;
  13. use yii\base\Exception;
  14. use yii\db\ActiveRecord;
  15. use yii\behaviors\TimestampBehavior;
  16. use Yii;
  17. use yii\helpers\ArrayHelper;
  18. use yii\helpers\Json;
  19. /**
  20. * This is the model class for table "{{%plugin_pool_config}}".
  21. *
  22. * @property integer $id
  23. * @property integer $store_id
  24. * @property integer $is_delete
  25. * @property integer $status
  26. * @property string $name
  27. * @property string $rate
  28. * @property string $time_type
  29. * @property integer $created_at
  30. * @property integer $reward_type
  31. * @property integer $time_value
  32. */
  33. class PluginPoolConfig extends \yii\db\ActiveRecord
  34. {
  35. const MODEL_SHOP = 0;
  36. const MODEL_ALLIANCE = 1;
  37. const TYPE_CONFIG_POOL_SECRET = 'config_pool_secret';
  38. const MODEL_NAME_LIST = array(
  39. self::MODEL_SHOP => '小模型',
  40. self::MODEL_ALLIANCE => '大模型',
  41. );
  42. /**
  43. * @inheritdoc
  44. */
  45. public static function tableName()
  46. {
  47. return '{{%plugin_pool_config}}';
  48. }
  49. public function behaviors()
  50. {
  51. return [
  52. [
  53. 'class' => TimestampBehavior::class,
  54. 'attributes' => [
  55. ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
  56. ]
  57. ]
  58. ];
  59. }
  60. /**
  61. * @inheritdoc
  62. */
  63. public function rules()
  64. {
  65. return [
  66. [['is_delete', 'store_id', 'status', 'time_value','reward_type'], 'integer'],
  67. [['name', 'time_type'], 'string'],
  68. [['rate'], 'number'],
  69. [['created_at'], 'safe']
  70. ];
  71. }
  72. public static function transfer($order)
  73. {
  74. if (!$order->is_pay) return false;
  75. $store_id = $order->store_id;
  76. $setting = Option::getDecode(OptionSetting::SHARE_STRING_CODE_DEFAULT_SETTING, $store_id, OptionSetting::SHARE_GROUP_NAME);
  77. /*$check_res = self::checkSettingByStoreId($store_id, $setting);
  78. if (!$check_res) return false;*/
  79. $is_scan = $order instanceof ScanOrder ? 1 : 0;
  80. if ($is_scan) return false;
  81. $t = \Yii::$app->db->beginTransaction();
  82. try {
  83. $config = PluginPoolConfig::poolSetting($store_id);
  84. if (empty($config)) throw new \Exception('无配置');
  85. if(($order->channel_id) == 9999){
  86. $pv0 = 0;
  87. $pv1 = $order->balance;
  88. }else{
  89. $pv0 = OrderDetail::find()->andWhere(['order_id' => $order['id'], 'is_delete' => 0])->select(['sum(pv_0)'])->scalar();
  90. $pv1 = OrderDetail::find()->andWhere(['order_id' => $order['id'], 'is_delete' => 0])->select(['sum(pv_1)'])->scalar();
  91. }
  92. //$pv0 = 0;
  93. //debug_log([__METHOD__, __LINE__, 'configPoolPush start'], "app_debug.log");
  94. self::configPoolPush($config, $order, $setting, $pv0, $pv1);//插入池子
  95. $add_money0 = bcmul($pv0, $setting['string_code_pv_scale_0'] / 100, 4);
  96. $add_money1 = bcmul($pv1, $setting['string_code_pv_scale_1'] / 100, 4);
  97. //self::areaAward($config, $store_id, $order, $add_money0, 0);
  98. self::areaAward($config, $store_id, $order, $add_money1, 1);
  99. self::appointAward($config, $order, $add_money0, $add_money1);
  100. self::currencyAward($config, $order, $add_money0, $add_money1);
  101. $t->commit();
  102. } catch (\Exception $e) {
  103. $t->rollBack();
  104. \Yii::$app->db->createCommand()->insert('cyy_pay_log', [
  105. 'order_no'=>$order->order_no,
  106. 'desc'=>'奖励错误'.$e->getMessage(),
  107. 'add_time'=>time(),
  108. ])->execute();
  109. ActionLog::addLog(1, '/PluginPoolConfig/transfer', $e->getMessage());
  110. }
  111. $UserStringCodeOrderlist = UserStringCodeOrder::findAll([
  112. 'order_id' => $order->id,
  113. 'is_scan' => $is_scan,
  114. 'type' => [
  115. UserStringCodeOrder::TYPE_STORE_USER_REFERRER,
  116. UserStringCodeOrder::TYPE_STORE_USER_AFFILIATED,
  117. UserStringCodeOrder::TYPE_STORE_REFERRER,
  118. UserStringCodeOrder::TYPE_BRAND_USER_REFERRER,
  119. UserStringCodeOrder::TYPE_BRAND_USER_AFFILIATED,
  120. UserStringCodeOrder::TYPE_BRAND_REFERRER,
  121. UserStringCodeOrder::TYPE_AREA_AWARD,
  122. UserStringCodeOrder::TYPE_BRAND_USER_STORE_REFERRER,
  123. UserStringCodeOrder::TYPE_SUPRISE_RED,
  124. ] // 用数组指定多个类型
  125. ]);
  126. $league_arr = UserStringCodeOrder::TYPE_LEAGUE_PRICE;
  127. $Leaguelist = SaaSLeaguePriceLog::findAll([
  128. 'order_id' => $order->id,
  129. 'is_scan' => $is_scan,
  130. 'type' => [
  131. $league_arr[UserStringCodeOrder::TYPE_STORE_USER_REFERRER],
  132. $league_arr[UserStringCodeOrder::TYPE_STORE_USER_AFFILIATED],
  133. $league_arr[UserStringCodeOrder::TYPE_STORE_REFERRER],
  134. $league_arr[UserStringCodeOrder::TYPE_BRAND_USER_REFERRER],
  135. $league_arr[UserStringCodeOrder::TYPE_BRAND_USER_AFFILIATED],
  136. $league_arr[UserStringCodeOrder::TYPE_BRAND_REFERRER],
  137. $league_arr[UserStringCodeOrder::TYPE_AREA_AWARD],
  138. $league_arr[UserStringCodeOrder::TYPE_BRAND_USER_STORE_REFERRER],
  139. $league_arr[UserStringCodeOrder::TYPE_SUPRISE_RED],
  140. ]
  141. ]);
  142. if (count($UserStringCodeOrderlist) > 0 || count($Leaguelist) > 0) {
  143. $order->team_reward_status = 1;
  144. $order->save();
  145. }
  146. }
  147. public static function configPoolPush($config, $order, $setting, $pv0, $pv1)
  148. {
  149. try {
  150. if (!isset($config['pools']) || empty($config['pools'])) throw new \Exception('无配置分红池');
  151. foreach ($config['pools'] as $p) {
  152. $pool = self::find()->where(['status' => 1, 'is_delete' => 0, 'id' => $p['config_id']])->one();
  153. if (empty($pool)) continue;
  154. if (!isset($p['rate']) || $p['rate'] <= 0) continue;
  155. self::pushCP($p['config_id'], $order, $pv0, $setting['string_code_pv_scale_0'], $p['rate'], $setting['string_code_model'], 0);
  156. self::pushCP($p['config_id'], $order, $pv1, $setting['string_code_pv_scale_1'], $p['rate'], $setting['string_code_model'], 1);
  157. }
  158. } catch (\Exception $e) {
  159. ActionLog::addLog(1, '/configPoolPush', $e->getMessage());
  160. }
  161. }
  162. public static function pushCP($config_id, $order, $order_pv, $code_percent, $rate, $string_code_model, $model_val)
  163. {
  164. try {
  165. $code_percent = 100;
  166. //if (in_array($model_val, $string_code_model)) {
  167. //debug_log([__METHOD__, __LINE__, "大模型比例 : " . ($code_percent / 100)], "app_debug.log");
  168. $add_money = bcmul($order_pv, $code_percent / 100, 4);
  169. $money = bcmul($add_money, $rate / 100, 4);
  170. //debug_log([__METHOD__, __LINE__, "大模型:order_pv : " . $order_pv . ',$add_money:' . $add_money . ',$money:' . $money], "app_debug.log");
  171. if (bccomp($money, 0, 4) > 0) {
  172. $desc = self::MODEL_NAME_LIST[$model_val] . " " . "订单号:【{$order->order_no}】分红增加";
  173. PluginPool::poolPush($config_id, $order, $money, $desc, $order_pv, $rate, $code_percent, $model_val, $add_money);
  174. }
  175. // }
  176. } catch (\Exception $e) {
  177. ActionLog::addLog(1, 'pushCP', $e->getMessage());
  178. }
  179. }
  180. public static function appointAward($config, $order, $pv0, $pv1)
  181. {
  182. try {
  183. if (!isset($config['users']) || empty($config['users'])) throw new \Exception('无配置分红池');
  184. foreach ($config['users'] as $p) {
  185. if (!isset($p['rate']) || $p['rate'] <= 0 || !isset($p['saas_id']) || !$p['saas_id'] || !isset($p['remark']) || !$p['remark']) continue;
  186. /*$money = bcmul($pv0, $p['rate'] / 100, 6);
  187. if ($money > 0){
  188. //UserStringCodeOrder::configAddUserWallet($order, $p['saas_id'], $money, UserStringCodeOrder::TYPE_APPOINT_AWARD, 0, $p['remark']);
  189. UserStringCodeOrder::goCouponAdd($order->store_id, $p['saas_id'], $money, UserStringCodeOrder::STORE_DESIGNATED_PROFIT_SHARING,$order);
  190. }*/
  191. $money = bcmul($pv1, $p['rate'] / 100, 6);
  192. if ($money > 0){
  193. //UserStringCodeOrder::configAddUserWallet($order, $p['saas_id'], $money, UserStringCodeOrder::TYPE_APPOINT_AWARD, 1, $p['remark']);
  194. UserStringCodeOrder::goCouponAdd($order->store_id, $p['saas_id'], $money, UserStringCodeOrder::STORE_DESIGNATED_PROFIT_SHARING,$order);
  195. }
  196. }
  197. } catch (\Exception $e) {
  198. //ActionLog::addLog(1, 'appointAward', $e->getMessage());
  199. }
  200. }
  201. public static function currencyAward($config, $order, $pv0, $pv1)
  202. {
  203. try {
  204. if (!isset($config['currencys']) || empty($config['currencys'])) throw new \Exception('无配置分红池');
  205. $saas_id = SaasUser::findSaasIdByUserId($order->user_id);
  206. foreach ($config['currencys'] as $p) {
  207. if (!isset($p['rate']) || $p['rate'] <= 0 || !isset($p['mark']) || !$p['mark']|| !isset($p['currency_id']) || !$p['currency_id']) continue;
  208. $currency = CurrencyCoin::findOne($p['currency_id']);
  209. if (!$currency) continue;
  210. //$currency_money_0 = bcdiv($pv0, $currency->cny_price, 6);
  211. $currency_money_1 = bcdiv($pv1, $currency->cny_price, 6);
  212. //$currency_money_0 = bcmul($currency_money_0, $p['rate']/100, 6);
  213. $currency_money_1 = bcmul($currency_money_1, $p['rate']/100, 6);
  214. /*if ($currency_money_0 > 0){
  215. $res = CurrencyCoin::addSaasMoney($currency->id,$saas_id, 1, $currency_money_0, $p['mark']);
  216. if (!$res) {
  217. continue;
  218. }
  219. }*/
  220. if ($currency_money_1 > 0){
  221. $res = CurrencyCoin::addSaasMoney($currency->id,$saas_id, 1, $currency_money_1, $p['mark'],$order->store_id);
  222. if (!$res) {
  223. continue;
  224. }
  225. }
  226. /*$wallet = UserWalletCoin::findOne(['currency_id'=>$currency->id,'store_id'=>$order->store_id,'is_open'=>1]);
  227. if (!$wallet) continue;
  228. $money = truncateDecimal(bcmul($pv0, $p['rate'] / 100, 6));
  229. if ($money > 0){
  230. $data = [
  231. "order_id"=>$order->id,
  232. "order_no"=>$order->order_no,
  233. "pay_price"=>$money,
  234. "desc"=>$p['mark'],
  235. 'huifu_id'=>$wallet->huifu_id,
  236. 'huifu_id_name'=>self::getHuifuName($wallet->huifu_id),
  237. 'percent'=>0,
  238. "create_time"=>time(),
  239. "is_type"=>2
  240. ];
  241. \Yii::$app->db->createCommand()->insert('cyy_scan_code_pay_order_percent', $data)->execute();
  242. }
  243. $money = truncateDecimal(bcmul($pv1, $p['rate'] / 100, 6));
  244. if ($money > 0){
  245. $data = [
  246. "order_id"=>$order->id,
  247. "order_no"=>$order->order_no,
  248. "pay_price"=>$money,
  249. "desc"=>$p['mark'],
  250. 'is_type'=>2,
  251. 'huifu_id'=>$wallet->huifu_id,
  252. 'huifu_id_name'=>self::getHuifuName($wallet->huifu_id),
  253. 'percent'=>0,
  254. "create_time"=>time(),
  255. ];
  256. \Yii::$app->db->createCommand()->insert('cyy_scan_code_pay_order_percent', $data)->execute();
  257. }*/
  258. }
  259. } catch (\Exception $e) {
  260. ActionLog::addLog(1, UserStringCodeOrder::TYPE_CURRENCY_AWARD, $e->getMessage());
  261. }
  262. }
  263. public static function getHuifuName($huifuId)
  264. {
  265. $storebanklist = Storebanklist::findOne(['huifu_id' => $huifuId]);
  266. if (empty($storebanklist)) {
  267. $localHuifuMerchant = LocalHuifuMerchant::findOne(['huifu_id' => $huifuId]);
  268. return $localHuifuMerchant->huifu_name ?? '';
  269. }
  270. return $storebanklist->short_name ?? '';
  271. }
  272. public static function areaAward($config, $store_id, $order, $order_pv, $model)
  273. {
  274. try {
  275. if ($order_pv <= 0) throw new Exception('pv为0,' . self::MODEL_NAME_LIST[$model]);
  276. self::stringCodeStoreArea($config, $store_id, $order, $order_pv, 4, $model);//区代理
  277. self::stringCodeStoreArea($config, $store_id, $order, $order_pv, 3, $model);//区代理
  278. self::stringCodeStoreArea($config, $store_id, $order, $order_pv, 2, $model);//市代理
  279. self::stringCodeStoreArea($config, $store_id, $order, $order_pv, 1, $model);//省代理
  280. //debug_log([__METHOD__, __LINE__, "PV:" . $order_pv], "app_debug.log");
  281. //self::stringCodeStoreReferrer($order, $config, $store_id, $order_pv, $model);
  282. //self::stringCodeStoreUserAffiliated($order, $config, $order_pv, $model);
  283. //self::stringCodeStoreUserReferrer($order, $config, $order_pv, $model);
  284. //self::supriseRed($order, $config, $order_pv, $model);
  285. self::brandUserStoreReferrer($order, $config, $order_pv, $model);
  286. } catch (\Exception $e) {
  287. ActionLog::addLog(1, 'areaAward', $e->getMessage());
  288. }catch (\Throwable $e) {
  289. ActionLog::addLog(1, 'areaAward', $e->getMessage());
  290. }
  291. }
  292. public static function stringCodeStoreArea($config, $store_id, $order, $order_pv, $agent_type, $model)
  293. {
  294. try {
  295. $holder = null;
  296. $area_type = "";
  297. switch ($agent_type) {
  298. case 4:
  299. $desc = '街道代理';
  300. //$rate = $config['street_rate'];
  301. $area_type = UserStringCodeOrder::STORE_T_PROFIT_SHARING;
  302. $rate = 0;
  303. $holder = PoolConfigHolder::find()->where(['store_id' => $store_id, 'status' => 1, 'is_delete' => 0, 'agent_type' => 4, 'town_id' => $order['town_id']])->one();
  304. if (!empty($holder) && $holder->user_id) break;
  305. case 3:
  306. if ($agent_type == 3){
  307. $desc = '区代理';
  308. $rate = $config['area_rate'];
  309. $area_type = UserStringCodeOrder::STORE_A_PROFIT_SHARING;
  310. }
  311. $holder = PoolConfigHolder::find()->where(['store_id' => $store_id, 'status' => 1, 'is_delete' => 0, 'agent_type' => 3, 'district_id' => $order['district_id']])->one();
  312. if (!empty($holder) && $holder->user_id) break;
  313. case 2:
  314. if ($agent_type == 2) {
  315. $desc = '市代理';
  316. $rate = $config['city_rate'];
  317. $area_type = UserStringCodeOrder::STORE_C_PROFIT_SHARING;
  318. }
  319. $holder = PoolConfigHolder::find()->where(['store_id' => $store_id, 'status' => 1, 'is_delete' => 0, 'agent_type' => 2, 'city_id' => $order['city_id']])->one();
  320. if (!empty($holder) && $holder->user_id) break;
  321. case 1:
  322. if ($agent_type == 1) {
  323. $desc = '省代理';
  324. $rate = $config['province_rate'];
  325. $area_type = UserStringCodeOrder::STORE_P_PROFIT_SHARING;
  326. }
  327. $holder = PoolConfigHolder::find()->where(['store_id' => $store_id, 'status' => 1, 'is_delete' => 0, 'agent_type' => 1, 'province_id' => $order['province_id']])->one();
  328. break;
  329. default:
  330. $rate = 0;
  331. break;
  332. }
  333. $agent_id = !empty($holder) && $holder->user_id ? SaasUser::findSaasIdByUserId($holder->user_id) : ($config['string_code_store_push_id'] ?: 0);
  334. if ($rate <= 0 || !$agent_id) {
  335. throw new \Exception('rate:' . $rate . ',agent_id:' . $agent_id);
  336. }
  337. $money = bcmul($order_pv, $rate / 100, 4);
  338. //UserStringCodeOrder::configAddUserWallet($order, $agent_id, $money, UserStringCodeOrder::TYPE_AREA_AWARD, $model, $desc);
  339. UserStringCodeOrder::goCouponAdd($order->store_id, $agent_id, $money, $area_type,$order);
  340. } catch (\Exception $e) {
  341. ActionLog::addLog(1, 'stringCodeStoreArea', $e->getMessage());
  342. }
  343. }
  344. public static function stringCodeStoreReferrer($order, $setting, $store_id, $transfer_money, $model_val)
  345. {
  346. $referrer = 0;
  347. if ($order instanceof ScanOrder) {
  348. if (isset($setting['string_code_store_referrer']) && $setting['string_code_store_referrer'] > 0) {
  349. $referrer = $setting['string_code_store_referrer'];
  350. }
  351. } else {
  352. if (isset($setting['string_code_brand_referrer']) && $setting['string_code_brand_referrer'] > 0) {
  353. $referrer = $setting['string_code_brand_referrer'];
  354. }
  355. }
  356. if ($referrer > 0) {
  357. $store_referral = SaasStoreReferral::findOne(['store_id' => $store_id]);
  358. $saas_id = $store_referral['referral_id'];
  359. $money = bcmul($transfer_money, $referrer / 100, 4);
  360. //debug_log([__METHOD__, __LINE__, "stringCodeStoreReferrer:" . $transfer_money.'$referrer'.$referrer], "app_debug.log");
  361. $type = UserStringCodeOrder::TYPE_STORE_REFERRER;
  362. UserStringCodeOrder::transferAddUserWallet($order, $saas_id, $money, $type, $model_val);
  363. }
  364. }
  365. public static function brandUserStoreReferrer($order, $setting, $transfer_money, $model_val)
  366. {
  367. $referrer = 0;
  368. if ($order instanceof ScanOrder) {
  369. if (isset($setting['string_code_store_referrer']) && $setting['string_code_store_referrer'] > 0) {
  370. $referrer = $setting['string_code_store_referrer'];
  371. }
  372. } else {
  373. if (isset($setting['brand_user_store_referrer']) && $setting['brand_user_store_referrer'] > 0) {
  374. $referrer = $setting['brand_user_store_referrer'];
  375. }
  376. }
  377. $user = User::findOne($order->user_id);
  378. $user->parent_id = $user->old_parent_id;
  379. \Yii::$app->db->createCommand()->insert('cyy_pay_log', [
  380. 'order_no'=>$order->order_no,
  381. 'desc'=>"推荐人".$user->parent_id."百分比:".$referrer,
  382. 'add_time'=>time(),
  383. ])->execute();
  384. if ($user->parent_id > 0 && $referrer > 0) {
  385. $saas_id = SaasUser::findSaasIdByUserId($user->parent_id);
  386. $money = bcmul($transfer_money, $referrer / 100, 4);
  387. //$type = UserStringCodeOrder::TYPE_BRAND_USER_STORE_REFERRER;
  388. $type = UserStringCodeOrder::STORE_DIRECT_REFERRAL_REWARD;
  389. //debug_log([__METHOD__, __LINE__, "brandUserStoreReferrer:" . $transfer_money.'$referrer'.$referrer], "app_debug.log");
  390. //UserStringCodeOrder::transferAddUserWallet($order, $saas_id, $money, $type, $model_val);
  391. UserStringCodeOrder::goCouponAdd($order->store_id, $saas_id, $money, $type,$order);
  392. }
  393. }
  394. public static function supriseRed($order, $setting, $transfer_money, $model_val)
  395. {
  396. $referrer = 0;
  397. if ($order instanceof ScanOrder) {
  398. if (isset($setting['store_surprise_Red']) && $setting['store_surprise_Red'] > 0) {
  399. $referrer = $setting['store_surprise_Red'];
  400. }
  401. } else {
  402. if (isset($setting['surprise_Red']) && $setting['surprise_Red'] > 0) {
  403. $referrer = $setting['surprise_Red'];
  404. }
  405. }
  406. $saas_id = SaasUser::findSaasIdByUserId($order->user_id);
  407. if ($saas_id > 0 && $referrer > 0){
  408. $money = bcmul($transfer_money, $referrer / 100, 4);
  409. $type = UserStringCodeOrder::TYPE_SUPRISE_RED;
  410. UserStringCodeOrder::transferAddUserWallet($order, $saas_id, $money, $type, $model_val);
  411. }
  412. }
  413. public static function stringCodeStoreUserAffiliated($order, $setting, $transfer_money, $model_val)
  414. {
  415. $affiliated = 0;
  416. if ($order instanceof ScanOrder) {
  417. if (isset($setting['string_code_store_user_affiliated']) && $setting['string_code_store_user_affiliated'] > 0) {
  418. $affiliated = $setting['string_code_store_user_affiliated'];
  419. }
  420. } else {
  421. if (isset($setting['string_code_brand_user_affiliated']) && $setting['string_code_brand_user_affiliated'] > 0) {
  422. $affiliated = $setting['string_code_brand_user_affiliated'];
  423. }
  424. }
  425. if ($affiliated > 0) {
  426. $first_store_id = SaasUser::findFirstStoreIdByUserId($order->user_id);
  427. $store_admin = Admin::findOne(['type' => 'store', 'type_id' => $first_store_id, 'is_delete' => 0]);
  428. $saas_id = $store_admin['saas_user_id'];
  429. $money = bcmul($transfer_money, $affiliated / 100, 4);
  430. $type = UserStringCodeOrder::TYPE_STORE_USER_AFFILIATED;
  431. //debug_log([__METHOD__, __LINE__, "stringCodeStoreUserAffiliated:" . $transfer_money.'$referrer'.$affiliated], "app_debug.log");
  432. UserStringCodeOrder::transferAddUserWallet($order, $saas_id, $money, $type, $model_val);
  433. }
  434. }
  435. public static function stringCodeStoreUserReferrer($order, $setting, $transfer_money, $model_val)
  436. {
  437. $referrer = 0;
  438. if ($order instanceof ScanOrder) {
  439. if (isset($setting['string_code_store_user_referrer']) && $setting['string_code_store_user_referrer'] > 0) {
  440. $referrer = $setting['string_code_store_user_referrer'];
  441. }
  442. } else {
  443. if (isset($setting['string_code_brand_user_referrer']) && $setting['string_code_brand_user_referrer'] > 0) {
  444. $referrer = $setting['string_code_brand_user_referrer'];
  445. }
  446. }
  447. if ($referrer > 0) {
  448. $user = User::findOne($order->user_id);
  449. if ($user->id > 0) {
  450. $saas_id = SaasUser::findSaasParentIdByUserId($order->user_id);
  451. $money = bcmul($transfer_money, $referrer / 100, 4);
  452. $type = UserStringCodeOrder::TYPE_STORE_USER_REFERRER;
  453. //debug_log([__METHOD__, __LINE__, "stringCodeStoreUserReferrer:" . $transfer_money.'$referrer'.$referrer], "app_debug.log");
  454. UserStringCodeOrder::transferAddUserWallet($order, $saas_id, $money, $type, $model_val);
  455. }
  456. }
  457. }
  458. /**
  459. * @param $store_id
  460. * @return bool
  461. */
  462. public static function checkSettingByStoreId($store_id, $setting)
  463. {
  464. if (!$setting) {
  465. //debug_log([__METHOD__, __LINE__, "店铺:【{$store_id}】未配置串码设置"], "app_debug.log");
  466. return false;
  467. }
  468. if (!isset($setting['string_code_store_switch']) || $setting['string_code_store_switch'] != 1) {
  469. //debug_log([__METHOD__, __LINE__, "店铺:【{$store_id}】未开启串码功能"], "app_debug.log");
  470. return false;
  471. }
  472. if (!isset($setting['string_code_model'])) {
  473. //debug_log([__METHOD__, __LINE__, "店铺:【{$store_id}】未设置串码模式"], "app_debug.log");
  474. return false;
  475. }
  476. if (!is_array($setting['string_code_model'])) {
  477. //debug_log([__METHOD__, __LINE__, "店铺:【{$store_id}】串码模式配置不是数组"], "app_debug.log");
  478. return false;
  479. }
  480. return true;
  481. }
  482. public static function getDefPools($bd_pools, $store_pools)
  483. {
  484. // 确保都是数组格式
  485. $bd_pools = array_map(function($item) {
  486. return is_array($item) ? $item : (array)$item;
  487. }, $bd_pools);
  488. $store_pools = array_map(function($item) {
  489. return is_array($item) ? $item : (array)$item;
  490. }, $store_pools);
  491. $currentIds = ArrayHelper::getColumn($bd_pools, 'config_id');
  492. $targetIds = ArrayHelper::getColumn($store_pools, 'config_id');
  493. $idsToDelete = array_diff($currentIds, $targetIds);
  494. $idsToAdd = array_diff($targetIds, $currentIds);
  495. // 删除操作
  496. if (!empty($idsToDelete)) {
  497. $bd_pools = array_values(array_filter($bd_pools, function($pool) use ($idsToDelete) {
  498. return !in_array($pool['config_id'], $idsToDelete);
  499. }));
  500. }
  501. // 新增操作
  502. if (!empty($idsToAdd)) {
  503. foreach ($store_pools as $item) {
  504. if (in_array($item['config_id'], $idsToAdd)) {
  505. $newItem = $item;
  506. $newItem['rate'] = 0;
  507. $bd_pools[] = $newItem;
  508. }
  509. }
  510. }
  511. return $bd_pools;
  512. }
  513. public static function getAwardType($store_id, $is_scan,$award_type = 'award_type')
  514. {
  515. if (!$is_scan){
  516. $store_setting = Option::getDecode(OptionSetting::PLUGIN_CONFIG_POOL_SETTING, $store_id, OptionSetting::SHARE_GROUP_NAME);
  517. $type = $store_setting[$award_type] ?? 1;
  518. }else{
  519. $bd_admin = Admin::findOne(['store_id'=>$store_id,'type'=>'bd_agent','is_delete'=>0,'is_enable'=>1]);
  520. if (empty($bd_admin)){
  521. $admin_id = Salesman::getBdId($store_id);
  522. if ($admin_id){
  523. $bd_admin = Admin::findOne(['id'=>$admin_id,'type'=>'bd_agent','is_delete'=>0,'is_enable'=>1]);
  524. }
  525. }
  526. $bd_setting = [];
  527. if ($bd_admin){
  528. $bd_setting = Option::getDecode(OptionSetting::SHARE_STRING_CODE_SALE_SETTING_BD, $bd_admin->id, OptionSetting::SHARE_GROUP_NAME);
  529. }
  530. $type = $bd_setting[$award_type] ?? 1;
  531. }
  532. return $type;
  533. }
  534. public static function poolSetting($store_id)
  535. {
  536. $plugin_setting = Option::getDecode(OptionSetting::PLUGIN_CONFIG_POOL_SETTING, $store_id, OptionSetting::SHARE_GROUP_NAME);
  537. if (empty($plugin_setting) || !isset($plugin_setting['is_enable']) || !$plugin_setting['is_enable']) {
  538. $plugin_setting = Option::getDecode(OptionSetting::PLUGIN_CONFIG_POOL_SETTING, 197, OptionSetting::SHARE_GROUP_NAME);
  539. if (empty($plugin_setting) || !isset($plugin_setting['is_enable']) || !$plugin_setting['is_enable']) {
  540. return false;
  541. }
  542. }
  543. return $plugin_setting;
  544. }
  545. }