| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- package com.loan.system.utils;
- import cn.hutool.core.bean.BeanUtil;
- import cn.hutool.core.util.StrUtil;
- import cn.hutool.json.JSONObject;
- import cn.hutool.json.JSONUtil;
- import com.loan.system.domain.dto.*;
- import com.loan.system.domain.entity.*;
- import com.loan.system.domain.enums.BusinessAttrEnum;
- import com.loan.system.domain.vo.CollateralVO;
- import com.loan.system.domain.vo.ContractVO;
- import com.loan.system.domain.vo.CustomersOtherVO;
- import com.loan.system.domain.vo.DocumentVO;
- import com.loan.system.service.CustomerService;
- import com.loan.system.service.UserService;
- import io.swagger.models.auth.In;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.redis.core.StringRedisTemplate;
- import org.springframework.stereotype.Component;
- import org.springframework.util.ObjectUtils;
- import java.time.LocalDateTime;
- import java.time.ZoneOffset;
- import java.time.format.DateTimeFormatter;
- import java.util.*;
- @Component
- public class RedisData {
- private final StringRedisTemplate stringRedisTemplate;
- private final String CONTRACT_KEY = "contract:";
- private final String COLLATERAL_KEY = "collateral:";
- private final String CONTRACT_COLLATERAL_KEY = "contractAndCollateral:";
- private final String CUSTOMER_OTHER_KEY = "customerOther:";
- private final String DOCUMENT_KEY = "document:";
- private final String APPROVAL_VERSION_KEY = "version:";
- private final String COMMENT_KEY = "comment:";
- public final String COLLATERAL_STATUS_KEY = "collateralStatus:";
- private static final long BEGIN_TIMESTAMP = 1735689600L;
- public RedisData(StringRedisTemplate stringRedisTemplate) {
- this.stringRedisTemplate = stringRedisTemplate;
- }
- public boolean isEmpty(Long caseId){
- return stringRedisTemplate.hasKey(caseId.toString());
- }
- public void setLoanCase(Long caseId, LoanCaseDTO loanCaseDTO){
- String contractKey = caseId +":"+ CONTRACT_KEY;
- String collateralKey = caseId +":"+ COLLATERAL_KEY;
- String contractAndCollateralKey = caseId +":"+ CONTRACT_COLLATERAL_KEY;
- String customerOtherKey = caseId +":"+ CUSTOMER_OTHER_KEY;
- long now = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC);
- long timestamp = now - BEGIN_TIMESTAMP;
- int index = 1;
- List<ContractDTO> contractVOS = loanCaseDTO.getContracts();
- List<Contract> contracts = new ArrayList<>();
- for (ContractDTO contractDTO : contractVOS){
- Contract contract = BeanUtil.copyProperties(contractDTO, Contract.class);
- Long id = timestamp<<20 | index;
- if (contract.getId() == null)
- //contract.setId(-id);
- continue;
- String[] split = contractDTO.getCustomerIds().split(",");
- contract.setCustomerId(split.length>0?Long.valueOf(split[0]):null);
- contract.setCustomerId1(split.length>1?Long.valueOf(split[1]):null);
- contract.setCustomerId2(split.length>2?Long.valueOf(split[2]):null);
- String name=contract.getBusinessAttr().equals(BusinessAttrEnum.ESTATE_MORTGAGE.getMsg())?"抵押合同":"质押合同";
- contract.setContractName("浙江宝路同典当"+name);
- if(contract.getContractNo()==null)
- contract.setContractNo("临时编号:"+UUID.randomUUID().toString().replace("-",""));
- contract.setIsDelete(false);
- contract.setSignedByCustomer(false);
- contract.setIsPush( false);
- contract.setCreateTime(LocalDateTime.now()
- .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
- contract.setUpdateTime(LocalDateTime.now()
- .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
- contract.setIsDelete(false);
- contracts.add( contract);
- index++;
- }
- stringRedisTemplate.opsForValue().set(contractKey, JSONUtil.toJsonStr(contracts));
- List<CollateralDTO> collateralVOS = loanCaseDTO.getCollateral();
- List<Long> collateralIds = new ArrayList<>();
- if(!ObjectUtils.isEmpty(collateralVOS)){
- List<Collateral> collaterals = BeanUtil.copyToList(collateralVOS, Collateral.class);
- for (Collateral collateral : collaterals) {
- Long id = collateral.getId();
- if(id == null){//空数据
- // id = timestamp<<20 | index;
- // id = -id;
- // collateral.setId(id);
- continue;
- }
- collateralIds.add( id);
- collateral.setIsDelete( false);
- collateral.setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
- collateral.setUpdateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
- index++;
- }
- stringRedisTemplate.opsForValue().set(collateralKey, JSONUtil.toJsonStr(collaterals));
- }
- // List<ContractAndCollateral> contractAndCollaterals = new ArrayList<>();
- // for(int i=0 ; i < loanCaseDTO.getCollateralAndContract().size(); i++){
- // Contract contract = contracts.get( i);
- // List<Integer> collaterals = loanCaseDTO.getCollateralAndContract().get(i).get(contract.getBusinessAttr());
- // for (Integer collateralId : collaterals){
- // ContractAndCollateral contractAndCollateral = new ContractAndCollateral();
- // contractAndCollateral.setId(timestamp<<20 | index);
- // contractAndCollateral.setContractId(contract.getId());
- // contractAndCollateral.setCollateralId(collateralIds.get(collateralId-1));
- // contractAndCollateral.setCaseId(caseId);
- // contractAndCollateral.setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
- // contractAndCollateral.setUpdateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
- // contractAndCollateral.setIsDelete(false);
- //
- // contractAndCollaterals.add(contractAndCollateral);
- //
- // index++;
- // }
- // }
- // if(!ObjectUtils.isEmpty(contractAndCollaterals)){
- // stringRedisTemplate.opsForValue().set(contractAndCollateralKey, JSONUtil.toJsonStr(contractAndCollaterals));
- // }
- List<ContractAndCollateral> contractAndCollaterals = new ArrayList<>();
- Map<Integer, List<Integer>> contractSeqAndCollateralSeq = loanCaseDTO.getContractSeqAndCollateralSeq();
- Iterator<Map.Entry<Integer, List<Integer>>> iterator = contractSeqAndCollateralSeq.entrySet().iterator();
- while (iterator.hasNext()) {
- Map.Entry<Integer, List<Integer>> entry = iterator.next();
- Integer contractSeq = entry.getKey();
- List<Integer> collateralSeqs = entry.getValue();
- Contract contract = contracts.get(contractSeq-1);
- for (Integer collateralId : collateralSeqs){
- ContractAndCollateral contractAndCollateral = new ContractAndCollateral();
- contractAndCollateral.setId(timestamp<<20 | index);
- contractAndCollateral.setContractId(contract.getId());
- contractAndCollateral.setCollateralId(collateralIds.get(collateralId-1));
- contractAndCollateral.setCaseId(caseId);
- contractAndCollateral.setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
- contractAndCollateral.setUpdateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
- contractAndCollateral.setIsDelete(false);
- contractAndCollaterals.add(contractAndCollateral);
- index++;
- }
- }
- if(!ObjectUtils.isEmpty(contractAndCollaterals)){
- stringRedisTemplate.opsForValue().set(contractAndCollateralKey, JSONUtil.toJsonStr(contractAndCollaterals));
- }
- // CustomersOtherDTO customers1 = loanCaseDTO.getCustomers1();
- // CustomersOtherDTO customers2 = loanCaseDTO.getCustomers2();
- // List<CustomersOther> customers = new ArrayList<>();
- // if(!ObjectUtils.isEmpty(customers1)){
- // customers.add(BeanUtil.copyProperties(customers1, CustomersOther.class));
- // }
- // if (!ObjectUtils.isEmpty(customers2)){
- // customers.add(BeanUtil.copyProperties(customers2, CustomersOther.class));
- // }
- //
- // for (CustomersOther customer : customers){
- // System.out.println( customer);
- // Long id = timestamp<<20 | index;
- // if(customer.getId() == null)
- // customer.setId(-id);
- // customer.setIsDelete( false);
- //
- // index++;
- // }
- // if (!ObjectUtils.isEmpty(customers)){
- // stringRedisTemplate.opsForValue().set(customerOtherKey, JSONUtil.toJsonStr(customers));
- // }
- }
- public List<Contract> getContracts(Long caseId){
- String key = caseId +":"+ CONTRACT_KEY;
- String json = stringRedisTemplate.opsForValue().get(key);
- //2.未命中,返回空值
- if(StrUtil.isBlank(json))
- return null;
- //3.命中,需要将json反序列化为java
- List<Contract> contracts = JsonUtil.toList(json, Contract.class);
- return contracts;
- }
- public List<CollateralVO> getCollaterals(Long caseId){
- String key = caseId +":"+ COLLATERAL_KEY;
- String json = stringRedisTemplate.opsForValue().get(key);
- //2.未命中,返回空值
- if(StrUtil.isBlank(json))
- return null;
- //3.命中,需要将json反序列化为java
- List<Collateral> collaterals = JsonUtil.toList(json, Collateral.class);
- return BeanUtil.copyToList(collaterals, CollateralVO.class);
- }
- public List<ContractAndCollateral> getContractAndCollateral(Long caseId){
- String key = caseId +":"+ CONTRACT_COLLATERAL_KEY;
- String json = stringRedisTemplate.opsForValue().get(key);
- //2.未命中,返回空值
- if(StrUtil.isBlank(json))
- return null;
- //3.命中,需要将json反序列化为java
- return JsonUtil.toList(json, ContractAndCollateral.class);
- }
- public List<CustomersOtherVO> getCustomersOther(Long caseId){
- String key = caseId +":"+ CUSTOMER_OTHER_KEY;
- String json = stringRedisTemplate.opsForValue().get(key);
- //2.未命中,返回空值
- if(StrUtil.isBlank(json))
- return null;
- //3.命中,需要将json反序列化为java
- List<CustomersOther> customersOthers = JsonUtil.toList(json, CustomersOther.class);
- return BeanUtil.copyToList(customersOthers, CustomersOtherVO.class);
- }
- //TODO:用于业务确认版本
- public void setVersion( Long approvalVersion,Integer code){
- String key =APPROVAL_VERSION_KEY + code+":";
- stringRedisTemplate.opsForValue().set(key, approvalVersion.toString());
- }
- public Long getVersion(Integer code){
- String key =APPROVAL_VERSION_KEY + code+":";
- String json = stringRedisTemplate.opsForValue().get(key);
- //2.未命中,返回空值
- if(StrUtil.isBlank(json)){
- setVersion(0L,code);
- return 0L;
- }
- //3.命中,需要将json反序列化为java
- return Long.parseLong(json);
- }
- //TODO:展示驳回时的审批意见
- public void setRejectApprovalRecord(Long caseId, String stepName, Integer stepCode, String comment){
- String key = caseId +":"+ COMMENT_KEY+stepName +stepCode+":";
- stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(comment));
- }
- public String getRejectApprovalRecord(Long caseId,String stepName,Integer stepCode){
- String key = caseId +":"+ COMMENT_KEY+stepName +stepCode+":";
- String json = stringRedisTemplate.opsForValue().get(key);
- //2.未命中,返回空值
- if(StrUtil.isBlank(json))
- return null;
- //3.命中,需要将json反序列化为java
- return json;
- }
- public void deleteApprovalByKey(Long caseId,String stepName,Integer stepCode){
- String key = caseId +":"+ COMMENT_KEY+stepName +stepCode+":";
- stringRedisTemplate.delete(key);
- }
- //设置抵押物状态
- public void setCollateralStatus(Long caseId,Long collateralId, Integer status){
- String key=caseId+":"+COLLATERAL_STATUS_KEY+":";
- //stringRedisTemplate.opsForValue().set(key, status.toString());
- }
- public Map<Long, Integer> getCollateralStatus(Long collateralId){
- return null;
- }
- public void setDocuments(Long caseId, List<DocumentDTO> documentDTOS){
- String documentKey = caseId +":"+ DOCUMENT_KEY;
- long now = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC);
- long timestamp = now - BEGIN_TIMESTAMP;
- int index = 1;
- List<Document> documents = new ArrayList<>();
- if(!ObjectUtils.isEmpty( documentDTOS)){
- documents = BeanUtil.copyToList(documentDTOS, Document.class);
- for (Document document : documents) {
- Long id = timestamp<<20 | index;
- if (document.getId() == null)
- document.setId(-id);
- index++;
- }
- stringRedisTemplate.opsForValue().set(documentKey, JSONUtil.toJsonStr(documents));
- }
- }
- public List<DocumentVO> getDocuments(Long caseId){
- String key = caseId +":"+ DOCUMENT_KEY;
- String json = stringRedisTemplate.opsForValue().get(key);
- //2.未命中,返回空值
- if(StrUtil.isBlank(json))
- return null;
- //3.命中,需要将json反序列化为java
- List<Document> documents = JsonUtil.toList(json, Document.class);
- return BeanUtil.copyToList(documents,DocumentVO.class);
- }
- public void delete(Long caseId){
- String contractKey = caseId +":"+ CONTRACT_KEY;
- String collateralKey = caseId +":"+ COLLATERAL_KEY;
- String contractAndCollateralKey = caseId +":"+ CONTRACT_COLLATERAL_KEY;
- String customerOtherKey = caseId +":"+ CUSTOMER_OTHER_KEY;
- stringRedisTemplate.delete(contractKey);
- stringRedisTemplate.delete(collateralKey);
- stringRedisTemplate.delete(contractAndCollateralKey);
- stringRedisTemplate.delete(customerOtherKey);
- }
- }
|