forked from gzzfw/backEnd/gz-dyh

wangwh
2024-08-28 b47d4a7accabce974e19d2a1ffb3c5c67507d74b
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
package cn.huge.module.sync.service;
 
import cn.huge.module.client.api.impl.CustClientImpl;
import cn.huge.module.sync.dao.mapper.SyncMapper;
import cn.huge.module.sync.domain.source.SourceCtAccount;
import cn.huge.module.sync.domain.source.SourceCtUnit;
import cn.huge.module.sync.domain.target.TargetCtAccount;
import cn.huge.module.sync.domain.target.TargetCtUnit;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
 
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
 
/**
 * @author liyj
 * @version 1.0.0
 * @title: 客户用户账号表业务逻辑处理
 * @Description 客户用户账号表业务逻辑处理
 * @company hugeinfo
 * @Time 2024-08-17 15:30:56
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class SyncService {
    @Autowired
    private SyncMapper syncMapper;
    @Autowired
    private CustClientImpl custClient;
 
    private static final String MYSQL_TABLE = "dyh_ct_account";
 
    public void syncData() {
        try {
            List<SourceCtAccount> sourceTableList = new ArrayList<>();
 
            int page = 1;
            int size = 1000;
 
            //查询mysql总量
            int total = syncMapper.countData(MYSQL_TABLE);
 
            //查出最大页数
            int maxPage = 1;
            maxPage = (total / size);
            if (0 != total % size) {
                maxPage += 1;
            }
 
            for (int i = 1; i <= maxPage; i++) {
                // 查询MySQL表结构
                Sort sort = Sort.by(Sort.Direction.ASC, "create_time");
                PageRequest pageRequest = PageRequest.of(page-1, size, sort);
                sourceTableList = syncMapper.pageInfo(MYSQL_TABLE, pageRequest);
                page++;
 
                List<TargetCtAccount> targetTableList = new ArrayList<>();
                for(SourceCtAccount sourceTable: sourceTableList){
                    TargetCtAccount tagetTable = new TargetCtAccount();
                    ConvertUtils.register(new DateConverter(null), java.util.Date.class);
                    BeanUtils.copyProperties(tagetTable, sourceTable);
//                    for(Field field: sourceCtUnit.getClass().getDeclaredFields()){
//                        convertType(field.get(sourceCtUnit), field.getName(), targetCtUnit);
//                    }
                    //字段名不一致,手动set
                    tagetTable.setCipher(sourceTable.getCipher());
                    tagetTable.setAccType(1);
                    targetTableList.add(tagetTable);
                }
                if(CollectionUtils.isNotEmpty(targetTableList)){
                    custClient.saveTableList1(targetTableList);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 时间转换的时候,调用初始化,时间类型转换
     *         使用:直接在实体中加上一个注解形式。
     */
    public static void initDate(){
//        时间注册
        DateConverter converter = new DateConverter(null);
        converter.setPattern(new String("yyyy-MM-dd HH:mm:ss"));
        ConvertUtils.register(converter, Date.class);
    }
 
 
    public void convertType(Object value, String columnName, TargetCtUnit targetCtUnit) {
        if (value == null) {
            targetCtUnit.setUnitType(Integer.valueOf("NULL"));
        }else if("unitType".equals(columnName) || "joinWay".equals(columnName) || "findStatus".equals(columnName)
                || "dispStatus".equals(columnName)|| "deleteStatus".equals(columnName)){
            targetCtUnit.setUnitType(Integer.parseInt((String) value));
        }
 
    }
}