| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- namespace addons\Area\common\service;
- use addons\Area\common\models\AreaCommunity;
- use addons\Area\common\models\AreaOrder;
- use addons\Area\common\enums\AreaEnum;
- use common\enums\StatusEnum;
- use common\models\order\Order;
- use Exception;
- class OrderService
- {
- public function preview($params)
- {
- try {
- $configs = \Yii::$app->services->setting->addons->get($params['mall_id'], AreaEnum::ADDONS_NAME, 'basic');
- if ($configs['is_buy_area'] != 1) throw new Exception('未开启区代申请条件按钮');
- if ($params['level'] != 3) throw new Exception('非申请区代身份无需下单');
- //检查是否存在已购买订单
- $order = AreaOrder::getOne([
- ['mall_id' => $params['mall_id']],
- ['user_id' => $params['user_id']],
- ['level' => $params['level']],
- ['area_id' => $params['district_id']],
- ['is_pay' => StatusEnum::ENABLED],
- ['status' => StatusEnum::ENABLED],
- ], true);
- if (!empty($order)) return ['is_pay' => 1, 'order_id' => $order['id']];
- //获取最近购买该区代身份的订单
- $near_order = AreaOrder::getOne([
- ['mall_id' => $params['mall_id']],
- ['level' => $params['level']],
- ['area_id' => $params['district_id']],
- ['is_pay' => StatusEnum::ENABLED],
- ['status' => StatusEnum::ENABLED],
- ], true);
- //处理底价
- $price = $configs['area_buy_price'];
- if (!empty($near_order)) {
- $price = bcmul($near_order['price'] + ($near_order['price'] * $configs['add_price_pre']/100), 1, 2);
- }
- return ['total_pay_money' => $price];
- } catch (\Exception $e) {
- throw new Exception($e->getMessage());
- }
- }
- public function created($params)
- {
- try {
- $configs = \Yii::$app->services->setting->addons->get($params['mall_id'], AreaEnum::ADDONS_NAME, 'basic');
- if ($configs['is_buy_area'] != 1) throw new Exception('未开启区代申请条件按钮');
- if ($params['level'] != 3) throw new Exception('非申请区代身份无需下单');
- //检查是否存在已购买订单
- $order = AreaOrder::getOne([
- ['mall_id' => $params['mall_id']],
- ['user_id' => $params['user_id']],
- ['level' => $params['level']],
- ['area_id' => $params['district_id']],
- ['is_pay' => StatusEnum::ENABLED],
- ['status' => StatusEnum::ENABLED],
- ], true);
- if (!empty($order)) return ['is_pay' => 1, 'order_id' => $order['id']];
- //获取最近购买该区代身份的订单
- $near_order = AreaOrder::getOne([
- ['mall_id' => $params['mall_id']],
- ['level' => $params['level']],
- ['area_id' => $params['district_id']],
- ['is_pay' => StatusEnum::ENABLED],
- ['status' => StatusEnum::ENABLED],
- ], true);
- //处理底价
- $price = $configs['area_buy_price'];
- if (!empty($near_order)) {
- $price = bcmul($near_order['price'] + ($near_order['price'] * $configs['add_price_pre']/100), 1, 2);
- }
- $order_data = [
- 'mall_id' => $params['mall_id'],
- 'user_id' => $params['user_id'],
- 'level' => $params['level'],
- 'area_id' => $params['district_id'],
- 'price' => $price,
- 'order_no' => self::generateOrderNo('sa'),
- ];
- $order = AreaOrder::setData($order_data);
- if (!$order) throw new Exception(AreaOrder::$static_error);
- $order_trade_info[] = [
- 'order_id' => $order->id,
- 'order_type' => 'Area',
- 'order_no' => $order->order_no,
- 'amount' => $order->price,
- 'title' => '购买区代申请资格',
- 'notify_class' => 'addons\Area\common\logic\notify\Notify',
- ];
- $res = \Yii::$app->services->pay->createTrade($params['mall_id'],$params['user_id'], $order_trade_info);
- $res['order_id'] = $order->id;
- $arr['transaction'] = $res;
- $payment_expire_time = \Yii::$app->services->setting->mall->get($params['mall_id'], 'order.payment_expire_time');
- $arr['surplus_second'] = 30*60;//剩余秒数
- if($payment_expire_time){
- $arr['surplus_second'] = intval($payment_expire_time) * 60;//剩余秒数
- }
- return $arr;
- } catch (\Exception $e) {
- throw new Exception($e->getMessage());
- }
- }
- public static function generateOrderNo($prefix = ''){
- // 优化唯一性保证:使用random_int()代替rand(),并结合微调以生成更唯一的ID
- $randInt = random_int(100000, 999999);
- // 日期时间戳处理
- $dateTimeStr = date('YmdHis');
- // 保证前缀不为空字符串且不超过一定长度(例如20个字符)
- $prefix = trim($prefix, '0');
- if (strlen($prefix) > 20) {
- $prefix = substr($prefix, 0, 20);
- }
- // 生成订单号,保证总长度为17(前缀+日期时间戳+随机数)
- $orderNo = $prefix . $dateTimeStr . str_pad($randInt, 6, '0', STR_PAD_LEFT);
- return $orderNo;
- }
- }
|