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.lang3.StringUtils;
|
import org.hibernate.Session;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.util.CollectionUtils;
|
|
import java.util.Date;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* @Author:YAN
|
*/
|
@Service
|
public class HDrugLogApplyService extends HibernateDao {
|
@Autowired
|
private RedisUtil redisUtil;
|
/**
|
* 缓存熏蒸备案id
|
*/
|
public static final String CACHE_DRUG_LOG_APPLY_ID = "DRUG_LOG_APPLY_ID";
|
|
/**
|
* 分页查询熏蒸作业主表信息
|
*
|
* @param page
|
* @param param
|
* @throws Exception
|
*/
|
public void pageDrugLogApply(Page<DrugLogApply> page, Map<String, Object> param) throws Exception {
|
StringBuffer hql = new StringBuffer();
|
Map<String, Object> args = new HashMap<>();
|
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 ");
|
String countHql = "select count(*) " + hql.toString();
|
this.pagingQuery(page, String.valueOf(hql), countHql, args);
|
}
|
|
/**
|
* 更新或添加熏蒸备案主表信息
|
*
|
* @param data
|
* @return
|
*/
|
public String saveDrugLogApply(DrugLogApply 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());
|
try {
|
if (null == data.getId()) {
|
String id = createId(data.getCompanyId());
|
data.setId(id);
|
session.save(data);
|
} else {
|
session.update(data);
|
}
|
if (CollectionUtils.isEmpty(data.getDrugLogPeople())) {
|
for (DrugLogPeople drugLogPeople : data.getDrugLogPeople()) {
|
if (null == drugLogPeople.getId()) {
|
drugLogPeople.setId(ContextUtil.getUUID());
|
drugLogPeople.setDrugLogId(data.getId());
|
drugLogPeople.setCompanyId(data.getCompanyId());
|
drugLogPeople.setDeptId(data.getDeptId());
|
session.save(drugLogPeople);
|
} else {
|
session.update(drugLogPeople);
|
}
|
}
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
session.flush();
|
session.close();
|
}
|
return null;
|
}
|
|
/**
|
* 删除熏蒸备案主表信息
|
*
|
* @param data
|
* @return
|
*/
|
public String delDrugLogApply(DrugLogApply data) {
|
Session session = this.getSessionFactory().openSession();
|
try {
|
if (null != data.getId()) {
|
session.delete(data);
|
for (DrugLogPeople drugLogPeople : data.getDrugLogPeople()) {
|
session.delete(drugLogPeople);
|
}
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
session.flush();
|
session.close();
|
}
|
return null;
|
}
|
|
/**
|
* 获取DrugLogApply列表
|
*
|
* @param param
|
* @throws Exception
|
*/
|
public List<DrugLogApply> drugLogApplyList(Map<String, Object> param) throws Exception {
|
StringBuffer hql = new StringBuffer();
|
Map<String, Object> args = new HashMap<>();
|
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");
|
return this.query(String.valueOf(hql), args);
|
}
|
|
/**
|
* 获取DrugLogDtl列表
|
*
|
* @param param
|
* @throws Exception
|
*/
|
public List<DrugLogDtl> drugLogDtlList(Map<String, Object> param) throws Exception {
|
StringBuffer hql = new StringBuffer();
|
Map<String, Object> args = new HashMap<>();
|
hql.append(" from " + DrugLogDtl.class.getName() + " where companyId=:companyId");
|
args.put("companyId", ContextUtil.getCompanyId());
|
if (null != param) {
|
buildHql(hql, param, args);
|
}
|
return this.query(String.valueOf(hql), args);
|
}
|
|
/**
|
* 更新或添加熏蒸备案储粮详情信息
|
*
|
* @param data
|
* @return
|
*/
|
public String saveDrugLogDtl(DrugLogDtl data) {
|
if (null == data.getCompanyId()) {
|
data.setCompanyId(ContextUtil.getCompanyId());
|
}
|
if (null == data.getDeptId()) {
|
data.setDeptId(ContextUtil.subDeptId(null));
|
}
|
Session session = this.getSessionFactory().openSession();
|
try {
|
if (null == data.getId()) {
|
data.setId(ContextUtil.getUUID());
|
session.save(data);
|
} else {
|
session.update(data);
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
session.flush();
|
session.close();
|
}
|
return null;
|
}
|
|
|
/**
|
* 删除熏蒸备案主表信息
|
*
|
* @param data
|
* @return
|
*/
|
public String delDrugLogDtl(DrugLogDtl 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;
|
}
|
|
|
/**
|
* 获取DrugLogPeople列表
|
*
|
* @param param
|
* @throws Exception
|
*/
|
public List<DrugLogPeople> drugLogPeopleList(Map<String, Object> param) throws Exception {
|
StringBuffer hql = new StringBuffer();
|
Map<String, Object> args = new HashMap<>();
|
hql.append(" from " + DrugLogPeople.class.getName() + " where companyId=:companyId");
|
args.put("companyId", ContextUtil.getCompanyId());
|
if (null != param) {
|
buildHql(hql, param, args);
|
}
|
return this.query(String.valueOf(hql), args);
|
}
|
|
/**
|
* 更新或添加熏蒸工作人员信息
|
*
|
* @param data
|
* @return
|
*/
|
public String saveDrugLogPeople(DrugLogPeople data) {
|
if (null == data.getCompanyId()) {
|
data.setCompanyId(ContextUtil.getCompanyId());
|
}
|
if (null == data.getDeptId()) {
|
data.setDeptId(ContextUtil.subDeptId(null));
|
}
|
Session session = this.getSessionFactory().openSession();
|
try {
|
if (null == data.getId()) {
|
data.setId(ContextUtil.getUUID());
|
session.save(data);
|
} else {
|
session.update(data);
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
session.flush();
|
session.close();
|
}
|
return null;
|
}
|
|
|
/**
|
* 删除熏蒸工作人员信息
|
*
|
* @param data
|
* @return
|
*/
|
public String delDrugLogPeople(DrugLogPeople 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;
|
}
|
|
/**
|
* 获取DrugLogWay列表
|
*
|
* @param param
|
* @throws Exception
|
*/
|
public List<DrugLogWay> drugLogWayList(Map<String, Object> param) throws Exception {
|
StringBuffer hql = new StringBuffer();
|
Map<String, Object> args = new HashMap<>();
|
hql.append(" from " + DrugLogWay.class.getName() + " where companyId=:companyId");
|
args.put("companyId", ContextUtil.getCompanyId());
|
if (null != param) {
|
buildHql(hql, param, args);
|
}
|
return this.query(String.valueOf(hql), args);
|
}
|
|
|
/**
|
* 更新或添加熏蒸备案主表信息
|
*
|
* @param data
|
* @return
|
*/
|
public String saveDrugLogWay(DrugLogWay data) {
|
if (null == data.getCompanyId()) {
|
data.setCompanyId(ContextUtil.getCompanyId());
|
}
|
if (null == data.getDeptId()) {
|
data.setDeptId(ContextUtil.subDeptId(null));
|
}
|
Session session = this.getSessionFactory().openSession();
|
try {
|
if (null == data.getId()) {
|
data.setId(ContextUtil.getUUID());
|
session.save(data);
|
} else {
|
session.update(data);
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
session.flush();
|
session.close();
|
}
|
return null;
|
}
|
|
/**
|
* 删除熏蒸备案主表信息
|
*
|
* @param data
|
* @return
|
*/
|
public String delDrugLogWay(DrugLogWay 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;
|
}
|
|
/**
|
* 构建hql
|
*
|
* @param hql
|
* @param param
|
* @param args
|
*/
|
public void buildHql(StringBuffer hql, Map<String, Object> param, Map<String, Object> args) {
|
String str = (String) param.get("drugLogId");
|
if (StringUtils.isNotEmpty(str)) {
|
hql.append(" and drugLogId=:drugLogId");
|
args.put("drugLogId", str);
|
}
|
str = (String) param.get("depotId");
|
if (StringUtils.isNotEmpty(str)) {
|
hql.append(" and depotId=:depotId");
|
args.put("depotId", str);
|
}
|
Date date = (Date) param.get("start");
|
if (null != date) {
|
hql.append(" and updateTime >=:start");
|
args.put("start", date);
|
}
|
date = (Date) param.get("end");
|
if (null != date) {
|
hql.append(" and updateTime <=:end");
|
args.put("end", date);
|
}
|
}
|
|
public String createId(String companyId) throws Exception {
|
|
// 从缓存中获取已有的组织编码
|
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);
|
} else {
|
List<DrugLogApply> drugLogApplies = this.drugLogApplyList(null);
|
if (null == drugLogApplies || drugLogApplies.size() == 0) {
|
cacheId = "0001";
|
} else {
|
|
String temp = drugLogApplies.get(0).getId();
|
Integer i = Integer.valueOf(temp);
|
cacheId = String.format("%04d", ++i);
|
}
|
}
|
// 更新缓存
|
redisUtil.set(cacheKey, cacheId);
|
|
return cacheId;
|
}
|
}
|