FooterService.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace App\JsonRpc;
  3. use App\Model\FooterCategory;
  4. use App\Model\Website;
  5. use App\Model\FooterContent;
  6. use Hyperf\RpcServer\Annotation\RpcService;
  7. use App\Tools\Result;
  8. #[RpcService(name: "FooterService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  9. class FooterService implements FooterServiceInterface
  10. {
  11. /**
  12. * 获取底部导航
  13. * @param array $data
  14. * @return array
  15. */
  16. public function getFooterCategory(array $data): array
  17. {
  18. $where = [];
  19. if(isset($data['website_name'])){
  20. array_push($where, ['website.website_name','like','%'.$data['website_name'].'%']);
  21. }
  22. if(isset($data['name'])){
  23. array_push($where, ['footer_category.name','like','%'.$data['name'].'%']);
  24. }
  25. // var_dump($where);
  26. if(!empty($where)){
  27. $rep = FooterCategory::where($where)
  28. ->leftJoin("website","website.id","footer_category.website_id")
  29. ->select("footer_category.*","website.website_name","website.id as website_id")
  30. ->limit($data['pageSize'])->offset(($data['page']-1)*$data['pageSize'])->orderBy("updated_at","desc")
  31. ->get();
  32. $count = count($rep);
  33. }else{
  34. $rep = FooterCategory::leftJoin("website","website.id","footer_category.website_id")
  35. ->select("footer_category.*","website.website_name","website.id as website_id")
  36. ->limit($data['pageSize'])->offset(($data['page']-1)*$data['pageSize'])->orderBy("updated_at","desc")
  37. ->get();
  38. $count = FooterCategory::count();
  39. }
  40. $result = [];
  41. $result = [
  42. 'rows'=>$rep,
  43. 'count'=>$count
  44. ];
  45. if($count == 0){
  46. return Result::error("没有查到相关数据!");
  47. }
  48. return Result::success($result);
  49. }
  50. /**
  51. * 添加底部导航
  52. * @param array $data
  53. * @return array
  54. */
  55. public function addFooterCategory(array $data): array
  56. {
  57. if(empty($data)){
  58. $result = Website::select('website_name','id')->get();
  59. }else{
  60. // 底部导航类型 0:内容型;1:列表型;
  61. if(in_array($data['type'], [0, 1])){
  62. // 同一网站下的底部导航名称不能重复
  63. $name = FooterCategory::where('website_id',$data['website_id'])->where('name',$data['name'])->first();
  64. if(!empty($name)){
  65. return Result::error("该底部导航名称已存在!");
  66. }else{
  67. $result = FooterCategory::insertGetId($data);
  68. }
  69. }else{
  70. return Result::error("请输入正确的底部导航类型!");
  71. }
  72. }
  73. if(empty($result)){
  74. return Result::error("添加失败!");
  75. }else{
  76. return Result::success($result);
  77. }
  78. }
  79. /**
  80. * 修改底部导航
  81. * @param array $data
  82. * @return array
  83. */
  84. public function upFooterCategory(array $data): array
  85. {
  86. $footer_category = FooterCategory::where('id', $data['id'])->first();
  87. if (!$footer_category) {
  88. return Result::error("该底部导航不存在!");
  89. }
  90. if(empty($data['website_id'])){
  91. $web = Website::select('website_name','id')->get();
  92. $footer_category = FooterCategory::where('footer_category.id',$data['id'])
  93. ->leftJoin("website","website.id","footer_category.website_id")
  94. ->select("footer_category.*","website.website_name","website.id as website_id")
  95. ->first();
  96. $result = [
  97. 'rows'=>$footer_category,
  98. 'web'=>$web
  99. ];
  100. }else{
  101. $all_categories = FooterCategory::where('website_id',$data['website_id'])->pluck('name')->toArray();
  102. // 检查修改后的数据是否与已有数据重复
  103. if (in_array($data['name'], $all_categories)) {
  104. return Result::error("修改后的底部导航名称已存在!");
  105. }
  106. $result = FooterCategory::where('id', $data['id'])->update($data);
  107. }
  108. if (empty($result)) {
  109. return Result::error("修改失败!");
  110. }else{
  111. return Result::success($result);
  112. }
  113. }
  114. /**
  115. * 删除底部导航
  116. * @param array $data
  117. * @return array
  118. */
  119. public function delFooterCategory(array $data): array
  120. {
  121. $footer_category = FooterCategory::where('id', $data['id'])->first();
  122. if (!$footer_category) {
  123. return Result::error("该底部导航不存在!");
  124. }else{
  125. $result = FooterCategory::where('footer_category.id',$data['id'])
  126. ->leftJoin("footer_content","footer_content.fcat_id","footer_category.id")
  127. ->delete();
  128. }
  129. if(empty($result)){
  130. return Result::error("删除失败!");
  131. }else{
  132. return Result::success($result);
  133. }
  134. }
  135. /**
  136. * 添加底部导航(列表)内容
  137. * @param array $data
  138. * @return array
  139. */
  140. public function addFooterContent(array $data): array
  141. {
  142. // 底部导航类型 0:内容型;1:列表型;
  143. // var_dump($data);
  144. unset($data['type']);
  145. $result = FooterContent::insertGetId($data);
  146. if(empty($result)){
  147. return Result::error("添加失败!");
  148. }else{
  149. return Result::success($result);
  150. }
  151. }
  152. /**
  153. * 获取底部导航(列表)内容
  154. * @param array $data
  155. * @return array
  156. */
  157. public function getFooterContent(array $data): array
  158. {
  159. $where = [];
  160. array_push($where, ['fcat_id',$data['fcat_id']]);
  161. $type = FooterCategory::where('id', $data['fcat_id'])->value('type');
  162. if($type == 1){
  163. if(isset($data['list_title'])){
  164. array_push($where, ['list_title','like','%'.$data['list_title'].'%']);
  165. }
  166. if(isset($data['con_title'])){
  167. array_push($where, ['con_title','like','%'.$data['con_title'].'%']);
  168. }
  169. }
  170. $rep = FooterContent::where($where)
  171. ->limit($data['pageSize'])->offset(($data['page']-1)*$data['pageSize'])
  172. ->orderBy("updated_at","desc")->get();
  173. $count = FooterContent::where($where)->count();
  174. if($count == 0){
  175. return Result::error("没有查到相关数据!");
  176. }else{
  177. $result = [
  178. 'rows'=>$rep,
  179. 'count'=>$count
  180. ];
  181. return Result::success($result);
  182. }
  183. }
  184. /**
  185. * 获取某个底部导航(列表)内容
  186. * @param array $data
  187. * @return array
  188. */
  189. public function getOneFooterContent(array $data): array
  190. {
  191. $result = FooterContent::where('footer_content.id', $data['id'])
  192. ->leftJoin('footer_category','footer_category.id','fcat_id')
  193. ->select('footer_content.*','footer_category.type')
  194. ->first();
  195. if(empty($result)){
  196. return Result::error("请输入正确的底部导航内容id!");
  197. }else{
  198. return Result::success($result);
  199. }
  200. }
  201. /**
  202. * 编辑底部导航(列表)内容
  203. * @param array $data
  204. * @return array
  205. */
  206. public function upFooterContent(array $data): array
  207. {
  208. unset($data['type']);
  209. $result = FooterContent::where('id', $data['id'])->update($data);
  210. if(empty($result)){
  211. return Result::error("修改失败!");
  212. }else{
  213. return Result::success($result);
  214. }
  215. }
  216. /**
  217. * 删除底部导航(列表)内容
  218. * @param array $data
  219. * @return array
  220. */
  221. public function delFooterContent(array $data): array
  222. {
  223. $result = FooterContent::where('id', $data['id'])->delete($data);
  224. if(empty($result)){
  225. return Result::error("删除失败!");
  226. }else{
  227. return Result::success($result);
  228. }
  229. }
  230. }