FooterService.php 10 KB

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