测试用户
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
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<ContactDto> insertList(List<ContactDto> contactList, String txId) {
        List<ContactDto> respList = new ArrayList<>();
        CacheList<ContactDto> dtoCacheList = new CacheList<>();
        dtoCacheList.setTableName(tableName);
        List<ContactDto> 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<ContactDto> batchInsert(List<ContactDto> contactList) {
        List<ContactDto> respList = new ArrayList<>();
        CacheList<ContactDto> 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<ContactDto> updateList(List<ContactDto> contactList, String txId) {
        List<ContactDto> respList = new ArrayList<>();
        CacheList<ContactDto> dtoCacheList = new CacheList<>();
        dtoCacheList.setTableName(tableName);
        List<ContactDto> 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<ContactDto> searchList(ContactSearchDto searchDto) {
        List<String> 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<Contact> contactList = Contact.dao.findList(queryParam.toString());
            return contactList.stream()
                    .map(contact -> BeanHelper.copyAs(contact,ContactDto.class)).collect(Collectors.toList());
        }
    }
 
    public List<ContactDto> decryptUpdate(List<ContactDto> contactList) {
        List<ContactDto> 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;
    }
 
}