AdService.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace App\JsonRpc;
  3. use App\Model\Ad;
  4. use App\Model\AdPlace;
  5. use Hyperf\RpcServer\Annotation\RpcService;
  6. use App\Tools\Result;
  7. #[RpcService(name: "AdService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  8. class AdService implements AdServiceInterface
  9. {
  10. /**
  11. * @param array $data
  12. * @return string
  13. */
  14. public function createAd(array $data): array
  15. {
  16. $insertData = [
  17. 'name'=>$data['name']??'',
  18. 'pid'=>$data['pid']??0,
  19. 'areaid'=>$data['areaid']??0,
  20. 'amount'=>$data['amount']??0,//浏览数量
  21. 'introduce'=>$data['introduce']??'',
  22. 'hits'=>$data['hits']??0, //点击数量
  23. 'fromtime'=>$data['f_t_date'][0]??0,
  24. 'totime'=>$data['f_t_date'][1]??0,
  25. 'text_name'=>$data['text_name']??'',
  26. 'text_url'=>$data['text_url']??'',
  27. 'text_title'=>$data['text_title']??'',
  28. 'image_src'=>$data['image_src']??'',
  29. 'image_url'=>$data['image_url']??'',
  30. 'image_alt'=>$data['image_alt']??'',
  31. 'video_src'=>$data['video_src']??'',//视频
  32. 'video_url'=>$data['video_url']??'',
  33. 'video_auto'=>$data['video_auto'] ??0,
  34. 'video_loop'=>$data['video_loop'] ??0,
  35. 'status'=>$data['status']??2,
  36. 'remark'=>$data['remark']??'',
  37. ];
  38. $result = Ad::query()->insertGetId($insertData);
  39. if($result){
  40. return Result::success();
  41. }else{
  42. return Result::error("创建广告失败",500);
  43. }
  44. }
  45. /**
  46. * @param int $id
  47. * @return array
  48. */
  49. public function getAdInfo(int $id): array
  50. {
  51. $adInfo = Ad::query()->find($id);
  52. if (empty($adInfo)) {
  53. return Result::error("没有数据",0);
  54. }
  55. return Result::success($adInfo->toArray());
  56. }
  57. /**
  58. * @param array $data
  59. * @return array
  60. */
  61. public function getAdList(array $data): array
  62. {
  63. if(isset($data['pid'])){
  64. $where[] = ['ad.pid','=',$data['pid']];
  65. }
  66. $rep = Ad::where($where)
  67. ->leftJoin("ad_place","ad.pid","ad_place.id")
  68. ->leftJoin("website","website.id","ad_place.website_id")
  69. ->select("ad.*","ad_place.name as place_name","ad_place.typeid","website.website_name")
  70. ->limit($data['pageSize'])->orderBy("ad.id","desc")->offset(($data['page']-1)*$data['pageSize'])->get();
  71. $count = Ad::where($where)
  72. ->leftJoin("ad_place","ad.pid","ad_place.id")
  73. ->leftJoin("website","website.id","ad_place.website_id")
  74. ->count();
  75. $data = [
  76. 'rows'=>$rep->toArray(),
  77. 'count'=>$count
  78. ];
  79. if(empty($rep->toArray())){
  80. return Result::error("没有数据");
  81. }
  82. return Result::success($data);
  83. }
  84. public function updateAd(array $data): array
  85. {
  86. $where = [
  87. 'id'=>$data['id']
  88. ];
  89. $insertData = [
  90. 'name'=>$data['name']??'',
  91. 'pid'=>$data['pid']??0,
  92. 'areaid'=>$data['areaid']??0,
  93. 'amount'=>$data['amount']??0,//浏览数量
  94. 'introduce'=>$data['introduce']??'',
  95. 'hits'=>$data['hits']??0, //点击数量
  96. 'fromtime'=>$data['f_t_date'][0]??0,
  97. 'totime'=>$data['f_t_date'][1]??0,
  98. 'text_name'=>$data['text_name']??'',
  99. 'text_url'=>$data['text_url']??'',
  100. 'text_title'=>$data['text_title']??'',
  101. 'image_src'=>$data['image_src']??'',
  102. 'image_url'=>$data['image_url']??'',
  103. 'image_alt'=>$data['image_alt']??'',
  104. 'video_src'=>$data['video_src']??'',//视频
  105. 'video_url'=>$data['video_url']??'',
  106. 'video_auto'=>isset($data['video_auto']) && empty($data['video_auto'])?0:$data['video_auto'],
  107. 'video_loop'=>isset($data['video_loop']) && empty($data['video_loop'])?0:$data['video_loop'],
  108. 'status'=>$data['status']??2,
  109. 'remark'=>$data['remark']??'',
  110. ];
  111. $result = Ad::where($where)->update($insertData);
  112. if($result){
  113. return Result::success($result);
  114. }else{
  115. return Result::error("更新失败");
  116. }
  117. }
  118. public function delAd(array $data): array
  119. {
  120. $result = Ad::where($data)->delete();
  121. if(!$result){
  122. return Result::error("删除失败");
  123. }
  124. return Result::success($result);
  125. }
  126. /**
  127. * 广告位列表
  128. * @param array $data
  129. * @return array
  130. */
  131. public function getAdPlaceList(array $data): array
  132. {
  133. $where = [];
  134. if(isset($data['name']) && $data['name']){
  135. array_push($where,['ad_place.name','like','%'.$data['name'].'%']);
  136. }
  137. if(isset($data['website_id']) && $data['website_id']){
  138. array_push($where,['ad_place.website_id','=',$data['website_id']]);
  139. }
  140. $rep = AdPlace::where($where)
  141. ->leftJoin("ad_size","ad_place.ad_size_id","ad_size.id")
  142. ->leftJoin("website","website.id","ad_place.website_id")
  143. ->limit($data['pageSize'])
  144. ->orderBy("id","desc")
  145. ->offset(($data['page']-1)*$data['pageSize'])
  146. ->select("ad_place.*","ad_size.width","ad_size.height","website.website_name")
  147. ->get();
  148. $count = AdPlace::where($where)->count();
  149. $reponse = [
  150. 'rows'=>$rep->toArray(),
  151. 'count'=>$count
  152. ];
  153. if(empty($rep->toArray())){
  154. return Result::error("没有数据");
  155. }
  156. return Result::success($reponse);
  157. }
  158. public function createAdPlace(array $data): array
  159. {
  160. unset($data['size']);
  161. $result = AdPlace::query()->insertGetId($data);
  162. if($result){
  163. return Result::success();
  164. }else{
  165. return Result::error("创建广告位失败",500);
  166. }
  167. }
  168. /**
  169. * 'name' => 'required', //广告位名称
  170. * 'website_id' => 'required',//关联的网站id
  171. * 'price' => 'required',//价格
  172. * 'thumb'=>'required', //广告示意图
  173. * 'typeid'=>'required', //广告类型
  174. * 'ad_size_id'=>'required', //广告位大小
  175. * 'status'=>'required', //状态
  176. * @param array $data
  177. * @return array
  178. */
  179. public function updateAdPlace(array $data): array
  180. {
  181. $where = [
  182. 'id'=>$data['id']
  183. ];
  184. $Insdata = [
  185. 'website_id'=>$data['website_id']??'',
  186. 'typeid'=>$data['typeid']??1,
  187. 'status'=>$data['status']??2,
  188. 'name'=>$data['name']??'',
  189. 'thumb'=>$data['thumb']??'',
  190. 'introduce'=>$data['introduce']??'',
  191. 'code'=>$data['code']??'',
  192. 'price'=>$data['price']??0,
  193. 'ad_size_id'=>$data['ad_size_id']??0,
  194. 'ad_tag'=>$data['ad_tag']??'',
  195. ];
  196. $result = AdPlace::where($where)->update($Insdata);
  197. if($result){
  198. return Result::success($result);
  199. }else{
  200. return Result::error("更新失败");
  201. }
  202. }
  203. /**
  204. * 删除广告位
  205. * @param array $data
  206. * @return array
  207. */
  208. public function delAdPlace(array $data): array
  209. {
  210. $adList = Ad::where(['pid'=>$data['id']])->get();
  211. if($adList->toArray()){
  212. return Result::error("广告位里面还有广告,不能删除广告位");
  213. }
  214. $result = AdPlace::where($data)->delete();
  215. if(!$result){
  216. return Result::error("删除失败");
  217. }
  218. return Result::success($result);
  219. }
  220. /**
  221. * @param int $id
  222. * @return array
  223. */
  224. public function getAdPlaceInfo(int $id): array
  225. {
  226. $adInfo = AdPlace::query()->find($id);
  227. if (empty($adInfo)) {
  228. return Result::error("没有数据",0);
  229. }
  230. return Result::success($adInfo->toArray());
  231. }
  232. }