FooterService.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. use Overtrue\Pinyin\Pinyin;
  12. use function Hyperf\Support\retry;
  13. use function PHPSTORM_META\type;
  14. use function PHPUnit\Framework\assertIsNotInt;
  15. use function PHPUnit\Framework\isNull;
  16. #[RpcService(name: "FooterService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  17. class FooterService implements FooterServiceInterface
  18. {
  19. /**
  20. * 获取底部导航列表
  21. * @param array $data
  22. * @return array
  23. */
  24. public function getFooterCategory(array $data): array
  25. {
  26. $where = ['pid' => 0];
  27. if(isset($data['website_name'])){
  28. array_push($where, ['website.website_name','like','%'.$data['website_name'].'%']);
  29. }
  30. if(isset($data['name'])){
  31. array_push($where, ['footer_category.name','like','%'.$data['name'].'%']);
  32. }
  33. $query = FooterCategory::query();
  34. if (!empty($where)) {
  35. $query->where($where);
  36. }
  37. $count = $query->count();
  38. $rep = $query->leftJoin("website", "website.id", "footer_category.website_id")
  39. ->select("footer_category.*", "website.website_name", "website.id as website_id")
  40. ->offset(($data['page'] - 1) * $data['pageSize'])
  41. ->limit($data['pageSize'])
  42. ->orderByDesc("updated_at")
  43. ->get();
  44. // var_dump($where);
  45. $result = [];
  46. $result = [
  47. 'rows'=>$rep,
  48. 'count'=>$count
  49. ];
  50. if(empty($result)){
  51. return Result::error("没有查到相关数据!");
  52. }
  53. return Result::success($result);
  54. }
  55. /**
  56. * 添加底部导航
  57. * @param array $data
  58. * @return array
  59. */
  60. public function addFooterCategory(array $data): array
  61. {
  62. if(empty($data)){
  63. $result = Website::select('website_name','id')->get();
  64. }else{
  65. // 底部导航类型 0:内容型;1:列表型;
  66. $webid = Website::select('website_name','id')->where('id',$data['website_id'])->first();
  67. if(empty($webid)){
  68. return Result::error("该网站不存在!");
  69. }
  70. $isChild = 0;
  71. if(isset($data['is_child']) && !empty($data['is_child']) && $data['is_child'] == 1){
  72. $child = [
  73. 'name' => $data['child_name'],
  74. 'website_id' => $data['website_id'],
  75. 'type' => 0,
  76. ];
  77. $isChild = $data['is_child'];
  78. unset($data['child_name']);
  79. unset($data['is_child']);
  80. }
  81. // return Result::success($data);
  82. Db::beginTransaction();
  83. try{
  84. // 同一网站下的底部导航名称不能重复
  85. $name = FooterCategory::where('website_id',$data['website_id'])->where('name',$data['name'])->first();
  86. if(!empty($name)){
  87. Db::rollBack();
  88. return Result::error("该底部导航名称已存在!");
  89. }
  90. $pinyin = new Pinyin();
  91. $data['name_pinyin'] = $pinyin->permalink($data['name'], '') ;
  92. $result = [];
  93. $result['parent'] = FooterCategory::insertGetId($data);
  94. if($isChild == 1){
  95. $child['pid'] = $result['parent'];
  96. $child['name_pinyin'] = $pinyin->permalink($child['name'], '');
  97. // 暂时用不到 若是一个栏目对应多个子级栏目 则需要判断子级栏目名称是否重复
  98. // $child_name = FooterCategory::where('website_id',$data['website_id'])->where('pid',$child['pid'])->first();
  99. // if(!empty($child_name)){
  100. // Db::rollBack();
  101. // return Result::error("该子级栏目名称已存在!");
  102. // }
  103. $result['child'] = FooterCategory::insertGetId($child);
  104. if(empty($result['child'])){
  105. Db::rollBack();
  106. return Result::error("子级栏目添加失败!");
  107. }
  108. }
  109. Db::commit();
  110. } catch(\Throwable $ex){
  111. Db::rollBack();
  112. $errorMessage = $ex->getMessage();
  113. return Result::error("添加失败!",$errorMessage);
  114. }
  115. }
  116. return Result::success($result);
  117. }
  118. /**
  119. * 修改底部导航
  120. * @param array $data
  121. * @return array
  122. */
  123. public function upFooterCategory(array $data): array
  124. {
  125. $footer_category = FooterCategory::where('id', $data['id'])->first();
  126. if(empty($footer_category)) {
  127. return Result::error("该底部导航不存在!");
  128. }
  129. if(empty($data['website_id'])){
  130. $footer_category = FooterCategory::where('footer_category.id',$data['id'])
  131. ->leftJoin("website","website.id","footer_category.website_id")
  132. ->select("footer_category.*","website.website_name","website.id as website_id")
  133. ->first();
  134. $child = FooterCategory::where('pid',$footer_category['id'])->first();
  135. if(!empty($child)){
  136. $footer_category['child_name'] = $child['name'];
  137. $footer_category['is_child'] = 1;
  138. }
  139. $result = [
  140. 'rows'=>$footer_category,
  141. ];
  142. }else{
  143. $all_categories = FooterCategory::where('website_id',$data['website_id'])->where('id','!=',$data['id'])->pluck('name')->toArray();
  144. // 检查修改后的数据是否与已有数据重复
  145. if (in_array($data['name'], $all_categories)) {
  146. return Result::error("修改后的底部导航名称已存在!");
  147. }
  148. $webid = Website::where('id',$data['website_id'])->first();
  149. if(empty($webid)){
  150. return Result::error("该网站不存在!");
  151. }
  152. $pinyin = new Pinyin();
  153. $child_data = [];
  154. $result = [];
  155. Db::beginTransaction();
  156. try{
  157. if(isset($data['is_child']) && $data['is_child']!=''){
  158. $child = FooterCategory::where('pid',$data['id'])->first();
  159. if($data['is_child'] == 1){
  160. if(empty($data['child_name'])){
  161. Db::rollBack();
  162. return Result::error("请输入子级栏目名称!");
  163. var_dump($data['child_name']);
  164. }
  165. if(empty($child)){
  166. $child_data = [
  167. 'name' => $data['child_name'],
  168. 'name_pinyin' => $pinyin->permalink($data['child_name'], ''),
  169. 'website_id' => $data['website_id'],
  170. 'type' => 0,
  171. 'pid' => $data['id'],
  172. ];
  173. $result['addchild'] = FooterCategory::insertGetId($child_data);
  174. if(empty($result['addchild'])){
  175. Db::rollBack();
  176. return Result::error("子级栏目添加失败!");
  177. }
  178. }else{
  179. $child_data = [
  180. 'name' => $data['child_name'],
  181. 'name_pinyin' => $pinyin->permalink($data['child_name'], ''),
  182. ];
  183. $result['upchild'] = FooterCategory::where('pid',$data['id'])->update($child_data);
  184. if(empty($result['upchild'])){
  185. Db::rollBack();
  186. return Result::error("子级栏目修改失败!");
  187. }
  188. }
  189. }else{
  190. if(!empty($child)){
  191. $result['del_child_content'] = FooterContent::where('fcat_id',$child['id'])->delete();
  192. $result['delchild'] = FooterCategory::where('pid',$data['id'])->delete();
  193. if(empty($result['delchild'])) {
  194. Db::rollBack();
  195. return Result::error("子级栏目删除失败!");
  196. }
  197. }
  198. }
  199. unset($data['child_name']);
  200. unset($data['is_child']);
  201. $data['name_pinyin'] = $pinyin->permalink($data['name'], '');
  202. $result['rows'] = FooterCategory::where('id', $data['id'])->update($data);
  203. if(empty($result['rows'])){
  204. Db::rollBack();
  205. return Result::error("栏目修改失败!");
  206. }
  207. Db::commit();
  208. }else{
  209. return Result::error("请选择是否添加子级栏目!");
  210. }
  211. } catch(\Throwable $ex){
  212. Db::rollBack();
  213. var_dump($ex->getMessage());
  214. return Result::error("修改失败!",0);
  215. }
  216. }
  217. if (empty($result)) {
  218. return Result::error("修改失败!");
  219. }else{
  220. return Result::success($result);
  221. }
  222. }
  223. /**
  224. * 删除底部导航
  225. * @param array $data
  226. * @return array
  227. */
  228. public function delFooterCategory(array $data): array
  229. {
  230. Db::beginTransaction();
  231. try{
  232. $footer_category = FooterCategory::where('id', $data['id'])->first();
  233. if (!$footer_category) {
  234. Db::rollBack();
  235. return Result::error("该底部导航不存在!");
  236. }else{
  237. $result['footer_category'] = FooterCategory::where('id', $data['id'])->delete();
  238. $result['footer_content'] = FooterContent::where('fcat_id', $data['id'])->delete();
  239. $child = FooterCategory::where('pid', $data['id'])->first();
  240. if(!empty($child)){
  241. $result['child'] = FooterCategory::where('pid', $data['id'])->delete();
  242. $result['child_content'] = FooterContent::where('fcat_id', $child['id'])->delete();
  243. }
  244. Db::commit();
  245. }
  246. } catch(\Throwable $ex){
  247. Db::rollBack();
  248. var_dump($ex->getMessage());
  249. return Result::error("删除失败",0);
  250. }
  251. return Result::success($result);
  252. }
  253. /**
  254. * 添加底部导航(列表)内容
  255. * @param array $data
  256. * @return array
  257. */
  258. public function addFooterContent(array $data): array
  259. {
  260. // 底部导航类型 0:内容型;1:列表型;
  261. // var_dump($data);
  262. $cat = FooterCategory::where('id', $data['fcat_id'])->first();
  263. // return Result::success($cat);
  264. if (!$cat) {
  265. return Result::error("该底部导航不存在!");
  266. }
  267. if($cat['type'] != $data['type_id']){
  268. return Result::error("请输入正确的底部导航类型!");
  269. }
  270. // 内容型底部导航只能有一条内容
  271. if($cat['type'] == 0){
  272. $content = FooterContent::where('fcat_id', $data['fcat_id'])->first();
  273. if(!empty($content)){
  274. return Result::error("该底部导航已添加内容!");
  275. }
  276. }else{
  277. // 列表型底部导航\列表标头型底部导航列表标题不能重复
  278. if(!isset($data['list_title']) || empty($data['list_title'])){
  279. return Result::error("请输入底部导航列表标题!");
  280. }
  281. $content = FooterContent::where('fcat_id', $data['fcat_id'])->where('list_title',$data['list_title'])->first();
  282. if(!empty($content)){
  283. return Result::error("该列表标题已存在!");
  284. }
  285. }
  286. $result = FooterContent::insertGetId($data);
  287. if(empty($result)){
  288. return Result::error("添加失败!");
  289. }else{
  290. return Result::success($result);
  291. }
  292. }
  293. /**
  294. * 获取底部导航(列表)内容
  295. * @param array $data
  296. * @return array
  297. */
  298. public function getFooterContent(array $data): array
  299. {
  300. $where = [];
  301. $fcat_id = [];
  302. $footer_category = FooterCategory::where('id', $data['fcat_id'])->first();
  303. if(empty($footer_category)){
  304. return Result::error("该底部导航不存在!");
  305. }
  306. $footer_category = $footer_category->toArray();
  307. $fcat_id = $footer_category['id'];
  308. if($footer_category['type'] == 1){
  309. if(isset($data['list_title'])){
  310. array_push($where, ['list_title','like','%'.$data['list_title'].'%']);
  311. }
  312. if(isset($data['con_title'])){
  313. array_push($where, ['con_title','like','%'.$data['con_title'].'%']);
  314. }
  315. $child = FooterCategory::where('pid', $data['fcat_id'])->first();
  316. if($child){
  317. $fcat_id = [$footer_category['id'], $child->id];
  318. }
  319. }
  320. $count = FooterContent::whereIn('fcat_id', (array)$fcat_id)->where($where)->count();
  321. $rep = FooterContent::whereIn('fcat_id', (array)$fcat_id)->where($where)
  322. ->leftJoin('footer_category', 'footer_category.id', '=', 'footer_content.fcat_id')
  323. ->select('footer_content.*', 'footer_category.type', 'footer_category.name as fcat_name')
  324. ->limit($data['pageSize'])
  325. ->offset(($data['page'] - 1) * $data['pageSize'])
  326. ->orderBy("updated_at", "desc")
  327. ->get();
  328. if(empty($rep)){
  329. return Result::error("没有查到相关数据!");
  330. }else{
  331. $result = [
  332. 'rows' => $rep,
  333. 'count' => $count
  334. ];
  335. return Result::success($result);
  336. }
  337. }
  338. /**
  339. * 获取某个底部导航(列表)内容
  340. * @param array $data
  341. * @return array
  342. */
  343. public function getOneFooterContent(array $data): array
  344. {
  345. $result = FooterContent::where('footer_content.id', $data['id'])
  346. ->leftJoin('footer_category','footer_category.id','fcat_id')
  347. ->select('footer_content.*','footer_category.type','footer_category.pid','footer_category.name as fcat_name')
  348. ->first();
  349. if(empty($result)){
  350. return Result::error("请输入正确的底部导航内容id!");
  351. }
  352. return Result::success($result);
  353. }
  354. /**
  355. * 编辑底部导航(列表)内容
  356. * @param array $data
  357. * @return array
  358. */
  359. public function upFooterContent(array $data): array
  360. {
  361. $content = FooterContent::where('footer_content.id', $data['id'])
  362. ->leftJoin('footer_category','footer_category.id','fcat_id')
  363. ->select('footer_content.*','footer_category.type','footer_category.id as fcat_id')
  364. ->first();
  365. if(!$content){
  366. return Result::error("该底部导航内容不存在!");
  367. }
  368. if($content['type'] != $data['type_id']){
  369. return Result::error("请输入正确的底部导航类型!");
  370. }
  371. if($content['type_id'] == 1){
  372. if(!isset($data['list_title']) || empty($data['list_title'])){
  373. return Result::error("请输入底部导航列表标题!");
  374. }
  375. $list_title = FooterContent::where('fcat_id', $data['fcat_id'])->where('list_title',$data['list_title'])->first();
  376. if(!empty($list_title)){
  377. return Result::error("该列表标题已存在!");
  378. }
  379. }
  380. unset($data['type_id']);
  381. $result = FooterContent::where('id', $data['id'])->update($data);
  382. if(empty($result)){
  383. return Result::error("修改失败!");
  384. }else{
  385. return Result::success($result);
  386. }
  387. }
  388. /**
  389. * 删除底部导航(列表)内容
  390. * @param array $data
  391. * @return array
  392. */
  393. public function delFooterContent(array $data): array
  394. {
  395. $result = FooterContent::where('id', $data['id'])->delete($data);
  396. if(empty($result)){
  397. return Result::error("删除失败!");
  398. }else{
  399. return Result::success($result);
  400. }
  401. }
  402. }