AdService.php 9.5 KB

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