OrderLimitLogic.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace addons\QiDingDong\common\logic;
  3. use addons\QiDingDong\common\enums\QiDingDongEnums;
  4. use addons\QiDingDong\common\expand\sdk\qidingdong\src\model\ShippingFeeModel;
  5. use addons\QiDingDong\common\expand\sdk\qidingdong\src\request\ShippingFeeRequest;
  6. use addons\QiDingDong\common\helper\RequestHelper;
  7. use addons\QiDingDong\common\models\QiDingDongGoods;
  8. use addons\QiDingDong\common\models\QiDingDongGoodsAttr;
  9. use addons\QiDingDong\common\models\QiDingDongRegion;
  10. use common\enums\StatusEnum;
  11. use common\logic\order\marketing\BaseMarketing;
  12. use common\models\user\UserAddress;
  13. class OrderLimitLogic extends BaseMarketing
  14. {
  15. public function execute($form)
  16. {
  17. // TODO: Implement execute() method.
  18. }
  19. /**
  20. * 此方法不在使用
  21. * @param $form
  22. * @return void
  23. * @throws \Exception
  24. */
  25. public static function compute($form)
  26. {
  27. if (!empty($form['data']['attr_id'])) {
  28. // 判断这件商品是否为企叮咚的商品
  29. $attr = QiDingDongGoodsAttr::findOne(['goods_attr_id' => $form['data']['attr_id']]);
  30. if (!empty($attr)) {
  31. // 判断件数
  32. $qGoods = QiDingDongGoods::findOne(['goods_id' => $attr->goods_id]);
  33. $floor = $qGoods->content['goods']['sampling_num']; // 起订量
  34. $specs = array_column($qGoods->content['specs'], 'package_num', 'spec_id');
  35. $_floor = $specs[$attr->third_spec_id] ?? 0;
  36. if ($form['data']['num'] != $floor && ($form['data']['num'] % $_floor) !== 0)
  37. throw new \Exception("起订量为 {$floor} 或 {$_floor} 的倍数");
  38. }
  39. }
  40. }
  41. /**
  42. * @param $params
  43. * @return array|void
  44. * @throws \Exception
  45. */
  46. public function validBuyNum($params)
  47. {
  48. $return = [
  49. 'status' => true,
  50. 'msg' => 'success'
  51. ];
  52. $attrs = $params['attrs'] ?? [];
  53. if(empty($attrs)) return $return;
  54. try {
  55. foreach ($attrs as $attr_id => $num) {
  56. $attr = QiDingDongGoodsAttr::findOne(['goods_attr_id' => $attr_id]);
  57. if (!empty($attr)) {
  58. // 判断件数
  59. $qGoods = QiDingDongGoods::findOne(['goods_id' => $attr->goods_id]);
  60. if (empty($qGoods->can_sale)) throw new \Exception("当前商品不可售");
  61. $floor = $qGoods->content['goods']['sampling_num']; // 起订量
  62. $specs = array_column($qGoods->content['specs'], 'package_num', 'spec_id');
  63. $_floor = $specs[$attr->third_spec_id] ?? 0;
  64. if ($num != $floor && ($num % $_floor) !== 0) throw new \Exception("起订量为 {$floor} 或 {$_floor} 的倍数");
  65. $addr = UserAddress::getUserAddress($params['user_id'], $params['address_id'] ?? 0);
  66. if (!empty($addr)) {
  67. $is_d = true;
  68. $region = QiDingDongRegion::findOne(['local_id' => $addr['district_id']]);
  69. if (empty($region)) {
  70. $is_d = false;
  71. $region = QiDingDongRegion::findOne(['local_id' => $addr['city_id']]);
  72. }
  73. // 取运费
  74. $shipContentModel = RequestHelper::contentModelInstance($qGoods->mall_id);
  75. $shipContentModel->content->data = new ShippingFeeModel();
  76. $shipContentModel->content->data->goods_id = $qGoods->third_goods_id;
  77. $shipContentModel->content->data->spec_id = $attr->third_spec_id;
  78. $shipContentModel->content->data->province = $region->province_id;
  79. $shipContentModel->content->data->city = $region->city_id;
  80. $shipContentModel->content->data->area = $is_d ? $region->id : $region->district_id;
  81. $shipContentModel->content->data->num = $num;
  82. $shipContentModel->content->data->yf_mode = StatusEnum::ENABLED;
  83. $shipContentModel->content->data->device = QiDingDongEnums::REQUEST_PARAMS_DEVICE;
  84. $req = new ShippingFeeRequest();
  85. $res = RequestHelper::send($req, RequestHelper::enCrypto($shipContentModel, $qGoods->mall_id), $qGoods->mall_id);
  86. if (is_string($res)) throw new \Exception($res);
  87. $return['extra'][$attr->goods_id]['shipping_fee'] = bcmul($res['total_ship_price'] ?? 0, 0.01, 2);
  88. }
  89. }
  90. }
  91. } catch (\Exception $e) {
  92. $return = [
  93. 'status' => false,
  94. 'msg' => $e->getMessage()
  95. ];
  96. }
  97. return $return;
  98. }
  99. }