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.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.Opportunity; import com.deloitte.system.model.Quotes; import com.deloitte.system.request.OpportunityDto; import com.deloitte.system.request.QuotesDto; 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 OpportunityService { private String tableName = "opportunity"; @Autowired private CryptoService cryptoService; @Autowired private IdUtils idWorker; @Autowired private RedisUtil redisUtil; public OpportunityDto queryForOne(String dataId) { Opportunity opportunity = Opportunity.dao.findById(dataId); if (!ObjectUtil.isEmpty(opportunity)) { return BeanHelper.copyAs(opportunity,OpportunityDto.class); } return null; } public List insertList(List opportunityList, String txId) { List respList = new ArrayList<>(); CacheList dtoCacheList = new CacheList<>(); dtoCacheList.setTableName(tableName); List dtoItemList = new ArrayList<>(); opportunityList.forEach(e -> { String dataId = idWorker.nextId(); OpportunityDto cacheItem = BeanHelper.copyAs(e, OpportunityDto.class); cacheItem.setDataId(dataId); dtoItemList.add(cacheItem); OpportunityDto target = BeanHelper.copyAs(e, OpportunityDto.class); target.setDataId(dataId); OpportunityDto dto = cryptoService.encryptModelColumns(e); dto.setDataId(dataId); target.setDealerServiceDEncrypt(dto.getDealerServiceD()); target.setDealerServiceEncrypt(dto.getDealerService()); target.setDealerSalesStaffNameDEncrypt(dto.getDealerSalesStaffNameD()); target.setDealerSalesStaffNameEncrypt(dto.getDealerSalesStaffName()); target.setExpectedDeliveryDateEncrypt(dto.getExpectedDeliveryDate()); target.setSalesAccountCodeEncrypt(dto.getSalesAccountCode()); target.setSpecialDeliveryAddressEncrypt(dto.getSpecialDeliveryAddress()); 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 opportunityList, String txId) { List respList = new ArrayList<>(); CacheList dtoCacheList = new CacheList<>(); dtoCacheList.setTableName(tableName); List dtoItemList = new ArrayList<>(); opportunityList.forEach(e -> { Opportunity opportunity = Opportunity.dao.findById(e.getDataId()); OpportunityDto cacheItem = BeanHelper.copyAs(e, OpportunityDto.class); dtoItemList.add(cacheItem); OpportunityDto dto = BeanHelper.copyAs(BeanHelper.copy(cacheItem,opportunity),OpportunityDto.class); OpportunityDto target = BeanHelper.copyAs(dto, OpportunityDto.class); dto = cryptoService.encryptModelColumns(dto); target.setDealerServiceDEncrypt(dto.getDealerServiceD()); target.setDealerServiceEncrypt(dto.getDealerService()); target.setDealerSalesStaffNameDEncrypt(dto.getDealerSalesStaffNameD()); target.setDealerSalesStaffNameEncrypt(dto.getDealerSalesStaffName()); target.setExpectedDeliveryDateEncrypt(dto.getExpectedDeliveryDate()); target.setSalesAccountCodeEncrypt(dto.getSalesAccountCode()); target.setSpecialDeliveryAddressEncrypt(dto.getSpecialDeliveryAddress()); 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) { Opportunity opportunity = Opportunity.dao.findById(dataId); opportunity.setIsDelete(1); return opportunity.saveOrUpdate(); } public Boolean undeleteOne(String dataId) { Opportunity opportunity = Opportunity.dao.findById(dataId); opportunity.setIsDelete(0); return opportunity.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 = Opportunity.dao.findList(queryParam.toString()); return quotesList.stream() .map(quotes -> BeanHelper.copyAs(quotes, OpportunityDto.class)).collect(Collectors.toList()); } public List decryptUpdate(List opportunityList) { List respList =new ArrayList<>(); String[] decryptColumn =new String[]{"expected_delivery_date","sales_account_code","special_delivery_address","dealer_service_d","dealer_service","dealer_sales_staff_name_d","dealer_sales_staff_name"}; Db.tx(() -> { for (OpportunityDto e : opportunityList) { Opportunity opportunity =Opportunity.dao.findById(e.getDataId()); if (opportunity == null) { throw new BizException("无效的DataId"); } Opportunity item=new Opportunity(); BeanHelper.copyEncrypt(e, item); cryptoService.decryptoColumnModel(item,decryptColumn); BeanHelper.copy(item, opportunity); opportunity.setSfRecordId(e.getSfRecordId()); if (!opportunity.saveOrUpdate()) { throw new BizException(ResultCodeEnum.RT_ERROR); } respList.add(e); } return true; }); return respList; } public List decryptInsert(List opportunityList) { List respList =new ArrayList<>(); Db.tx(() -> { for (OpportunityDto e : opportunityList) { Opportunity opportunity = new Opportunity(); String dataId =idWorker.nextId(); BeanHelper.copyEncrypt(e, opportunity); cryptoService.decryptoModel(opportunity); opportunity.setSfRecordId(e.getSfRecordId()); opportunity.setDataId(Long.valueOf(dataId)); if(!opportunity.save()) { throw new BizException(ResultCodeEnum.RT_ERROR); } e.setDataId(dataId); respList.add(e); } return true; }); return respList; } }