测试用户
2023-04-13 43393f2bb11cbf9e6af40077bbc5284660e8a754
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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<QuotesDto> insertList(List<QuotesDto> quotesDtoList, String txId) {
        List<QuotesDto> respList = new ArrayList<>();
        CacheList<QuotesDto> dtoCacheList = new CacheList<>();
        dtoCacheList.setTableName(tableName);
        List<QuotesDto> dtoItemList = new ArrayList<>();
        quotesDtoList.forEach(-> {
            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<QuotesDto> updateList(List<QuotesDto> quotesDtoList, String txId) {
        List<QuotesDto> respList = new ArrayList<>();
        CacheList<QuotesDto> dtoCacheList = new CacheList<>();
        dtoCacheList.setTableName(tableName);
        List<QuotesDto> dtoItemList = new ArrayList<>();
        quotesDtoList.forEach(-> {
            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<QuotesDto> search(List<String> idList){
        if (CollUtil.isEmpty(idList)) {
            return null;
        }
        StringBuilder queryParam = new StringBuilder();
        queryParam.append("and id in ").append(idList.stream().collect(Collectors.joining(", ", "(", ")")));
        List<Quotes> quotesList = Quotes.dao.findList(queryParam.toString());
        return quotesList.stream()
                .map(quotes -> BeanHelper.copyAs(quotes, QuotesDto.class)).collect(Collectors.toList());
 
    }
}