|  |  |  | 
|---|
|  |  |  | package com.ld.igds.drug.service; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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.*; | 
|---|
|  |  |  | import com.ld.igds.util.ContextUtil; | 
|---|
|  |  |  | import com.ld.igds.util.RedisUtil; | 
|---|
|  |  |  | import org.apache.commons.lang.time.DateFormatUtils; | 
|---|
|  |  |  | import org.apache.commons.lang3.StringUtils; | 
|---|
|  |  |  | import org.apache.commons.lang3.time.DateFormatUtils; | 
|---|
|  |  |  | import org.hibernate.Session; | 
|---|
|  |  |  | import org.springframework.beans.BeanUtils; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | 
|---|
|  |  |  | if (null != param) { | 
|---|
|  |  |  | buildHql(hql, param, args); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | hql.append(" order by id "); | 
|---|
|  |  |  | hql.append(" order by xzbm "); | 
|---|
|  |  |  | String countHql = "select count(*) " + hql.toString(); | 
|---|
|  |  |  | this.pagingQuery(page, String.valueOf(hql), countHql, args); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | data.setUpdateTime(new Date()); | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | if (null == data.getXzbm()) { | 
|---|
|  |  |  | String id = createId(data.getCompanyId()); | 
|---|
|  |  |  | String id = createId(data.getCompanyId(), data.getTbrq()); | 
|---|
|  |  |  | data.setXzbm(id); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (StringUtils.isEmpty(data.getDtlId())) { | 
|---|
|  |  |  | 
|---|
|  |  |  | if (StringUtils.isEmpty(data.getWayId())) { | 
|---|
|  |  |  | data.setWayId(ContextUtil.getUUID()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //添加粮情子表 | 
|---|
|  |  |  | saveDtl(data); | 
|---|
|  |  |  | //添加备案方式子表 | 
|---|
|  |  |  | saveWay(data); | 
|---|
|  |  |  | //添加主表 | 
|---|
|  |  |  | session.saveOrUpdate(data); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!CollectionUtils.isEmpty(data.getDrugLogPeople())) { | 
|---|
|  |  |  | for (DrugLogPeople drugLogPeople : data.getDrugLogPeople()) { | 
|---|
|  |  |  | if (null == drugLogPeople.getPropleId()) { | 
|---|
|  |  |  | String tbrq = DateFormatUtils.format(data.getTbrq(), "yyyyMMdd"); | 
|---|
|  |  |  | drugLogPeople.setPropleId(ContextUtil.getUUID()); | 
|---|
|  |  |  | drugLogPeople.setId(data.getDeptId() + tbrq + data.getXzbm()); | 
|---|
|  |  |  | drugLogPeople.setTbrq(data.getTbrq()); | 
|---|
|  |  |  | drugLogPeople.setDrugLogId(data.getXzbm()); | 
|---|
|  |  |  | drugLogPeople.setDeptId(data.getDeptId()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | session.saveOrUpdate(drugLogPeople); | 
|---|
|  |  |  | session.flush(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | DrugLogWay way = new DrugLogWay(); | 
|---|
|  |  |  | BeanUtils.copyProperties(data, way); | 
|---|
|  |  |  | way.setDrugLogId(data.getXzbm()); | 
|---|
|  |  |  | session.saveOrUpdate(way); | 
|---|
|  |  |  | DrugLogDtl dtl = new DrugLogDtl(); | 
|---|
|  |  |  | BeanUtils.copyProperties(data, dtl); | 
|---|
|  |  |  | way.setDrugLogId(data.getXzbm()); | 
|---|
|  |  |  | session.saveOrUpdate(dtl); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } finally { | 
|---|
|  |  |  | 
|---|
|  |  |  | DrugLogWay way = new DrugLogWay(); | 
|---|
|  |  |  | BeanUtils.copyProperties(data, way); | 
|---|
|  |  |  | way.setWayId(ContextUtil.getUUID()); | 
|---|
|  |  |  | String tbrq = DateFormatUtils.format(data.getTbrq(), "yyyyMMdd"); | 
|---|
|  |  |  | way.setId(data.getDeptId() + tbrq + data.getXzbm()); | 
|---|
|  |  |  | session.saveOrUpdate(way); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | DrugLogDtl dtl = new DrugLogDtl(); | 
|---|
|  |  |  | BeanUtils.copyProperties(data, dtl); | 
|---|
|  |  |  | String tbrq = DateFormatUtils.format(data.getTbrq(), "yyyyMMdd"); | 
|---|
|  |  |  | dtl.setId(data.getDeptId() + tbrq + data.getXzbm()); | 
|---|
|  |  |  | session.saveOrUpdate(dtl); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | 
|---|
|  |  |  | hql.append(" from " + DrugLogApply.class.getName() + " where companyId=:companyId"); | 
|---|
|  |  |  | args.put("companyId", ContextUtil.getCompanyId()); | 
|---|
|  |  |  | if (null != param) { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | buildHql(hql, param, args); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | hql.append(" order by id desc"); | 
|---|
|  |  |  | hql.append(" order by xzbm desc"); | 
|---|
|  |  |  | return this.query(String.valueOf(hql), args); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public String saveDrugLogDtl(DrugLogDtl data) { | 
|---|
|  |  |  | if (null == data.getDeptId()) { | 
|---|
|  |  |  | data.setDeptId(ContextUtil.subDeptId(null)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Session session = this.getSessionFactory().openSession(); | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | if (null == data.getId()) { | 
|---|
|  |  |  | data.setId(ContextUtil.getUUID()); | 
|---|
|  |  |  | if (null == data.getDtlId()) { | 
|---|
|  |  |  | data.setDtlId(ContextUtil.getUUID()); | 
|---|
|  |  |  | session.save(data); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | session.update(data); | 
|---|
|  |  |  | 
|---|
|  |  |  | public String delDrugLogDtl(DrugLogDtl data) { | 
|---|
|  |  |  | Session session = this.getSessionFactory().openSession(); | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | if (null != data.getId()) { | 
|---|
|  |  |  | if (null != data.getDtlId()) { | 
|---|
|  |  |  | session.delete(data); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public String saveDrugLogPeople(DrugLogPeople data) { | 
|---|
|  |  |  | if (null == data.getDeptId()) { | 
|---|
|  |  |  | data.setDeptId(ContextUtil.subDeptId(null)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Session session = this.getSessionFactory().openSession(); | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | if (null == data.getId()) { | 
|---|
|  |  |  | data.setId(ContextUtil.getUUID()); | 
|---|
|  |  |  | if (null == data.getPropleId()) { | 
|---|
|  |  |  | data.setPropleId(ContextUtil.getUUID()); | 
|---|
|  |  |  | session.save(data); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | session.update(data); | 
|---|
|  |  |  | 
|---|
|  |  |  | public String delDrugLogPeople(DrugLogPeople data) { | 
|---|
|  |  |  | Session session = this.getSessionFactory().openSession(); | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | if (null != data.getId()) { | 
|---|
|  |  |  | if (null != data.getPropleId()) { | 
|---|
|  |  |  | session.delete(data); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | 
|---|
|  |  |  | if (null != param) { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | String xzbm = (String) param.get("xzbm"); | 
|---|
|  |  |  | String deptId = (String) param.get("deptId"); | 
|---|
|  |  |  | String tbrq = DateFormatUtils.format((Date) param.get("tbsj"), "yyMMdd"); | 
|---|
|  |  |  | String id = xzbm + deptId + tbrq; | 
|---|
|  |  |  | hql.append(" from " + DrugLogWay.class.getName() + " where id=:id"); | 
|---|
|  |  |  | args.put("id", id); | 
|---|
|  |  |  | args.put("id", xzbm); | 
|---|
|  |  |  | buildHql(hql, param, args); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return this.query(String.valueOf(hql), args); | 
|---|
|  |  |  | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public String saveDrugLogWay(DrugLogWay data) { | 
|---|
|  |  |  | if (null == data.getDeptId()) { | 
|---|
|  |  |  | data.setDeptId(ContextUtil.subDeptId(null)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Session session = this.getSessionFactory().openSession(); | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | if (null == data.getId()) { | 
|---|
|  |  |  | data.setId(ContextUtil.getUUID()); | 
|---|
|  |  |  | if (null == data.getWayId()) { | 
|---|
|  |  |  | data.setWayId(ContextUtil.getUUID()); | 
|---|
|  |  |  | session.save(data); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | session.update(data); | 
|---|
|  |  |  | 
|---|
|  |  |  | public String delDrugLogWay(DrugLogWay data) { | 
|---|
|  |  |  | Session session = this.getSessionFactory().openSession(); | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | if (null != data.getId()) { | 
|---|
|  |  |  | if (null != data.getWayId()) { | 
|---|
|  |  |  | session.delete(data); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | 
|---|
|  |  |  | hql.append(" and drugLogId=:drugLogId"); | 
|---|
|  |  |  | args.put("drugLogId", str); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | str = (String) param.get("id"); | 
|---|
|  |  |  | if (StringUtils.isNotEmpty(str)) { | 
|---|
|  |  |  | hql.append(" and xzbm like:id"); | 
|---|
|  |  |  | args.put("id", str + "%"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | str = (String) param.get("depotId"); | 
|---|
|  |  |  | if (StringUtils.isNotEmpty(str)) { | 
|---|
|  |  |  | hql.append(" and depotId=:depotId"); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public String createId(String companyId) throws Exception { | 
|---|
|  |  |  | public String createId(String companyId, Date time) throws Exception { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | String timeKey = DateFormatUtils.format(time, "yyyyMMdd"); | 
|---|
|  |  |  | // 从缓存中获取已有的组织编码 | 
|---|
|  |  |  | String cacheKey = RedisConst.buildKey(companyId, HDrugLogApplyService.CACHE_DRUG_LOG_APPLY_ID); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | String cacheId = (String) redisUtil.get(cacheKey); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (null != cacheId) { | 
|---|
|  |  |  | Integer i = Integer.valueOf(cacheId); | 
|---|
|  |  |  | cacheId = String.format("%04d", ++i); | 
|---|
|  |  |  | if (null != cacheId && cacheId.indexOf(timeKey) >= 0) { | 
|---|
|  |  |  | String temp = cacheId.substring(cacheId.length() - 4); | 
|---|
|  |  |  | Integer i = Integer.valueOf(temp); | 
|---|
|  |  |  | cacheId = timeKey + String.format("%04d", ++i); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | List<DrugLogApply> drugLogApplies = this.drugLogApplyList(null); | 
|---|
|  |  |  | Map<String, Object> param = new HashMap<>(); | 
|---|
|  |  |  | param.put("id", timeKey); | 
|---|
|  |  |  | List<DrugLogApply> drugLogApplies = this.drugLogApplyList(param); | 
|---|
|  |  |  | if (null == drugLogApplies || drugLogApplies.size() == 0) { | 
|---|
|  |  |  | cacheId = "0001"; | 
|---|
|  |  |  | cacheId = timeKey + "0001"; | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | String temp = drugLogApplies.get(0).getXzbm(); | 
|---|
|  |  |  | Integer i = Integer.valueOf(temp); | 
|---|
|  |  |  | cacheId = String.format("%04d", ++i); | 
|---|
|  |  |  | cacheId = timeKey + String.format("%04d", ++i); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // 更新缓存 | 
|---|