RewardLogic.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <?php
  2. namespace addons\HiGo\common\logic;
  3. use addons\HiGo\common\enums\HiGoEnum;
  4. use addons\HiGo\common\models\HigoGoodsSetting;
  5. use addons\HiGo\common\models\HigoHibeiLog;
  6. use addons\HiGo\common\models\HigoMall;
  7. use addons\HiGo\common\models\HigoMerchantRewardLog;
  8. use addons\HiGo\common\models\HigoStoreGoodsMode;
  9. use addons\HiGo\common\models\HigoWallet;
  10. use addons\HiGo\common\models\HigoWalletFreezeLog;
  11. use addons\HiGo\common\models\HigoWalletLog;
  12. use addons\Mch\common\models\Mch;
  13. use addons\Store\common\models\Store;
  14. use addons\Store\common\models\StoreCashierOrder;
  15. use common\enums\RabbitMqEnum;
  16. use common\enums\StatusEnum;
  17. use common\models\common\CapitalLog;
  18. use common\models\order\Order;
  19. use common\models\order\OrderDetail;
  20. use common\models\user\User;
  21. use common\traits\ErrorTrait;
  22. use Exception;
  23. /**
  24. * Class HiValueLogic
  25. * 打赏逻辑类
  26. * @Author: mz
  27. * @DateTime: 2022/11/18 16:39
  28. * @Copyright: copyright (c) 2022 广东七件事集团
  29. */
  30. class RewardLogic
  31. {
  32. use ErrorTrait;
  33. /**
  34. * 获取门店订单对应打赏金额
  35. * @param $order_no
  36. * @param array $mch 门店身份/多商户身份
  37. * @return array|bool
  38. */
  39. public static function getOrderMerchantReward($order_no,$mch)
  40. {
  41. try{
  42. if(isset($mch['store'])){
  43. $mch_id = $mch['store']['mch_id'];
  44. $order = Order::findOne(['order_no' => $order_no,'store_id' => $mch_id,'pay_status' => 1,'status' => StatusEnum::ENABLED]);
  45. }
  46. if(!$order && isset($mch['mch'])){
  47. $mch_id = $mch['mch']['mch_id'];
  48. $order = Order::findOne(['order_no' => $order_no,'mch_id' => $mch_id,'pay_status' => 1,'status' => StatusEnum::ENABLED]);
  49. }
  50. if(!$order) throw new Exception('订单不存在');
  51. $setting = \Yii::$app->services->setting->addons->get($order['mall_id'], HiGoEnum::ADDONS_NAME, 'basic');
  52. if (empty($setting['merchant_buy_goods'])) throw new Exception('系统不支持该操作');
  53. //查询是否已打赏
  54. if(HigoMerchantRewardLog::checkReward($order['mall_id'],Order::tableName(),$order->id)){
  55. throw new Exception('订单已打赏');
  56. }
  57. //商家让利部分
  58. $profit = bcmul($order['pay_money'],$setting['merchant_profit_multiple']/100,10);
  59. //金额 换算成 嗨呗
  60. $now_price = HiBeiLogic::getNowPrice($order['mall_id']);
  61. //$hi_bei_num = bcmul($profit,$now_price,10);
  62. $hi_bei_num = bcdiv($profit,$now_price,10);
  63. $return = [
  64. 'hi_bei' => $hi_bei_num,
  65. 'hi_value' => $hi_bei_num,
  66. 'pay_money' => $order['pay_money']
  67. ];
  68. return $return;
  69. }catch(Exception $e){
  70. self::setStaticError($e->getMessage(),1);
  71. return false;
  72. }
  73. }
  74. /**
  75. * 订单进行打赏
  76. * @param $order_no
  77. * @param $mch_id
  78. * @param $mch_user_id
  79. * @param $mch_name
  80. * @return
  81. */
  82. public static function doMerchantReward($order_no,$mch_id,$mch_user_id,$mch_name)
  83. {
  84. try{
  85. $is_store = true;
  86. $order = Order::findOne(['order_no' => $order_no,'store_id' => $mch_id,'pay_status' => 1,'status' => StatusEnum::ENABLED]);
  87. if(!$order){
  88. $order = Order::findOne(['order_no' => $order_no,'mch_id' => $mch_id,'pay_status' => 1,'status' => StatusEnum::ENABLED]);
  89. $is_store = false;
  90. }
  91. if(!$order) throw new Exception('订单不存在');
  92. $setting = \Yii::$app->services->setting->addons->get($order['mall_id'], HiGoEnum::ADDONS_NAME, 'basic');
  93. $hi_bei_name = $setting['hi_bei_name'] ?? '嗨贝';
  94. if (empty($setting['merchant_buy_goods'])) return ;
  95. //查询是否已打赏
  96. if(HigoMerchantRewardLog::checkReward($order['mall_id'],Order::tableName(),$order->id)){
  97. throw new Exception('订单已打赏');
  98. }
  99. //商家让利部分
  100. $profit = bcmul($order['pay_money'],$setting['merchant_profit_multiple']/100,10);
  101. $consumer_rewards_hi_value = bcmul($profit,$setting['consumer_rewards'],10);
  102. $merchant_rewards_hi_value = bcmul($profit,$setting['merchant_rewards'],10);
  103. //金额 换算成 嗨呗
  104. $now_price = HiBeiLogic::getNowPrice($order['mall_id']);
  105. if(!$now_price) throw new Exception('嗨呗价格为0');
  106. //$hi_bei_num = bcmul($profit,$now_price,10);
  107. $hi_bei_num = bcdiv($profit,$now_price,10);
  108. //扣除商家 嗨呗
  109. if($is_store){
  110. $store = Store::findOne(['id' => $mch_id]);
  111. if(!$store) throw new Exception('商家不存在');
  112. }else{
  113. $mch = Mch::findOne(['id' => $mch_id]);
  114. if(!$mch) throw new Exception('商家不存在');
  115. }
  116. //说明:对订单(1122)进行打赏了20个嗨贝扣除多商户账户嗨贝
  117. $desc = '对订单('.$order->id.')进行打赏了'.$hi_bei_num.'个'.$hi_bei_name.'扣除多商户账户'.$hi_bei_name;
  118. $wallet_log = [
  119. 'mall_id' => $order->mall_id,
  120. 'user_id' => $mch_user_id,
  121. 'change_type' => CapitalLog::CHANGE_TYPE_SUB,
  122. 'change_num' => $hi_bei_num * -1,
  123. 'desc' => $desc,
  124. 'source_table' => Order::tableName(),
  125. 'source_table_id' => $order->id,
  126. 'from_type' => HiGoEnum::FROM_TYPE_MERCHANT_REWARD,
  127. 'wallet_type' => HiGoEnum::WALLET_TYPE_HI_BEI,
  128. 'is_send' => false
  129. ];
  130. $res = WalletLogic::handleInQueue($wallet_log,true);
  131. if(!$res) throw new Exception('打赏失败');
  132. $platform_account_user = $setting['platform_account_user'] ? json_decode($setting['platform_account_user'],true) : [];
  133. $platform_account_uid = $setting['enable_platform_account'] ? $platform_account_user['user_id'] : 0;
  134. if($setting['enable_platform_account']){
  135. $platform_account_uid = $platform_account_user['user_id'] ?? 0;
  136. $radio = $platform_account_user['ratio'] ?? 0;
  137. $platform_account_reward = bcmul($profit,$radio/100,10);
  138. }
  139. //添加打赏记录
  140. HigoMerchantRewardLog::setData([
  141. 'mall_id' => $order->mall_id,
  142. 'user_id' => $order->user_id,
  143. 'source_table' => Order::tableName(),
  144. 'source_table_id' => $order->id,
  145. 'profit_price' => $profit,
  146. 'profit' => $hi_bei_num,
  147. 'destroy' => bcmul($profit,$setting['destroy_settings']/100,10),
  148. 'platform_account_uid' => $platform_account_uid,
  149. 'platform_account_reward' => $platform_account_reward ?? 0,
  150. 'remaining_rewards' => bcmul($profit,$setting['remaining_rewards']/100,10),
  151. 'mch_name' => $mch_name,
  152. 'merchant_hi_value' => $merchant_rewards_hi_value,
  153. 'consumer_hi_value' => $consumer_rewards_hi_value,
  154. ]);
  155. //打赏后需要进行发放嗨值/嗨呗
  156. //打赏成功,发放对应嗨值,嗨呗模式
  157. $reward = [
  158. 'mall_id' => $order->mall_id,
  159. 'order_id' => $order['id'],
  160. 'operator_id' => 0,
  161. 'operator_name' => ''
  162. ];
  163. $event = new \addons\HiGo\common\events\RewardEvent($order->mall_id);
  164. \Yii::$app->services->events->dispatch($event, $reward, $event->listeners);
  165. return true;
  166. }catch(Exception $e){
  167. self::setStaticError($e->getMessage().',line:'.$e->getLine(),1);
  168. return false;
  169. }
  170. }
  171. /**
  172. * 打赏后处理
  173. * @param $log
  174. * @return
  175. * @throws
  176. */
  177. public static function afterReward($log)
  178. {
  179. if($log->handle_status) return true;
  180. $setting = \Yii::$app->services->setting->addons->get($log->mall_id, HiGoEnum::ADDONS_NAME, 'basic');
  181. //销毁
  182. if($log->destroy > 0){
  183. $order_no = '';
  184. //查询对应订单号
  185. if($log->source_table == Order::tableName()){
  186. $order = Order::findOne($log->source_table_id);
  187. $order_no = $order->order_no;
  188. }else if($log->source_table == StoreCashierOrder::tableName()){
  189. $order = StoreCashierOrder::findOne($log->source_table_id);
  190. $order_no = $order->order_no;
  191. }
  192. $res = HigoHibeiLog::setData([
  193. 'mall_id' => $log->mall_id,
  194. 'change_type' => CapitalLog::CHANGE_TYPE_SUB,
  195. 'change_num' => $log->destroy,
  196. 'desc' => '商家打赏销毁嗨呗,订单号:'.$order_no,
  197. 'from_type' => HigoHibeiLog::FROM_TYPE_DESTROY,
  198. 'setting' => json_encode($setting)
  199. ]);
  200. if(!$res) throw new Exception('添加嗨呗销毁记录失败');
  201. $higo_mall_model = HigoMall::findOne(['mall_id' => $log->mall_id]);
  202. $higo_mall_model->available_hi_bei = bcsub($higo_mall_model->available_hi_bei,$log->destroy,10);
  203. $higo_mall_model->save();
  204. }
  205. //平台账户奖励划分
  206. if($log->platform_account_uid > 0 && $log->platform_account_reward){
  207. $wallet_log = [
  208. 'mall_id' => $log->mall_id,
  209. 'user_id' => $log->platform_account_uid,
  210. 'change_type' => CapitalLog::CHANGE_TYPE_ADD,
  211. 'change_num' => $log->platform_account_reward,
  212. 'desc' => '商家打赏平台账户奖励划分',
  213. 'source_table' => $log->source_table,
  214. 'source_table_id' => $log->source_table_id,
  215. 'from_type' => HiGoEnum::FROM_TYPE_MERCHANT_REWARD,
  216. 'wallet_type' => HiGoEnum::WALLET_TYPE_HI_BEI,
  217. 'is_send' => 0,
  218. 'addon_name' => HiGoEnum::ADDONS_NAME,
  219. ];
  220. WalletLogic::handleInQueue($wallet_log);
  221. }
  222. $log->handle_status = 1;
  223. if(!$log->save()) throw new Exception('更新记录表状态失败');
  224. return true;
  225. }
  226. /**
  227. * 解冻 注册、复购等冻结嗨值
  228. * @param $order
  229. * @return null|void
  230. * @throws Exception
  231. */
  232. public static function unfreezeByOrder($order)
  233. {
  234. $mall_id = $order['mall_id'];
  235. $order_id = $order['id'];
  236. $user_id = $order['user_id'];
  237. $setting = \Yii::$app->services->setting->addons->get($mall_id, HiGoEnum::ADDONS_NAME, 'marketing');
  238. if(empty($setting)){
  239. return null;
  240. }
  241. $active_goods_list_ids = $goods_list_ids = [];
  242. //激活注册的商品
  243. $active_goods_list = $setting['active_goods_list'] ? json_decode($setting['active_goods_list'],true) : [];
  244. if($active_goods_list){
  245. $active_goods_list_ids = array_column($active_goods_list,'id');
  246. }
  247. //激活复购冻结的商品
  248. $goods_list = $setting['goods_list'] ? json_decode($setting['goods_list'],true) : [];
  249. if($goods_list){
  250. $goods_list_ids = array_column($goods_list,'id');
  251. }
  252. //判断商品
  253. $params = [];
  254. $params['where'] = [
  255. ['order_id' => $order_id],
  256. ['user_id' => $user_id], ['mall_id' => $mall_id],
  257. ];
  258. $order_detail_list = OrderDetail::lists($params);
  259. if (empty($order_detail_list)) return;
  260. $goods_id_arr = array_column($order_detail_list, 'goods_id');
  261. if($goods_list_ids && array_intersect($goods_list_ids,$goods_id_arr)){
  262. //复购激活
  263. //检测是否有冻结的嗨值
  264. $repurchase_freeze = HigoWalletFreezeLog::find()->where([
  265. 'mall_id' => $mall_id,
  266. 'user_id' => $user_id,
  267. // 'source_table' => User::tableName(),
  268. // 'source_table_id' => $user_id,
  269. 'from_type' => HiGoEnum::FROM_TYPE_REPURCHASE_ACTIVE,
  270. 'wallet_type' => HiGoEnum::WALLET_TYPE_HI_VALUE,
  271. 'status' => CapitalLog::STATUS_WAIT_SEND
  272. ])->asArray()->all();
  273. if($repurchase_freeze){
  274. foreach($repurchase_freeze as $freeze_log){
  275. $freeze_log['freeze_log_id'] = $freeze_log['id'];
  276. $freeze_log['unfreeze'] = true;
  277. unset($freeze_log['id']);
  278. WalletLogic::handleInQueue($freeze_log);
  279. }
  280. }
  281. }
  282. if($active_goods_list_ids && array_intersect($active_goods_list_ids,$goods_id_arr)){
  283. //注册激活
  284. //检测是否有冻结的嗨值
  285. $register_freeze = HigoWalletFreezeLog::find()->where(['mall_id' => $mall_id,
  286. // 'user_id' => $user_id,
  287. 'source_table' => User::tableName(),
  288. 'source_table_id' => $user_id,
  289. 'from_type' => [HiGoEnum::FROM_TYPE_REGISTER_REWARD,HiGoEnum::FROM_TYPE_REGISTER_PARENT],
  290. 'wallet_type' => HiGoEnum::WALLET_TYPE_HI_VALUE,
  291. 'status' => CapitalLog::STATUS_WAIT_SEND
  292. ])->asArray()->all();
  293. if($register_freeze){
  294. foreach($register_freeze as $freeze_log){
  295. //解冻
  296. $freeze_log['freeze_log_id'] = $freeze_log['id'];
  297. $freeze_log['unfreeze'] = true;
  298. unset($freeze_log['id']);
  299. WalletLogic::handleInQueue($freeze_log);
  300. }
  301. }
  302. }
  303. return true;
  304. }
  305. /**
  306. * @Description: 获取商家让利比例
  307. * @Author: zsan
  308. * @DateTime 2023-08-22 15:46:55
  309. * @param integer $mall_id
  310. * @param integer $user_id
  311. * @param integer $store_id
  312. * @return mixed
  313. */
  314. public static function getMerchantProfit($mall_id, $user_id, $store_id)
  315. {
  316. $profit_service_class = \addons\Store\common\services\MerchantReduceProfitService::class;
  317. /** @var \addons\Store\common\services\MerchantReduceProfitService $profit_service */
  318. $profit_service = new $profit_service_class();
  319. $profit = $profit_service->getProfit($mall_id, $user_id, $store_id);
  320. if($profit === false) {
  321. $setting = \Yii::$app->services->setting->addons->get($mall_id, HiGoEnum::ADDONS_NAME, 'basic');
  322. $profit = $setting['merchant_profit_multiple'] ?? 0;
  323. }
  324. return $profit;
  325. }
  326. }