FooterService.php 9.7 KB

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