package com.ld.igds.view.service; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.ld.igds.models.*; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.hibernate.SQLQuery; import org.hibernate.Session; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.bstek.bdf2.core.orm.hibernate.HibernateDao; import com.ld.igds.common.CoreCommonService; import com.ld.igds.util.ContextUtil; /** * @author kyle.zhao: * @version 创建时间:2018年1月29日 类说明 */ @Slf4j @Component public class HDepotService extends HibernateDao { @Autowired private CoreCommonService coreCommonService; public List getData(Map param) { if (null == param) { param = new HashMap<>(); } if (null == param.get("companyId")) { param.put("companyId", ContextUtil.getDefaultCompanyId()); } StringBuilder hql = new StringBuilder(); hql.append(" from " + Depot.class.getName() + " where companyId = :companyId "); Map args = new HashMap<>(); args.put("companyId", param.get("companyId")); if (null != param) { buildHql(hql, param, args); } hql.append(" order by id + 0 "); return this.query(hql.toString(), args); } private void buildHql(StringBuilder hql, Map param, Map args) { Object obj = param.get("type"); if (null != obj) { hql.append(" and depotType=:type "); args.put("type", obj); } obj = param.get("deptId"); if (null != obj) { hql.append(" and deptId=:deptId "); args.put("deptId", obj); } obj = param.get("depotType"); if (null != obj) { hql.append(" and depotType=:depotType "); args.put("depotType", obj); } String arg = (String) param.get("depotId"); if (StringUtils.isNotEmpty(arg)) { hql.append(" and depotId=:depotId "); args.put("depotId", arg); } } public List getAllCache(String name, String companyId) { if (null == companyId) companyId = ContextUtil.getCompanyId(); String deptId = ContextUtil.subDeptId(null); List list = coreCommonService.getCacheDepotList(companyId, deptId); return list; } public void flushCache(String companyId) { if (StringUtils.isEmpty(companyId)) { companyId = ContextUtil.getCompanyId(); } List list; try { Map args = new HashMap<>(); args.put("companyId", companyId); list = this.getData(args); coreCommonService.setCacheDepot(list, companyId); } catch (Exception e) { e.printStackTrace(); } } /** * 添加仓库信息 * * @throws Exception */ public void saveDepot(Depot depot) throws Exception { depot.setCompanyId(ContextUtil.getCompanyId()); if (org.apache.commons.lang3.StringUtils.isEmpty(depot.getId())) { Depot lastDepot = this.findLastDepot(depot.getDeptId()); String id; if (lastDepot != null) { id = ContextUtil.getNextDepotId(depot.getDeptId(), lastDepot.getId(), 1000); } else { id = ContextUtil.getNextDepotId(depot.getDeptId(), null, 1000); } depot.setId(id); } Session session = this.getSessionFactory().openSession(); try { flushCache(depot.getCompanyId()); session.save(depot); } finally { session.flush(); session.close(); } } private Depot findLastDepot(String deptId) throws Exception { Map param = new HashMap<>(); param.put("deptId", deptId); List list = this.getData(param); if (list != null && list.size() > 0) { return list.get(list.size() - 1); } else { return null; } } /** * 修改仓库信息 * * @throws Exception */ public void updateDepot(Depot depot) throws Exception { Session session = this.getSessionFactory().openSession(); try { flushCache(depot.getCompanyId()); session.update(depot); } finally { session.flush(); session.close(); } } /** * 删除仓库信息 */ public void deleteDepot(Depot depot) { Session session = this.getSessionFactory().openSession(); try { session.delete(depot); } finally { session.flush(); session.close(); } } public List getConfList(String companyId) { if (StringUtils.isEmpty(companyId)) companyId = ContextUtil.getCompanyId(); String hql = " from " + DepotConf.class.getName() + " where companyId=:companyId order by depotId + 0 "; Map args = new HashMap(); args.put("companyId", companyId); List list = this.query(hql, args); if (null == list || list.isEmpty()) { log.info("当前仓库配置数据为空,系统自动生成基础数据"); this.addConfByDepot(companyId); list = this.query(hql, args); } return list; } private void addConfByDepot(String companyId) { try { Map args = new HashMap<>(); args.put("companyId", companyId); List list = this.getData(args); if (null == list || list.isEmpty()) return; DepotConf conf; for (Depot depot : list) { conf = new DepotConf(); conf.setDepotId(depot.getId()); conf.setCompanyId(depot.getCompanyId()); this.saveConf(conf); } } catch (Exception e) { e.printStackTrace(); } } public void saveConf(DepotConf conf) { Session session = this.getSessionFactory().openSession(); try { if (StringUtils.isEmpty(conf.getCompanyId())) { conf.setCompanyId(ContextUtil.getCompanyId()); } session.saveOrUpdate(conf); } finally { session.flush(); session.close(); } } public String deleteDepotConf(DepotConf conf) { if (null == conf.getCompanyId()) return null; Session session = this.getSessionFactory().openSession(); try { session.delete(conf); } finally { session.flush(); session.close(); } flushConfCache(conf.getCompanyId()); return null; } public void flushConfCache(String companyId) { if (StringUtils.isEmpty(companyId)) { companyId = ContextUtil.getCompanyId(); } List list = getConfList(companyId); coreCommonService.setCacheDepotConf(list, companyId); } public void updateFreq(String freq) { String sql = "UPDATE D_DEPOT_CONF SET GRAIN_FREQ_ =:freq WHERE COMPANY_ID_ =:companyId"; Session session = this.getSessionFactory().openSession(); String companyId = ContextUtil.getCompanyId(); try { SQLQuery query = session.createSQLQuery(sql); query.setString("freq", freq); query.setString("companyId", companyId); query.executeUpdate(); flushConfCache(companyId); } finally { session.flush(); session.close(); } } public Depot getDepotById(String companyId, String depotId) { return coreCommonService.getDepotById(companyId, depotId); } public List getDepotByTimer(List list) { if (null == list || list.isEmpty()) return null; String hql = "from " + Depot.class.getName() + " where companyId=:companyId and id in(:ids)"; Map param = new HashMap(); param.put("companyId", list.get(0).getCompanyId()); List ids = new ArrayList<>(); for (TimingDepot tim : list) { ids.add(tim.getDepotId()); } param.put("ids", ids); return this.query(hql, param); } public void initUserDeptMap(String companyId) { List list = coreCommonService.getCacheDepotList(companyId); if (null == list || list.isEmpty()) return; for (Depot depot : list) { ContextUtil.updateSubDept(depot.getId(), depot.getDeptId()); } } }