DataDaoVo.java 4 KB
Newer Older
yanzg's avatar
yanzg committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
package com.yanzuoguang.util.vo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据库操作对象
 *
 * @param <T>
 * @author 颜佐光
 */
public class DataDaoVo<T> {
yanzg's avatar
yanzg committed
15 16 17 18
    private final List<T> creates = new ArrayList<>();
    private final List<T> updates = new ArrayList<>();
    private final List<T> removes = new ArrayList<>();
    private final Map<String, T> mapNow = new HashMap<>();
yanzg's avatar
yanzg committed
19 20

    /**
yanzg's avatar
yanzg committed
21
     * 将历史数据映射为HashMap
yanzg's avatar
yanzg committed
22
     *
yanzg's avatar
yanzg committed
23 24 25
     * @param hisitories 历史数据
     * @param keyFunc     获取主键函数
     * @return
yanzg's avatar
yanzg committed
26
     */
yanzg's avatar
yanzg committed
27
    private Map<String, T> getMapHistory(List<T> hisitories, DataDaoKey<T> keyFunc) {
yanzg's avatar
yanzg committed
28 29
        // 定义缓存集合
        Map<String, T> mapHistory = new HashMap<>(10);
yanzg's avatar
yanzg committed
30
        // 历史数据处理
yanzg's avatar
yanzg committed
31 32 33
        if (hisitories != null) {
            for (T his : hisitories) {
                String key = keyFunc.getKey(his);
yanzg's avatar
yanzg committed
34
                mapHistory.put(key, his);
yanzg's avatar
yanzg committed
35
            }
yanzg's avatar
yanzg committed
36
        }
yanzg's avatar
yanzg committed
37 38 39 40 41 42 43 44 45 46 47
        return mapHistory;
    }

    /**
     * 获取结果
     *
     * @param mapHistory 历史数据映射
     * @param nows        当前数据
     * @param keyFunc     获取主键函数
     * @return
     */
yanzg's avatar
yanzg committed
48
    private void addResult(Map<String, T> mapHistory, List<T> nows, DataDaoKey<T> keyFunc) {
yanzg's avatar
yanzg committed
49
        // 返回集
yanzg's avatar
yanzg committed
50 51 52 53 54
        if (nows != null) {
            for (T now : nows) {
                String key = keyFunc.getKey(now);
                T his = mapHistory.get(key);
                if (his == null) {
yanzg's avatar
yanzg committed
55 56
                    this.creates.add(now);
                    this.mapNow.put(key, now);
yanzg's avatar
yanzg committed
57
                } else {
yanzg's avatar
yanzg committed
58
                    mapHistory.remove(key);
59 60
                    boolean isChange = keyFunc.set(his, now);
                    if (isChange) {
yanzg's avatar
yanzg committed
61
                        this.updates.add(his);
62
                    }
yanzg's avatar
yanzg committed
63
                    this.mapNow.put(key, his);
yanzg's avatar
yanzg committed
64
                }
yanzg's avatar
yanzg committed
65 66
            }
        }
yanzg's avatar
yanzg committed
67
        this.removes.addAll(mapHistory.values());
yanzg's avatar
yanzg committed
68
    }
yanzg's avatar
yanzg committed
69

yanzg's avatar
yanzg committed
70

yanzg's avatar
yanzg committed
71 72 73 74 75 76 77
    /**
     * 初始话对象
     *
     * @param hisitories
     * @param nows
     * @param keyFunc
     */
yanzg's avatar
yanzg committed
78
    public void add(List<T> hisitories, List<T> nows, DataDaoKey<T> keyFunc) {
yanzg's avatar
yanzg committed
79
        Map<String, T> mapHistory = getMapHistory(hisitories, keyFunc);
yanzg's avatar
yanzg committed
80
        addResult(mapHistory, nows, keyFunc);
yanzg's avatar
yanzg committed
81 82
    }

yanzg's avatar
yanzg committed
83

yanzg's avatar
yanzg committed
84 85 86 87 88 89 90
    /**
     * 初始话对象
     *
     * @param hisitories
     * @param nows
     * @param keyFunc
     */
yanzg's avatar
yanzg committed
91
    public <M> void add(List<T> hisitories, List<M> nows, DataDaoKeyConvert<T, M> keyFunc) {
yanzg's avatar
yanzg committed
92
        Map<String, T> mapHistory = getMapHistory(hisitories, keyFunc);
yanzg's avatar
yanzg committed
93 94 95
        List<T> toNows = new ArrayList<>(10);
        if (nows != null) {
            for (M m : nows) {
yanzg's avatar
yanzg committed
96
                T t = keyFunc.convert(mapHistory, m);
yanzg's avatar
yanzg committed
97 98 99
                toNows.add(t);
            }
        }
yanzg's avatar
yanzg committed
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
        addResult(mapHistory, toNows, keyFunc);
    }

    /**
     * 初始话对象
     *
     * @param hisitories
     * @param nows
     * @param keyFunc
     */
    public static <T> DataDaoVo<T> init(List<T> hisitories, List<T> nows, DataDaoKey<T> keyFunc) {
        DataDaoVo<T> res = new DataDaoVo<T>();
        res.add(hisitories, nows, keyFunc);
        return res;
    }


    /**
     * 初始话对象
     *
     * @param hisitories
     * @param nows
     * @param keyFunc
     */
    public static <T, M> DataDaoVo<T> init(List<T> hisitories, List<M> nows, DataDaoKeyConvert<T, M> keyFunc) {
        DataDaoVo<T> res = new DataDaoVo<T>();
        res.add(hisitories, nows, keyFunc);
yanzg's avatar
yanzg committed
127
        return res;
yanzg's avatar
yanzg committed
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
    }

    /**
     * 需要创建的对象
     *
     * @return
     */
    public List<T> getCreates() {
        return creates;
    }

    /**
     * 需要修改的对象
     *
     * @return
     */
    public List<T> getUpdates() {
        return updates;
    }

    /**
     * 需要删除的对象
     *
     * @return
     */
    public List<T> getRemoves() {
        return removes;
    }

    /**
     * 所有需要修改和更新的对象
     *
     * @return
     */
    public Map<String, T> getMapNow() {
        return mapNow;
    }
}