JiamengService.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. if ($param['lv']=='11111') {
  90. unset($param['lv']);
  91. }
  92. $pcatid = 0;
  93. $enname = isset($param['enname']) ? trim($param['enname']) : '';
  94. if ($enname) {
  95. $pcatid = Db::name('category')->where('enname', $enname)->value('id');
  96. }
  97. $pcid = isset($param['pcid']) ? intval($param['pcid']) : 0;
  98. $pcid = $pcid > 0 ? $pcid : $pcatid;
  99. $catids = [];
  100. if ($pcid) {
  101. $catids = Db::name('category')->where('parent_id', $pcid)->column('id');
  102. $catids[] = $pcid;
  103. }
  104. $pageParams = $param;
  105. $pageType = isset($param['pageType']) ?: 0;
  106. if ($pageType == 1) {
  107. $pageParams = ['page' => input('page', 1)];
  108. }
  109. $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';
  110. $dataList = Db::name('jiameng')->alias('j')
  111. ->field($field)
  112. ->leftJoin('category c1', 'c1.id=j.catid')
  113. ->leftJoin('category c2', 'c2.id=j.pcatid')
  114. ->where(function ($query) use ($param, $catids) {
  115. $query->where('j.status', 1);
  116. $kw = isset($param['kw']) ? trim($param['kw']) : '';
  117. if ($kw) {
  118. $query->where('j.title', 'like', "%{$kw}%");
  119. }
  120. // 信息等级
  121. $level = isset($param['level']) ? intval($param['level']) : 0;
  122. if ($level > 0) {
  123. $query->where(['j.level' => $level]);
  124. }
  125. $catid = isset($param['catid']) ? intval($param['catid']) : 0;
  126. if ($catid > 0) {
  127. $query->where(['j.catid|j.pcatid' => $catid]);
  128. }
  129. if ($catids) {
  130. $query->where('catid', 'in', $catids);
  131. }
  132. $lv = isset($param['lv']) ? intval($param['lv']) : -1;
  133. $lv = $lv > 0 ? $lv : (isset($param['touzi_level']) ? intval($param['touzi_level']) : -1);
  134. if ($lv >= 0) {
  135. $query->where(['j.touzi_level' => $lv]);
  136. }
  137. $aid = isset($param['aid']) ? intval($param['aid']) : -1;
  138. $aid = $aid > 0 ? $aid : (isset($param['area_id']) ? intval($param['area_id']) : -1);
  139. if ($aid > 0) {
  140. $query->where(['j.area_id' => $aid]);
  141. }
  142. // if(isset($param['aid']) && $param['aid']!='不限'){
  143. // $query->where('j.area','like','%'.$param['aid'].'%');
  144. // }
  145. })
  146. ->order('j.update_time desc, j.create_time desc, j.list_order')
  147. ->paginate($pageSize, false, ['query' => $pageParams]);
  148. // echo '::'.Db::name('jiameng')->getLastSql();die();
  149. if ($dataList) {
  150. RedisService::set($cacheKey, $dataList, 3 * 24 * 3600);
  151. }
  152. return $dataList;
  153. }
  154. /**
  155. * 获取等级信息品牌列表
  156. * @param $level 等级:1--今日之星,2-品牌精选,3-品牌推荐,4-品牌严选,5-默认,6-推荐专题品牌
  157. * @param int $pageSize 记录数
  158. * @param string $field 返回字段
  159. * @return $this
  160. * @throws \think\db\exception\DataNotFoundException
  161. * @throws \think\db\exception\ModelNotFoundException
  162. * @throws \think\exception\DbException
  163. */
  164. public static function getListByLevel($level, $pageSize = 10, $field = '')
  165. {
  166. $touziarr = config('params.touziLevels');
  167. $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';
  168. $cacheKey = "cache:jiameng:listbylevel:level_" . $level.'_'.$pageSize;
  169. $dataList = RedisService::get($cacheKey);
  170. if ($dataList) {
  171. return $dataList;
  172. }
  173. $dataList = Db::name('jiameng')
  174. ->alias('j')
  175. ->leftJoin('category c1', 'c1.id=j.catid')
  176. ->field($field)
  177. ->where('j.level', $level)
  178. ->where('j.status', 1)
  179. ->order('j.list_order asc, j.id desc')
  180. ->limit($pageSize)
  181. ->select()
  182. ->each(function ($item, $k) use ($touziarr) {
  183. $touziLevel = isset($item['touzi_level']) ? $item['touzi_level'] : '-1';
  184. $item['touzi_level_name'] = isset($touziarr[$touziLevel]) ? $touziarr[$touziLevel] : '';
  185. return $item;
  186. });
  187. $dataList = $dataList ? $dataList->toArray() : [];
  188. if ($dataList) {
  189. RedisService::set($cacheKey, $dataList, 7 * 24 * 3600);
  190. }
  191. return $dataList;
  192. }
  193. /**
  194. * 获取排行榜数据
  195. * @param $where 条件
  196. * @param $pageSize 记录数
  197. * @param string $field 字段
  198. * @return $this
  199. * @throws \think\db\exception\DataNotFoundException
  200. * @throws \think\db\exception\ModelNotFoundException
  201. * @throws \think\exception\DbException
  202. */
  203. public static function getTopList($where, $pageSize = 10, $field = '')
  204. {
  205. $cacheKey = "cache:jiameng:toplist:" . md5(json_encode($where).$pageSize.$field);
  206. $dataList = RedisService::get($cacheKey);
  207. if ($dataList) {
  208. return $dataList;
  209. }
  210. $touziarr = config('params.touziLevels');
  211. $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';
  212. $dataList = Db::name('jiameng')
  213. ->alias('j')
  214. ->leftJoin('category c1', 'c1.id=j.catid')
  215. ->leftJoin('category c2', 'c2.id=j.pcatid')
  216. ->field($field)
  217. ->where('j.status', 1)
  218. ->where($where)
  219. ->order('j.hits desc')
  220. ->limit($pageSize)
  221. ->select()
  222. ->each(function ($item, $k) use ($touziarr) {
  223. $touziLevel = isset($item['touzi_level']) ? $item['touzi_level'] : '-1';
  224. $item['touzi_level_name'] = isset($touziarr[$touziLevel]) ? $touziarr[$touziLevel] : '';
  225. return $item;
  226. });
  227. $dataList = $dataList ? $dataList->toArray() : [];
  228. if ($dataList) {
  229. RedisService::set($cacheKey, $dataList, 3 * 24 * 3600);
  230. }
  231. return $dataList;
  232. }
  233. }