| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- namespace addons\Coupon\common\models;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use common\models\order\OrderMarketing;
- use Yii;
- /**
- * This is the model class for table "qimall_addons_coupon_use_log".
- *
- * @property int $id 主键
- * @property int $mall_id mall_id
- * @property int $user_id user_id
- * @property int $order_id mall_id
- * @property int $coupon_id coupon_id
- * @property int $user_coupon_id user_coupon_id
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- */
- class AddonsCouponUseLog extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_coupon_use_log}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id','user_id', 'order_id', 'coupon_id', 'user_coupon_id', 'status', 'created_at', 'updated_at'], 'integer'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => '主键',
- 'mall_id' => 'mall_id',
- 'user_id' => 'user_id',
- 'order_id' => 'mall_id',
- 'coupon_id' => 'coupon_id',
- 'user_coupon_id' => 'user_coupon_id',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => '创建时间',
- 'updated_at' => '修改时间',
- ];
- }
- public static function setData($params)
- {
- Yii::$app->db->close();
- try {
- $order_detail_id_arr = array_keys($params['order_detail_id_arr']);
- $list = OrderMarketing::lists(['where' => [['order_detail_id' => $order_detail_id_arr]], 'select' => 'user_coupon_id,order_detail_id']);
- $user_coupon_id_arr = [];
- $temp_arr = [];
- foreach ($list as $item) {
- $user_coupon_id = json_decode($item['user_coupon_id'], 'true');
- if (!empty($user_coupon_id)) {
- foreach ($user_coupon_id as $key => $val) {
- $user_coupon_id_arr[] = $key;
- $temp_arr[$key] = $item['order_detail_id'];
- }
- }
- }
- if (!empty($user_coupon_id_arr)) {
- $coupon_user_relate_list = AddonsCouponUserRelate::lists(['where' => [['id' => $user_coupon_id_arr]], 'select' => 'id,coupon_id']);
- $field = ['mall_id','user_id', 'order_id', 'coupon_id', 'user_coupon_id', 'status', 'created_at', 'updated_at'];
- $time = time();
- $insert = [];
- foreach ($coupon_user_relate_list as $val) {
- $arr['mall_id'] = $params['mall_id'];
- $arr['user_id'] = $params['user_id'];
- $arr['order_id'] = $params['order_detail_id_arr'][$temp_arr[$val['id']]];
- $arr['coupon_id'] = $val['coupon_id'];
- $arr['user_coupon_id'] = $val['id'];
- $arr['status'] = StatusEnum::ENABLED;
- $arr['created_at'] = $time;
- $arr['updated_at'] = $time;
- $insert[] = $arr;
- }
- // 批量插入数据
- $res = Yii::$app->db->createCommand()->batchInsert(self::tableName(), $field, $insert)->execute();
- if ($res == false) throw new \Exception('添加优惠券使用记录失败');
- }
- return true;
- }catch (\Exception $e) {
- var_dump($e->getMessage());
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- }
|