BusinessCardMaterialsShare.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace addons\BusinessCard\common\models;
  3. use common\enums\StatusEnum;
  4. use Exception;
  5. use Yii;
  6. /**
  7. * This is the model class for table "qimall_addons_business_card_material_share".
  8. *
  9. * @property int $id 用户分享素材统计
  10. * @property int|null $mall_id 商城ID
  11. * @property int|null $user_id 用户ID
  12. * @property int|null $material_id 素材ID
  13. * @property int|null $share_times 分享次数
  14. * @property int|null $recent_time 最近分享时间
  15. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  16. * @property int|null $created_at 创建时间
  17. * @property int|null $updated_at 更新时间
  18. */
  19. class BusinessCardMaterialsShare extends \common\models\base\BaseActiveRecord
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public static function tableName()
  25. {
  26. return 'qimall_addons_business_card_material_share';
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function rules()
  32. {
  33. return [
  34. [['mall_id', 'user_id', 'material_id', 'share_times', 'recent_time', 'status', 'created_at', 'updated_at'], 'integer'],
  35. ];
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function attributeLabels()
  41. {
  42. return [
  43. 'id' => 'ID',
  44. 'mall_id' => 'Mall ID',
  45. 'user_id' => 'User ID',
  46. 'material_id' => 'Material ID',
  47. 'share_times' => 'Share Times',
  48. 'recent_time' => 'Recent Time',
  49. 'status' => 'Status',
  50. 'created_at' => 'Created At',
  51. 'updated_at' => 'Updated At',
  52. ];
  53. }
  54. /**
  55. * 数据设置
  56. */
  57. public static function setData(array $params)
  58. {
  59. try{
  60. if (!isset($params['mall_id']) || empty($params['mall_id'])) throw new Exception('缺少商城ID');
  61. if (isset($params['id']) && !empty($params['id'])) {
  62. $model = self::findOne([
  63. 'id' => $params['id'],
  64. 'mall_id' => $params['mall_id'],
  65. 'status' => StatusEnum::ENABLED
  66. ]);
  67. if(empty($model)) throw new Exception('分享记录不存在或已删除');
  68. } else if (isset($params['user_id']) && !empty($params['user_id']) && isset($params['material_id']) && !empty($params['material_id'])) {
  69. $model = self::findOne([
  70. 'user_id' => $params['user_id'],
  71. 'material_id' => $params['material_id'],
  72. 'mall_id' => $params['mall_id'],
  73. 'status' => StatusEnum::ENABLED
  74. ]);
  75. }
  76. if (!empty($model)) {
  77. if (isset($params['share_times'])) {
  78. $model->share_times = $model->share_times + $params['share_times'];
  79. unset($params['share_times']);
  80. }
  81. } else {
  82. $model = new self();
  83. $model->loadDefaultValues();
  84. $model->mall_id = $params['mall_id'];
  85. }
  86. if (!$model->load($params,'') || !$model->save()) throw new Exception($model->getErrorMessage());
  87. return $model;
  88. }catch(Exception $e){
  89. self::setStaticError($e->getMessage());
  90. return false;
  91. }
  92. }
  93. }