JiamengService.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. $touziarr = config('params.touziLevels');
  21. $dataList = Db::name('jiameng')
  22. ->field('id,title,touzi_level,logo,mendian,hits')
  23. ->where('status', 1)
  24. ->order('update_time desc,create_time desc, id desc')
  25. ->limit($num)
  26. ->select()
  27. ->each(function ($item, $k) use ($touziarr) {
  28. $touziLevel = isset($item['touzi_level']) ? $item['touzi_level'] : '-1';
  29. $item['touzi_level_name'] = isset($touziarr[$touziLevel]) ? $touziarr[$touziLevel] : '';
  30. return $item;
  31. });
  32. return $dataList;
  33. }
  34. /**
  35. * 获取热门排行榜
  36. * @param int $num
  37. * @return array|\PDOStatement|string|\think\Collection
  38. * @throws \think\db\exception\DataNotFoundException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. * @throws \think\exception\DbException
  41. */
  42. public static function getHotList($num = 20)
  43. {
  44. return Db::name('jiameng')
  45. ->field('id,title,thumb,hits,zhiying')
  46. ->where(['status' => 1])->order('hits desc')
  47. ->limit($num)
  48. ->select();
  49. }
  50. /**
  51. * 获取加盟品牌列表
  52. * @param $param 参数
  53. * @param string $field 返回字段:连表别名j-jiameng,c1-category,c2-category
  54. * @param int $pageSize 分页大小
  55. * @return \think\Paginator
  56. * @throws \think\exception\DbException
  57. */
  58. public static function getList($param, $field = '', $pageSize = 20)
  59. {
  60. var_dump($param);
  61. $pcatid = 0;
  62. $enname = isset($param['enname'])? trim($param['enname']) : '';
  63. if($enname){
  64. $pcatid = Db::name('category')->where('enname', $enname)->value('id');
  65. }
  66. $pcid = isset($param['pcid']) ? intval($param['pcid']) : 0;
  67. $pcid = $pcid>0? $pcid : $pcatid;
  68. $catids = [];
  69. if($pcid){
  70. $catids = Db::name('category')->where('parent_id', $pcid)->column('id');
  71. $catids[] = $pcid;
  72. }
  73. $pageParams = $param;
  74. $pageType = isset($param['pageType'])? : 0;
  75. if($pageType == 1){
  76. $pageParams = ['page'=> input('page', 1)];
  77. }
  78. $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';
  79. $dataList = Db::name('jiameng')->alias('j')
  80. ->field($field)
  81. ->leftJoin('category c1', 'c1.id=j.catid')
  82. ->leftJoin('category c2', 'c2.id=j.pcatid')
  83. ->where(function ($query) use ($param, $catids) {
  84. $query->where('j.status', 1);
  85. $kw = isset($param['kw']) ? trim($param['kw']) : '';
  86. if ($kw) {
  87. $query->where('j.title', 'like', "%{$kw}%");
  88. }
  89. // 信息等级
  90. $level = isset($param['level'])? intval($param['level']) : 0;
  91. if($level>0){
  92. $query->where(['j.level' => $level]);
  93. }
  94. $catid = isset($param['catid']) ? intval($param['catid']) : 0;
  95. if ($catid > 0) {
  96. $query->where(['j.catid|j.pcatid' => $catid]);
  97. }
  98. if ($catids) {
  99. $query->where('catid','in', $catids);
  100. }
  101. $lv = isset($param['lv']) ? intval($param['lv']) : -1;
  102. $lv = $lv>=0? $lv : (isset($param['touzi_level']) ? intval($param['touzi_level']) : -1);
  103. if ($lv >= 0) {
  104. $query->where(['j.touzi_level' => $lv]);
  105. }
  106. $aid = isset($param['aid']) ? intval($param['aid']) : -1;
  107. $aid = $aid>0? $aid : (isset($param['area_id']) ? intval($param['area_id']) : -1);
  108. if ($aid > 0) {
  109. $query->where(function($query) use ($aid){
  110. $query->where(['j.area_id' => $aid])->whereOr(['j.area_id'=> 0]);
  111. });
  112. }
  113. })
  114. ->order('j.update_time desc, j.create_time desc, j.list_order')
  115. ->paginate($pageSize, false, ['query' => $pageParams]);
  116. // echo Db::name('jiameng')->getLastSql();
  117. return $dataList;
  118. }
  119. /**
  120. * 获取等级信息品牌列表
  121. * @param $level 等级:1--今日之星,2-品牌精选,3-品牌推荐,4-品牌严选,5-默认,6-推荐专题品牌
  122. * @param int $pageSize 记录数
  123. * @param string $field 返回字段
  124. * @return $this
  125. * @throws \think\db\exception\DataNotFoundException
  126. * @throws \think\db\exception\ModelNotFoundException
  127. * @throws \think\exception\DbException
  128. */
  129. public static function getListByLevel($level, $pageSize=10,$field=''){
  130. $touziarr = config('params.touziLevels');
  131. $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';
  132. return Db::name('jiameng')
  133. ->alias('j')
  134. ->leftJoin('category c1', 'c1.id=j.catid')
  135. ->field($field)
  136. ->where('j.level',$level)
  137. ->where('j.status',1)
  138. ->order('j.list_order asc, j.id desc')
  139. ->limit($pageSize)
  140. ->select()
  141. ->each(function($item, $k) use ($touziarr){
  142. $touziLevel = isset($item['touzi_level']) ? $item['touzi_level'] : '-1';
  143. $item['touzi_level_name'] = isset($touziarr[$touziLevel]) ? $touziarr[$touziLevel] : '';
  144. return $item;
  145. });
  146. }
  147. /**
  148. * 获取排行榜数据
  149. * @param $where 条件
  150. * @param $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 getTopList($where, $pageSize=10, $field=''){
  158. $touziarr = config('params.touziLevels');
  159. $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';
  160. return Db::name('jiameng')
  161. ->alias('j')
  162. ->leftJoin('category c1', 'c1.id=j.catid')
  163. ->leftJoin('category c2', 'c2.id=j.pcatid')
  164. ->field($field)
  165. ->where('j.status', 1)
  166. ->where($where)
  167. ->order('j.hits desc')
  168. ->limit($pageSize)
  169. ->select()
  170. ->each(function($item, $k) use ($touziarr){
  171. $touziLevel = isset($item['touzi_level']) ? $item['touzi_level'] : '-1';
  172. $item['touzi_level_name'] = isset($touziarr[$touziLevel]) ? $touziarr[$touziLevel] : '';
  173. return $item;
  174. });
  175. }
  176. }