JiamengService.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * 加盟品牌服务
  4. * @author wesmielr
  5. */
  6. namespace app\index\service;
  7. use think\Db;
  8. class JiamengService
  9. {
  10. /**
  11. * 获取最新品牌
  12. * @param int $num
  13. * @return $this
  14. * @throws \think\db\exception\DataNotFoundException
  15. * @throws \think\db\exception\ModelNotFoundException
  16. * @throws \think\exception\DbException
  17. */
  18. public static function getNewList($num = 10)
  19. {
  20. $cacheKey = "cache:jiameng:newlist_".$num;
  21. $dataList = RedisService::get($cacheKey);
  22. if ($dataList) {
  23. return $dataList;
  24. }
  25. $touziarr = config('params.touziLevels');
  26. $dataList = Db::name('jiameng')
  27. ->field('id,title,touzi_level,logo,mendian,hits')
  28. ->where('status', 1)
  29. ->order('update_time desc,create_time desc, id desc')
  30. ->limit($num)
  31. ->select()
  32. ->each(function ($item, $k) use ($touziarr) {
  33. $touziLevel = isset($item['touzi_level']) ? $item['touzi_level'] : '-1';
  34. $item['touzi_level_name'] = isset($touziarr[$touziLevel]) ? $touziarr[$touziLevel] : '';
  35. return $item;
  36. });
  37. $dataList = $dataList ? $dataList->toArray() : [];
  38. if ($dataList) {
  39. RedisService::set($cacheKey, $dataList, 6 * 3600);
  40. }
  41. return $dataList;
  42. }
  43. /**
  44. * 获取热门排行榜
  45. * @param int $num
  46. * @return array|\PDOStatement|string|\think\Collection
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. * @throws \think\exception\DbException
  50. */
  51. public static function getHotList($num = 20)
  52. {
  53. $cacheKey = "cache:jiameng:hotlist_".$num;
  54. $dataList = RedisService::get($cacheKey);
  55. if ($dataList) {
  56. // return $dataList;
  57. }
  58. $dataList = Db::name('jiameng')
  59. ->field('id,title,thumb,hits,zhiying')
  60. ->where(['status' => 1])
  61. ->order('hits desc')
  62. ->limit($num)
  63. ->select();
  64. $dataList = $dataList ? $dataList->toArray() : [];
  65. if ($dataList) {
  66. foreach ($dataList as &$item){
  67. $item['hits'] = intval($item['hits']) + 100;
  68. }
  69. RedisService::set($cacheKey, $dataList, 600);
  70. }
  71. return $dataList;
  72. }
  73. /**
  74. * 获取加盟品牌列表
  75. * @param $param 参数
  76. * @param string $field 返回字段:连表别名j-jiameng,c1-category,c2-category
  77. * @param int $pageSize 分页大小
  78. * @return \think\Paginator
  79. * @throws \think\exception\DbException
  80. */
  81. public static function getList($param, $field = '', $pageSize = 20)
  82. {
  83. $page = request()->get('page', 1);
  84. $cacheKey = "cache:jiameng:list:p" . $page . '_' . md5(json_encode($param).$field.$pageSize);
  85. $dataList = RedisService::get($cacheKey);
  86. if ($dataList) {
  87. return $dataList;
  88. }
  89. $pcatid = 0;
  90. $enname = isset($param['enname']) ? trim($param['enname']) : '';
  91. if ($enname) {
  92. $pcatid = Db::name('category')->where('enname', $enname)->value('id');
  93. }
  94. $pcid = isset($param['pcid']) ? intval($param['pcid']) : 0;
  95. $pcid = $pcid > 0 ? $pcid : $pcatid;
  96. $catids = [];
  97. if ($pcid) {
  98. $catids = Db::name('category')->where('parent_id', $pcid)->column('id');
  99. $catids[] = $pcid;
  100. }
  101. $pageParams = $param;
  102. $pageType = isset($param['pageType']) ?: 0;
  103. if ($pageType == 1) {
  104. $pageParams = ['page' => input('page', 1)];
  105. }
  106. $field = $field ? $field : 'j.id,j.title,j.logo,j.catid,j.pcatid,j.touzi_level,j.area,j.level,j.company,j.product,j.area_id,j.mendian,j.zhiying,j.found_at,j.is_choose,c1.catname as catname,c1.enname as encatname,c2.catname as pcatname,c2.enname as penname';
  107. $dataList = Db::name('jiameng')->alias('j')
  108. ->field($field)
  109. ->leftJoin('category c1', 'c1.id=j.catid')
  110. ->leftJoin('category c2', 'c2.id=j.pcatid')
  111. ->where(function ($query) use ($param, $catids) {
  112. $query->where('j.status', 1);
  113. $kw = isset($param['kw']) ? trim($param['kw']) : '';
  114. if ($kw) {
  115. $query->where('j.title', 'like', "%{$kw}%");
  116. }
  117. // 信息等级
  118. $level = isset($param['level']) ? intval($param['level']) : 0;
  119. if ($level > 0) {
  120. $query->where(['j.level' => $level]);
  121. }
  122. $catid = isset($param['catid']) ? intval($param['catid']) : 0;
  123. if ($catid > 0) {
  124. $query->where(['j.catid|j.pcatid' => $catid]);
  125. }
  126. if ($catids) {
  127. $query->where('catid', 'in', $catids);
  128. }
  129. $lv = isset($param['lv']) ? intval($param['lv']) : -1;
  130. $lv = $lv > 0 ? $lv : (isset($param['touzi_level']) ? intval($param['touzi_level']) : -1);
  131. if ($lv >= 0) {
  132. $query->where(['j.touzi_level' => $lv]);
  133. }
  134. $aid = isset($param['aid']) ? intval($param['aid']) : -1;
  135. $aid = $aid > 0 ? $aid : (isset($param['area_id']) ? intval($param['area_id']) : -1);
  136. if ($aid > 0) {
  137. $query->where(['j.area_id' => $aid]);
  138. }
  139. })
  140. ->order('j.update_time desc, j.create_time desc, j.list_order')
  141. ->paginate($pageSize, false, ['query' => $pageParams]);
  142. if ($dataList) {
  143. RedisService::set($cacheKey, $dataList, 3 * 24 * 3600);
  144. }
  145. return $dataList;
  146. }
  147. /**
  148. * 获取等级信息品牌列表
  149. * @param $level 等级:1--今日之星,2-品牌精选,3-品牌推荐,4-品牌严选,5-默认,6-推荐专题品牌
  150. * @param int $pageSize 记录数
  151. * @param string $field 返回字段
  152. * @return $this
  153. * @throws \think\db\exception\DataNotFoundException
  154. * @throws \think\db\exception\ModelNotFoundException
  155. * @throws \think\exception\DbException
  156. */
  157. public static function getListByLevel($level, $pageSize = 10, $field = '')
  158. {
  159. $touziarr = config('params.touziLevels');
  160. $field = $field ? $field : 'j.id,j.title,j.touzi_level,j.logo,j.hits,j.guanggaowei,j.touzi_level,j.cover_area,j.mendian,j.product,c1.catname';
  161. $cacheKey = "cache:jiameng:listbylevel:level_" . $level.'_'.$pageSize;
  162. $dataList = RedisService::get($cacheKey);
  163. if ($dataList) {
  164. return $dataList;
  165. }
  166. $dataList = Db::name('jiameng')
  167. ->alias('j')
  168. ->leftJoin('category c1', 'c1.id=j.catid')
  169. ->field($field)
  170. ->where('j.level', $level)
  171. ->where('j.status', 1)
  172. ->order('j.list_order asc, j.id desc')
  173. ->limit($pageSize)
  174. ->select()
  175. ->each(function ($item, $k) use ($touziarr) {
  176. $touziLevel = isset($item['touzi_level']) ? $item['touzi_level'] : '-1';
  177. $item['touzi_level_name'] = isset($touziarr[$touziLevel]) ? $touziarr[$touziLevel] : '';
  178. return $item;
  179. });
  180. $dataList = $dataList ? $dataList->toArray() : [];
  181. if ($dataList) {
  182. RedisService::set($cacheKey, $dataList, 7 * 24 * 3600);
  183. }
  184. return $dataList;
  185. }
  186. /**
  187. * 获取排行榜数据
  188. * @param $where 条件
  189. * @param $pageSize 记录数
  190. * @param string $field 字段
  191. * @return $this
  192. * @throws \think\db\exception\DataNotFoundException
  193. * @throws \think\db\exception\ModelNotFoundException
  194. * @throws \think\exception\DbException
  195. */
  196. public static function getTopList($where, $pageSize = 10, $field = '')
  197. {
  198. $cacheKey = "cache:jiameng:toplist:" . md5(json_encode($where).$pageSize.$field);
  199. $dataList = RedisService::get($cacheKey);
  200. if ($dataList) {
  201. return $dataList;
  202. }
  203. $touziarr = config('params.touziLevels');
  204. $field = $field ? $field : 'j.id,j.title,j.touzi_level,j.logo,j.guanggaowei,j.touzi_level,j.cover_area,j.mendian,j.product,c1.catname,c2.catname as pcatname';
  205. $dataList = Db::name('jiameng')
  206. ->alias('j')
  207. ->leftJoin('category c1', 'c1.id=j.catid')
  208. ->leftJoin('category c2', 'c2.id=j.pcatid')
  209. ->field($field)
  210. ->where('j.status', 1)
  211. ->where($where)
  212. ->order('j.hits desc')
  213. ->limit($pageSize)
  214. ->select()
  215. ->each(function ($item, $k) use ($touziarr) {
  216. $touziLevel = isset($item['touzi_level']) ? $item['touzi_level'] : '-1';
  217. $item['touzi_level_name'] = isset($touziarr[$touziLevel]) ? $touziarr[$touziLevel] : '';
  218. return $item;
  219. });
  220. $dataList = $dataList ? $dataList->toArray() : [];
  221. if ($dataList) {
  222. RedisService::set($cacheKey, $dataList, 3 * 24 * 3600);
  223. }
  224. return $dataList;
  225. }
  226. }