FooterService.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 = [];
  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. $web = Website::select('website_name','id')->get();
  131. $footer_category = FooterCategory::where('footer_category.id',$data['id'])
  132. ->leftJoin("website","website.id","footer_category.website_id")
  133. ->select("footer_category.*","website.website_name","website.id as website_id")
  134. ->first();
  135. $child = FooterCategory::where('pid',$footer_category['id'])->first();
  136. if(!empty($child)){
  137. $footer_category = array_merge([$footer_category->toArray()], [$child->toArray()]);
  138. // $footer_category['child'] = [];
  139. }
  140. $result = [
  141. 'rows'=>$footer_category,
  142. 'web'=>$web
  143. ];
  144. }else{
  145. $all_categories = FooterCategory::where('website_id',$data['website_id'])->where('id','!=',$data['id'])->pluck('name')->toArray();
  146. // 检查修改后的数据是否与已有数据重复
  147. if (in_array($data['name'], $all_categories)) {
  148. return Result::error("修改后的底部导航名称已存在!");
  149. }
  150. $webid = Website::where('id',$data['website_id'])->first();
  151. if(empty($webid)){
  152. return Result::error("该网站不存在!");
  153. }
  154. $pinyin = new Pinyin();
  155. $child_data = [];
  156. if(isset($data['child_name']) &&!empty($data['child_name'])){
  157. $child_data = [
  158. 'name' => $data['child_name'],
  159. 'name_pinyin' => $pinyin->permalink($data['child_name'], ''),
  160. ];
  161. unset($data['child_name']);
  162. }
  163. Db::beginTransaction();
  164. try{
  165. $result = [];
  166. $data['name_pinyin'] = $pinyin->permalink($data['name'], '');
  167. $result['rows'] = FooterCategory::where('id', $data['id'])->update($data);
  168. $child = FooterCategory::where('pid',$data['id'])->first()->toArray();
  169. if(!empty($child) && !empty($child_data)){
  170. $result['child'] = FooterCategory::where('id', $child['id'])->update($child_data);
  171. }
  172. Db::commit();
  173. } catch(\Throwable $ex){
  174. Db::rollBack();
  175. var_dump($ex->getMessage());
  176. return Result::error("修改失败!!",0);
  177. }
  178. }
  179. if (empty($result)) {
  180. return Result::error("修改失败!");
  181. }else{
  182. return Result::success($result);
  183. }
  184. }
  185. /**
  186. * 删除底部导航
  187. * @param array $data
  188. * @return array
  189. */
  190. public function delFooterCategory(array $data): array
  191. {
  192. Db::beginTransaction();
  193. try{
  194. $footer_category = FooterCategory::where('id', $data['id'])->first();
  195. if (!$footer_category) {
  196. Db::rollBack();
  197. return Result::error("该底部导航不存在!");
  198. }else{
  199. $result['footer_category'] = FooterCategory::where('id', $data['id'])->delete();
  200. $result['footer_content'] = FooterContent::where('fcat_id', $data['id'])->delete();
  201. $child = FooterCategory::where('pid', $data['id'])->first();
  202. if(!empty($child)){
  203. $result['child'] = FooterCategory::where('pid', $data['id'])->delete();
  204. $result['child_content'] = FooterContent::where('fcat_id', $child['id'])->delete();
  205. }
  206. Db::commit();
  207. }
  208. } catch(\Throwable $ex){
  209. Db::rollBack();
  210. var_dump($ex->getMessage());
  211. return Result::error("删除失败",0);
  212. }
  213. return Result::success($result);
  214. }
  215. /**
  216. * 添加底部导航(列表)内容
  217. * @param array $data
  218. * @return array
  219. */
  220. public function addFooterContent(array $data): array
  221. {
  222. // 底部导航类型 0:内容型;1:列表型;
  223. // var_dump($data);
  224. $cat = FooterCategory::where('id', $data['fcat_id'])->first();
  225. // return Result::success($cat);
  226. if (!$cat) {
  227. return Result::error("该底部导航不存在!");
  228. }
  229. if($cat['type'] != $data['type_id']){
  230. return Result::error("请输入正确的底部导航类型!");
  231. }
  232. // 内容型底部导航只能有一条内容
  233. if($cat['type'] == 0){
  234. $content = FooterContent::where('fcat_id', $data['fcat_id'])->first();
  235. if(!empty($content)){
  236. return Result::error("该底部导航已添加内容!");
  237. }
  238. }else{
  239. // 列表型底部导航\列表标头型底部导航列表标题不能重复
  240. if(!isset($data['list_title']) || empty($data['list_title'])){
  241. return Result::error("请输入底部导航列表标题!");
  242. }
  243. $content = FooterContent::where('fcat_id', $data['fcat_id'])->where('list_title',$data['list_title'])->first();
  244. if(!empty($content)){
  245. return Result::error("该列表标题已存在!");
  246. }
  247. }
  248. $result = FooterContent::insertGetId($data);
  249. if(empty($result)){
  250. return Result::error("添加失败!");
  251. }else{
  252. return Result::success($result);
  253. }
  254. }
  255. /**
  256. * 获取底部导航(列表)内容
  257. * @param array $data
  258. * @return array
  259. */
  260. public function getFooterContent(array $data): array
  261. {
  262. $where = [];
  263. $fcat_id = [];
  264. $footer_category = FooterCategory::where('id', $data['fcat_id'])->first();
  265. if(empty($footer_category)){
  266. return Result::error("该底部导航不存在!");
  267. }
  268. $footer_category = $footer_category->toArray();
  269. $fcat_id = $footer_category['id'];
  270. if($footer_category['type'] == 1){
  271. if(isset($data['list_title'])){
  272. array_push($where, ['list_title','like','%'.$data['list_title'].'%']);
  273. }
  274. if(isset($data['con_title'])){
  275. array_push($where, ['con_title','like','%'.$data['con_title'].'%']);
  276. }
  277. $child = FooterCategory::where('pid', $data['fcat_id'])->first();
  278. if($child){
  279. $fcat_id = [$footer_category['id'], $child->id];
  280. }
  281. }
  282. $count = FooterContent::whereIn('fcat_id', (array)$fcat_id)->where($where)->count();
  283. $rep = FooterContent::whereIn('fcat_id', (array)$fcat_id)->where($where)
  284. ->leftJoin('footer_category', 'footer_category.id', '=', 'footer_content.fcat_id')
  285. ->select('footer_content.*', 'footer_category.type', 'footer_category.name as fcat_name')
  286. ->limit($data['pageSize'])
  287. ->offset(($data['page'] - 1) * $data['pageSize'])
  288. ->orderBy("updated_at", "desc")
  289. ->get();
  290. if(empty($rep)){
  291. return Result::error("没有查到相关数据!");
  292. }else{
  293. $result = [
  294. 'rows' => $rep,
  295. 'count' => $count
  296. ];
  297. return Result::success($result);
  298. }
  299. }
  300. /**
  301. * 获取某个底部导航(列表)内容
  302. * @param array $data
  303. * @return array
  304. */
  305. public function getOneFooterContent(array $data): array
  306. {
  307. $result = FooterContent::where('footer_content.id', $data['id'])
  308. ->leftJoin('footer_category','footer_category.id','fcat_id')
  309. ->select('footer_content.*','footer_category.type','footer_category.pid','footer_category.name as fcat_name')
  310. ->first();
  311. if(empty($result)){
  312. return Result::error("请输入正确的底部导航内容id!");
  313. }
  314. return Result::success($result);
  315. }
  316. /**
  317. * 编辑底部导航(列表)内容
  318. * @param array $data
  319. * @return array
  320. */
  321. public function upFooterContent(array $data): array
  322. {
  323. $content = FooterContent::where('footer_content.id', $data['id'])
  324. ->leftJoin('footer_category','footer_category.id','fcat_id')
  325. ->select('footer_content.*','footer_category.type','footer_category.id as fcat_id')
  326. ->first();
  327. if(!$content){
  328. return Result::error("该底部导航内容不存在!");
  329. }
  330. if($content['type'] != $data['type_id']){
  331. return Result::error("请输入正确的底部导航类型!");
  332. }
  333. if($content['type_id'] == 1){
  334. if(!isset($data['list_title']) || empty($data['list_title'])){
  335. return Result::error("请输入底部导航列表标题!");
  336. }
  337. $list_title = FooterContent::where('fcat_id', $data['fcat_id'])->where('list_title',$data['list_title'])->first();
  338. if(!empty($list_title)){
  339. return Result::error("该列表标题已存在!");
  340. }
  341. }
  342. unset($data['type_id']);
  343. $result = FooterContent::where('id', $data['id'])->update($data);
  344. if(empty($result)){
  345. return Result::error("修改失败!");
  346. }else{
  347. return Result::success($result);
  348. }
  349. }
  350. /**
  351. * 删除底部导航(列表)内容
  352. * @param array $data
  353. * @return array
  354. */
  355. public function delFooterContent(array $data): array
  356. {
  357. $result = FooterContent::where('id', $data['id'])->delete($data);
  358. if(empty($result)){
  359. return Result::error("删除失败!");
  360. }else{
  361. return Result::success($result);
  362. }
  363. }
  364. }