CZT
2023-11-27 c206acfaedc69c390fb67daa81bc686f58a212ef
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
package com.ld.igds.view.service;
 
import com.bstek.bdf2.core.orm.hibernate.HibernateDao;
import com.ld.igds.common.CoreSerService;
import com.ld.igds.constant.Constant;
import com.ld.igds.models.DeviceSer;
import com.ld.igds.util.ContextUtil;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @author jiazx
 */
@Component
public class DeviceSerService extends HibernateDao {
 
    @Autowired
    private CoreSerService coreSerService;
 
    public void saveSer(DeviceSer ser) {
        Session session = null;
        try {
            if (null == ser.getNetworkType()) {
                ser.setNetworkType(Constant.NETWORK_01);
            }
            if (null == ser.getSn()) {
                ser.setSn(ser.getId());
            }
            session = this.getSessionFactory().openSession();
            if (null == ser.getCompanyId()) {
                ser.setCompanyId(ContextUtil.getCompanyId());
                session.save(ser);
            } else {
                session.update(ser);
            }
            refreshCache(ser.getCompanyId());
 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
    }
 
    public void delSerById(String id) {
        String companyId = ContextUtil.getCompanyId();
 
        String hql = "delete from " + DeviceSer.class.getName()
                + " where id = :id and companyId = :companyId";
        Session session = this.getSessionFactory().openSession();
        try {
            session.createQuery(hql).setString("id", id)
                    .setString("companyId", companyId).executeUpdate();
            coreSerService.delCache(companyId,id);
        } finally {
            session.flush();
            session.close();
        }
    }
 
    public List<DeviceSer> getAllSer(String companyId) throws Exception {
        if (null == companyId)
            companyId = ContextUtil.getCompanyId();
 
        String hql = " from " + DeviceSer.class.getName()
                + " where companyId=:companyId order by type,id + 0";
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("companyId", companyId);
 
        return this.query(hql, param);
    }
 
    public void refreshCache(String companyId) {
        try {
            List<DeviceSer> list = getAllSer(companyId);
            coreSerService.setCacheSer(list, companyId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public List<DeviceSer> getAllSerCache(String companyId) {
        return coreSerService.getCacheSerList(companyId);
    }
 
    public DeviceSer getDataById(String companyId, String id) {
        if (null == id)
            return null;
        if (null == companyId)
            companyId = ContextUtil.getCompanyId();
 
        String hql = " from " + DeviceSer.class.getName()
                + " where companyId=:companyId  and id=:id";
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("companyId", companyId);
        param.put("id", id);
        List<DeviceSer> list = this.query(hql, param);
        if (null == list)
            return null;
        return list.get(0);
    }
}