PkShareLog.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace addons\Pk\common\models;
  3. use addons\Pk\common\enums\PkEnum;
  4. use common\enums\StatusEnum;
  5. use Yii;
  6. use yii\base\Exception;
  7. /**
  8. * This is the model class for table "{{%addons_pk_share_log}}".
  9. *
  10. * @property int $id ID
  11. * @property int $mall_id 商城ID
  12. * @property int $share_user_id 分享者用户ID
  13. * @property string $url 分享url
  14. * @property string $log 分享log
  15. * @property string $desc 分享描述
  16. * @property int $activity_type 类型【1 个人PK 2战队PK】
  17. * @property int $status 状态[-1:删除;0:禁用;1启用]
  18. * @property int $created_at 创建时间
  19. * @property int $updated_at 更新时间
  20. */
  21. class PkShareLog extends \common\models\base\BaseActiveRecord
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function tableName()
  27. {
  28. return '{{%addons_pk_share_log}}';
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['mall_id', 'share_user_id', 'activity_type', 'status', 'created_at', 'updated_at', 'activity_id'], 'integer'],
  37. [['url', 'log', 'desc'], 'string', 'max' => 255],
  38. ];
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function attributeLabels()
  44. {
  45. return [
  46. 'id' => 'ID',
  47. 'mall_id' => '商城ID',
  48. 'activity_id' => 'PK活动ID',
  49. 'share_user_id' => '分享者用户ID',
  50. 'url' => '分享url',
  51. 'log' => '分享log',
  52. 'desc' => '分享描述',
  53. 'activity_type' => '类型【1 个人PK 2战队PK】',
  54. 'status' => '状态[-1:删除;0:禁用;1启用]',
  55. 'created_at' => '创建时间',
  56. 'updated_at' => '更新时间',
  57. ];
  58. }
  59. /**
  60. * 保存数据
  61. * @Author: ltm
  62. * @DateTime: 2022/6/8 0015 9:26
  63. * @Copyright: copyright (c) 2021
  64. * @param $mall_id
  65. * @param array $params
  66. * @return Rebate|bool
  67. */
  68. public static function setData(array $params){
  69. try{
  70. $mall_id = $params['mall_id'];
  71. if(!empty($params['id'])){
  72. $model = self::findOne(['and',['id' => $params['id'],'mall_id' => $mall_id],['=','status', StatusEnum::ENABLED]]);
  73. if(empty($model)) throw new \Exception('活动不存在或已删除');
  74. }else{
  75. $model = new self();
  76. $model->loadDefaultValues();
  77. $model->mall_id = $mall_id;
  78. }
  79. if(!$model->load($params,'') || !$model->save()) throw new Exception($model->getErrorMessage());
  80. return $model;
  81. }catch(\Exception $e){
  82. self::setStaticError($e->getMessage());
  83. return false;
  84. }
  85. }
  86. // 增加分享次数
  87. public static function addShareNum($mall_id, $activity_id, $user_id, $share_url, $share_desc)
  88. {
  89. $data = [
  90. 'mall_id' => $mall_id,
  91. 'activity_id' => $activity_id,
  92. 'share_user_id' => $user_id,
  93. 'url' => $share_url,
  94. 'desc' => $share_desc,
  95. 'activity_type' => PkEnum::ACTIVITY_TYPE_PERSON,
  96. 'status' => StatusEnum::ENABLED
  97. ];
  98. $save_share_log = self::setData($data);
  99. if (!$save_share_log) {
  100. throw new Exception('保存分享记录失败');
  101. }
  102. $statistic = [
  103. 'user_id' => $user_id,
  104. 'activity_id' => $activity_id,
  105. 'mall_id' => $mall_id,
  106. 'share_num' => 1
  107. ];
  108. $save_statistic = PkActivityStatistic::setData($statistic);
  109. if (!$save_statistic) {
  110. throw new Exception('保存活动统计失败');
  111. }
  112. $date_statistic = [
  113. 'mall_id' => $mall_id,
  114. 'date' => date('Y-m-d'),
  115. 'share_num' => 1
  116. ];
  117. $save_date_statistic = PkDateStatistic::setData($date_statistic);
  118. if (!$save_date_statistic) {
  119. throw new Exception('保存每天统计失败');
  120. }
  121. $pk_statistic = [
  122. 'mall_id' => $mall_id,
  123. 'share_num' => 1
  124. ];
  125. $save_pk_statistic = PkStatistic::setData($pk_statistic);
  126. if (!$save_pk_statistic) {
  127. throw new Exception('保存统计数据失败');
  128. }
  129. return true;
  130. }
  131. }