SecondaryCardRedemptionCode.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace addons\SecondaryCard\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use Yii;
  6. /**
  7. * This is the model class for table "qimall_addons_secondary_card_redemption_code".
  8. *
  9. * @property int $id
  10. * @property int $mall_id
  11. * @property int $user_id 会员id
  12. * @property string $code 兑换码
  13. * @property string $remarks 备注
  14. * @property int $secondary_card_id 次卡id
  15. * @property int $use_type 使用范围:0:全部店铺;1:指定店铺
  16. * @property int|null $is_use 状态:0:未使用;1:已使用
  17. * @property int $use_time 使用时间
  18. * @property string $order_no 兑换次卡号
  19. * @property int|null $status 状态[-1:删除;0:禁用;1启用;]
  20. * @property int $create_source_type 创建来源类型:0:平台;1:门店
  21. * @property int $create_source_id 创建来源id
  22. * @property int $created_at 创建时间
  23. * @property int $updated_at
  24. */
  25. class SecondaryCardRedemptionCode extends BaseActiveRecord
  26. {
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public static function tableName()
  31. {
  32. return '{{%addons_secondary_card_redemption_code}}';
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function rules()
  38. {
  39. return [
  40. [['mall_id'], 'required'],
  41. [['mall_id', 'user_id', 'secondary_card_id', 'use_type', 'is_use', 'use_time', 'status', 'created_at', 'updated_at',
  42. 'create_source_type', 'create_source_id',], 'integer'],
  43. [['code', 'remarks', 'order_no'], 'string', 'max' => 255],
  44. ];
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function attributeLabels()
  50. {
  51. return [
  52. 'id' => 'ID',
  53. 'mall_id' => 'Mall ID',
  54. 'user_id' => '会员id',
  55. 'code' => '兑换码',
  56. 'remarks' => '备注',
  57. 'secondary_card_id' => '次卡id',
  58. 'use_type' => '使用范围:0:全部店铺;1:指定店铺',
  59. 'is_use' => '状态:0:未使用;1:已使用',
  60. 'use_time' => '使用时间',
  61. 'order_no' => '兑换次卡号',
  62. 'status' => '状态[-1:删除;0:禁用;1启用;]',
  63. 'create_source_type' => '创建来源类型:0:平台;1:门店',
  64. 'create_source_id' => '创建来源id',
  65. 'created_at' => '创建时间',
  66. 'updated_at' => 'Updated At',
  67. ];
  68. }
  69. public static function getExportFields()
  70. {
  71. return [
  72. 'code' => '兑换码',
  73. 'remarks' => '批次备注',
  74. 'is_use' => '状态',
  75. 'created_at' => '创建时间',
  76. 'use_time' => '使用时间',
  77. 'order_no' => '兑换次卡号',
  78. 'source' => '创建来源',
  79. ];
  80. }
  81. public function getSecondaryCard()
  82. {
  83. return $this->hasOne(SecondaryCard::class, ['id' => 'secondary_card_id']);
  84. }
  85. public function getSecondaryCardOrder()
  86. {
  87. return $this->hasOne(SecondaryCardOrder::class, ['order_no' => 'order_no']);
  88. }
  89. public static function setData(array $params)
  90. {
  91. try {
  92. if (!empty($params['id'])) {
  93. $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  94. if (empty($model)) throw new \Exception('服务不存在或已删除');
  95. } else {
  96. $model = new self();
  97. $model->loadDefaultValues();
  98. }
  99. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  100. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  101. return $model;
  102. } catch (\Exception $e) {
  103. self::$static_error = $e->getMessage();
  104. return false;
  105. }
  106. }
  107. }