jiazx0107@163.com
2023-05-17 620eab6cca2bc9ef9ea6d3067a0a5ba1deadbd1c
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
package com.ld.igds.view.service;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import com.bstek.bdf2.core.orm.hibernate.HibernateDao;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.ld.igds.constant.RedisConst;
import com.ld.igds.models.Building;
import com.ld.igds.util.ContextUtil;
import com.ld.igds.util.RedisUtil;
 
/**
 * 
 * @author jiazx
 *
 */
@Component
public class BuildingService extends HibernateDao {
 
    @Autowired
    private RedisUtil redisUtil;
 
    public List<Building> loadData(Map<String, Object> parameter) {
 
        String hql = " from " + Building.class.getName()
                + " where companyId=:companyId ";
        if (null == parameter) {
            parameter = new HashMap<String, Object>();
        }
        
        String companyId = (String) parameter.get("companyId");
        if (StringUtils.isEmpty(companyId)) {
            companyId = ContextUtil.getCompanyId();
        }
        parameter.put("companyId", companyId);
        
        String deptId = (String) parameter.get("deptId");
        if(StringUtils.isNoneEmpty(deptId)){
            hql += " and deptId=:deptId ";
            parameter.put("deptId", deptId);
        }
        
        hql += " order by id + 0";
        return this.query(hql, parameter);
    }
 
    public String saveOrUpdate(Building data) {
 
        Session session = this.getSessionFactory().openSession();
        try {
            if(null == data.getDeptId()){
                data.setDeptId(ContextUtil.subDeptId(null));
            }
            data.setCompanyId(ContextUtil.getCompanyId());
            session.saveOrUpdate(data);
            refreshCache(data.getCompanyId());
        } finally {
            session.flush();
            session.close();
        }
        return null;
    }
 
    public String delData(Building data) {
        Session session = this.getSessionFactory().openSession();
        try {
            session.delete(data);
        } finally {
            session.flush();
            session.close();
        }
        return null;
    }
 
    public String saveAll(List<Building> items) {
        Session session = this.getSessionFactory().openSession();
        try {
            EntityState state;
            for (Building item : items) {
                state = EntityUtils.getState(item);
                if (state == EntityState.NEW) {
                    item.setCompanyId(ContextUtil.getCompanyId());
                    if(null == item.getDeptId()){
                        item.setDeptId(ContextUtil.subDeptId(null));
                    }
                    session.save(item);
                }
                if (state == EntityState.DELETED) {
                    session.delete(item);
                }
                if (state == EntityState.MODIFIED) {
                    session.update(item);
                }
            }
            
            refreshCache(items.get(0).getCompanyId());
        } finally {
            session.flush();
            session.close();
        }
        return "执行完成";
    }
 
    @SuppressWarnings("unchecked")
    public List<Building> getCacheBuilding(String companyId,String deptId) {
        if(StringUtils.isEmpty(companyId)){
            companyId = ContextUtil.getCompanyId();
        }
 
        String key = RedisConst.buildKey(companyId, RedisConst.KEY_BUILDING_LIST);
 
        List<Building> result = (List<Building>) redisUtil.get(key);
 
        if (null == result) {
            refreshCache(companyId);
            
            return null;
        }
 
        if(null == deptId) return result;
        
        return result.stream().filter(item -> item.getDeptId().equals(deptId))
                .collect(Collectors.toList());
    }
    
    
    public void refreshCache(String companyId) {
        Map<String, Object> parameter = new HashMap<String, Object>();
        parameter.put("companyId", companyId);
        List<Building> list = this.loadData(parameter);
        
        redisUtil.set(RedisConst.buildKey(companyId, RedisConst.KEY_BUILDING_LIST),list);
    }
}