OrderCheck.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace addons\HiGo\common\global_func;
  3. use addons\HiGo\common\enums\HiGoEnum;
  4. use addons\HiGo\common\logic\HiBeiLogic;
  5. use addons\HiGo\common\models\HigoWallet;
  6. use addons\Mch\common\models\Mch;
  7. use addons\Store\common\models\Store;
  8. use common\enums\StatusEnum;
  9. use common\models\user\UserWallet;
  10. use Yii;
  11. class OrderCheck
  12. {
  13. /**
  14. * 检测商家订单嗨呗数量是否充足
  15. * @param $params
  16. * @return array
  17. */
  18. public function getNeedHiBei($params)
  19. {
  20. $return = [
  21. 'status' => true,
  22. 'msg' => 'success'
  23. ];
  24. try{
  25. $mall_id = $params['mall_id'];
  26. $store_id = $params['store_id'] ?? 0;
  27. $mch_id = $params['mch_id'] ?? 0;
  28. //todo 修改验证门店是否参与嗨购
  29. if(!$store_id && !$mch_id) throw new \Exception('非商家订单');
  30. $pay_money = $params['pay_money'] ?? 0;
  31. $setting = \Yii::$app->services->setting->addons->get($mall_id, HiGoEnum::ADDONS_NAME, 'basic');
  32. if (empty($setting['merchant_buy_goods'])) throw new \Exception('未开启商家嗨购商品');
  33. if(empty($setting['is_order_settings'])) throw new \Exception('下单设置');
  34. //嗨呗当前价值
  35. $now_price = HiBeiLogic::getNowPrice($mall_id);
  36. if (bccomp($now_price, 0, 10) <= 0) {
  37. throw new \Exception('嗨呗当前价格异常');
  38. }
  39. //商家让利部分
  40. $profit = bcmul($pay_money,$setting['merchant_profit_multiple']/100,10);
  41. $hi_bei_num = bcdiv($profit,$now_price,10);
  42. //获取商家当前嗨呗数量
  43. if($store_id > 0 ){
  44. $store = Store::findOne(['mall_id' => $mall_id, 'id' => $store_id, 'status' => StatusEnum::ENABLED]);
  45. $mch_user_id = $store['user_id'];
  46. }else{
  47. $mch = Mch::findOne(['mall_id' => $mall_id, 'id' => $mch_id, 'status' => StatusEnum::ENABLED]);
  48. $mch_user_id = $mch['user_id'];
  49. }
  50. if(!$mch_user_id) throw new \Exception('查询不到该商家');
  51. $wallet = HigoWallet::findOne(['mall_id' => $mall_id,'user_id' => $mch_user_id,'status' => StatusEnum::ENABLED]);
  52. if(!$wallet || bccomp($wallet->hi_bei,$hi_bei_num,10) < 0){
  53. $return['status'] = false;
  54. $return['msg'] = $setting['buy_product_prompt'] ?? '商家嗨呗不足';
  55. }
  56. }catch(\Exception $e){
  57. Yii::error('getNeedHiBei error:'.$e->getMessage());
  58. }
  59. return $return;
  60. }
  61. }