CZT
2023-09-06 71c4fa1e27f75ae4b765c95c67a3069c84dc72ba
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
238
239
package com.ld.igds.drug.service;
 
import java.util.*;
 
import com.bstek.bdf2.core.model.DefaultDept;
import com.bstek.bdf2.core.orm.hibernate.HibernateDao;
 
import com.bstek.dorado.data.provider.Page;
import com.ld.igds.constant.RedisConst;
import com.ld.igds.models.Drug;
import com.ld.igds.models.DrugApply;
import com.ld.igds.models.DrugInout;
import com.ld.igds.sys.service.SysDeptService;
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.Service;
 
/**
 * 药剂管理
 *
 * @author Andy
 */
@Service
public class HDrugService extends HibernateDao {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private SysDeptService sysDeptService;
 
    /**
     * 缓存 药剂ID
     */
    public static final String CACHE_DRUG_ID = "DRUG_ID";
 
    /**
     * 分页查询药剂信息
     *
     * @param page
     * @param param
     * @throws Exception
     */
    public void pageDrug(Page<Drug> page, Map<String, Object> param) throws Exception {
        StringBuffer hql = new StringBuffer();
        Map<String, Object> args = new HashMap<>();
        hql.append(" from " + Drug.class.getName() + " where companyId=:companyId");
        args.put("companyId", ContextUtil.getCompanyId());
        if (null != param) {
            buildHql(hql, param, args);
        }
        hql.append(" order by id ");
        String countHql = "select count(*) " + hql.toString();
        this.pagingQuery(page, String.valueOf(hql), countHql, args);
    }
 
    /**
     * 获取药剂列表
     *
     * @param companyId
     * @return
     */
    public List<Drug> listDrug(String companyId, Map<String, Object> param) {
        StringBuffer hql = new StringBuffer(" from " + Drug.class.getName() + " where companyId=:companyId");
        Map<String, Object> args = new HashMap<>();
        args.put("companyId", companyId);
        if (null != param) {
            buildHql(hql, param, args);
        }
        hql.append(" order by id desc");
        return this.query(hql.toString(), args);
 
    }
 
    /**
     * 更新或添加药剂信息
     *
     * @param data
     * @return
     */
    public String saveDrug(Drug data) {
        if (null == data.getCompanyId()) {
            data.setCompanyId(ContextUtil.getCompanyId());
        }
        if (null == data.getDeptId()) {
            data.setDeptId(ContextUtil.subDeptId(null));
        }
        DefaultDept dept = sysDeptService.getDeptById(data.getDeptId());
        data.setDeptName(dept.getName());
        Session session = this.getSessionFactory().openSession();
        data.setUpdateTime(new Date());
        data.setUpdateUser(ContextUtil.getLoginUserCName());
        try {
            if (null == data.getId()) {
                String id = createId(data.getCompanyId(), data.getDeptId());
                data.setId(id);
                session.save(data);
            } else {
                session.saveOrUpdate(data);
            }
 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
        return null;
    }
 
    /**
     * 删除药剂信息
     *
     * @param data
     * @return
     */
    public String delDrug(Drug data) {
        Session session = this.getSessionFactory().openSession();
        try {
            if (null != data.getId()) {
                session.delete(data);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
        return null;
    }
 
 
    public void pageDrugInout(Page<DrugInout> page, Map<String, Object> param) {
 
    }
 
    public String delDrugInout(DrugInout data) {
 
        return null;
    }
 
    public String updateDrugInout(DrugInout data) {
 
        return null;
    }
 
    private String addDrugInout(DrugInout data) {
        return null;
    }
 
 
    public String auditDrugApply(DrugApply data) {
 
        if (null == data.getAuditUser()) {
            data.setAuditUser(ContextUtil.getLoginUserCName());
        }
        if (null == data.getAuditTime()) {
            data.setApplyTime(new Date());
        }
 
        if (data.getApplyUser().equals(data.getAuditUser())) {
            return "系统:审核人和申请人不可为同一个人。";
        }
 
        return updateDrugApply(data);
 
    }
 
    public String updateDrugApply(DrugApply data) {
 
        return null;
    }
 
    private String addDrugApply(DrugApply data) {
        return null;
    }
 
    public String delDrugApply(DrugApply data) {
        return null;
    }
 
    public void pageDrugApply(Page<DrugApply> page, Map<String, Object> param) {
 
    }
 
    public String createId(String companyId, String deptId) {
 
        // 从缓存中获取已有的组织编码
        String cacheKey = RedisConst.buildKey(companyId, HDrugService.CACHE_DRUG_ID);
 
        String cacheId = (String) redisUtil.get(cacheKey);
 
        if (null != cacheId && cacheId.indexOf(deptId) >= 0) {
            String temp = cacheId.substring(cacheId.length() - 3);
            Integer i = Integer.valueOf(temp);
            cacheId = deptId + String.format("%03d", ++i);
        } else {
            List<Drug> result = listDrug(companyId, null);
            if (null == result || result.size() == 0) {
                cacheId = deptId + "001";
            } else {
                Drug drug = result.get(0);
                String temp = drug.getId().substring(drug.getId().length() - 3);
                Integer i = Integer.valueOf(temp);
                cacheId = deptId + String.format("%03d", ++i);
            }
        }
        // 更新缓存
        redisUtil.set(cacheKey, cacheId);
 
        return cacheId;
    }
 
 
    public void buildHql(StringBuffer hql, Map<String, Object> param, Map<String, Object> args) {
        String str = (String) param.get("id");
        if (StringUtils.isNotEmpty(str)) {
            hql.append(" and id =:id");
            args.put("id", str);
        }
        str = (String) param.get("name");
        if (StringUtils.isNotEmpty(str)) {
            hql.append(" and name like :name");
            args.put("name", "%" + str + "%");
        }
        Date date = (Date) param.get("start");
        if (null != date) {
            hql.append(" and cgrq >=:start");
            args.put("start", date);
        }
        date = (Date) param.get("end");
        if (null != date) {
            hql.append(" and cgrq <=:end");
            args.put("end", date);
        }
    }
 
}