ShortVideoConfig.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace addons\ShortVideo\common\models;
  3. use addons\ShortVideo\common\enums\AddonsShortVideoEnum;
  4. use common\enums\StatusEnum;
  5. use common\models\goods\Goods;
  6. use Yii;
  7. /**
  8. * This is the model class for table "{{%addons_short_video_config}}".
  9. *
  10. * @property int $id
  11. * @property int $mall_id
  12. * @property int $is_enabled 是否开启 0--关闭|1--开启
  13. * @property int $is_check 是否需要审核0:否 1:是
  14. * @property int $release_rule 发布权限:0:无条件 1:有条件
  15. * @property int|null $award_status 是否开启全局带货奖励0:否 1:是
  16. * @property int $award_type 奖励类型 0:金额 1:百分比
  17. * @property float $award_percentage 奖励百分比
  18. * @property float $award_money 奖励金额
  19. * @property int|null $purchase_times_status 消费达到多少次 是否开启 0:未开启 1:开启
  20. * @property int|null $purchase_times 消费达到多少次
  21. * @property int|null $purchase_cost_status 消费达标是否开启0:未开启 1:已开启
  22. * @property float|null $purchase_cost 消费达到多少钱
  23. * @property int|null $purchase_goods_status 消费商品是否开启0:未开启 1:已开启
  24. * @property int|null $purchase_goods_id 消费某个商品
  25. * @property int $video_lenght 视频发布时长
  26. * @property string|null $agreement 视频协议
  27. * @property int $status 状态[-1:删除;0:禁用;1启用]
  28. * @property int|null $client_status 是否开启文本审核 0:否 1:是
  29. * @property string|null $start_up_diagram 启动图
  30. * @property string|null $share_img 分享图片
  31. * @property string|null $share_title 分享标题
  32. * @property string|null $share_content 分享内容
  33. * @property int $created_at
  34. * @property int $updated_at
  35. */
  36. class ShortVideoConfig extends \common\models\base\BaseActiveRecord
  37. {
  38. private static $cache_key = 'qimall:short-video:config:mall_id:';
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public static function tableName()
  43. {
  44. return '{{%addons_short_video_config}}';
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function rules()
  50. {
  51. return [
  52. [['mall_id'], 'required'],
  53. [['mall_id', 'is_enabled', 'is_check', 'release_rule', 'award_status', 'award_type', 'purchase_times_status', 'purchase_times', 'purchase_cost_status', 'purchase_goods_status', 'purchase_goods_id', 'video_lenght', 'status', 'client_status', 'created_at', 'updated_at'], 'integer'],
  54. [['award_percentage', 'award_money', 'purchase_cost'], 'number'],
  55. [['agreement'], 'string'],
  56. [['start_up_diagram', 'share_img'], 'string', 'max' => 255],
  57. [['share_title', 'share_content'], 'string', 'max' => 65],
  58. ];
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function attributeLabels()
  64. {
  65. return [
  66. 'id' => 'ID',
  67. 'mall_id' => 'Mall ID',
  68. 'is_enabled' => 'Is Enabled',
  69. 'is_check' => 'Is Check',
  70. 'release_rule' => 'Release Rule',
  71. 'award_status' => 'Award Status',
  72. 'award_type' => 'Award Type',
  73. 'award_percentage' => 'Award Percentage',
  74. 'award_money' => 'Award Money',
  75. 'purchase_times_status' => 'Purchase Times Status',
  76. 'purchase_times' => 'Purchase Times',
  77. 'purchase_cost_status' => 'Purchase Cost Status',
  78. 'purchase_cost' => 'Purchase Cost',
  79. 'purchase_goods_status' => 'Purchase Goods Status',
  80. 'purchase_goods_id' => 'Purchase Goods ID',
  81. 'video_lenght' => 'Video Lenght',
  82. 'agreement' => 'Agreement',
  83. 'status' => 'Status',
  84. 'client_status' => 'Client Status',
  85. 'start_up_diagram' => 'Start Up Diagram',
  86. 'share_img' => 'Share Img',
  87. 'share_title' => 'Share Title',
  88. 'share_content' => 'Share Content',
  89. 'created_at' => 'Created At',
  90. 'updated_at' => 'Updated At',
  91. ];
  92. }
  93. public static function getConfig(int $mall_id)
  94. {
  95. $cache = \Yii::$app->cache;
  96. $configs = $cache->get(self::$cache_key . $mall_id);
  97. // dd($configs);
  98. if (!empty($configs)) return $configs;
  99. $configs = self::find()
  100. ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'is_enabled' => AddonsShortVideoEnum::IS_ENABLE_YES])
  101. ->asArray()
  102. ->orderBy('updated_at desc')
  103. ->limit(1)
  104. ->one();
  105. if (!empty($configs)) {
  106. if (1 == $configs['purchase_goods_status'] && !empty($configs['purchase_goods_id'])) {
  107. $good = Goods::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $configs['purchase_goods_id']]);
  108. $configs['purchase_goods_name'] = $good->goods_name;
  109. }
  110. $cache->set(self::$cache_key . $mall_id, $configs, 3600);
  111. } else {
  112. $configs = [];
  113. }
  114. return $configs;
  115. }
  116. public static function setData(int $mall_id, array $data)
  117. {
  118. if (!empty($data['id'])) {
  119. $model = self::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $data['id']]);
  120. if (empty($model)) {
  121. self::$static_error = '配置数据不存在';
  122. return false;
  123. }
  124. } else {
  125. $model = new self();
  126. $model->loadDefaultValues();
  127. $model->mall_id = $mall_id;
  128. }
  129. foreach ($data as $key => $val) {
  130. $model->$key = $val;
  131. }
  132. if (!$model->save()) {
  133. self::$static_error = '保存奖励数据失败:' . $model->getErrorMessage();
  134. return false;
  135. }
  136. \Yii::$app->cache->delete(self::$cache_key . $mall_id);
  137. return $model;
  138. }
  139. }