MeetingModel.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. <?php
  2. namespace App\Web\Models;
  3. use App\Models\BaseModel;
  4. use Illuminate\Support\Facades\DB;
  5. class MeetingModel extends BaseModel
  6. {
  7. /**
  8. * 状态字段
  9. */
  10. const DELETED_AT = 'status';
  11. /**
  12. * @var string
  13. */
  14. protected $table = 'meeting';
  15. /**
  16. * 获取产品列表
  17. * */
  18. public function getMeetingList($params, $fields = 'a.*')
  19. {
  20. list($pageSize, $page, $skip) = $this->getPaginatorParams($params);
  21. $where = [];
  22. if (isset($params['status'])) {
  23. $where[] = ['a.status', '=', $params['status']];
  24. } else {
  25. $where[] = ['a.status', '<', 2];
  26. }
  27. if (!empty($params['lt_total_view'])) {
  28. $where[] = ['a.total_view', '<', $params['lt_total_view']];
  29. }
  30. $query = $this->newInstance()->alias('a')->where($where);
  31. if (!empty($params['keyword'])) {
  32. $keyword = $params['keyword'];
  33. $query->where('a.title', 'like', "%" . $keyword . "%");
  34. }
  35. if (!empty($params['sort'])) {
  36. if ($params['sort'] == 'score_ascending') {
  37. $query->orderBy('score', 'asc');
  38. } else if ($params['sort'] == 'score_descending') {
  39. $query->orderBy('score', 'desc');
  40. } else if ($params['sort'] == 'pub_date_ascending') {
  41. $query->orderBy('pub_date', 'asc');
  42. } else if ($params['sort'] == 'pub_date_descending') {
  43. $query->orderBy('pub_date', 'desc');
  44. } else if ($params['sort'] == 'create_time_ascending') {
  45. $query->orderBy('create_time', 'asc');
  46. } else if ($params['sort'] == 'create_time_descending') {
  47. $query->orderBy('create_time', 'desc');
  48. } else if ($params['sort'] == 'update_time_ascending') {
  49. $query->orderBy('update_time', 'asc');
  50. } else if ($params['sort'] == 'update_time_descending') {
  51. $query->orderBy('update_time', 'desc');
  52. }
  53. } else {
  54. $query->orderBy('is_top', 'desc')
  55. ->orderBy('sort');
  56. }
  57. $totalCount = $query->count();
  58. $list = $query->skip($skip)
  59. ->limit($pageSize)
  60. ->selectRaw($fields)
  61. ->get();
  62. if (!empty($list)) {
  63. $list = $list->toArray();
  64. } else {
  65. $list = [];
  66. }
  67. $result = $this->buildPaginator($list, $skip, $page, $pageSize, $totalCount);
  68. return $result;
  69. }
  70. /**
  71. * 获取产品分类未关联的产品
  72. * */
  73. public function getUnrelatedMeetingByTypeIds($params)
  74. {
  75. list($pageSize, $page, $skip) = $this->getPaginatorParams($params);
  76. $typeId = $params['type_id'] ?? '';
  77. $keyword = $params['keyword'] ?? '';
  78. $where = [];
  79. if ($keyword) {
  80. $keyword = addslashes($keyword);
  81. $where['_string'] = "((a.`title` like '%{$keyword}%' or a.`description` like '%{$keyword}%'))";
  82. }
  83. $totalCount = $this->newInstance()->buildQuery($where)->from($this->getTable() . ' as a')
  84. ->leftJoin('meeting_type_relation as b', function ($join) use ($typeId) {
  85. $join->on('a.id', '=', 'b.meeting_id')
  86. ->where('b.status', '=', 0)
  87. ->where('b.type_id', '=', $typeId);
  88. })->where('a.status', '<', 2)->whereNull('b.id')->count();
  89. $fields = 'a.id,a.title,a.pub_date,a.image_url,a.description,a.status,a.update_time,a.create_time';
  90. $list = $this->newInstance()->buildQuery($where)->from($this->getTable() . ' as a')
  91. ->leftJoin('meeting_type_relation as b', function ($join) use ($typeId) {
  92. $join->on('a.id', '=', 'b.meeting_id')
  93. ->where('b.status', '=', 0)
  94. ->where('b.type_id', '=', $typeId);
  95. })->where('a.status', '<', 2)->whereNull('b.id')->skip($skip)
  96. ->limit($pageSize)
  97. ->selectRaw($fields)
  98. ->orderBy('sort')
  99. ->get();
  100. if (!empty($list)) {
  101. $list = $list->toArray();
  102. } else {
  103. $list = [];
  104. }
  105. $result = $this->buildPaginator($list, $skip, $page, $pageSize, $totalCount);
  106. return $result;
  107. }
  108. /**
  109. * 获取产品标签未关联的产品
  110. * */
  111. public function getUnrelatedMeetingByTagIds($params)
  112. {
  113. list($pageSize, $page, $skip) = $this->getPaginatorParams($params);
  114. $tagId = $params['tag_id'] ?? '';
  115. $keyword = $params['keyword'] ?? '';
  116. $where = [];
  117. if ($keyword) {
  118. $keyword = addslashes($keyword);
  119. $where['_string'] = "((a.`title` like '%{$keyword}%' or a.`description` like '%{$keyword}%'))";
  120. }
  121. $totalCount = $this->newInstance()->buildQuery($where)->from($this->getTable() . ' as a')
  122. ->leftJoin('meeting_tag_relation as b', function ($join) use ($tagId) {
  123. $join->on('a.id', '=', 'b.meeting_id')
  124. ->where('b.status', '=', 0)
  125. ->where('b.tag_id', '=', $tagId);
  126. })->where('a.status', '<', 2)->whereNull('b.id')->count();
  127. $fields = 'a.id,a.title,a.pub_date,a.image_url,a.description,a.status,a.update_time,a.create_time';
  128. $list = $this->newInstance()->buildQuery($where)->from($this->getTable() . ' as a')
  129. ->leftJoin('meeting_tag_relation as b', function ($join) use ($tagId) {
  130. $join->on('a.id', '=', 'b.meeting_id')
  131. ->where('b.status', '=', 0)
  132. ->where('b.tag_id', '=', $tagId);
  133. })->where('a.status', '<', 2)->whereNull('b.id')->skip($skip)
  134. ->limit($pageSize)
  135. ->selectRaw($fields)
  136. ->orderBy('sort')
  137. ->get();
  138. if (!empty($list)) {
  139. $list = $list->toArray();
  140. } else {
  141. $list = [];
  142. }
  143. $result = $this->buildPaginator($list, $skip, $page, $pageSize, $totalCount);
  144. return $result;
  145. }
  146. /**
  147. * 产品保存
  148. * @param array $data
  149. * */
  150. public function saveMeetingData($data)
  151. {
  152. if (!empty($data['id'])) {
  153. $id = $data['id'];
  154. $this->newInstance()->where('id', $data['id'])->update($data);
  155. } else {
  156. $id = $this->newInstance()->insertGetId($data);
  157. }
  158. return $id;
  159. }
  160. /**
  161. * 检查 标签是否唯一
  162. * */
  163. public function checkNameUnique($name, $id = 0)
  164. {
  165. $where = [];
  166. if (!empty($id)) {
  167. $where[] = ['id', '<>', $id];
  168. }
  169. $where[] = ['status', '<', 2];
  170. $where[] = ['title', '=', trim($name)];
  171. return $this->checkFieldUnique('title', $where);
  172. }
  173. /**
  174. * 获取产品详情
  175. * @param array $params
  176. * */
  177. public function getMeetingInfo($params = [])
  178. {
  179. if (empty($params)) {
  180. return [];
  181. }
  182. $where = [];
  183. $where[] = ['status', '<', 2];
  184. if (!empty($params['id'])) {
  185. $where['id'] = $params['id'];
  186. }
  187. $info = $this->newInstance()->where($where)->first();
  188. if (!empty($info)) {
  189. return $info->toArray();
  190. } else {
  191. return [];
  192. }
  193. }
  194. /**
  195. * 获取产品基本列表
  196. * */
  197. public function getPublishMeetingList($params, $fields = 'a.*')
  198. {
  199. $where = [];
  200. $where['a.status'] = 0;
  201. $query = $this->newInstance()->alias('a')
  202. ->leftJoin('web_seo as b', 'a.seo_id', '=', 'b.id')
  203. ->where($where);
  204. $pageSize = $this->getPageSize($params);
  205. if (!empty($params['sort'])) {
  206. if (!empty($params['sort']['is_hot'])) {
  207. $query = $query->orderBy('a.total_view', 'desc');
  208. }
  209. if (!empty($params['sort']['is_top'])) {
  210. $query = $query->orderBy('a.is_top', 'desc');
  211. }
  212. if (!empty($params['sort']['is_recommend'])) {
  213. $query = $query->orderBy('a.is_recommend', 'desc');
  214. }
  215. if (!empty($params['sort']['pub_date'])) {
  216. $query = $query->orderBy('a.pub_date', 'desc');
  217. }
  218. if (!empty($params['sort']['start_date'])) {
  219. $query = $query->orderBy('a.start_date', 'asc');
  220. $query = $query->orderBy('a.end_date', 'desc');
  221. }
  222. } else {
  223. $query = $query->orderBy('a.sort');
  224. }
  225. if (!empty($params['keyword'])) {
  226. $keyword = $params['keyword'];
  227. $query = $query->where(function ($queryStr) use ($keyword) {
  228. $queryStr->where('a.title', 'like', "%" . $keyword . "%")
  229. ->orWhere('a.description', 'like', '%' . $keyword . '%');
  230. });
  231. }
  232. if (!empty($params['country'])) {
  233. $query->where('a.country', $params['country']);
  234. }
  235. if (!empty($params['ids'])) {
  236. $query->whereIn('a.id', $params['ids']);
  237. }
  238. if(!empty($params['expo_date'])) {
  239. $dateArr = $params['expo_date'];
  240. $dateArr = explode(' ', $dateArr);
  241. if(!empty($dateArr[0]) && !empty($dateArr[1])) {
  242. $startDate = $dateArr[0].'-01';
  243. $date = new \DateTime($dateArr[1]);
  244. $date->modify('last day of this month');
  245. $endDate = $date->format('Y-m-d');
  246. //有日期落在选定区间内的
  247. $query = $query->where(function ($queryStr) use ($startDate, $endDate) {
  248. $queryStr->where('a.start_date', '<=', $endDate)
  249. ->where('a.end_date', '>=', $startDate);
  250. });
  251. }
  252. }
  253. if(!empty($params['is_paginate'])){
  254. $list= $query->selectRaw($fields)->paginate($pageSize)
  255. ->toArray();
  256. }else{
  257. $list= $query->limit($pageSize)->selectRaw($fields)->get();
  258. if(!empty($list)){
  259. $list=$list->toArray();
  260. }else{
  261. $list=[];
  262. }
  263. }
  264. return $list;
  265. }
  266. /**
  267. * 根据类型ids获取个类型产品
  268. * */
  269. public function getRenderListByTypeIds($typeIds, $params, $fields = 'a.*')
  270. {
  271. $where = [];
  272. $where['a.status'] = 0;
  273. $where['b.status'] = 0;
  274. $pageSize = $this->getPageSize($params);
  275. $sql = '';
  276. foreach ($typeIds as $item) {
  277. $sqlStr = $this->newInstance()->alias('a')
  278. ->leftJoin('meeting_type_relation as b', 'a.id', '=', 'b.meeting_id')
  279. ->leftJoin('web_seo as c', 'a.seo_id', '=', 'c.id')
  280. ->where($where)
  281. ->where('b.type_id', '=', DB::raw($item))
  282. ->orderBy('a.is_top', 'desc')
  283. ->orderBy('a.pub_date', 'desc')
  284. ->limit($pageSize)->selectRaw($fields);
  285. if (empty($sql)) {
  286. $sql = $sqlStr;
  287. } else {
  288. $sql->unionAll($sqlStr);
  289. }
  290. }
  291. $list = [];
  292. if (!empty($sql)) {
  293. $list = $sql->get();
  294. if ($list) {
  295. $list = $list->toArray();
  296. }
  297. }
  298. return $list;
  299. }
  300. /**
  301. * 根据类型id获取产品
  302. * */
  303. public function getPublishMeetingListByTypeId($params, $fields = 'a.*')
  304. {
  305. list($pageSize, $page, $skip) = $this->getPaginatorParams($params);
  306. $where = [];
  307. $where['a.status'] = 0;
  308. if (!empty($params['type_id'])) {
  309. $where['b.type_id'] = $params['type_id'];
  310. $where['b.status'] = 0;
  311. }
  312. $typeId = empty($params['type_id']) ? 0 : $params['type_id'];
  313. $list = $this->newInstance()->alias('a')->selectRaw($fields)
  314. ->leftJoin('meeting_type_relation as b', 'a.id', '=', 'b.meeting_id')
  315. ->leftJoin('web_seo as c', 'a.seo_id', '=', 'c.id')
  316. ->where($where)
  317. ->groupBy('a.id')
  318. ->orderBy('a.is_top', 'desc')
  319. ->orderBy('a.pub_date', 'desc')
  320. ->paginate($pageSize)
  321. ->toArray();
  322. return $list;
  323. }
  324. /**
  325. * 根据标签id获取产品
  326. * */
  327. public function getPublishMeetingListByTagId($params, $fields = 'a.*')
  328. {
  329. list($pageSize, $page, $skip) = $this->getPaginatorParams($params);
  330. $where = [];
  331. $where['a.status'] = 0;
  332. if (!empty($params['tag_id'])) {
  333. $where['b.tag_id'] = $params['tag_id'];
  334. $where['b.status'] = 0;
  335. }
  336. $totalCount = $this->newInstance()->alias('a')
  337. ->leftJoin('meeting_tag_relation as b', 'a.id', '=', 'b.meeting_id')
  338. ->leftJoin('web_seo as c', 'a.seo_id', '=', 'c.id')
  339. ->where($where)->count();
  340. $list = $this->newInstance()->alias('a')
  341. ->leftJoin('meeting_tag_relation as b', 'a.id', '=', 'b.meeting_id')
  342. ->leftJoin('web_seo as c', 'a.seo_id', '=', 'c.id')
  343. ->where($where)
  344. ->orderBy('a.is_top', 'desc')
  345. ->orderBy('a.pub_date', 'desc')
  346. ->limit($pageSize)->selectRaw($fields)->get();
  347. if ($list) {
  348. $list = $list->toArray();
  349. } else {
  350. $list = [];
  351. }
  352. $result = $this->buildPaginator($list, $skip, $page, $pageSize, $totalCount);
  353. return $result;
  354. }
  355. /**
  356. * 根据类型ids获取标签产品
  357. * */
  358. public function getRenderListByTagIds($tagIds, $params, $fields = 'a.*')
  359. {
  360. $where = [];
  361. $where['a.status'] = 0;
  362. $where['b.status'] = 0;
  363. $pageSize = $this->getPageSize($params);
  364. $sql = '';
  365. foreach ($tagIds as $item) {
  366. $sqlStr = $this->newInstance()->alias('a')
  367. ->leftJoin('meeting_tag_relation as b', 'a.id', '=', 'b.meeting_id')
  368. ->leftJoin('web_seo as c', 'a.seo_id', '=', 'c.id')
  369. ->where($where)
  370. ->where('b.tag_id', '=', DB::raw($item))
  371. ->orderBy('a.is_top', 'desc')
  372. ->orderBy('a.pub_date', 'desc')
  373. ->limit($pageSize)->selectRaw($fields);
  374. if (empty($sql)) {
  375. $sql = $sqlStr;
  376. } else {
  377. $sql->unionAll($sqlStr);
  378. }
  379. }
  380. $list = [];
  381. if (!empty($sql)) {
  382. $list = $sql->get();
  383. if ($list) {
  384. $list = $list->toArray();
  385. }
  386. }
  387. return $list;
  388. }
  389. public function updatePv($id)
  390. {
  391. DB::update('update meeting set votes = 1');
  392. }
  393. public function getLocalCountryList()
  394. {
  395. $where = [];
  396. $where['a.status'] = 0;
  397. $list = $this->newInstance()->alias('a')
  398. ->where($where)
  399. ->selectRaw('DISTINCT country')
  400. ->get();
  401. $country = [];
  402. if (!empty($list)) {
  403. $list = $list->toArray();
  404. $country = array_column($list, 'country');
  405. }
  406. return $country;
  407. }
  408. public function getPrevItem($meetingId)
  409. {
  410. $data = $this->alias('a')
  411. ->leftJoin('web_seo as b', 'a.seo_id', '=', 'b.id')
  412. ->where('a.id', '<', $meetingId)
  413. ->where('a.status', 0)
  414. ->selectRaw('a.id,b.urla,a.title,a.image_url,a.image_alt')
  415. ->orderBy('a.id', 'asc')
  416. ->first();
  417. return $data ? $data->toArray() : [];
  418. }
  419. public function getNextItem($meetingId)
  420. {
  421. $data = $this->alias('a')
  422. ->leftJoin('web_seo as b', 'a.seo_id', '=', 'b.id')
  423. ->where('a.id', '>', $meetingId)
  424. ->where('a.status', 0)
  425. ->selectRaw('a.id,b.urla,a.title,a.image_url,a.image_alt')
  426. ->orderBy('a.id', 'asc')
  427. ->first();
  428. return $data ? $data->toArray() : [];
  429. }
  430. public function getAreaDataWithTagId($tagId)
  431. {
  432. $where = ['a.status' => 0];
  433. $where[] = ['a.country', '<>', ''];
  434. $where['b.tag_id'] = $tagId;
  435. return $this->alias('a')
  436. ->leftJoin('meeting_tag_relation as b', 'a.id', '=', 'b.meeting_id')
  437. ->selectRaw('a.country AS id,a.country AS text')
  438. ->where($where)
  439. ->groupBy('a.country')
  440. ->limit(999)
  441. ->get()
  442. ->toArray();
  443. }
  444. public function getTypesWithTagId($tagId)
  445. {
  446. $where = ['a.status' => 0];
  447. $where['b.status'] = 0;
  448. $where['c.status'] = 0;
  449. $where['d.status'] = 0;
  450. $where['b.tag_id'] = $tagId;
  451. return $this->alias('a')
  452. ->leftJoin('meeting_tag_relation as b', 'a.id', '=', 'b.meeting_id')
  453. ->leftJoin('meeting_type_relation as c', 'b.meeting_id', '=', 'c.meeting_id')
  454. ->leftJoin('meeting_type as d', 'd.id', '=', 'c.type_id')
  455. ->selectRaw('d.id,d.type_name AS text')
  456. ->where($where)
  457. ->groupBy('d.type_name')
  458. ->limit(999)
  459. ->get()->toArray();
  460. }
  461. }