FooterService.php 11 KB

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