HiValueLogic.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. <?php
  2. namespace addons\HiGo\common\logic;
  3. use addons\BusinessBonus\common\models\BusinessBonusStore;
  4. use addons\HiGo\common\enums\HiGoEnum;
  5. use addons\HiGo\common\models\HigoGoodsSetting;
  6. use addons\HiGo\common\models\HigoMerchantRewardLog;
  7. use addons\HiGo\common\models\HigoStoreGoodsMode;
  8. use addons\HiGo\common\models\HigoWallet;
  9. use addons\HiGo\common\models\HigoWalletFrozenLog;
  10. use addons\HiGo\common\models\HigoWalletLog;
  11. use addons\Mch\common\models\Mch;
  12. use addons\Store\common\models\Store;
  13. use addons\Store\common\models\StoreCashierOrder;
  14. use common\enums\AddonsEnum;
  15. use common\enums\StatusEnum;
  16. use common\models\common\CapitalLog;
  17. use common\models\mall\MallAddons;
  18. use common\models\order\Order;
  19. use common\models\order\OrderDetail;
  20. use common\traits\ErrorTrait;
  21. use Exception;
  22. /**
  23. * Class HiValueLogic
  24. * 嗨值逻辑类
  25. * @Author: mz
  26. * @DateTime: 2022/11/08 16:39
  27. * @Copyright: copyright (c) 2022 广东七件事集团
  28. */
  29. class HiValueLogic
  30. {
  31. use ErrorTrait;
  32. /**
  33. * 商品设置营销赠送嗨值-商城订单、门店订单、多商户订单发放嗨值
  34. * @param $order
  35. * @throws
  36. * @return bool
  37. */
  38. public static function sendGoodsSettingHiValue($order)
  39. {
  40. $mall_id = $order['mall_id'];
  41. $user_id = $order['user_id'];
  42. $source_table = Order::tableName();
  43. $from_type= HiGoEnum::FROM_TYPE_ORDER_GOODS;
  44. //嗨值自定义名称
  45. $setting = \Yii::$app->services->setting->addons->get($mall_id, HiGoEnum::ADDONS_NAME, 'basic');
  46. $name = $setting['hi_value_name'] ?? '嗨值';
  47. //查询订单商品
  48. $order_goods = OrderDetail::find()->where(['order_id' => $order['id']])->asArray()->all();
  49. foreach ($order_goods as $info){
  50. $is_frozen = false;
  51. $source = $info['store_id'] > 0 ? 'Store' : ($info['mch_id'] > 0 ? 'Mch' : 'Main');
  52. $goods_setting = HigoGoodsSetting::getGoodsSetting($info['goods_id'],HiGoEnum::WALLET_TYPE_HI_VALUE,$source);
  53. if(!$goods_setting){
  54. continue;
  55. }
  56. //检测该订单是否发放过该奖励
  57. if(HigoWalletLog::checkSend($mall_id,$user_id,$source_table,$order['id'],$from_type,HiGoEnum::WALLET_TYPE_HI_VALUE)){
  58. continue;
  59. }
  60. if(HigoWalletFrozenLog::checkSend($mall_id,$user_id,$source_table,$order['id'],$from_type,HiGoEnum::WALLET_TYPE_HI_VALUE)){
  61. continue;
  62. }
  63. if($goods_setting['settlement_method'] == HiGoEnum::SETTLEMENT_METHOD_AFTER_FINISH){
  64. //订单完成时结算,先存入冻结记录表,待订单完成时去 结算该奖励
  65. $is_frozen = true;
  66. }
  67. $change_num = $goods_setting['give'];
  68. if($change_num <= 0 ) continue;
  69. //计算赠送嗨值
  70. if($goods_setting['is_percent'] == 1){
  71. //百分比
  72. $change_num = bcmul($info['goods_price'],$goods_setting['give']/100,10);
  73. }
  74. $desc = '用户('.$user_id.')获得订单('.$order['id'].')营销赠送'.$name;
  75. $wallet_log = [
  76. 'mall_id' => $mall_id,
  77. 'user_id' => $user_id,
  78. 'change_type' => CapitalLog::CHANGE_TYPE_ADD,
  79. 'change_num' => $change_num,
  80. 'desc' => $desc,
  81. 'source_table' => $source_table,
  82. 'source_table_id' => $order['id'],
  83. 'from_type' => $from_type,
  84. 'wallet_type' => HiGoEnum::WALLET_TYPE_HI_VALUE,
  85. 'is_frozen' => $is_frozen,
  86. 'addon_name' => HiGoEnum::ADDONS_NAME,
  87. 'setting' => json_encode($goods_setting)
  88. ];
  89. if($is_frozen){
  90. $res = HigoWalletFrozenLog::setData($wallet_log);
  91. if(!$res) throw new Exception( HigoWalletFrozenLog::getStaticError());
  92. }else{
  93. WalletLogic::handleInQueue($wallet_log);
  94. }
  95. }
  96. return true;
  97. }
  98. /**
  99. * 门店/多商户 订单发放嗨值奖励
  100. * @param $order
  101. * @param int $settlement_method
  102. * @return bool|void
  103. * @throws Exception
  104. */
  105. public static function sendMerchantOrderHiValue($order,$settlement_method = 0)
  106. {
  107. $mall_id = $order['mall_id'];
  108. $user_id = $order['user_id'];
  109. $source_table = Order::tableName();
  110. $from_type= HiGoEnum::FROM_TYPE_STORE_ORDER;
  111. //嗨值自定义名称
  112. $setting = \Yii::$app->services->setting->addons->get($mall_id, HiGoEnum::ADDONS_NAME, 'basic');
  113. $name = $setting['hi_value_name'] ?? '嗨值';
  114. if (empty($setting['merchant_buy_goods'])) return ;
  115. $is_frozen = false;
  116. if($settlement_method != $setting['payment_method'] && $settlement_method != HiGoEnum::SETTLEMENT_METHOD_AFTER_FINISH){
  117. $is_frozen = true;
  118. }
  119. $store_user_id = 0;
  120. $store_id = 0;
  121. if($order['store_id'] > 0){
  122. $store = Store::findOne(['id' => $order['store_id']]);
  123. $store_user_id = $store['user_id'] ?? 0;
  124. $store_id = $store['id'];
  125. }elseif($order['mch_id'] > 0){
  126. $mch = Mch::findOne(['id' => $order['mch_id']]);
  127. $store_user_id = $mch['user_id'] ?? 0;
  128. }
  129. if(!$store_user_id) return ;
  130. $business_user_id = 0;
  131. if (MallAddons::isInstall($mall_id, AddonsEnum::ADDONS_NAME_BUSINESS_BONUS) ){
  132. $business = BusinessBonusStore::findOne(['mall_id' => $mall_id,'store_id' => $order['store_id'] > 0 ? $order['store_id'] : $order['mch_id'],'store_type' => $order['store_id'] > 0 ? 1 : 2]);
  133. $business_user_id = $business['business_user_id'] ?? 0;
  134. }
  135. //查询订单商品
  136. $insert_wallet = [];
  137. $order_goods = OrderDetail::find()->where(['order_id' => $order['id']])->asArray()->all();
  138. if($store_id > 0){
  139. // 商家让利比例
  140. $merchant_profit_multiple = RewardLogic::getMerchantProfit($order['mall_id'], $store['user_id'], $store_id);
  141. }else{
  142. $merchant_profit_multiple = $setting['merchant_profit_multiple'];
  143. }
  144. foreach ($order_goods as $info){
  145. $goods_setting = HigoStoreGoodsMode::findOne(['mall_id' => $mall_id,'goods_id' =>$info['goods_id'],'is_enable' => 1 ,'status' => StatusEnum::ENABLED]);
  146. if(!$goods_setting){
  147. continue;
  148. }
  149. //检测该订单是否发放过该奖励
  150. if(HigoWalletLog::checkSend($mall_id,$user_id,$source_table,$order['id'],$from_type,HiGoEnum::WALLET_TYPE_HI_VALUE)){
  151. continue;
  152. }
  153. //商家让利部分
  154. // $profit = bcmul($info['goods_price'],$setting['merchant_profit_multiple']/100,10);
  155. $profit = bcmul($info['goods_price'], $merchant_profit_multiple/100,10);
  156. $consumer_rewards = bcmul($profit,$setting['consumer_rewards'],10);
  157. $merchant_rewards = bcmul($profit,$setting['merchant_rewards'],10);
  158. $business_rewards = bcmul($profit,$setting['business_rewards'],10);
  159. if($consumer_rewards > 0){
  160. $desc = '用户('.$user_id.')获得订单('.$order['id'].')消费者奖励,获得奖励'.$name;
  161. $insert_wallet[] = [
  162. 'mall_id' => $mall_id,
  163. 'user_id' => $user_id,
  164. 'change_type' => CapitalLog::CHANGE_TYPE_ADD,
  165. 'change_num' => $consumer_rewards,
  166. 'desc' => $desc,
  167. 'source_table' => $source_table,
  168. 'source_table_id' => $order['id'],
  169. 'from_type' => HiGoEnum::FROM_TYPE_STORE_ORDER,
  170. 'wallet_type' => HiGoEnum::WALLET_TYPE_HI_VALUE,
  171. 'addon_name' => HiGoEnum::ADDONS_NAME,
  172. 'is_send' => 1
  173. ];
  174. //WalletLogic::handleInQueue($wallet_log,false);
  175. }
  176. if($merchant_rewards && $store_user_id > 0){
  177. $desc = '用户下单获得订单('.$order['id'].')商家奖励,获得'.$name;
  178. $insert_wallet[] = [
  179. 'mall_id' => $mall_id,
  180. 'user_id' => $store_user_id,
  181. 'change_type' => CapitalLog::CHANGE_TYPE_ADD,
  182. 'change_num' => $merchant_rewards,
  183. 'desc' => $desc,
  184. 'source_table' => $source_table,
  185. 'source_table_id' => $order['id'],
  186. 'from_type' => HiGoEnum::FROM_TYPE_STORE_ORDER,
  187. 'wallet_type' => HiGoEnum::WALLET_TYPE_HI_VALUE,
  188. 'addon_name' => HiGoEnum::ADDONS_NAME,
  189. 'is_send' => 1
  190. ];
  191. //WalletLogic::handleInQueue($wallet_log,false);
  192. }
  193. if($business_user_id >0 && $business_rewards){
  194. $desc = '用户下单获得订单('.$order['id'].')招商员奖励获得'.$name;
  195. $insert_wallet[] = [
  196. 'mall_id' => $mall_id,
  197. 'user_id' => $business_user_id,
  198. 'change_type' => CapitalLog::CHANGE_TYPE_ADD,
  199. 'change_num' => $business_rewards,
  200. 'desc' => $desc,
  201. 'source_table' => $source_table,
  202. 'source_table_id' => $order['id'],
  203. 'from_type' => HiGoEnum::FROM_TYPE_STORE_ORDER,
  204. 'wallet_type' => HiGoEnum::WALLET_TYPE_HI_VALUE,
  205. 'addon_name' => HiGoEnum::ADDONS_NAME,
  206. 'is_send' => 1
  207. ];
  208. //WalletLogic::handleInQueue($wallet_log,false);
  209. }
  210. }
  211. if(!empty($insert_wallet)){
  212. foreach($insert_wallet as $wallet_log){
  213. if($is_frozen){
  214. //冻结记录
  215. $res = HigoWalletFrozenLog::setData($wallet_log);
  216. if(!$res) throw new \Exception(HigoWalletFrozenLog::getStaticError());
  217. }else{
  218. $res = WalletLogic::handleInQueue($wallet_log,false);
  219. if(!$res) throw new \Exception(WalletLogic::getStaticError());
  220. }
  221. }
  222. }
  223. return true;
  224. }
  225. public static function sendMerchantCashierOrderHiValue($order)
  226. {
  227. $mall_id = $order['mall_id'];
  228. $user_id = $order['user_id'];
  229. $source_table = StoreCashierOrder::tableName();
  230. $from_type = HiGoEnum::FROM_TYPE_STORE_ORDER;
  231. //嗨值自定义名称
  232. $setting = \Yii::$app->services->setting->addons->get($mall_id, HiGoEnum::ADDONS_NAME, 'basic');
  233. $name = $setting['hi_value_name'] ?? '嗨值';
  234. if (empty($setting['merchant_buy_goods'])) return;
  235. $store = Store::findOne(['id' => $order['store_id']]);
  236. $store_user_id = $store['user_id'] ?? 0;
  237. if (!$store_user_id) return;
  238. $business_user_id = 0;
  239. if (MallAddons::isInstall($mall_id, AddonsEnum::ADDONS_NAME_BUSINESS_BONUS)) {
  240. $business = BusinessBonusStore::findOne(['mall_id' => $mall_id, 'store_id' => $order['store_id'] > 0 ? $order['store_id'] : $order['mch_id'], 'store_type' => $order['store_id'] > 0 ? 1 : 2]);
  241. $business_user_id = $business['business_user_id'] ?? 0;
  242. }
  243. //查询订单商品
  244. $insert_wallet = [];
  245. //检测该订单是否发放过该奖励
  246. if (HigoWalletLog::checkSend($mall_id, $user_id, $source_table, $order['id'], $from_type, HiGoEnum::WALLET_TYPE_HI_VALUE)) {
  247. return;
  248. }
  249. $merchant_profit_multiple = RewardLogic::getMerchantProfit($order['mall_id'], $store['user_id'], $store['id']);
  250. //商家让利部分
  251. // $profit = bcmul($order['pay_money'], $setting['merchant_profit_multiple'] / 100, 10);
  252. $profit = bcmul($order['pay_money'], $merchant_profit_multiple / 100, 10);
  253. // 消费者奖励的嗨值
  254. $consumer_rewards = bcmul($profit, $setting['consumer_rewards'], 10);
  255. // 商家奖励的嗨值
  256. $merchant_rewards = bcmul($profit, $setting['merchant_rewards'], 10);
  257. // 招商员奖励的嗨值
  258. $business_rewards = bcmul($profit, $setting['business_rewards'], 10);
  259. if ($consumer_rewards > 0) {
  260. $desc = '用户(' . $user_id . ')获得订单(' . $order['id'] . ')消费者奖励,获得奖励' . $name;
  261. $insert_wallet[] = [
  262. 'mall_id' => $mall_id,
  263. 'user_id' => $user_id,
  264. 'change_type' => CapitalLog::CHANGE_TYPE_ADD,
  265. 'change_num' => $consumer_rewards,
  266. 'desc' => $desc,
  267. 'source_table' => $source_table,
  268. 'source_table_id' => $order['id'],
  269. 'from_type' => HiGoEnum::FROM_TYPE_STORE_ORDER,
  270. 'wallet_type' => HiGoEnum::WALLET_TYPE_HI_VALUE,
  271. 'addon_name' => HiGoEnum::ADDONS_NAME,
  272. 'is_send' => 1
  273. ];
  274. //WalletLogic::handleInQueue($wallet_log,false);
  275. }
  276. if ($merchant_rewards && $store_user_id > 0) {
  277. $desc = '用户下单获得订单(' . $order['id'] . ')商家奖励,获得' . $name;
  278. $insert_wallet[] = [
  279. 'mall_id' => $mall_id,
  280. 'user_id' => $store_user_id,
  281. 'change_type' => CapitalLog::CHANGE_TYPE_ADD,
  282. 'change_num' => $merchant_rewards,
  283. 'desc' => $desc,
  284. 'source_table' => $source_table,
  285. 'source_table_id' => $order['id'],
  286. 'from_type' => HiGoEnum::FROM_TYPE_STORE_ORDER,
  287. 'wallet_type' => HiGoEnum::WALLET_TYPE_HI_VALUE,
  288. 'addon_name' => HiGoEnum::ADDONS_NAME,
  289. 'is_send' => 1
  290. ];
  291. //WalletLogic::handleInQueue($wallet_log,false);
  292. }
  293. if ($business_user_id > 0 && $business_rewards) {
  294. $desc = '用户下单获得订单(' . $order['id'] . ')招商员奖励获得' . $name;
  295. $insert_wallet[] = [
  296. 'mall_id' => $mall_id,
  297. 'user_id' => $business_user_id,
  298. 'change_type' => CapitalLog::CHANGE_TYPE_ADD,
  299. 'change_num' => $business_rewards,
  300. 'desc' => $desc,
  301. 'source_table' => $source_table,
  302. 'source_table_id' => $order['id'],
  303. 'from_type' => HiGoEnum::FROM_TYPE_STORE_ORDER,
  304. 'wallet_type' => HiGoEnum::WALLET_TYPE_HI_VALUE,
  305. 'addon_name' => HiGoEnum::ADDONS_NAME,
  306. 'is_send' => 1
  307. ];
  308. //WalletLogic::handleInQueue($wallet_log,false);
  309. }
  310. if (!empty($insert_wallet)) {
  311. foreach ($insert_wallet as $wallet_log) {
  312. $res = WalletLogic::handleInQueue($wallet_log, false);
  313. if (!$res) throw new \Exception(WalletLogic::getStaticError());
  314. }
  315. }
  316. return true;
  317. }
  318. /**
  319. * 检测奖励是否发放过
  320. * @param $mall_id
  321. * @param $user_id
  322. * @param $source_table
  323. * @param $source_id
  324. * @param $from_type
  325. * @return HigoWalletLog|null
  326. */
  327. // private static function checkSend($mall_id,$user_id,$source_table,$source_id,$from_type)
  328. // {
  329. //
  330. // $log = HigoWalletLog::findOne(['mall_id' => $mall_id,'user_id' => $user_id,'wallet_type' => HiGoEnum::WALLET_TYPE_HI_VALUE,
  331. // 'change_type' => 'add','source_table' => $source_table,'source_table_id' => $source_id,'from_type' => $from_type,'status' => StatusEnum::ENABLED]);
  332. // return $log;
  333. // }
  334. /**
  335. * 嗨值销毁
  336. * @param $mall_id
  337. * @param $destroy_formula 0-按比例,1-个人今日嗨值=个人昨日持有嗨值-个人昨日持有嗨贝*昨日嗨贝价值,2-个人今日嗨值=个人昨日持有嗨值-个人昨日获取嗨贝*昨日嗨贝价值
  338. * @param int $destroy_ratio 销毁比例
  339. * @throws
  340. * @return
  341. */
  342. public static function destroy($mall_id,$destroy_formula,$destroy_ratio = 0)
  343. {
  344. $where = [
  345. 'and',
  346. ['mall_id' => $mall_id],
  347. ['>', 'hi_value', 0],
  348. ['status' => StatusEnum::ENABLED],
  349. ];
  350. //查询商城下嗨值大于0的用户
  351. foreach (HigoWallet::find()->where($where)->each(500) as $user_wallet) {
  352. $need_destory_value = self::computeUserDestoryHiValue($user_wallet,$destroy_formula,$destroy_ratio);
  353. if($need_destory_value > 0 ){
  354. $desc = '每天嗨值销毁';
  355. $wallet_log = [
  356. 'mall_id' => $user_wallet->mall_id,
  357. 'user_id' => $user_wallet->user_id,
  358. 'change_type' => CapitalLog::CHANGE_TYPE_SUB,
  359. 'change_num' => $need_destory_value * -1,
  360. 'desc' => $desc,
  361. 'source_table' => '',
  362. 'source_table_id' => 0,
  363. 'from_type' => HiGoEnum::FROM_TYPE_DESTROY,
  364. 'wallet_type' => HiGoEnum::WALLET_TYPE_HI_VALUE
  365. ];
  366. WalletLogic::handleInQueue($wallet_log);
  367. }
  368. }
  369. return true;
  370. }
  371. /**
  372. * 计算用户需要销毁的嗨值
  373. * @param $user_wallet
  374. * @param $destroy_formula
  375. * @param $destroy_ratio
  376. * @return
  377. */
  378. private static function computeUserDestoryHiValue($user_wallet,$destroy_formula,$destroy_ratio)
  379. {
  380. $return = 0;
  381. //检测用户今天是否已销毁
  382. $check = HigoWalletLog::find()->where(['user_id' => $user_wallet['user_id'],'mall_id' => $user_wallet['mall_id'],'from_type' =>HiGoEnum::FROM_TYPE_DESTROY, 'wallet_type' => HiGoEnum::WALLET_TYPE_HI_VALUE])->andWhere(['>','created_at',strtotime(date('Y-m-d 00:00:00'))])->one();
  383. if($check){
  384. return $return;
  385. }
  386. if($destroy_formula == 0){
  387. //按比例销毁
  388. $destroy_ratio = $destroy_ratio > 100 ? 100 : $destroy_ratio; //最大100
  389. $return = bcmul($user_wallet['hi_value'],$destroy_ratio/100,10);
  390. }elseif($destroy_formula == 1){
  391. //1-个人今日嗨值=个人昨日持有嗨值-个人昨日持有嗨贝*昨日嗨贝价值
  392. $now_hi_bei_price = HiBeiLogic::getNowPrice($user_wallet['mall_id']);
  393. $return = bcmul($now_hi_bei_price,$user_wallet['hi_bei'],10);
  394. }elseif ($destroy_formula == 2){
  395. //2-个人今日嗨值=个人昨日持有嗨值-个人昨日获取嗨贝*昨日嗨贝价值
  396. $now_hi_bei_price = HiBeiLogic::getNowPrice($user_wallet['mall_id']);
  397. //个人昨日获取嗨贝
  398. $yesterday = HigoWalletLog::find()->where([
  399. 'mall_id' => $user_wallet['mall_id'],
  400. 'user_id'=>$user_wallet['user_id'],
  401. 'wallet_type'=> HiGoEnum::WALLET_TYPE_HI_BEI,
  402. 'change_type' => CapitalLog::CHANGE_TYPE_ADD,
  403. 'status' => StatusEnum::ENABLED
  404. ])->andWhere(['>','created_at',strtotime('yesterday')])
  405. ->andWhere(['<','created_at',strtotime(date('Y-m-d'))])
  406. ->select('sum(change_num) as total')->asArray()->one();
  407. $yesterday_hi_bei = $yesterday['total'] ?:0;
  408. $return = bcmul($now_hi_bei_price,$yesterday_hi_bei,10);
  409. }
  410. return $return;
  411. }
  412. }