package com.deloitte.system.service; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.ObjectUtil; import com.alibaba.fastjson.JSONArray; import com.common.core.constant.GlobalConst; import com.common.core.exception.BizException; import com.common.core.service.CryptoService; import com.common.core.utils.BeanHelper; import com.common.core.utils.DesensitiveUtils; import com.common.core.utils.IdUtils; import com.common.redis.util.RedisUtil; import com.deloitte.system.model.CacheList; import com.deloitte.system.model.Quotes; import com.deloitte.system.request.QuotesDto; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import static com.common.core.constant.GlobalConst.PIPL_UNCONFIRM_INSERT; import static com.common.core.constant.GlobalConst.PIPL_UNCONFIRM_UPDATE; @Service public class QuotesService { private String tableName="quotes"; @Autowired private CryptoService cryptoService; @Autowired private IdUtils idWorker; @Autowired private RedisUtil redisUtil; public QuotesDto queryForOne(String dataId) { Quotes quotes =Quotes.dao.findById(dataId); if (!ObjectUtil.isEmpty(quotes)){ QuotesDto dto = BeanHelper.copyAs(quotes, QuotesDto.class); return dto; } return null; } public List insertList(List quotesDtoList, String txId) { List respList = new ArrayList<>(); CacheList dtoCacheList = new CacheList<>(); dtoCacheList.setTableName(tableName); List dtoItemList = new ArrayList<>(); quotesDtoList.forEach(e -> { String dataId = idWorker.nextId(); QuotesDto cacheItem = BeanHelper.copyAs(e, QuotesDto.class); cacheItem.setDataId(dataId); dtoItemList.add(cacheItem); QuotesDto target = BeanHelper.copyAs(e, QuotesDto.class); target.setDataId(dataId); QuotesDto dto = cryptoService.encryptModelColumns(e); dto.setDataId(dataId); target.setAuthorEncrypt(dto.getAuthor()); target.setMessageEncrypt(dto.getMessage()); target.setContactEmailEncrypt(dto.getContactEmail()); target.setContactFaxEncrypt(dto.getContactFax()); target.setContactNameEncrypt(dto.getContactName()); target.setContactPhoneEncrypt(dto.getContactPhone()); target.setPrimaryRecipientEncrypt(dto.getPrimaryRecipient()); target.setShipToEncrypt(dto.getShipTo()); target.setBillToEncrypt(dto.getBillTo()); DesensitiveUtils.format(target); respList.add(target); }); dtoCacheList.setData(dtoItemList); String insertJsonString = JSONArray.toJSONString(dtoCacheList); String insertKey = PIPL_UNCONFIRM_INSERT + txId; boolean redisFlag = redisUtil.set(insertKey, insertJsonString, GlobalConst.DATA_EXPIRE); if (!redisFlag){ throw new BizException("缓存写入失败"); } return respList; } public List updateList(List quotesDtoList, String txId) { List respList = new ArrayList<>(); CacheList dtoCacheList = new CacheList<>(); dtoCacheList.setTableName(tableName); List dtoItemList = new ArrayList<>(); quotesDtoList.forEach(e -> { Quotes quotes = Quotes.dao.findById(e.getDataId()); QuotesDto cacheItem = BeanHelper.copyAs(e, QuotesDto.class); dtoItemList.add(cacheItem); QuotesDto dto = BeanHelper.copyAs(BeanHelper.copy(cacheItem,quotes),QuotesDto.class); QuotesDto target = BeanHelper.copyAs(dto, QuotesDto.class); cryptoService.encryptModelColumns(dto); target.setAuthorEncrypt(dto.getAuthor()); target.setMessageEncrypt(dto.getMessage()); target.setContactEmailEncrypt(dto.getContactEmail()); target.setContactFaxEncrypt(dto.getContactFax()); target.setContactNameEncrypt(dto.getContactName()); target.setContactPhoneEncrypt(dto.getContactPhone()); target.setPrimaryRecipientEncrypt(dto.getPrimaryRecipient()); target.setShipToEncrypt(dto.getShipTo()); target.setBillToEncrypt(dto.getBillTo()); DesensitiveUtils.format(target); respList.add(target); }); dtoCacheList.setData(dtoItemList); String updateJsonString = JSONArray.toJSONString(dtoCacheList); String updateKey = PIPL_UNCONFIRM_UPDATE + txId; boolean redisFlag = redisUtil.set(updateKey, updateJsonString,GlobalConst.DATA_EXPIRE); if (!redisFlag){ throw new BizException("缓存写入失败"); } return respList; } public Boolean deleteOne(String dataId) { Quotes quotes = Quotes.dao.findById(dataId); quotes.setIsDelete(1); return quotes.saveOrUpdate(); } public Boolean undeleteOne(String dataId) { Quotes quotes = Quotes.dao.findById(dataId); quotes.setIsDelete(0); return quotes.saveOrUpdate(); } public List search(List idList){ if (CollUtil.isEmpty(idList)) { return null; } StringBuilder queryParam = new StringBuilder(); queryParam.append("and id in ").append(idList.stream().collect(Collectors.joining(", ", "(", ")"))); List quotesList = Quotes.dao.findList(queryParam.toString()); return quotesList.stream() .map(quotes -> BeanHelper.copyAs(quotes, QuotesDto.class)).collect(Collectors.toList()); } }