| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- <?php
- namespace addons\ElectronCoupon\common\models;
- use addons\ElectronCoupon\common\enums\ElectronCouponEnums;
- use Yii;
- use common\enums\StatusEnum;
- use yii\db\Exception;
- /**
- * This is the model class for table "{{%addons_electron_coupon_order}}".
- *
- * @property int $id
- * @property int $order_id 订单id
- * @property int $user_id 用户id
- * @property int $user_e_coupon_id 会员电子券id
- * @property float $price 金额
- * @property int $created_at
- * @property int $updated_at
- * @property int|null $status 状态[-1:删除;0:禁用;1启用]
- * @property int $mall_id 商城id
- * @property int $is_pay 是否支付
- * @property int $number 数量
- */
- class ElectronCouponOrder extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_electron_coupon_order}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['order_no', 'user_id', 'user_e_coupon_id', 'price'], 'required'],
- [['user_id', 'user_e_coupon_id', 'pay_type', 'created_at', 'updated_at', 'status', 'mall_id', 'is_pay', 'number', 'pay_time'], 'integer'],
- [['price'], 'number'],
- [['order_no', 'trade_no'], 'string'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => '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;
- }
- }
|