| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- <?php
- namespace addons\QiDingDong\common\logic;
- use addons\QiDingDong\common\enums\QiDingDongEnums;
- use addons\QiDingDong\common\expand\sdk\qidingdong\src\model\ShippingFeeModel;
- use addons\QiDingDong\common\expand\sdk\qidingdong\src\request\ShippingFeeRequest;
- use addons\QiDingDong\common\helper\RequestHelper;
- use addons\QiDingDong\common\models\QiDingDongGoods;
- use addons\QiDingDong\common\models\QiDingDongGoodsAttr;
- use addons\QiDingDong\common\models\QiDingDongRegion;
- use common\enums\StatusEnum;
- use common\logic\order\marketing\BaseMarketing;
- use common\models\user\UserAddress;
- class OrderLimitLogic extends BaseMarketing
- {
- public function execute($form)
- {
- // TODO: Implement execute() method.
- }
- /**
- * 此方法不在使用
- * @param $form
- * @return void
- * @throws \Exception
- */
- public static function compute($form)
- {
- if (!empty($form['data']['attr_id'])) {
- // 判断这件商品是否为企叮咚的商品
- $attr = QiDingDongGoodsAttr::findOne(['goods_attr_id' => $form['data']['attr_id']]);
- if (!empty($attr)) {
- // 判断件数
- $qGoods = QiDingDongGoods::findOne(['goods_id' => $attr->goods_id]);
- $floor = $qGoods->content['goods']['sampling_num']; // 起订量
- $specs = array_column($qGoods->content['specs'], 'package_num', 'spec_id');
- $_floor = $specs[$attr->third_spec_id] ?? 0;
- if ($form['data']['num'] != $floor && ($form['data']['num'] % $_floor) !== 0)
- throw new \Exception("起订量为 {$floor} 或 {$_floor} 的倍数");
- }
- }
- }
- /**
- * @param $params
- * @return array|void
- * @throws \Exception
- */
- public function validBuyNum($params)
- {
- $return = [
- 'status' => true,
- 'msg' => 'success'
- ];
- $attrs = $params['attrs'] ?? [];
- if(empty($attrs)) return $return;
- try {
- foreach ($attrs as $attr_id => $num) {
- $attr = QiDingDongGoodsAttr::findOne(['goods_attr_id' => $attr_id]);
- if (!empty($attr)) {
- // 判断件数
- $qGoods = QiDingDongGoods::findOne(['goods_id' => $attr->goods_id]);
- if (empty($qGoods->can_sale)) throw new \Exception("当前商品不可售");
- $floor = $qGoods->content['goods']['sampling_num']; // 起订量
- $specs = array_column($qGoods->content['specs'], 'package_num', 'spec_id');
- $_floor = $specs[$attr->third_spec_id] ?? 0;
- if ($num != $floor && ($num % $_floor) !== 0) throw new \Exception("起订量为 {$floor} 或 {$_floor} 的倍数");
- $addr = UserAddress::getUserAddress($params['user_id'], $params['address_id'] ?? 0);
- if (!empty($addr)) {
- $is_d = true;
- $region = QiDingDongRegion::findOne(['local_id' => $addr['district_id']]);
- if (empty($region)) {
- $is_d = false;
- $region = QiDingDongRegion::findOne(['local_id' => $addr['city_id']]);
- }
- // 取运费
- $shipContentModel = RequestHelper::contentModelInstance($qGoods->mall_id);
- $shipContentModel->content->data = new ShippingFeeModel();
- $shipContentModel->content->data->goods_id = $qGoods->third_goods_id;
- $shipContentModel->content->data->spec_id = $attr->third_spec_id;
- $shipContentModel->content->data->province = $region->province_id;
- $shipContentModel->content->data->city = $region->city_id;
- $shipContentModel->content->data->area = $is_d ? $region->id : $region->district_id;
- $shipContentModel->content->data->num = $num;
- $shipContentModel->content->data->yf_mode = StatusEnum::ENABLED;
- $shipContentModel->content->data->device = QiDingDongEnums::REQUEST_PARAMS_DEVICE;
- $req = new ShippingFeeRequest();
- $res = RequestHelper::send($req, RequestHelper::enCrypto($shipContentModel, $qGoods->mall_id), $qGoods->mall_id);
- if (is_string($res)) throw new \Exception($res);
- $return['extra'][$attr->goods_id]['shipping_fee'] = bcmul($res['total_ship_price'] ?? 0, 0.01, 2);
- }
- }
- }
- } catch (\Exception $e) {
- $return = [
- 'status' => false,
- 'msg' => $e->getMessage()
- ];
- }
- return $return;
- }
- }
|