FooterService.php 8.4 KB

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