GroupsRelationNetflixServiceImpl.java 9.1 KB

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