AddonsCouponUseLog.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace addons\Coupon\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use common\models\order\OrderMarketing;
  6. use Yii;
  7. /**
  8. * This is the model class for table "qimall_addons_coupon_use_log".
  9. *
  10. * @property int $id 主键
  11. * @property int $mall_id mall_id
  12. * @property int $user_id user_id
  13. * @property int $order_id mall_id
  14. * @property int $coupon_id coupon_id
  15. * @property int $user_coupon_id user_coupon_id
  16. * @property int $status 状态[-1:删除;0:禁用;1启用]
  17. * @property int $created_at 创建时间
  18. * @property int $updated_at 修改时间
  19. */
  20. class AddonsCouponUseLog extends BaseActiveRecord
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public static function tableName()
  26. {
  27. return '{{%addons_coupon_use_log}}';
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function rules()
  33. {
  34. return [
  35. [['mall_id','user_id', 'order_id', 'coupon_id', 'user_coupon_id', 'status', 'created_at', 'updated_at'], 'integer'],
  36. ];
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function attributeLabels()
  42. {
  43. return [
  44. 'id' => '主键',
  45. 'mall_id' => 'mall_id',
  46. 'user_id' => 'user_id',
  47. 'order_id' => 'mall_id',
  48. 'coupon_id' => 'coupon_id',
  49. 'user_coupon_id' => 'user_coupon_id',
  50. 'status' => '状态[-1:删除;0:禁用;1启用]',
  51. 'created_at' => '创建时间',
  52. 'updated_at' => '修改时间',
  53. ];
  54. }
  55. public static function setData($params)
  56. {
  57. Yii::$app->db->close();
  58. try {
  59. $order_detail_id_arr = array_keys($params['order_detail_id_arr']);
  60. $list = OrderMarketing::lists(['where' => [['order_detail_id' => $order_detail_id_arr]], 'select' => 'user_coupon_id,order_detail_id']);
  61. $user_coupon_id_arr = [];
  62. $temp_arr = [];
  63. foreach ($list as $item) {
  64. $user_coupon_id = json_decode($item['user_coupon_id'], 'true');
  65. if (!empty($user_coupon_id)) {
  66. foreach ($user_coupon_id as $key => $val) {
  67. $user_coupon_id_arr[] = $key;
  68. $temp_arr[$key] = $item['order_detail_id'];
  69. }
  70. }
  71. }
  72. if (!empty($user_coupon_id_arr)) {
  73. $coupon_user_relate_list = AddonsCouponUserRelate::lists(['where' => [['id' => $user_coupon_id_arr]], 'select' => 'id,coupon_id']);
  74. $field = ['mall_id','user_id', 'order_id', 'coupon_id', 'user_coupon_id', 'status', 'created_at', 'updated_at'];
  75. $time = time();
  76. $insert = [];
  77. foreach ($coupon_user_relate_list as $val) {
  78. $arr['mall_id'] = $params['mall_id'];
  79. $arr['user_id'] = $params['user_id'];
  80. $arr['order_id'] = $params['order_detail_id_arr'][$temp_arr[$val['id']]];
  81. $arr['coupon_id'] = $val['coupon_id'];
  82. $arr['user_coupon_id'] = $val['id'];
  83. $arr['status'] = StatusEnum::ENABLED;
  84. $arr['created_at'] = $time;
  85. $arr['updated_at'] = $time;
  86. $insert[] = $arr;
  87. }
  88. // 批量插入数据
  89. $res = Yii::$app->db->createCommand()->batchInsert(self::tableName(), $field, $insert)->execute();
  90. if ($res == false) throw new \Exception('添加优惠券使用记录失败');
  91. }
  92. return true;
  93. }catch (\Exception $e) {
  94. var_dump($e->getMessage());
  95. self::$static_error = $e->getMessage();
  96. return false;
  97. }
  98. }
  99. }