FooterService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. $webid = Website::where('id',$data['website_id'])->first();
  112. if(empty($webid)){
  113. return Result::error("该网站不存在!");
  114. }
  115. $result = FooterCategory::where('id', $data['id'])->update($data);
  116. }
  117. if (empty($result)) {
  118. return Result::error("修改失败!");
  119. }else{
  120. return Result::success($result);
  121. }
  122. }
  123. /**
  124. * 删除底部导航
  125. * @param array $data
  126. * @return array
  127. */
  128. public function delFooterCategory(array $data): array
  129. {
  130. Db::beginTransaction();
  131. try{
  132. $footer_category = FooterCategory::where('id', $data['id'])->first();
  133. if (!$footer_category) {
  134. return Result::error("该底部导航不存在!");
  135. }else{
  136. $result['footer_category'] = FooterCategory::where('id', $data['id'])->delete();
  137. $result['footer_content'] = FooterContent::where('fcat_id', $data['id'])->delete();
  138. // $result = FooterCategory::where('footer_category.id',$data['id'])
  139. // ->leftJoin("footer_content","footer_content.fcat_id","footer_category.id")
  140. // ->delete();
  141. }
  142. Db::commit();
  143. } catch(\Throwable $ex){
  144. Db::rollBack();
  145. var_dump($ex->getMessage());
  146. return Result::error("修改失败",0);
  147. }
  148. return Result::success($result);
  149. }
  150. /**
  151. * 添加底部导航(列表)内容
  152. * @param array $data
  153. * @return array
  154. */
  155. public function addFooterContent(array $data): array
  156. {
  157. // 底部导航类型 0:内容型;1:列表型;
  158. // var_dump($data);
  159. $cat = FooterCategory::where('id', $data['fcat_id'])->first();
  160. if (!$cat) {
  161. return Result::error("该底部导航不存在!");
  162. }
  163. if($cat['type'] != $data['type']){
  164. return Result::error("请输入正确的底部导航类型!");
  165. }
  166. if($cat['type'] == 0){
  167. $content = FooterContent::where('fcat_id', $data['fcat_id'])->first();
  168. if(!empty($content)){
  169. return Result::error("该底部导航已添加内容!");
  170. }
  171. }
  172. unset($data['type']);
  173. $result = FooterContent::insertGetId($data);
  174. if(empty($result)){
  175. return Result::error("添加失败!");
  176. }else{
  177. return Result::success($result);
  178. }
  179. }
  180. /**
  181. * 获取底部导航(列表)内容
  182. * @param array $data
  183. * @return array
  184. */
  185. public function getFooterContent(array $data): array
  186. {
  187. $where = [];
  188. array_push($where, ['fcat_id',$data['fcat_id']]);
  189. $type = FooterCategory::where('id', $data['fcat_id'])->value('type');
  190. if($type == 1){
  191. if(isset($data['list_title'])){
  192. array_push($where, ['list_title','like','%'.$data['list_title'].'%']);
  193. }
  194. if(isset($data['con_title'])){
  195. array_push($where, ['con_title','like','%'.$data['con_title'].'%']);
  196. }
  197. }
  198. $rep = FooterContent::where($where)
  199. ->limit($data['pageSize'])
  200. ->offset(($data['page']-1)*$data['pageSize'])
  201. ->orderBy("updated_at","desc")
  202. ->get();
  203. $count = FooterContent::where($where)->count();
  204. if($count == 0){
  205. return Result::error("没有查到相关数据!");
  206. }else{
  207. $result = [
  208. 'rows'=>$rep,
  209. 'count'=>$count
  210. ];
  211. return Result::success($result);
  212. }
  213. }
  214. /**
  215. * 获取某个底部导航(列表)内容
  216. * @param array $data
  217. * @return array
  218. */
  219. public function getOneFooterContent(array $data): array
  220. {
  221. $result = FooterContent::where('footer_content.id', $data['id'])
  222. ->leftJoin('footer_category','footer_category.id','fcat_id')
  223. ->select('footer_content.*','footer_category.type')
  224. ->first();
  225. if(empty($result)){
  226. return Result::error("请输入正确的底部导航内容id!");
  227. }else{
  228. return Result::success($result);
  229. }
  230. }
  231. /**
  232. * 编辑底部导航(列表)内容
  233. * @param array $data
  234. * @return array
  235. */
  236. public function upFooterContent(array $data): array
  237. {
  238. $content = FooterContent::where('footer_content.id', $data['id'])
  239. ->leftJoin('footer_category','footer_category.id','fcat_id')
  240. ->select('footer_content.*','footer_category.type','footer_category.id as fcat_id')
  241. ->first();
  242. if(!$content){
  243. return Result::error("该底部导航内容不存在!");
  244. }
  245. if($content['type'] != $data['type']){
  246. return Result::error("请输入正确的底部导航类型!");
  247. }
  248. if($content['type'] == 1){
  249. if(!isset($data['list_title']) || empty($data['list_title'])){
  250. return Result::error("请输入底部导航内容标题!");
  251. }
  252. }
  253. unset($data['type']);
  254. $result = FooterContent::where('id', $data['id'])->update($data);
  255. if(empty($result)){
  256. return Result::error("修改失败!");
  257. }else{
  258. return Result::success($result);
  259. }
  260. }
  261. /**
  262. * 删除底部导航(列表)内容
  263. * @param array $data
  264. * @return array
  265. */
  266. public function delFooterContent(array $data): array
  267. {
  268. $result = FooterContent::where('id', $data['id'])->delete($data);
  269. if(empty($result)){
  270. return Result::error("删除失败!");
  271. }else{
  272. return Result::success($result);
  273. }
  274. }
  275. }