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 contractVOS = loanCaseDTO.getContracts(); List 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 collateralVOS = loanCaseDTO.getCollateral(); List collateralIds = new ArrayList<>(); if(!ObjectUtils.isEmpty(collateralVOS)){ List 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 contractAndCollaterals = new ArrayList<>(); // for(int i=0 ; i < loanCaseDTO.getCollateralAndContract().size(); i++){ // Contract contract = contracts.get( i); // List 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 contractAndCollaterals = new ArrayList<>(); Map> contractSeqAndCollateralSeq = loanCaseDTO.getContractSeqAndCollateralSeq(); Iterator>> iterator = contractSeqAndCollateralSeq.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry> entry = iterator.next(); Integer contractSeq = entry.getKey(); List 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 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 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 contracts = JsonUtil.toList(json, Contract.class); return contracts; } public List 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 collaterals = JsonUtil.toList(json, Collateral.class); return BeanUtil.copyToList(collaterals, CollateralVO.class); } public List 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 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 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 getCollateralStatus(Long collateralId){ return null; } public void setDocuments(Long caseId, List documentDTOS){ String documentKey = caseId +":"+ DOCUMENT_KEY; long now = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC); long timestamp = now - BEGIN_TIMESTAMP; int index = 1; List 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 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 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); } }