测试用户
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
package com.deloitte.system.service;
 
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.common.annotation.Table;
import com.common.aws.util.S3Util;
import com.common.core.constant.Constants;
import com.common.core.constant.GlobalConst;
import com.common.core.domain.BaseModel;
import com.common.core.enums.ResultCodeEnum;
import com.common.core.enums.YesNoEnum;
import com.common.core.exception.BizException;
import com.common.core.utils.DistributedLock;
import com.common.core.utils.KitClassUtils;
import com.common.core.utils.ModelUtils;
import com.common.core.utils.StringUtils;
import com.common.redis.util.RedisUtil;
import com.deloitte.system.request.TxConfirmDto;
import com.jfinal.plugin.activerecord.Db;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
@Slf4j
@Service
public class ConfirmTxService {
 
    @Autowired
    private RedisUtil redisUtil;
 
    @Autowired
    private DistributedLock distributedLock;
    @Value("${aws.s3.bucketName}")
    private String bucketName;
 
    public void confirm(TxConfirmDto dto) {
        String txid=dto.getTxId();
        String sfRecordId = dto.getSfRecordId();
        if(null== redisUtil.get(GlobalConst.PIPL_UNCONFIRM_INSERT + txid) && null == redisUtil.get(GlobalConst.PIPL_UNCONFIRM_UPDATE + txid)){
            throw new BizException("不存在的事务ID(txid)");
        }
        //遍历insert
        Object object = redisUtil.get(GlobalConst.PIPL_UNCONFIRM_INSERT + txid);
        if(object!=null && StringUtils.isNotEmpty(object.toString()) && distributedLock.getLock(GlobalConst.PIPL_UNCONFIRM_LOCK +txid,3600 * 1000)) {
            try {
                log.info(object.toString());
                if(YesNoEnum.YES.getCode().equals(dto.getIsSuccess())) {
                    List<BaseModel> models = generateModelInfo(object, sfRecordId,dto.getIdList(), true);
                    Db.tx(() -> {
                        for (BaseModel model : models) {
                            if(!model.save()) return false;
                        }
                        return true;
                    });
                }
                redisUtil.deleteKey(GlobalConst.PIPL_UNCONFIRM_INSERT + txid);
            }catch (Exception e){
                log.error(e.getMessage(), e);
                throw new BizException(ResultCodeEnum.RT_ERROR);
            }finally {
                distributedLock.releaseLock(GlobalConst.PIPL_UNCONFIRM_LOCK +txid);
            }
        }
 
        //遍历update
        object = redisUtil.get(GlobalConst.PIPL_UNCONFIRM_UPDATE + txid);
        if(object!=null && StringUtils.isNotEmpty(object.toString()) && distributedLock.getLock(GlobalConst.PIPL_UNCONFIRM_LOCK +txid,3600 * 1000)) {
            try {
                log.info(object.toString());
                if(YesNoEnum.YES.getCode().equals(dto.getIsSuccess())) {
                    List<BaseModel> models = generateModelInfo(object,sfRecordId,dto.getIdList(),false);
                    Db.tx(() -> {
                        for (BaseModel model : models) {
                            if(!model.update()) return false;
                        }
                        return true;
                    });
                }
                redisUtil.deleteKey(GlobalConst.PIPL_UNCONFIRM_UPDATE + txid);
            }catch (Exception e){
                log.error(e.getMessage(), e);
                throw new BizException(ResultCodeEnum.RT_ERROR);
            }finally {
                distributedLock.releaseLock(GlobalConst.PIPL_UNCONFIRM_LOCK +txid);
            }
        }
    }
 
    public void confirmFile(TxConfirmDto dto) {
        String txid = dto.getTxId();
        if(null== redisUtil.get(GlobalConst.PIPL_UNCONFIRM_FILE + txid)){
            throw new BizException("不存在的事务ID(txid)");
        }
        Object object = redisUtil.get(GlobalConst.PIPL_UNCONFIRM_FILE + txid);
        if(object!=null && StringUtils.isNotEmpty(object.toString()) && distributedLock.getLock(GlobalConst.PIPL_UNCONFIRM_LOCK +txid,3600 * 1000)) {
           try {
               if(YesNoEnum.NO.getCode().equals(dto.getIsSuccess())) {
                   List<String> keys = JSONArray.parseArray(object.toString(),String.class);
                   S3Util.deleteByKeys(keys,bucketName);
               }
               redisUtil.deleteKey(GlobalConst.PIPL_UNCONFIRM_FILE + txid);
           }catch (Exception e){
               log.error(e.getMessage(), e);
               throw new BizException(ResultCodeEnum.RT_ERROR);
           }finally {
               distributedLock.releaseLock(GlobalConst.PIPL_UNCONFIRM_LOCK +txid);
           }
        }
    }
 
    private List<BaseModel> generateModelInfo(Object object, String sfRecordId, List<TxConfirmDto.ConfimrListDto> idList,boolean isCreate) throws InstantiationException, IllegalAccessException {
        JSONObject jsonobject = JSONObject.parseObject(object.toString());
        JSONArray dataArray = jsonobject.getJSONArray("data");
        String table_name = jsonobject.getString("tableName");
        List<BaseModel> models = new ArrayList<>();
        Map<String,String> idMap= null;
        if(CollectionUtil.isNotEmpty(idList)){
            idMap=idList.stream().collect(Collectors.toMap(TxConfirmDto.ConfimrListDto::getAwsId, TxConfirmDto.ConfimrListDto::getSfRecordId));
        }
        for (Class<?> clazz : KitClassUtils.tableclass) {
            Table table = clazz.getAnnotation(Table.class);
            String tablename = table.tableName();
            if (tablename.equals(table_name)) {
                Class<? extends BaseModel<?>> classmodel = table.clazz();
                for (int i = 0; i < dataArray.size(); i++) {
                    JSONObject jsonObject = dataArray.getJSONObject(i);
                    BaseModel baseModel = ModelUtils.JsonToModel(jsonObject, classmodel.newInstance());
                    if(idMap!=null && CollectionUtil.isNotEmpty(idMap)){
                        baseModel.set("sf_record_id",idMap.get(baseModel.getStr("id")));
                    }else {
                        if (StrUtil.isNotBlank(sfRecordId)) {
                            baseModel.set("sf_record_id", sfRecordId);
                        }
                    }
                    if(isCreate){
                        ModelUtils.generateCommonInfo(baseModel, Constants.ACTION_CREATE);
                    }else {
                        ModelUtils.generateCommonInfo(baseModel, Constants.ACTION_UPDATE);
                    }
                    models.add(baseModel);
                }
            }
        }
        return models;
    }
}