package com.deloitte.system.service; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.util.ObjectUtil; import com.alibaba.fastjson.JSONArray; import com.common.core.constant.GlobalConst; import com.common.core.enums.ResultCodeEnum; import com.common.core.exception.BizException; import com.common.core.service.CryptoService; import com.common.core.utils.*; import com.common.redis.util.RedisUtil; import com.deloitte.system.model.CacheList; import com.deloitte.system.model.Contact; import com.deloitte.system.request.ContactDto; import com.deloitte.system.request.ContactSearchDto; import com.jfinal.plugin.activerecord.Db; 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 ContactService { private String tableName = "contact"; @Autowired private CryptoService cryptoService; @Autowired private IdUtils idWorker; @Autowired private RedisUtil redisUtil; public ContactDto queryForOne(String dataId) { Contact contact = Contact.dao.findById(dataId); if (!ObjectUtil.isEmpty(contact)) { return BeanHelper.copyAs(contact,ContactDto.class); } return null; } public List insertList(List contactList, String txId) { List respList = new ArrayList<>(); CacheList dtoCacheList = new CacheList<>(); dtoCacheList.setTableName(tableName); List dtoItemList = new ArrayList<>(); contactList.forEach(e -> { String dataId = idWorker.nextId(); ContactDto cacheItem = BeanHelper.copyAs(e, ContactDto.class); cacheItem.setDataId(dataId); dtoItemList.add(cacheItem); ContactDto target = BeanHelper.copyAs(e, ContactDto.class); target.setDataId(dataId); ContactDto dto = cryptoService.encryptModelColumns(e); dto.setDataId(dataId); target.setFirstNameEncrypt(dto.getFirstName()); target.setLastNameEncrypt(dto.getLastName()); target.setPostcodeEncrypt(dto.getPostcode()); target.setPostcodeDEncrypt(dto.getPostcodeD()); target.setTitleDEncrypt(dto.getTitleD()); target.setTitleEncrypt(dto.getTitle()); target.setContactEnglishNameEncrypt(dto.getContactEnglishName()); target.setEnglishAddressEncrypt(dto.getEnglishAddress()); target.setAddress1Encrypt(dto.getAddress1()); target.setAddress2Encrypt(dto.getAddress2()); target.setAddress3Encrypt(dto.getAddress3()); target.setAddress1DEncrypt(dto.getAddress1D()); target.setAddress2DEncrypt(dto.getAddress2D()); target.setAddress3DEncrypt(dto.getAddress3D()); target.setFaxEncrypt(dto.getFax()); target.setFaxDEncrypt(dto.getFaxD()); target.setEmailDEncrypt(dto.getEmailD()); target.setEmailEncrypt(dto.getEmail()); target.setMobilePhoneDEncrypt(dto.getMobilePhoneD()); target.setOtherPhoneDEncrypt(dto.getOtherPhoneD()); target.setOtherPhoneDEncrypt(dto.getOtherPhoneD()); target.setPhoneDEncrypt(dto.getPhoneD()); target.setHomePhoneEncrypt(dto.getHomePhone()); target.setMobilePhoneEncrypt(dto.getMobilePhone()); target.setOtherPhoneEncrypt(dto.getOtherPhone()); target.setPhoneEncrypt(dto.getPhone()); 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 batchInsert(List contactList) { List respList = new ArrayList<>(); CacheList dtoCacheList = new CacheList<>(); dtoCacheList.setTableName(tableName); Db.tx(() -> { for (ContactDto e : contactList) { String dataId = idWorker.nextId(); e.setDataId(dataId); Contact item= BeanHelper.copyAs(e, Contact.class); item.set("id", dataId); if (!item.save()) { throw new BizException(ResultCodeEnum.RT_ERROR); } ContactDto target = BeanHelper.copyAs(e, ContactDto.class); target.setDataId(dataId); ContactDto dto = cryptoService.encryptModelColumns(e); dto.setDataId(dataId); target.setFirstNameEncrypt(dto.getFirstName()); target.setLastNameEncrypt(dto.getLastName()); target.setPostcodeEncrypt(dto.getPostcode()); target.setPostcodeDEncrypt(dto.getPostcodeD()); target.setTitleDEncrypt(dto.getTitleD()); target.setTitleEncrypt(dto.getTitle()); target.setContactEnglishNameEncrypt(dto.getContactEnglishName()); target.setEnglishAddressEncrypt(dto.getEnglishAddress()); target.setAddress1Encrypt(dto.getAddress1()); target.setAddress2Encrypt(dto.getAddress2()); target.setAddress3Encrypt(dto.getAddress3()); target.setAddress1DEncrypt(dto.getAddress1D()); target.setAddress2DEncrypt(dto.getAddress2D()); target.setAddress3DEncrypt(dto.getAddress3D()); target.setFaxEncrypt(dto.getFax()); target.setFaxDEncrypt(dto.getFaxD()); target.setEmailDEncrypt(dto.getEmailD()); target.setEmailEncrypt(dto.getEmail()); target.setMobilePhoneDEncrypt(dto.getMobilePhoneD()); target.setOtherPhoneDEncrypt(dto.getOtherPhoneD()); target.setOtherPhoneDEncrypt(dto.getOtherPhoneD()); target.setPhoneDEncrypt(dto.getPhoneD()); target.setHomePhoneEncrypt(dto.getHomePhone()); target.setMobilePhoneEncrypt(dto.getMobilePhone()); target.setOtherPhoneEncrypt(dto.getOtherPhone()); target.setPhoneEncrypt(dto.getPhone()); DesensitiveUtils.format(target); respList.add(target); } return true; }); return respList; } public List updateList(List contactList, String txId) { List respList = new ArrayList<>(); CacheList dtoCacheList = new CacheList<>(); dtoCacheList.setTableName(tableName); List dtoItemList = new ArrayList<>(); contactList.forEach(e -> { Contact contact = Contact.dao.findById(e.getDataId()); ContactDto cacheItem = BeanHelper.copyAs(e, ContactDto.class); dtoItemList.add(cacheItem); ContactDto dto =BeanHelper.copyAs(BeanHelper.copy(cacheItem,contact),ContactDto.class); ContactDto target = BeanHelper.copyAs(dto, ContactDto.class); dto = cryptoService.encryptModelColumns(dto); target.setFirstNameEncrypt(dto.getFirstName()); target.setLastNameEncrypt(dto.getLastName()); target.setPostcodeEncrypt(dto.getPostcode()); target.setPostcodeDEncrypt(dto.getPostcodeD()); target.setTitleDEncrypt(dto.getTitleD()); target.setTitleEncrypt(dto.getTitle()); target.setContactEnglishNameEncrypt(dto.getContactEnglishName()); target.setEnglishAddressEncrypt(dto.getEnglishAddress()); target.setAddress1Encrypt(dto.getAddress1()); target.setAddress2Encrypt(dto.getAddress2()); target.setAddress3Encrypt(dto.getAddress3()); target.setAddress1DEncrypt(dto.getAddress1D()); target.setAddress2DEncrypt(dto.getAddress2D()); target.setAddress3DEncrypt(dto.getAddress3D()); target.setFaxEncrypt(dto.getFax()); target.setFaxDEncrypt(dto.getFaxD()); target.setEmailDEncrypt(dto.getEmailD()); target.setEmailEncrypt(dto.getEmail()); target.setMobilePhoneDEncrypt(dto.getMobilePhoneD()); target.setOtherPhoneDEncrypt(dto.getOtherPhoneD()); target.setPhoneDEncrypt(dto.getPhoneD()); target.setPhoneEncrypt(dto.getPhone()); target.setHomePhoneEncrypt(dto.getHomePhone()); target.setMobilePhoneEncrypt(dto.getMobilePhone()); target.setOtherPhoneEncrypt(dto.getOtherPhone()); 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) { Contact contact = Contact.dao.findById(dataId); contact.setIsDelete(1); return contact.saveOrUpdate(); } public Boolean undeleteOne(String dataId) { Contact contact = Contact.dao.findById(dataId); contact.setIsDelete(0); return contact.saveOrUpdate(); } public List searchList(ContactSearchDto searchDto) { List dataIds = searchDto.getDataIds(); String contactName = searchDto.getContactName(); StringBuilder queryParam = new StringBuilder(); if (CollectionUtil.isEmpty(dataIds) && StringUtils.isEmpty(contactName)) { //参数都为空,直接返回Null return null; } else { if(CollectionUtil.isNotEmpty(dataIds)){ queryParam.append("and id in").append(dataIds.stream().collect(Collectors.joining(", ", "(", ")"))); } if(StringUtils.isNotEmpty(contactName)) { queryParam.append("and last_name like ").append("\'%").append(contactName).append("%\'"); } List contactList = Contact.dao.findList(queryParam.toString()); return contactList.stream() .map(contact -> BeanHelper.copyAs(contact,ContactDto.class)).collect(Collectors.toList()); } } public List decryptUpdate(List contactList) { List respList =new ArrayList<>(); String[] decryptColumn =new String[]{"first_name","last_name","postcode","postcode_d","title_d","title","phone","address3_d","address2_d","address1_d","address3","address2","address1", "english_address","contact_english_name","other_phone","mobile_phone","home_phone","phone_d","other_phone_d","mobile_phone_d","email","email_d","fax_d","fax"}; Db.tx(() -> { for (ContactDto e : contactList) { Contact contact =Contact.dao.findById(e.getDataId()); if (contact == null) { throw new BizException("无效的DataId"); } Contact item=new Contact(); BeanHelper.copyEncrypt(e,item); cryptoService.decryptoColumnModel(item,decryptColumn); BeanHelper.copy(item, contact); contact.setSfRecordId(e.getSfRecordId()); if (!contact.saveOrUpdate()) { throw new BizException(ResultCodeEnum.RT_ERROR); } respList.add(e); } return true; }); return respList; } }