jinlin
2023-11-16 6af5b3def3f511a19c7ccff6f223ef239b178d48
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
package com.zt.life.sync.service;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zt.common.db.query.QueryFilter;
import com.zt.common.service.BaseService;
import com.zt.life.sync.dao.SyncConfigDao;
import com.zt.life.sync.dto.SyncConfigDto;
import com.zt.life.sync.model.SyncConfig;
import com.zt.life.sync.model.SyncConfigTables;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 *
 *
 * @author zt czx
 * @since 1.0.0 2020-07-20
 */
@Service
public class SyncConfigService extends BaseService<SyncConfigDao, SyncConfig> {
    @Autowired
    private SyncConfigTableService syncConfigTableService;
 
    public List<SyncConfigDto> page(QueryFilter queryFilter) {
        List<SyncConfigDto> resultList = new ArrayList<>();
        // 获取查询结果
        List<SyncConfig> syncConfigList = query(queryFilter);
        resultList = JSONArray.parseArray(JSONArray.toJSONString(syncConfigList), SyncConfigDto.class);
        // 依赖项ID集合
        List<Long> ids = new ArrayList<>();
        resultList.stream().map(SyncConfig::getOtherCond).distinct().forEach(s -> {
            if (StrUtil.isNotEmpty(s)) {
                ids.add(Long.parseLong(s));
            }
        });
        if (CollUtil.isNotEmpty(ids)) {
            // 所有ID结果
            List<SyncConfig> syncConfigIds = baseDao.selectBatchIds(ids);
            resultList.stream().forEach(s -> {
                SyncConfigDto syncConfigDto = (SyncConfigDto) s;
                for (SyncConfig ss : syncConfigIds) {
                    if (StrUtil.isNotEmpty(s.getOtherCond()) && Long.parseLong(s.getOtherCond()) == ss.getId()) {
                        syncConfigDto.setOtherCondDesc(ss.getTabledesc());
                    }
                }
            });
        }
        return resultList;
    }
 
    public List<SyncConfig> getList(Map<String, Object> map) {
        return baseDao.selectByMap(map);
    }
 
    public List<String> getTableNameList() {
        List<String> tableNameList = this.baseDao.getTableNameList();
        return tableNameList;
    }
 
    public void insert(SyncConfigDto syncConfigDto) {
        SyncConfig syncConfig = new SyncConfig();
        syncConfig = JSONObject.toJavaObject(JSONObject.parseObject(JSONObject.toJSONString(syncConfigDto)),
                SyncConfig.class);
        // 新增配置
        super.insert(syncConfig);
        for (SyncConfigTables syt : syncConfigDto.getSyncConfigTableList()) {
            syt.setSyncConfigId(syncConfig.getId());
            syncConfigTableService.insert(syt);
        }
    }
 
    public void update(SyncConfigDto syncConfigDto) {
        SyncConfig syncConfig = new SyncConfig();
        syncConfig = JSONObject.toJavaObject(JSONObject.parseObject(JSONObject.toJSONString(syncConfigDto)),
                SyncConfig.class);
        // 新增配置
        super.update(syncConfig);
        // 现有集合
        Map<String, Object> parMap = new HashMap<>();
        parMap.put("sync_config_id", syncConfig.getId());
        List<SyncConfigTables> syncConfigTables = syncConfigTableService.getList(parMap);
        System.out.println("前端提交数据");
        syncConfigDto.getSyncConfigTableList().forEach(System.out::println);
        System.out.println("数据库保存的数据");
        syncConfigTables.forEach(System.out::println);
        // 清空历史数据
        syncConfigTableService.deleteByMap(parMap);
        for (SyncConfigTables sct : syncConfigDto.getSyncConfigTableList()) {
            sct.setSyncConfigId(syncConfig.getId());
            // 保存新数据
            syncConfigTableService.insert(sct);
        }
    }
}