'ID', 'order_no' => '订单号', 'trade_no' => '流水号', 'user_id' => '用户id', 'user_e_coupon_id' => '会员电子券id', 'price' => '金额', 'created_at' => 'Created At', 'updated_at' => 'Updated At', 'status' => '状态[-1:删除;0:禁用;1启用]', 'mall_id' => '商城id', 'is_pay' => '是否支付', 'pay_type' => '支付方式', 'pay_time' => '支付时间', 'number' => '数量', ]; } // 数据设置 public static function setData(array $params) { try{ if (!isset($params['mall_id']) || empty($params['mall_id'])) throw new Exception('缺少商城ID'); $mall_id = $params['mall_id']; if (isset($params['id']) && !empty($params['id'])) { $model = self::findOne(['id' => $params['id'], 'mall_id' => $mall_id, 'status' => StatusEnum::ENABLED]); if(empty($model)) throw new Exception('订单不存在或已删除'); } else { $model = new self(); $model->loadDefaultValues(); $model->mall_id = $mall_id; } if (!$model->load($params,'') || !$model->save()) throw new Exception($model->getErrorMessage()); return $model; }catch(Exception $e){ self::setStaticError($e->getMessage()); return false; } } // 创建激活订单 public static function createActiveOrder($user_coupon_id, $user_id, $mall_id, $number) { try { // 查出券信息 // 判断是否有效 // 生成订单 $config = Yii::$app->services->setting->addons->get($mall_id, ElectronCouponEnums::ADDONS_NAME, 'basic'); $is_open = $config['is_open'] ?? 0; $customize_name = $config['customize_name'] ?? '电子券'; if (!$is_open) { throw new Exception('暂时无法激活'); } $user_coupon = ElectronCouponUserCoupon::getOne([ ['id' => $user_coupon_id], ['mall_id' => $mall_id], ['user_id' => $user_id], ['>=', 'status', StatusEnum::DISABLED] ], false); if (!$user_coupon) { throw new Exception('找不到对应的' . $customize_name . '信息'); } // 使用时间 if ($user_coupon['use_end_time'] < time()) { throw new Exception('该' . $customize_name . '已过期,无法激活'); } if ($number > $user_coupon['receive_amount']) { throw new Exception('激活数量过大,当前仅剩余' . $user_coupon['receive_amount']); } $order_data = [ 'order_no' => self::getOrderNo($user_id), 'user_id' => $user_id, 'user_e_coupon_id' => $user_coupon_id, 'price' => bcmul($user_coupon['price'], $number, 2), 'status' => StatusEnum::ENABLED, 'mall_id' => $mall_id, 'is_pay' => ElectronCouponEnums::NOT_PAY, 'number' => $number ]; $order_trade_info[] = [ 'order_no' => $order_data['order_no'], 'amount' => $order_data['price'], 'title' => $customize_name . '激活订单', 'notify_class' => ElectronCouponEnums::NOTIFY_CLASS, 'order_type' => ElectronCouponEnums::ADDONS_NAME ]; $trade_result = \Yii::$app->services->pay->createTrade($mall_id, $user_id, $order_trade_info); if ($trade_result == false) { throw new Exception('生成订单流失失败'); } $order_data['trade_no'] = $trade_result['trade_no']; $result = self::setData($order_data); if ($result == false) { throw new Exception(self::getStaticError()); } return $result; } catch (Exception $e) { self::setStaticError($e->getMessage()); return false; } } /** * 生成订单号 * @return string */ public static function getOrderNo($user_id) { $randLen = 6; $id = base_convert(substr(uniqid($user_id), 0 - $randLen), 16, 10); if (strlen($id) > 10) { $id = substr($id, -10); } elseif (strlen($id) < 10) { $rLen = 10 - strlen($id); $id = $id . rand(pow(10, $rLen - 1), pow(10, $rLen) - 1); } $dateTimeStr = date('YmdHis'); return 'EC' . $dateTimeStr . $id; } }