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.apache.commons.lang3.time.DateFormatUtils; 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 page, Map param) throws Exception { StringBuffer hql = new StringBuffer(); Map args = new HashMap<>(); hql.append(" from " + Drug.class.getName() + " where deptId=:deptId"); args.put("deptId", ContextUtil.subDeptId(null)); 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 listDrug(String companyId, Map param) { StringBuffer hql = new StringBuffer(" from " + Drug.class.getName() + " where companyId=:companyId"); Map 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)); } Session session = this.getSessionFactory().openSession(); data.setUpdateTime(new Date()); data.setUpdateUser(ContextUtil.getLoginUserCName()); try { if (null == data.getId()) { String id = createId(data.getCompanyId(), data.getCgrq()); 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 page, Map 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 page, Map param) { } public String createId(String companyId, Date time) { String timeKey = DateFormatUtils.format(time, "yyyyMMdd"); // 从缓存中获取已有的组织编码 String cacheKey = RedisConst.buildKey(companyId, HDrugService.CACHE_DRUG_ID); String cacheId = (String) redisUtil.get(cacheKey); if (null != cacheId && cacheId.indexOf(timeKey) >= 0) { String temp = cacheId.substring(cacheId.length() - 3); Integer i = Integer.valueOf(temp); cacheId = timeKey + String.format("%03d", ++i); } else { List result = listDrug(companyId, null); if (null == result || result.size() == 0) { cacheId = timeKey + "001"; } else { Drug drug = result.get(0); String temp = drug.getId().substring(drug.getId().length() - 3); Integer i = Integer.valueOf(temp); cacheId = timeKey + String.format("%03d", ++i); } } // 更新缓存 redisUtil.set(cacheKey, cacheId); return cacheId; } public void buildHql(StringBuffer hql, Map param, Map 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); } } }