SecondaryCardRelateStore.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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_relate_store".
  8. *
  9. * @property int $id
  10. * @property int $mall_id
  11. * @property int $create_source_type 创建来源类型:0:平台;1:门店
  12. * @property int $store_type 店铺类型:1:o2o门店
  13. * @property int $secondary_card_id 次卡id
  14. * @property int $store_id 门店id
  15. * @property int|null $status 状态[-1:删除;0:禁用;1启用;]
  16. * @property int $created_at 创建时间
  17. * @property int $updated_at
  18. */
  19. class SecondaryCardRelateStore extends BaseActiveRecord
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public static function tableName()
  25. {
  26. return '{{%addons_secondary_card_relate_store}}';
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function rules()
  32. {
  33. return [
  34. [['mall_id'], 'required'],
  35. [['mall_id', 'create_source_type', 'store_type', 'secondary_card_id', 'store_id', 'status', 'created_at', 'updated_at'], 'integer'],
  36. ];
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function attributeLabels()
  42. {
  43. return [
  44. 'id' => 'ID',
  45. 'mall_id' => 'Mall ID',
  46. 'create_source_type' => '创建来源类型:0:平台;1:门店',
  47. 'store_type' => '店铺类型:1:o2o门店',
  48. 'secondary_card_id' => '次卡id',
  49. 'store_id' => '门店id',
  50. 'status' => '状态[-1:删除;0:禁用;1启用;]',
  51. 'created_at' => '创建时间',
  52. 'updated_at' => 'Updated At',
  53. ];
  54. }
  55. public static function setData($mall_id, $secondary_card_id, $store_ids, $create_source_type, $store_type)
  56. {
  57. try {
  58. foreach ($store_ids as $store_id) {
  59. $model = self::findOne(['mall_id' => $mall_id, 'secondary_card_id' => $secondary_card_id, 'store_id' => $store_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  60. if (empty($model)) {
  61. $model = new self();
  62. $model->loadDefaultValues();
  63. }
  64. $params = [
  65. 'mall_id' => $mall_id,
  66. 'secondary_card_id' => $secondary_card_id,
  67. 'create_source_type' => $create_source_type,
  68. 'store_type' => $store_type,
  69. 'store_id' => $store_id,
  70. ];
  71. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  72. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  73. }
  74. return true;
  75. } catch (\Exception $e) {
  76. self::$static_error = $e->getMessage();
  77. return false;
  78. }
  79. }
  80. }