JiamengService.php 8.9 KB

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