GroupsRelationNetflixServiceImpl.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. package com.cyksj.service.relation.impl;
  2. import cn.hutool.core.date.DateTime;
  3. import cn.hutool.core.util.StrUtil;
  4. import cn.hutool.http.HttpUtil;
  5. import cn.hutool.json.JSONObject;
  6. import com.cyksj.common.constant.Constant;
  7. import com.cyksj.common.exception.BusinessRuntimeException;
  8. import com.cyksj.common.util.IoKit;
  9. import com.cyksj.common.util.J11HttpC;
  10. import com.cyksj.common.util.Jsons;
  11. import com.cyksj.common.util.StringUtil;
  12. import com.cyksj.mapper.AccountMapper;
  13. import com.cyksj.mapper.GroupsMapper;
  14. import com.cyksj.mapper.GroupsRelationMapper;
  15. import com.cyksj.mapper.UserMapper;
  16. import com.cyksj.model.entity.Account;
  17. import com.cyksj.model.entity.GroupsRelation;
  18. import com.cyksj.model.entity.GroupsTrips;
  19. import com.cyksj.model.entity.User;
  20. import com.cyksj.model.request.NfDeviceReq;
  21. import com.cyksj.redis.RedisService;
  22. import com.cyksj.service.relation.GroupsRelationNetflixService;
  23. import com.cyksj.service.user.UserBindRelationService;
  24. import com.cyksj.task.JobManager;
  25. import lombok.RequiredArgsConstructor;
  26. import lombok.extern.slf4j.Slf4j;
  27. import org.springframework.stereotype.Service;
  28. import java.net.http.HttpRequest;
  29. import java.net.http.HttpResponse;
  30. import java.util.Date;
  31. import java.util.List;
  32. import java.util.concurrent.atomic.AtomicInteger;
  33. /**
  34. * 项目名: yhlxj2
  35. * 文件名: GroupsRelationNetflixServiceImpl
  36. * 创建者: JavaZou
  37. * 创建时间:2024/6/17 15:45
  38. */
  39. @Service
  40. @RequiredArgsConstructor
  41. @Slf4j
  42. public class GroupsRelationNetflixServiceImpl implements GroupsRelationNetflixService {
  43. private final UserMapper userMapper;
  44. private final GroupsRelationMapper relationMapper;
  45. private final GroupsMapper groupsMapper;
  46. private final AccountMapper accountMapper;
  47. private final UserBindRelationService userBindRelationService;
  48. private final RedisService redisService;
  49. private final JobManager jobManager;
  50. @Override
  51. public JSONObject setNetflixSeatNum(Integer seatNum, String seatName, String account, String password, Date expiryTime, String code) {
  52. JSONObject params = new JSONObject();
  53. params.putOpt("username", account);
  54. params.putOpt("password", password);
  55. params.putOpt("seat_name", seatName);
  56. params.putOpt("seat_num", seatNum);
  57. params.putOpt("seat_time", expiryTime);
  58. if (StrUtil.isNotBlank(code)) {
  59. params.putOpt("pin_code", code);
  60. }
  61. //设置座位
  62. String url = Constant.ZHAOJU_MOVIES + "/netflix_seat";
  63. log.info("设置账号:{} 奈飞用户:{}座位信息", account, seatName);
  64. try {
  65. HttpResponse<String> res =
  66. J11HttpC.custom()
  67. .ofPost()
  68. .url(url)
  69. .headers(J11HttpC.ReqType.raw_json)
  70. .body(HttpRequest.BodyPublishers.ofString(Jsons.toJson(params), IoKit.Charsets.UTF_8.getCharset()))
  71. .send(HttpResponse.BodyHandlers.ofString());
  72. String body = res.body();
  73. JSONObject jsonObject = Jsons.parseObject(body, JSONObject.class);
  74. JSONObject data = Jsons.parseObject(jsonObject.get("data"), JSONObject.class);
  75. Integer status = data.getInt("status");
  76. if (status == 2) {
  77. log.error("设置用户:{}奈飞座位号失败", seatName);
  78. }
  79. return data;
  80. } catch (Exception e) {
  81. log.error("设置用户:{}奈飞座位号错误,msg:{}", seatName, StringUtil.getErrorText(e));
  82. }
  83. JSONObject jsonObject = new JSONObject();
  84. jsonObject.putOpt("status", 2);
  85. return jsonObject;
  86. }
  87. @Override
  88. public JSONObject setNetflixPin(Long relationId, long userId, String code) {
  89. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  90. GroupsRelation relation = relationMapper.selectById(relationId);
  91. if (!userIdList.contains(relation.getUserId())) {
  92. throw BusinessRuntimeException.getInstance("您车票已失效");
  93. }
  94. GroupsTrips groupsTrips = groupsMapper.selectById(relation.getGroupsId());
  95. Account account = accountMapper.selectById(groupsTrips.getAccountId());
  96. if (account.getGoodsId() != 1) {
  97. throw BusinessRuntimeException.getInstance("系统错误");
  98. }
  99. User user = userMapper.selectById(relation.getUserId());
  100. if (!redisService.setNx(RedisService.key.NF_SET_TIME_LIMIT.getName() + relationId, relationId, RedisService.key.NF_SET_TIME_LIMIT.getTimeout())) {
  101. throw BusinessRuntimeException.getInstance("一分钟内可执行一次");
  102. }
  103. JSONObject jsonObject = setNetflixSeatNum(relation.getNum(), user.getNickname(), account.getAccount(), account.getPassword(), relation.getExpiryTime(), code);
  104. relation.setPinCode(code);
  105. relationMapper.updateById(relation);
  106. return jsonObject;
  107. }
  108. @Override
  109. public Integer getPinStatus(String taskId) throws Exception {
  110. JSONObject params = new JSONObject();
  111. params.putOpt("transId", taskId);
  112. //查询任务状态
  113. String url = Constant.ZHAOJU_MOVIES + "/netflix_task_status";
  114. HttpResponse<String> res =
  115. J11HttpC.custom()
  116. .ofPost()
  117. .url(url)
  118. .headers(J11HttpC.ReqType.raw_json)
  119. .body(HttpRequest.BodyPublishers.ofString(Jsons.toJson(params), IoKit.Charsets.UTF_8.getCharset()))
  120. .send(HttpResponse.BodyHandlers.ofString());
  121. String body = res.body();
  122. JSONObject jsonObject = Jsons.parseObject(body, JSONObject.class);
  123. JSONObject data = Jsons.parseObject(jsonObject.get("data"), JSONObject.class);
  124. Integer status = data.getInt("status");
  125. return status;
  126. }
  127. @Override
  128. public Boolean confirmUpdate(String nfUrl) {
  129. String key = RedisService.key.NETFLIX_UPDATE_LIMIT_TIME.getName();
  130. if (!redisService.setNx(key, 1L, 30L)) {
  131. throw BusinessRuntimeException.getInstance("系统繁忙,请稍后点击..");
  132. }
  133. JSONObject params = new JSONObject();
  134. params.putOpt("url", nfUrl);
  135. //确认更新点击
  136. String url = Constant.ZHAOJU_MOVIES + "/netflix_primary_action";
  137. try {
  138. cn.hutool.http.HttpResponse execute = HttpUtil
  139. .createPost(url)
  140. .body(params.toString(), "application/json")
  141. .setConnectionTimeout(30000)
  142. .setReadTimeout(30000)
  143. .execute();
  144. JSONObject resp = Jsons.parseObject(execute.body(), JSONObject.class);
  145. redisService.del(key);
  146. return resp.getBool("success");
  147. } catch (Exception e) {
  148. }
  149. return false;
  150. }
  151. /**
  152. * 定时设置座位号
  153. * 30s执行一次
  154. */
  155. @Override
  156. public void timingSetNetflix(Integer num, String nickname, String account, String password, Date expiryTime, AtomicInteger start) {
  157. try {
  158. String key = RedisService.key.NETFLIX_SET_LIMIT_TIME.getName();
  159. if (!redisService.setNx(key, 1L, 30L)) {
  160. jobManager.addJob(30001, () -> {
  161. if (start.get() > 3) {
  162. return;
  163. }
  164. start.incrementAndGet();
  165. timingSetNetflix(num, nickname, account, password, expiryTime, start);
  166. });
  167. } else {
  168. setNetflixSeatNum(num, nickname, account, password, expiryTime, null);
  169. log.info("当前时间:{}开始调用设置奈飞座位信息接口", DateTime.now());
  170. }
  171. } catch (Exception e) {
  172. }
  173. }
  174. @Override
  175. public JSONObject nfKickOut(long userId, Long relationId) {
  176. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  177. GroupsRelation relation = relationMapper.selectById(relationId);
  178. if (!userIdList.contains(relation.getUserId())) {
  179. throw BusinessRuntimeException.getInstance("您车票已失效");
  180. }
  181. GroupsTrips groupsTrips = groupsMapper.selectById(relation.getGroupsId());
  182. Account account = accountMapper.selectById(groupsTrips.getAccountId());
  183. if (account.getGoodsId() != 1) {
  184. throw BusinessRuntimeException.getInstance("系统错误");
  185. }
  186. User user = userMapper.selectById(relation.getUserId());
  187. if (!redisService.setNx(RedisService.key.NF_KICKOUT_TIME_LIMIT.getName() + relationId, relationId, RedisService.key.NF_KICKOUT_TIME_LIMIT.getTimeout())) {
  188. throw BusinessRuntimeException.getInstance("一分钟内可执行一次");
  189. }
  190. return doNfKickOut(relation.getNum(), user.getNickname(), account.getAccount(), account.getPassword(),account.getCookie(), relation.getExpiryTime(), relation.getPinCode());
  191. }
  192. @Override
  193. public void clickNfHouseholdDevice(NfDeviceReq nfDeviceReq) {
  194. String url = nfDeviceReq.getUrl();
  195. if (!confirmUpdate(url)) {
  196. throw BusinessRuntimeException.getInstance("程序代点失败");
  197. }
  198. }
  199. /**
  200. * 踢出奈飞设备
  201. */
  202. private JSONObject doNfKickOut(Integer seatNum, String seatName, String account, String password, String cookie, Date expiryTime, String code) {
  203. JSONObject params = new JSONObject();
  204. params.putOpt("username", account);
  205. params.putOpt("password", password);
  206. params.putOpt("seat_name", seatName);
  207. params.putOpt("seat_num", seatNum);
  208. params.putOpt("seat_time", expiryTime);
  209. if (StrUtil.isNotBlank(code)) {
  210. params.putOpt("pin_code", code);
  211. }
  212. if (StrUtil.isNotEmpty(cookie)) {
  213. params.putOpt("cookie", cookie);
  214. }
  215. //设置座位
  216. String url = Constant.ZHAOJU_MOVIES + "/netflix_seat";
  217. log.info("设置账号:{} 奈飞用户:{}座位信息", account, seatName);
  218. try {
  219. HttpResponse<String> res =
  220. J11HttpC.custom()
  221. .ofPost()
  222. .url(url)
  223. .headers(J11HttpC.ReqType.raw_json)
  224. .body(HttpRequest.BodyPublishers.ofString(Jsons.toJson(params), IoKit.Charsets.UTF_8.getCharset()))
  225. .send(HttpResponse.BodyHandlers.ofString());
  226. String body = res.body();
  227. JSONObject jsonObject = Jsons.parseObject(body, JSONObject.class);
  228. JSONObject data = Jsons.parseObject(jsonObject.get("data"), JSONObject.class);
  229. Integer status = data.getInt("status");
  230. if (status == 2) {
  231. log.error("设置用户:{}奈飞座位号失败", seatName);
  232. }
  233. return data;
  234. } catch (Exception e) {
  235. log.error("设置用户:{}奈飞座位号错误,msg:{}", seatName, StringUtil.getErrorText(e));
  236. }
  237. JSONObject jsonObject = new JSONObject();
  238. jsonObject.putOpt("status", 2);
  239. return jsonObject;
  240. }
  241. }