czt
2024-07-13 bd01ba57ed8526a0ee94a50e1f52034251bb54f6
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
package com.ld.igds.sys.service;
 
import com.bstek.bdf2.core.orm.hibernate.HibernateDao;
import com.bstek.dorado.data.provider.Page;
import com.bstek.dorado.util.Assert;
import com.ld.igds.constant.Constant;
import com.ld.igds.constant.RedisConst;
import com.ld.igds.models.DicTrigger;
import com.ld.igds.util.ContextUtil;
import com.ld.igds.util.RedisUtil;
 
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 *
 */
@Component(DicTriggerService.BEAN_ID)
public class DicTriggerServiceImpl extends HibernateDao implements
        DicTriggerService {
 
    @Autowired
    private RedisUtil redisUtil;
 
    @Override
    public List<DicTrigger> findByParentCode(String companyId,
            String parentCode, boolean hasDisabled) {
        String hql = " from " + DicTrigger.class.getName()
                + " where parentCode=:parentCode and companyId=:companyId ";
        Map<String, Object> m = new HashMap<>();
        if (!hasDisabled) {
            hql += " and disabledTag = :disabledTag ";
            m.put("disabledTag", Constant.YN_Y);
        }
 
        m.put("parentCode", parentCode);
        m.put("companyId", companyId);
 
        return this.query(hql, m);
    }
 
    public List<DicTrigger> findAll(String flag, String companyId) {
        if (null == companyId) {
            companyId = ContextUtil.getCompanyId();
        }
        String hql = " from " + DicTrigger.class.getName()
                + " where  companyId=:companyId ";
        Map<String, Object> m = new HashMap<>();
        if (null != flag) {
            hql += " and disabledTag = :disabledTag ";
            m.put("disabledTag", flag);
        }
        m.put("companyId", companyId);
 
        hql += "order By code";
        return this.query(hql, m);
    }
 
    @Override
    public List<DicTrigger> findAllParent(String companyId) {
        String hql = "from " + DicTrigger.class.getName()
                + " where parentCode =:parentCode and companyId = :companyId";
        Map<String, Object> parameterMap = new HashMap<String, Object>();
        parameterMap.put("parentCode", Constant.DEFAULT_PARENT_CODE);
        parameterMap.put("companyId", companyId);
        return this.query(hql, parameterMap);
    }
 
    @Override
    public void saveNode(DicTrigger dic) {
        this.getSession().save(dic);
        // 刷新缓存
        this.refreshCache(null);
    }
 
    @Override
    public void updateNode(DicTrigger dic) {
        this.getSession().saveOrUpdate(dic);
        // 刷新缓存
        this.refreshCache(null);
    }
 
    @Override
    public void deleteNode(DicTrigger dic) {
        this.getSession().delete(dic);
        // 刷新缓存
        this.refreshCache(null);
    }
 
    @Override
    public DicTrigger findByCode(String companyId, String code,
            String parentCode) {
        Assert.notNull(code, "字典编码没有获取到");
        Assert.notNull(parentCode, "字典父编码没有获取到");
 
        String hql = "from "
                + DicTrigger.class.getName()
                + " where code = :code and parentCode=:parentCode and companyId = :companyId";
        Map<String, Object> parameterMap = new HashMap<String, Object>();
        parameterMap.put("code", code);
        parameterMap.put("parentCode", parentCode);
        parameterMap.put("companyId", companyId);
        List<DicTrigger> list = this.query(hql, parameterMap);
        if (null != list) {
            return list.get(0);
        }
        return null;
    }
 
    @Override
    public String delByCode(String companyId, String code, String parentCode) {
        DicTrigger dic = findByCode(companyId, code, parentCode);
        if (null != dic) {
            this.deleteNode(dic);
        }
        return null;
    }
 
    @Override
    public void refreshCache(String companyId) {
        if (null == companyId) {
            companyId = ContextUtil.getDefaultCompanyId();
        }
        List<DicTrigger> list = findAll(Constant.YN_N, companyId);
 
        if (null == list || list.isEmpty())
            return;
 
        Map<String, List<DicTrigger>> map = new HashMap<>();
        // 将部分字字典表存在MAP中,
        for (DicTrigger dic : list) {
            if (Constant.TRIGGER_PARENT_FOOD_VARIETY
                    .equals(dic.getParentCode())) {
                DicTrigger.mapFoodVariety.put(dic.getCode(), dic.getName());
            }
 
            if (Constant.TRIGGER_PARENT_FOOD_TYPE.equals(dic.getParentCode())) {
                DicTrigger.mapFoodType.put(dic.getCode(), dic.getName());
            }
 
            if (Constant.TRIGGER_PARENT_PROCESS_STATUS.equals(dic
                    .getParentCode())) {
                DicTrigger.mapProgressName.put(dic.getCode(), dic.getName());
            }
 
            // 封装MAP
            if (null == map.get(dic.getParentCode()))
                map.put(dic.getParentCode(), new ArrayList<>());
 
            map.get(dic.getParentCode()).add(dic);
        }
 
        // 将MAP转换到缓存中
        String cacheKey;
        for (String key : map.keySet()) {
            cacheKey = RedisConst.buildKey(companyId,
                    RedisConst.KEY_DIC_TRIGGER, key);
            redisUtil.set(cacheKey, map.get(key));
        }
 
    }
 
    @SuppressWarnings("unchecked")
    @Override
    public List<DicTrigger> getCacheDicByParent(String companyId,
            String parentCode) {
 
        String cacheKey = RedisConst.buildKey(companyId,
                RedisConst.KEY_DIC_TRIGGER, parentCode);
 
        return (List<DicTrigger>) redisUtil.get(cacheKey);
    }
 
    @Override
    public List<DicTrigger> queryDicByKey(Map<String, Object> parameter) {
 
        String hql = " from " + DicTrigger.class.getName()
                + " where companyId=:companyId";
 
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("companyId", ContextUtil.getCompanyId());
 
        if (null != parameter) {
 
            String str = (String) parameter.get("parentCode");
            if (StringUtils.isNotEmpty(str)) {
                hql += " and parentCode =:parentCode ";
                args.put("parentCode", str);
            }
            str = (String) parameter.get("key");
            if (StringUtils.isNotEmpty(str)) {
                hql += " and (name like :name or code like:code) ";
                args.put("name", "%" + str + "%");
                args.put("code", "%" + str + "%");
            }
        }
 
        hql += " order by code ";
        return this.query(hql, args);
    }
    @Override
    public String copy2Target(String sourceId, String targetId) {
 
        if (sourceId.equals(targetId))
            return null;
 
        List<DicTrigger> list = this.findAll(null, targetId);
 
        if (null != list && list.size() > 0)
            return null;
 
        list = this.findAll(null, sourceId);
 
        if (null == list || list.isEmpty())
            return null;
 
        Session session = this.getSessionFactory().openSession();
        try {
            for (DicTrigger data : list) {
                data.setCompanyId(targetId);
                session.save(data);
            }
        } finally {
            session.flush();
            session.close();
        }
        return null;
    }
 
}