| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- <?php
- namespace addons\Store\common\models;
- use common\enums\UserWalletEnum;
- use common\models\base\BaseActiveRecord;
- use services\common\UserWalletService;
- use Yii;
- /**
- * This is the model class for table "qimall_addons_store_order_give".
- *
- * @property int $id 主键
- * @property int $mall_id mall_id
- * @property int $store_id 门店id
- * @property int $order_id 订单id
- * @property float $give_score 赠送积分
- * @property string|null $give_coupon 赠送优惠券(json字符串保存)
- * @property string|null $give_currency 赠送货币(json字符串保存)
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- */
- class StoreOrderGive extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_store_order_give}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'store_id', 'order_id', 'status', 'created_at', 'updated_at'], 'integer'],
- [['give_score'], 'number'],
- [['give_coupon', 'give_currency'], 'string'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'store_id' => 'Store ID',
- 'order_id' => 'Order ID',
- 'give_score' => 'Give Score',
- 'give_coupon' => 'Give Coupon',
- 'give_currency' => 'Give Currency',
- 'status' => 'Status',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- public static function setData($give_data, $order_id, $store_id, $mall_id)
- {
- if (empty($give_data)) return true;
- $model = new self();
- $model->loadDefaultValues();
- $give_score = 0;
- $give_coupon = [];
- $give_currency = [];
- foreach ($give_data as $item) {
- if ($item['type'] == 'score') {
- $give_score = $item['num'];
- }
- if ($item['type'] == 'coupon') {
- $give_coupon[] = $item;
- }
- }
- $model->give_score = $give_score;
- $model->give_coupon = json_encode($give_coupon);
- $model->give_currency = json_encode($give_currency);
- $model->order_id = $order_id;
- $model->store_id = $store_id;
- $model->mall_id = $mall_id;
- return $model->save();
- }
- public static function give($mall_id, $user_id, $order_id, $store_id)
- {
- try {
- $order_give = self::findOne(['order_id' => $order_id, 'mall_id' => $mall_id]);
- if (empty($order_give)) return true;
- /*if (!empty($order_give['give_score'])) {
- $insert_wallet[] = [
- 'mall_id' => $mall_id,
- 'user_id' => $user_id,
- 'is_frozen' => false,
- 'wallet_type' => 'score',
- 'money' => $order_give['give_score'],
- 'desc' => 'o2o订单支付赠送',
- 'source_table' => StoreOrder::tableName(),
- 'source_table_id' => $order_id,
- 'from_type' => UserWalletEnum::CONSUME,
- 'capital_type' => UserWalletEnum::CONSUME_ORDER_GIVE,
- ];
- $res = UserWalletService::batchHandleByQueue($insert_wallet);
- if (empty($res)) throw new \Exception(UserWalletService::getStaticError());
- }*/
- $give_coupon = json_decode($order_give['give_coupon'], true);
- if (!empty($give_coupon)) {
- foreach ($give_coupon as $item) {
- StoreCouponUserRelate::giveCouponByOrder($mall_id, $store_id, $user_id, [$item['id']=>$item['num']], $item['from_type'], 2, 'o2o');
- }
- }
- return true;
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- }
|