IsvMedicCardServiceImpl.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. package com.ywt.alipaympapi.service.impl;
  2. import com.alibaba.fastjson.JSON;
  3. import com.ywt.alipaympapi.core.utils.*;
  4. import com.ywt.alipaympapi.models.BaseResponse;
  5. import com.ywt.alipaympapi.models.enums.RelationshipEnum;
  6. import com.ywt.alipaympapi.models.enums.SexEnum;
  7. import com.ywt.alipaympapi.models.enums.SmsCodeTypeEnum;
  8. import com.ywt.alipaympapi.service.AuthService;
  9. import com.ywt.alipaympapi.service.IsvMedicCardService;
  10. import com.ywt.alipaympapi.service.services.MessageSrv;
  11. import com.ywt.core.exception.AppMessageException;
  12. import com.ywt.gapi.Result;
  13. import com.ywt.gapi.ResultCode;
  14. import com.ywt.gapi.ResultInt;
  15. import com.ywt.gapi.taihe.register.CreateMedicalCardRequest;
  16. import com.ywt.gapi.taihe.register.CreateMedicalCardResponse;
  17. import com.ywt.gapi.taihe.register.TaiheRegisterServiceGrpc;
  18. import com.ywt.gapi.user.*;
  19. import com.ywt.model.PageVO;
  20. import com.ywt.model.Pagination;
  21. import com.ywt.request.card.UpdatePatientRequest;
  22. import com.ywt.request.card.*;
  23. import com.ywt.response.card.*;
  24. import lombok.extern.slf4j.Slf4j;
  25. import org.slf4j.Logger;
  26. import org.slf4j.LoggerFactory;
  27. import org.springframework.beans.factory.annotation.Autowired;
  28. import org.springframework.stereotype.Service;
  29. import java.util.Comparator;
  30. import java.util.LinkedList;
  31. import java.util.List;
  32. import java.util.stream.Collectors;
  33. @Slf4j
  34. @Service
  35. public class IsvMedicCardServiceImpl implements IsvMedicCardService {
  36. @Autowired
  37. private TaiheRegisterServiceGrpc.TaiheRegisterServiceBlockingStub taiheRegisterServiceBlockingStub;
  38. @Autowired
  39. private UserServiceGrpc.UserServiceBlockingStub userServiceBlockingStub;
  40. @Autowired
  41. private MessageSrv messageSrv;
  42. @Override
  43. public CreateOrBindCardResponseData createOrBindMedicCard(CreateOrBindCardRequest request) throws Exception {
  44. //用户唯一id
  45. // String userId = request.getUserId();
  46. //标志新建or绑定操作; 0=新建 1=绑定
  47. Integer type = request.getType();
  48. //就诊卡卡号,仅绑定操作时有效
  49. String cardNum = request.getCardNum();
  50. //性别; 男|女
  51. String sex = request.getSex();
  52. //生日(yyyy-MM-dd)
  53. String birthDay = request.getBirthDay();
  54. //验证码
  55. String authCode = request.getAuthCode();
  56. //关系
  57. String relationShip = request.getRelationShip();
  58. //是否默认(1-是,2-否)
  59. String defaultCard = request.getDefaultCard();
  60. //手机号
  61. String phoneNo = request.getPhoneNumber();
  62. //身份证号码
  63. String idNo = request.getIdCardNo();
  64. //姓名
  65. String patientName = Checker.getStringValue(request.getName());
  66. int hospitalId = BizUtil.getCurrentHospitalId();
  67. int userId = ContextHelper.getCurrentUserIdWrapped();
  68. CreateOrBindCardResponseData resp = new CreateOrBindCardResponseData();
  69. // type: 标志新建or绑定操作; 0=新建 1=绑定
  70. if (type == 0) {
  71. String city = "广州市";
  72. String address = "默认详情地址";
  73. String isInsu = "";
  74. if (!IdCardUtil.verify(idNo)) {
  75. throw new AppMessageException(String.format("身份证不合法:%s", idNo));
  76. }
  77. CheckUtil.ensureValidPhoneNumber(phoneNo, String.format("手机号不合法:%s", phoneNo));
  78. CheckUtil.ensureNotEmpty(patientName, "请完善姓名");
  79. CreateMedicalCardResponse response = taiheRegisterServiceBlockingStub.createMedicalCard(CreateMedicalCardRequest.newBuilder()
  80. .setPhoneNo(phoneNo)
  81. .setIdNo(idNo)
  82. .setPatientName(patientName)
  83. .setCity(city)
  84. .setAddress(address)
  85. .setIsInsu(isInsu)
  86. .setHospitalId(hospitalId)
  87. .setUserId(userId)
  88. .build());
  89. if (response.getCode() == BaseResponse.SUCCEED) {
  90. resp.setUserId(request.getUserId());
  91. resp.setCardNum(response.getMedicalCardNo());
  92. resp.setName(patientName);
  93. resp.setSex(sex);
  94. resp.setBirthDay(birthDay);
  95. resp.setIdCardNo(idNo);
  96. resp.setPhoneNumber(phoneNo);
  97. resp.setBalance("0.00");
  98. } else {
  99. throw new AppMessageException(response.getInfo());
  100. }
  101. return resp;
  102. }
  103. // type: 标志新建or绑定操作; 0=新建 1=绑定
  104. if (type == 1) {
  105. if (hospitalId < 1) {
  106. throw new AppMessageException("请选择医院");
  107. }
  108. if (StringHelper.isNullOrWhiteSpace(cardNum)) {
  109. throw new AppMessageException("请填写就诊卡号");
  110. }
  111. if (StringHelper.isNullOrWhiteSpace(patientName)) {
  112. throw new AppMessageException("请填写真实的姓名");
  113. }
  114. if (StringHelper.isNullOrWhiteSpace(phoneNo)) {
  115. throw new AppMessageException("请填写在医院预留的手机号");
  116. }
  117. if (StringHelper.isNullOrWhiteSpace(authCode)) {
  118. throw new AppMessageException("请输入验证码");
  119. }
  120. BaseResponse response = messageSrv.checkSmsCode(phoneNo, authCode, SmsCodeTypeEnum.Medical_Card_Bind.getValue());
  121. if (response.getCode() != BaseResponse.SUCCEED) {
  122. throw new AppMessageException(response.getInfo());
  123. }
  124. // 通过枚举类的方法,将文字转换成数字
  125. int relationshipInt = RelationshipEnum.valueOf(relationShip).getValue();
  126. // defaultCardInt: 1-是,0-否
  127. int defaultCardInt = "1".equals(defaultCard) ? 1 : 0;
  128. AddMedicalCardRequest addMedicalCardRequest = AddMedicalCardRequest.newBuilder()
  129. .setUserId(userId)
  130. .setHospitalId(hospitalId)
  131. .setCardNo(cardNum)
  132. .setRealName(patientName)
  133. .setMobile(phoneNo)
  134. .setRelationship(relationshipInt)
  135. .setDefaultCard(defaultCardInt)
  136. .build();
  137. ResultInt resultInt = userServiceBlockingStub.addMedicalCard(addMedicalCardRequest);
  138. if (resultInt.getCode() == ResultCode.SUCCEED) {
  139. int cent = Checker.parseInt(resultInt.getInfo());
  140. double balance = cent / 100d;
  141. resp.setUserId(request.getUserId());
  142. resp.setCardNum(request.getCardNum());
  143. resp.setName(patientName);
  144. resp.setSex(sex);
  145. resp.setBirthDay(birthDay);
  146. resp.setIdCardNo(idNo);
  147. resp.setPhoneNumber(phoneNo);
  148. resp.setBalance(String.format("%.2f", balance));
  149. } else {
  150. throw new AppMessageException(response.getInfo());
  151. }
  152. }
  153. return resp;
  154. }
  155. @Override
  156. public UpdateCardResponseData updateMedicCard(UpdateCardRequest request) throws Exception {
  157. if (1 == 1) {
  158. throw new AppMessageException("尚无修改诊疗卡功能");
  159. }
  160. return null;
  161. }
  162. // 就诊卡详情
  163. @Override
  164. public QuerySingleCardResponseData querySingleMedicCard(QuerySingleCardRequest request) throws Exception {
  165. // 此处传诊疗卡ID和userId
  166. String cardNum = request.getCardNum();
  167. CheckUtil.ensureNotEmpty(cardNum, "诊疗卡号不能为空");
  168. int hospitalId = BizUtil.getCurrentHospitalId();
  169. int userId = ContextHelper.getCurrentUserIdWrapped();
  170. GetMedicalCardListRequest getMedicalCardListRequest = GetMedicalCardListRequest.newBuilder()
  171. .setHospitalId(hospitalId)
  172. .setUserId(userId)
  173. .build();
  174. GetMedicalCardListResponse getMedicalCardListResponse = userServiceBlockingStub.getMedicalCardList(getMedicalCardListRequest);
  175. Result result = getMedicalCardListResponse.getResult();
  176. if (result.getCode() != ResultCode.SUCCEED) throw new AppMessageException(result.getInfo());
  177. List<MedicalCard> list = getMedicalCardListResponse.getMedicalCardsList().stream()
  178. .filter(c -> Checker.getStringValue(c.getCardNo()).equals(cardNum))
  179. .collect(Collectors.toList());
  180. if (list.isEmpty()) return null;
  181. MedicalCard medicalCard = list.get(0);
  182. double balance = medicalCard.getBalance() / 100d;
  183. QuerySingleCardResponseData item = new QuerySingleCardResponseData();
  184. item.setCardNum(medicalCard.getCardNo());//就诊卡卡号/医保卡卡号
  185. String cardType = medicalCard.getCardType();
  186. item.setType("0");// 标记查询卡类型
  187. item.setName(medicalCard.getPatientName());//姓名
  188. item.setSex(SexEnum.getSexDisplayName(medicalCard.getSex()));//性别
  189. item.setBirthDay(medicalCard.getBirthday());//生日
  190. item.setIdCardNo(medicalCard.getIdNo());//身份证号码
  191. item.setPhoneNumber(medicalCard.getMobile());//手机号
  192. item.setBalance(balance + "");//卡内余额
  193. item.setRelationShip(RelationshipEnum.valueOf(medicalCard.getRelationship()).getDisplayName()); // 关系
  194. item.setDefaultCard(medicalCard.getIsDefault() ? "是" : "否");// FIXME: 是否是默认就诊卡
  195. item.setId(medicalCard.getCardId());
  196. return item;
  197. }
  198. // 我的证件
  199. @Override
  200. public QueryDefaultCardResponseData queryDefaultMedicCard(QueryDefaultCardRequest request) throws Exception {
  201. int hospitalId = BizUtil.getCurrentHospitalId();
  202. int userId = ContextHelper.getCurrentUserIdWrapped();
  203. GetMedicalCardListRequest getMedicalCardListRequest = GetMedicalCardListRequest.newBuilder()
  204. .setHospitalId(hospitalId)
  205. .setUserId(userId)
  206. .build();
  207. GetMedicalCardListResponse getMedicalCardListResponse = userServiceBlockingStub.getMedicalCardList(getMedicalCardListRequest);
  208. Result result = getMedicalCardListResponse.getResult();
  209. if (result.getCode() != ResultCode.SUCCEED) throw new AppMessageException(result.getInfo());
  210. List<MedicalCard> list = getMedicalCardListResponse.getMedicalCardsList().stream()
  211. .sorted(Comparator.comparing(MedicalCard::getLastChosenTime).reversed())
  212. .collect(Collectors.toList());
  213. if (list.isEmpty()) return null;
  214. MedicalCard medicalCard = list.get(0);
  215. double balance = medicalCard.getBalance() / 100d;
  216. QueryDefaultCardResponseData item = new QueryDefaultCardResponseData();
  217. item.setCardNum(medicalCard.getCardNo());//就诊卡卡号/医保卡卡号
  218. item.setType("0");// 标记查询卡类型,默认普通卡
  219. item.setName(medicalCard.getPatientName());//姓名
  220. item.setSex(SexEnum.getSexDisplayName(medicalCard.getSex()));//性别
  221. item.setBirthDay(medicalCard.getBirthday());//生日
  222. item.setIdCardNo(medicalCard.getIdNo());//身份证号码
  223. item.setPhoneNumber(medicalCard.getMobile());//手机号
  224. item.setBalance(balance + "");//卡内余额
  225. item.setRelationShip(RelationshipEnum.valueOf(medicalCard.getRelationship()).getDisplayName()); // 关系
  226. item.setDefaultCard(medicalCard.getIsDefault() ? "是" : "否");//是否是默认就诊卡
  227. item.setPatientId("");//如果是医保卡,传参保城市
  228. item.setId(medicalCard.getCardId());
  229. return item;
  230. }
  231. // 查询就诊卡列表
  232. @Override
  233. public PageVO<QueryListCardResponseData> queryListMedicCard(QueryListCardRequest request) throws Exception {
  234. int hospitalId = BizUtil.getCurrentHospitalId();
  235. int userId = ContextHelper.getCurrentUserIdWrapped();
  236. GetMedicalCardListRequest getMedicalCardListRequest = GetMedicalCardListRequest.newBuilder()
  237. .setHospitalId(hospitalId)
  238. .setUserId(userId)
  239. .build();
  240. GetMedicalCardListResponse getMedicalCardListResponse = userServiceBlockingStub.getMedicalCardList(getMedicalCardListRequest);
  241. Result result = getMedicalCardListResponse.getResult();
  242. if (result.getCode() != ResultCode.SUCCEED) throw new AppMessageException(result.getInfo());
  243. int total;
  244. List<QueryListCardResponseData> lst = new LinkedList<>();
  245. List<MedicalCard> medicalCards = getMedicalCardListResponse.getMedicalCardsList();
  246. total = medicalCards.size();
  247. for (MedicalCard medicalCard : medicalCards) {
  248. double balance = medicalCard.getBalance() / 100d;
  249. QueryListCardResponseData item = new QueryListCardResponseData();
  250. item.setCardNum(medicalCard.getCardNo());//就诊卡卡号/医保卡卡号
  251. String cardType = medicalCard.getCardType();
  252. item.setType(1);//标记查询卡类型
  253. item.setName(medicalCard.getPatientName());//姓名
  254. item.setSex(SexEnum.getSexDisplayName(medicalCard.getSex()));//性别
  255. item.setBirthDay(medicalCard.getBirthday());//生日
  256. item.setIdCardNo(medicalCard.getIdNo());//身份证号码
  257. item.setPhoneNumber(medicalCard.getMobile());//手机号
  258. item.setBalance(balance + "");//卡内余额
  259. item.setIsDefault(medicalCard.getIsDefault());//是否是默认就诊卡
  260. item.setCity("");//如果是医保卡,传参保城市
  261. item.setCardOrgName("");//如果是医保卡,传发卡机构
  262. lst.add(item);
  263. }
  264. Pagination pagination = new Pagination(1, 1, total);
  265. return new PageVO<>(pagination, lst);
  266. }
  267. // 删除就诊卡
  268. @Override
  269. public DeleteCardResponseData deleteMedicCard(DeleteCardRequest request) throws Exception {
  270. int cardId = Checker.parseInt(request.getCardId());
  271. CheckUtil.ensureLargerThanZero(cardId, "诊疗卡 id 不正确");
  272. int userId = ContextHelper.getCurrentUserIdWrapped();
  273. DeleteCardResponseData responseData = new DeleteCardResponseData();
  274. DeleteMedicalCardRequest deleteMedicalCardRequest = DeleteMedicalCardRequest.newBuilder()
  275. .setCardId(cardId)
  276. .setUserId(userId)
  277. .build();
  278. Result result = userServiceBlockingStub.deleteMedicalCard(deleteMedicalCardRequest);
  279. responseData.setIsSuccess(result.getCode() == ResultCode.SUCCEED);
  280. responseData.setInfo(result.getInfo());
  281. return responseData;
  282. }
  283. @Override
  284. public BindMedInsuranceCardResponseData bindMedInsuranceCard(BindMedInsuranceCardRequest request) throws Exception {
  285. BindMedInsuranceCardResponseData resp = new BindMedInsuranceCardResponseData();
  286. String patientName = request.getName();//姓名
  287. String sex = request.getSex();//性别
  288. String birthDay = request.getBirthDay();//生日
  289. String idCardNo = request.getIdCardNo();//身份证号码
  290. String phoneNo = request.getPhoneNumber();//手机号
  291. String cardNum = request.getMedInsuranceNum();//医保卡识别号
  292. int hospitalId = BizUtil.getCurrentHospitalId();
  293. int userId = ContextHelper.getCurrentUserIdWrapped();
  294. if (hospitalId < 1) {
  295. throw new AppMessageException("请选择医院");
  296. }
  297. if (StringHelper.isNullOrWhiteSpace(cardNum)) {
  298. throw new AppMessageException("请填写就诊卡号");
  299. }
  300. if (StringHelper.isNullOrWhiteSpace(patientName)) {
  301. throw new AppMessageException("请填写真实的姓名");
  302. }
  303. if (StringHelper.isNullOrWhiteSpace(phoneNo)) {
  304. throw new AppMessageException("请填写在医院预留的手机号");
  305. }
  306. // 通过枚举类的方法,将文字转换成数字
  307. int relationshipInt = 0;
  308. // defaultCardInt: 1-是,0-否
  309. int defaultCardInt = 0;
  310. AddMedicalCardRequest addMedicalCardRequest = AddMedicalCardRequest.newBuilder()
  311. .setUserId(userId)
  312. .setHospitalId(hospitalId)
  313. .setCardNo(cardNum)
  314. .setRealName(patientName)
  315. .setMobile(phoneNo)
  316. .setRelationship(relationshipInt)
  317. .setDefaultCard(defaultCardInt)
  318. .build();
  319. ResultInt resultInt = userServiceBlockingStub.addMedicalCard(addMedicalCardRequest);
  320. if (resultInt.getCode() == ResultCode.SUCCEED) {
  321. resp.setUserId(request.getUserId());
  322. resp.setCardNum(request.getMedInsuranceNum());
  323. resp.setName(patientName);
  324. resp.setSex(sex);
  325. resp.setBirthDay(birthDay);
  326. resp.setIdCardNo(idCardNo);
  327. resp.setPhoneNumber(phoneNo);
  328. } else {
  329. throw new AppMessageException(resultInt.getInfo());
  330. }
  331. return resp;
  332. }
  333. // 就诊人列表
  334. @Override
  335. public List<QueryPatientListResponse> queryPatientList(QueryPatientListRequest request) throws Exception {
  336. return null;
  337. }
  338. @Override
  339. public PatientBindCardResponse bindMedicCard(PatientBindCardRequest request) throws Exception {
  340. return null;
  341. }
  342. @Override
  343. public DeletePatientResponse deletePatient(DeletePatientRequest request) throws Exception {
  344. return null;
  345. }
  346. @Override
  347. public AddPatientResponse addPatient(AddPatientRequest request) throws Exception {
  348. return null;
  349. }
  350. @Override
  351. public UpdatePatientResponse updatePatient(UpdatePatientRequest request) throws Exception {
  352. return null;
  353. }
  354. @Override
  355. public QueryPatientDetailResponse querySinglePatient(QueryPatientDetailRequest request) throws Exception {
  356. return null;
  357. }
  358. @Override
  359. public List<MyCardListResponse> myCardList(MyCardListRequest request) throws Exception {
  360. return null;
  361. }
  362. }