| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- <?php
- namespace addons\Pk\common\models;
- use addons\Pk\common\enums\PkEnum;
- use common\enums\StatusEnum;
- use common\models\user\User;
- use Yii;
- use Exception;
- /**
- * This is the model class for table "{{%addons_pk_order}}".
- *
- * @property int $id
- * @property int|null $trade_no
- * @property string $order_no 订单编号
- * @property int $order_type 1个人报名;2战队创建
- * @property int $is_pay 是否支付;0未支付;1已支付;2已退款
- * @property float $money 支付金额
- * @property string $pay_type wechat微信;balance余额
- * @property int $created_at
- * @property int $updated_at
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $mall_id 商城id
- * @property int $user_id 用户id
- * @property int $activity_id 活动id
- * @property int|null $performance_goal 目标值
- */
- class PkOrder extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_pk_order}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [[ 'order_type', 'is_pay', 'created_at', 'updated_at', 'status', 'mall_id', 'user_id', 'activity_id', 'performance_goal'], 'integer'],
- [['order_no', 'is_pay', 'money', 'mall_id', 'user_id', 'activity_id'], 'required'],
- [['money'], 'number'],
- [['trade_no','order_no'], 'string', 'max' => 255],
- [['pay_type'], 'string', 'max' => 125],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'trade_no' => 'Trade No',
- 'order_no' => '订单编号',
- 'order_type' => '1个人报名;2战队创建',
- 'is_pay' => '是否支付;0未支付;1已支付;2已退款',
- 'money' => '支付金额',
- 'pay_type' => 'wechat微信;balance余额',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'mall_id' => '商城id',
- 'user_id' => '用户id',
- 'activity_id' => '活动id',
- 'performance_goal' => '目标值',
- ];
- }
- /**
- * 生成订单号
- * @return string
- */
- public static function getOrderNo()
- {
- $randLen = 6;
- $id = base_convert(substr(uniqid(), 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 'PK' . $dateTimeStr . $id;
- }
- public function getUser()
- {
- return $this->hasOne(User::ClassName(), ['id' => 'user_id']);
- }
- public function getActivity()
- {
- return $this->hasOne(PkActivity::ClassName(), ['id' => 'activity_id']);
- }
- /**
- * 保存数据
- * @Author: ltm
- * @DateTime: 2022/6/8 0015 9:26
- * @Copyright: copyright (c) 2021
- * @param $mall_id
- * @param array $params
- * @return Rebate|bool
- */
- public static function setData(array $params){
- try{
- //生成流水订单
- $order_trade_info[] = [
- 'order_no' => $params['order_no'],
- 'amount' => $params['money'],
- 'title' => '参加pk费用',
- 'notify_class' => PkEnum::NOTIFY_CLASS,
- 'order_type' => PkEnum::ADDONS_NAME
- ];
- $data = \Yii::$app->services->pay->createTrade($params['mall_id'], $params['user_id'], $order_trade_info);
- $mall_id = $params['mall_id'];
- if(!empty($params['id'])){
- $model = self::findOne(['and',['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;
- }
- $model->trade_no = $data['trade_no'];
- if(!$model->load($params,'') || !$model->save()) throw new Exception($model->getErrorMessage());
- //生成支付流水
- return $data;
- }catch(\Exception $e){
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- }
|