BusinessCardMaterialsStatistics.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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_statistics".
  8. *
  9. * @property int $id 名片素材数据统计
  10. * @property int|null $mall_id 商城ID
  11. * @property int|null $material_id 素材ID
  12. * @property int|null $view_seconds 观看总时长(秒)
  13. * @property int|null $view_numbers 观看人数
  14. * @property int|null $view_times 观看次数
  15. * @property int|null $share_times 分享次数
  16. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  17. * @property int|null $created_at 创建时间
  18. * @property int|null $updated_at 更新时间
  19. */
  20. class BusinessCardMaterialsStatistics extends \common\models\base\BaseActiveRecord
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public static function tableName()
  26. {
  27. return 'qimall_addons_business_card_material_statistics';
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function rules()
  33. {
  34. return [
  35. [['mall_id', 'material_id', 'view_seconds', 'view_numbers', 'view_times', 'share_times', '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. 'material_id' => 'Material ID',
  47. 'view_seconds' => 'View Seconds',
  48. 'view_numbers' => 'View Numbers',
  49. 'view_times' => 'View Times',
  50. 'share_times' => 'Share Times',
  51. 'status' => 'Status',
  52. 'created_at' => 'Created At',
  53. 'updated_at' => 'Updated At',
  54. ];
  55. }
  56. public function getMaterial()
  57. {
  58. return $this->hasOne(BusinessCardMaterials::class, ['id' => 'material_id'])->select('id,title,cover_img');
  59. }
  60. /**
  61. * 数据设置
  62. */
  63. public static function setData(array $params)
  64. {
  65. try{
  66. if (!isset($params['mall_id']) || empty($params['mall_id'])) throw new Exception('缺少商城ID');
  67. if (isset($params['id']) && !empty($params['id'])) {
  68. $model = self::findOne([
  69. 'id' => $params['id'],
  70. 'mall_id' => $params['mall_id'],
  71. 'status' => StatusEnum::ENABLED
  72. ]);
  73. if(empty($model)) throw new Exception('统计记录不存在或已删除');
  74. } else if (isset($params['material_id']) && !empty($params['material_id'])) {
  75. $model = self::findOne([
  76. 'material_id' => $params['material_id'],
  77. 'mall_id' => $params['mall_id'],
  78. 'status' => StatusEnum::ENABLED
  79. ]);
  80. }
  81. if (!empty($model)) {
  82. if (isset($params['view_seconds'])) {
  83. $model->view_seconds = $model->view_seconds + $params['view_seconds'];
  84. unset($params['view_seconds']);
  85. }
  86. if (isset($params['view_numbers'])) {
  87. $model->view_numbers = $model->view_numbers + $params['view_numbers'];
  88. unset($params['view_numbers']);
  89. }
  90. if (isset($params['view_times'])) {
  91. $model->view_times = $model->view_times + $params['view_times'];
  92. unset($params['view_times']);
  93. }
  94. if (isset($params['share_times'])) {
  95. $model->share_times = $model->share_times + $params['share_times'];
  96. unset($params['share_times']);
  97. }
  98. } else {
  99. $model = new self();
  100. $model->loadDefaultValues();
  101. $model->mall_id = $params['mall_id'];
  102. }
  103. if (!$model->load($params,'') || !$model->save()) throw new Exception($model->getErrorMessage());
  104. return $model;
  105. }catch(Exception $e){
  106. self::setStaticError($e->getMessage());
  107. return false;
  108. }
  109. }
  110. }