FooterService.php 8.6 KB

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