MediaModel.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace App\Models\Fpdx;
  3. use App\Models\Model;
  4. use Illuminate\Support\Facades\Redis;
  5. /**
  6. * App\Models\Fpdx\MediaModel
  7. *
  8. * @property int $id 主键
  9. * @property string $media_id 公众号
  10. * @property string $public_name 公众号名称
  11. * @property string $head_img 头像
  12. * @property string $school 学校
  13. * @property int $sync_at 同步时间
  14. * @property float $gold_flower 累计小fa
  15. * @property float $get_flower 已获得小fa
  16. * @property string|null $keywords 关键字集
  17. * @property int $verify_type_info 认证状态
  18. * @property float $fpdx_money 分配对象收益
  19. * @property string|null $attach 附近值
  20. * @property int $type media_id类型
  21. * 0=公众号渠道
  22. * 1=口袋渠道
  23. * 2=kol渠道
  24. * 3=广告结单渠道
  25. * @property string $username 账号
  26. * @property int $fans 粉丝
  27. * @property int $kol_type kol渠道类型
  28. * @property \Illuminate\Support\Carbon $created_at 创建时间
  29. * @property \Illuminate\Support\Carbon $updated_at 修改时间
  30. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\MediaModel newModelQuery()
  31. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\MediaModel newQuery()
  32. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\MediaModel query()
  33. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\MediaModel whereAttach($value)
  34. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\MediaModel whereCreatedAt($value)
  35. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\MediaModel whereFans($value)
  36. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\MediaModel whereFpdxMoney($value)
  37. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\MediaModel whereGetFlower($value)
  38. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\MediaModel whereGoldFlower($value)
  39. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\MediaModel whereHeadImg($value)
  40. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\MediaModel whereId($value)
  41. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\MediaModel whereKeywords($value)
  42. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\MediaModel whereKolType($value)
  43. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\MediaModel whereMediaId($value)
  44. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\MediaModel wherePublicName($value)
  45. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\MediaModel whereSchool($value)
  46. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\MediaModel whereSyncAt($value)
  47. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\MediaModel whereType($value)
  48. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\MediaModel whereUpdatedAt($value)
  49. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\MediaModel whereUsername($value)
  50. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Fpdx\MediaModel whereVerifyTypeInfo($value)
  51. * @mixin \Eloquent
  52. */
  53. class MediaModel extends Model
  54. {
  55. protected $table = 'kdgx_partner_charge_media';
  56. protected $fillable = [
  57. 'media_id',
  58. 'public_name',
  59. 'gold_flower',
  60. 'red_flower',
  61. 'partner_id',
  62. 'from_media',
  63. 'pay_media',
  64. 'attach'
  65. ];
  66. public $dateFormat = 'U';
  67. /**
  68. * 获取公众号信息
  69. * @param string $media_id
  70. * @param string $app
  71. * @return object
  72. * {
  73. * media_id: 公众号id
  74. * public_name: 公众号名称
  75. * school: 公众号学校
  76. * keyword: [
  77. * "关键词1",
  78. * "关键词2"
  79. * ]
  80. * verify_type_info: 是否认证 [0认证 | -1未认证]
  81. * }
  82. * @throws \ApiException
  83. */
  84. public static function mediaInfo(string $media_id, string $app = 'fpdx')
  85. {
  86. $data = self::where('media_id', $media_id)->first();
  87. if (collect($data)->isEmpty()) {
  88. throw new \ApiException("不存在的公众号", 404);
  89. }
  90. $media_key = new MediaKeyModel();
  91. $keywords = $media_key->where([
  92. ['media_id', $media_id],
  93. ['app', $app]
  94. ])->get();
  95. $keys = array();
  96. $keywords->each(function ($item, $key) use (&$keys) {
  97. array_map(function ($value) use (&$keys) {
  98. array_push($keys, $value);
  99. }, json_decode($item->keywords, true));
  100. });
  101. $data->keywords = $keys ?: [];
  102. return $data;
  103. }
  104. /**
  105. * 是否开启某个app
  106. * @param string 公众号id
  107. * @param string app 应用
  108. * @param string plat 平台
  109. * @return boolean
  110. */
  111. public function isOpen(string $media_id, string $app, string $plat = '')
  112. {
  113. if ($app == 'msy') {
  114. $data = self::where('media_id', $media_id)->first();
  115. if (empty($data->keywords)) {
  116. return false;
  117. } else {
  118. return true;
  119. }
  120. }
  121. $media_key = new MediaKeyModel();
  122. $first = $media_key->where([
  123. ['media_id', $media_id],
  124. ['app', $app]
  125. ])->when($plat, function ($query) use ($plat) {
  126. return $query->where('plat', $plat);
  127. })->first();
  128. if (collect($first)->isEmpty()) {
  129. return false;
  130. } else {
  131. return true;
  132. }
  133. }
  134. /**
  135. * 公众号收益总计
  136. * @param string media_id 公众号id
  137. * @return array [
  138. * 'count' => 交易笔数,
  139. * 'msy_money' => 累计收益,
  140. * 'today_count' => 今日笔数
  141. * ]
  142. */
  143. public function incomeData(string $media_id)
  144. {
  145. $msy_money = self::where('media_id', $media_id)->first(['gold_flower']);
  146. $count = MediaLogModel::where(function ($query) use ($media_id) {
  147. $query->where('pay_media', $media_id)->orWhere('from_media', $media_id);
  148. })->count();
  149. $today_count = MediaLogModel::where(function ($query) use ($media_id) {
  150. $query->where('pay_media', $media_id)->orWhere('from_media', $media_id);
  151. })->whereBetween('create_at', [mktime(0, 0, 0), mktime(0, 0, 0) + 86400])->count();
  152. return [
  153. 'count' => $count,
  154. 'msy_money' => isset($msy_money->gold_flower) ? $msy_money->gold_flower : 0,
  155. 'today_count' => $today_count
  156. ];
  157. }
  158. /**
  159. * 获取开启的公众号列表
  160. * @param string app 应用
  161. * @return array
  162. * [
  163. * [
  164. * 'media_id' => 公众号id,
  165. * ‘public_name' => 公众号名称,
  166. * 'school' => 学校
  167. * ]
  168. * ]
  169. */
  170. public function openMedias(string $app)
  171. {
  172. $hides = Redis::lrange("{pair_hides}", 0, -1);
  173. $medias = MediaKeyModel::where('app', $app)->whereNotIn(
  174. 'media_id',
  175. $hides
  176. )->groupBy('media_id')->get(['media_id'])->pluck('media_id');
  177. $data = $this->where('type', 0)->whereIn('media_id', $medias)->get(['media_id', 'public_name', 'school']);
  178. return $data;
  179. }
  180. }