RedisData.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. package com.loan.system.utils;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import cn.hutool.json.JSONObject;
  5. import cn.hutool.json.JSONUtil;
  6. import com.loan.system.domain.dto.*;
  7. import com.loan.system.domain.entity.*;
  8. import com.loan.system.domain.enums.BusinessAttrEnum;
  9. import com.loan.system.domain.vo.CollateralVO;
  10. import com.loan.system.domain.vo.ContractVO;
  11. import com.loan.system.domain.vo.CustomersOtherVO;
  12. import com.loan.system.domain.vo.DocumentVO;
  13. import com.loan.system.service.CustomerService;
  14. import com.loan.system.service.UserService;
  15. import io.swagger.models.auth.In;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.data.redis.core.StringRedisTemplate;
  18. import org.springframework.stereotype.Component;
  19. import org.springframework.util.ObjectUtils;
  20. import java.time.LocalDateTime;
  21. import java.time.ZoneOffset;
  22. import java.time.format.DateTimeFormatter;
  23. import java.util.*;
  24. @Component
  25. public class RedisData {
  26. private final StringRedisTemplate stringRedisTemplate;
  27. private final String CONTRACT_KEY = "contract:";
  28. private final String COLLATERAL_KEY = "collateral:";
  29. private final String CONTRACT_COLLATERAL_KEY = "contractAndCollateral:";
  30. private final String CUSTOMER_OTHER_KEY = "customerOther:";
  31. private final String DOCUMENT_KEY = "document:";
  32. private final String APPROVAL_VERSION_KEY = "version:";
  33. private final String COMMENT_KEY = "comment:";
  34. public final String COLLATERAL_STATUS_KEY = "collateralStatus:";
  35. private static final long BEGIN_TIMESTAMP = 1735689600L;
  36. public RedisData(StringRedisTemplate stringRedisTemplate) {
  37. this.stringRedisTemplate = stringRedisTemplate;
  38. }
  39. public boolean isEmpty(Long caseId){
  40. return stringRedisTemplate.hasKey(caseId.toString());
  41. }
  42. public void setLoanCase(Long caseId, LoanCaseDTO loanCaseDTO){
  43. String contractKey = caseId +":"+ CONTRACT_KEY;
  44. String collateralKey = caseId +":"+ COLLATERAL_KEY;
  45. String contractAndCollateralKey = caseId +":"+ CONTRACT_COLLATERAL_KEY;
  46. String customerOtherKey = caseId +":"+ CUSTOMER_OTHER_KEY;
  47. long now = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC);
  48. long timestamp = now - BEGIN_TIMESTAMP;
  49. int index = 1;
  50. List<ContractDTO> contractVOS = loanCaseDTO.getContracts();
  51. List<Contract> contracts = new ArrayList<>();
  52. for (ContractDTO contractDTO : contractVOS){
  53. Contract contract = BeanUtil.copyProperties(contractDTO, Contract.class);
  54. Long id = timestamp<<20 | index;
  55. if (contract.getId() == null)
  56. //contract.setId(-id);
  57. continue;
  58. String[] split = contractDTO.getCustomerIds().split(",");
  59. contract.setCustomerId(split.length>0?Long.valueOf(split[0]):null);
  60. contract.setCustomerId1(split.length>1?Long.valueOf(split[1]):null);
  61. contract.setCustomerId2(split.length>2?Long.valueOf(split[2]):null);
  62. String name=contract.getBusinessAttr().equals(BusinessAttrEnum.ESTATE_MORTGAGE.getMsg())?"抵押合同":"质押合同";
  63. contract.setContractName("浙江宝路同典当"+name);
  64. if(contract.getContractNo()==null)
  65. contract.setContractNo("临时编号:"+UUID.randomUUID().toString().replace("-",""));
  66. contract.setIsDelete(false);
  67. contract.setSignedByCustomer(false);
  68. contract.setIsPush( false);
  69. contract.setCreateTime(LocalDateTime.now()
  70. .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
  71. contract.setUpdateTime(LocalDateTime.now()
  72. .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
  73. contract.setIsDelete(false);
  74. contracts.add( contract);
  75. index++;
  76. }
  77. stringRedisTemplate.opsForValue().set(contractKey, JSONUtil.toJsonStr(contracts));
  78. List<CollateralDTO> collateralVOS = loanCaseDTO.getCollateral();
  79. List<Long> collateralIds = new ArrayList<>();
  80. if(!ObjectUtils.isEmpty(collateralVOS)){
  81. List<Collateral> collaterals = BeanUtil.copyToList(collateralVOS, Collateral.class);
  82. for (Collateral collateral : collaterals) {
  83. Long id = collateral.getId();
  84. if(id == null){//空数据
  85. // id = timestamp<<20 | index;
  86. // id = -id;
  87. // collateral.setId(id);
  88. continue;
  89. }
  90. collateralIds.add( id);
  91. collateral.setIsDelete( false);
  92. collateral.setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
  93. collateral.setUpdateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
  94. index++;
  95. }
  96. stringRedisTemplate.opsForValue().set(collateralKey, JSONUtil.toJsonStr(collaterals));
  97. }
  98. // List<ContractAndCollateral> contractAndCollaterals = new ArrayList<>();
  99. // for(int i=0 ; i < loanCaseDTO.getCollateralAndContract().size(); i++){
  100. // Contract contract = contracts.get( i);
  101. // List<Integer> collaterals = loanCaseDTO.getCollateralAndContract().get(i).get(contract.getBusinessAttr());
  102. // for (Integer collateralId : collaterals){
  103. // ContractAndCollateral contractAndCollateral = new ContractAndCollateral();
  104. // contractAndCollateral.setId(timestamp<<20 | index);
  105. // contractAndCollateral.setContractId(contract.getId());
  106. // contractAndCollateral.setCollateralId(collateralIds.get(collateralId-1));
  107. // contractAndCollateral.setCaseId(caseId);
  108. // contractAndCollateral.setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
  109. // contractAndCollateral.setUpdateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
  110. // contractAndCollateral.setIsDelete(false);
  111. //
  112. // contractAndCollaterals.add(contractAndCollateral);
  113. //
  114. // index++;
  115. // }
  116. // }
  117. // if(!ObjectUtils.isEmpty(contractAndCollaterals)){
  118. // stringRedisTemplate.opsForValue().set(contractAndCollateralKey, JSONUtil.toJsonStr(contractAndCollaterals));
  119. // }
  120. List<ContractAndCollateral> contractAndCollaterals = new ArrayList<>();
  121. Map<Integer, List<Integer>> contractSeqAndCollateralSeq = loanCaseDTO.getContractSeqAndCollateralSeq();
  122. Iterator<Map.Entry<Integer, List<Integer>>> iterator = contractSeqAndCollateralSeq.entrySet().iterator();
  123. while (iterator.hasNext()) {
  124. Map.Entry<Integer, List<Integer>> entry = iterator.next();
  125. Integer contractSeq = entry.getKey();
  126. List<Integer> collateralSeqs = entry.getValue();
  127. Contract contract = contracts.get(contractSeq-1);
  128. for (Integer collateralId : collateralSeqs){
  129. ContractAndCollateral contractAndCollateral = new ContractAndCollateral();
  130. contractAndCollateral.setId(timestamp<<20 | index);
  131. contractAndCollateral.setContractId(contract.getId());
  132. contractAndCollateral.setCollateralId(collateralIds.get(collateralId-1));
  133. contractAndCollateral.setCaseId(caseId);
  134. contractAndCollateral.setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
  135. contractAndCollateral.setUpdateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
  136. contractAndCollateral.setIsDelete(false);
  137. contractAndCollaterals.add(contractAndCollateral);
  138. index++;
  139. }
  140. }
  141. if(!ObjectUtils.isEmpty(contractAndCollaterals)){
  142. stringRedisTemplate.opsForValue().set(contractAndCollateralKey, JSONUtil.toJsonStr(contractAndCollaterals));
  143. }
  144. // CustomersOtherDTO customers1 = loanCaseDTO.getCustomers1();
  145. // CustomersOtherDTO customers2 = loanCaseDTO.getCustomers2();
  146. // List<CustomersOther> customers = new ArrayList<>();
  147. // if(!ObjectUtils.isEmpty(customers1)){
  148. // customers.add(BeanUtil.copyProperties(customers1, CustomersOther.class));
  149. // }
  150. // if (!ObjectUtils.isEmpty(customers2)){
  151. // customers.add(BeanUtil.copyProperties(customers2, CustomersOther.class));
  152. // }
  153. //
  154. // for (CustomersOther customer : customers){
  155. // System.out.println( customer);
  156. // Long id = timestamp<<20 | index;
  157. // if(customer.getId() == null)
  158. // customer.setId(-id);
  159. // customer.setIsDelete( false);
  160. //
  161. // index++;
  162. // }
  163. // if (!ObjectUtils.isEmpty(customers)){
  164. // stringRedisTemplate.opsForValue().set(customerOtherKey, JSONUtil.toJsonStr(customers));
  165. // }
  166. }
  167. public List<Contract> getContracts(Long caseId){
  168. String key = caseId +":"+ CONTRACT_KEY;
  169. String json = stringRedisTemplate.opsForValue().get(key);
  170. //2.未命中,返回空值
  171. if(StrUtil.isBlank(json))
  172. return null;
  173. //3.命中,需要将json反序列化为java
  174. List<Contract> contracts = JsonUtil.toList(json, Contract.class);
  175. return contracts;
  176. }
  177. public List<CollateralVO> getCollaterals(Long caseId){
  178. String key = caseId +":"+ COLLATERAL_KEY;
  179. String json = stringRedisTemplate.opsForValue().get(key);
  180. //2.未命中,返回空值
  181. if(StrUtil.isBlank(json))
  182. return null;
  183. //3.命中,需要将json反序列化为java
  184. List<Collateral> collaterals = JsonUtil.toList(json, Collateral.class);
  185. return BeanUtil.copyToList(collaterals, CollateralVO.class);
  186. }
  187. public List<ContractAndCollateral> getContractAndCollateral(Long caseId){
  188. String key = caseId +":"+ CONTRACT_COLLATERAL_KEY;
  189. String json = stringRedisTemplate.opsForValue().get(key);
  190. //2.未命中,返回空值
  191. if(StrUtil.isBlank(json))
  192. return null;
  193. //3.命中,需要将json反序列化为java
  194. return JsonUtil.toList(json, ContractAndCollateral.class);
  195. }
  196. public List<CustomersOtherVO> getCustomersOther(Long caseId){
  197. String key = caseId +":"+ CUSTOMER_OTHER_KEY;
  198. String json = stringRedisTemplate.opsForValue().get(key);
  199. //2.未命中,返回空值
  200. if(StrUtil.isBlank(json))
  201. return null;
  202. //3.命中,需要将json反序列化为java
  203. List<CustomersOther> customersOthers = JsonUtil.toList(json, CustomersOther.class);
  204. return BeanUtil.copyToList(customersOthers, CustomersOtherVO.class);
  205. }
  206. //TODO:用于业务确认版本
  207. public void setVersion( Long approvalVersion,Integer code){
  208. String key =APPROVAL_VERSION_KEY + code+":";
  209. stringRedisTemplate.opsForValue().set(key, approvalVersion.toString());
  210. }
  211. public Long getVersion(Integer code){
  212. String key =APPROVAL_VERSION_KEY + code+":";
  213. String json = stringRedisTemplate.opsForValue().get(key);
  214. //2.未命中,返回空值
  215. if(StrUtil.isBlank(json)){
  216. setVersion(0L,code);
  217. return 0L;
  218. }
  219. //3.命中,需要将json反序列化为java
  220. return Long.parseLong(json);
  221. }
  222. //TODO:展示驳回时的审批意见
  223. public void setRejectApprovalRecord(Long caseId, String stepName, Integer stepCode, String comment){
  224. String key = caseId +":"+ COMMENT_KEY+stepName +stepCode+":";
  225. stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(comment));
  226. }
  227. public String getRejectApprovalRecord(Long caseId,String stepName,Integer stepCode){
  228. String key = caseId +":"+ COMMENT_KEY+stepName +stepCode+":";
  229. String json = stringRedisTemplate.opsForValue().get(key);
  230. //2.未命中,返回空值
  231. if(StrUtil.isBlank(json))
  232. return null;
  233. //3.命中,需要将json反序列化为java
  234. return json;
  235. }
  236. public void deleteApprovalByKey(Long caseId,String stepName,Integer stepCode){
  237. String key = caseId +":"+ COMMENT_KEY+stepName +stepCode+":";
  238. stringRedisTemplate.delete(key);
  239. }
  240. //设置抵押物状态
  241. public void setCollateralStatus(Long caseId,Long collateralId, Integer status){
  242. String key=caseId+":"+COLLATERAL_STATUS_KEY+":";
  243. //stringRedisTemplate.opsForValue().set(key, status.toString());
  244. }
  245. public Map<Long, Integer> getCollateralStatus(Long collateralId){
  246. return null;
  247. }
  248. public void setDocuments(Long caseId, List<DocumentDTO> documentDTOS){
  249. String documentKey = caseId +":"+ DOCUMENT_KEY;
  250. long now = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC);
  251. long timestamp = now - BEGIN_TIMESTAMP;
  252. int index = 1;
  253. List<Document> documents = new ArrayList<>();
  254. if(!ObjectUtils.isEmpty( documentDTOS)){
  255. documents = BeanUtil.copyToList(documentDTOS, Document.class);
  256. for (Document document : documents) {
  257. Long id = timestamp<<20 | index;
  258. if (document.getId() == null)
  259. document.setId(-id);
  260. index++;
  261. }
  262. stringRedisTemplate.opsForValue().set(documentKey, JSONUtil.toJsonStr(documents));
  263. }
  264. }
  265. public List<DocumentVO> getDocuments(Long caseId){
  266. String key = caseId +":"+ DOCUMENT_KEY;
  267. String json = stringRedisTemplate.opsForValue().get(key);
  268. //2.未命中,返回空值
  269. if(StrUtil.isBlank(json))
  270. return null;
  271. //3.命中,需要将json反序列化为java
  272. List<Document> documents = JsonUtil.toList(json, Document.class);
  273. return BeanUtil.copyToList(documents,DocumentVO.class);
  274. }
  275. public void delete(Long caseId){
  276. String contractKey = caseId +":"+ CONTRACT_KEY;
  277. String collateralKey = caseId +":"+ COLLATERAL_KEY;
  278. String contractAndCollateralKey = caseId +":"+ CONTRACT_COLLATERAL_KEY;
  279. String customerOtherKey = caseId +":"+ CUSTOMER_OTHER_KEY;
  280. stringRedisTemplate.delete(contractKey);
  281. stringRedisTemplate.delete(collateralKey);
  282. stringRedisTemplate.delete(contractAndCollateralKey);
  283. stringRedisTemplate.delete(customerOtherKey);
  284. }
  285. }