FooterService.php 11 KB

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