package com.ld.igds.m.service; import com.bstek.bdf2.core.orm.hibernate.HibernateDao; import com.bstek.dorado.data.provider.Page; import com.ld.igds.m.InoutManageUtil; import com.ld.igds.models.InoutPlan; import com.ld.igds.models.InoutPlanDetail; import com.ld.igds.util.ContextUtil; import org.apache.commons.lang3.StringUtils; import org.hibernate.Session; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @Component public class HPlanManageService extends HibernateDao { public void pagePlan(Page page, Map param) throws Exception { String hql = " from " + InoutPlan.class.getName() + " where companyId =:companyId and deptId =:deptId"; Map args = new HashMap(); args.put("companyId", ContextUtil.getCompanyId()); args.put("deptId", ContextUtil.subDeptId(null)); if (null != param) { String str = (String) param.get("id"); if (StringUtils.isNotEmpty(str)) { hql += " and id like:id"; args.put("id", "%" + str + "%"); } Integer year = (Integer) param.get("year"); if (null != year) { hql += " and year =:year"; args.put("year", String.valueOf(year)); } str = (String) param.get("type"); if (StringUtils.isNotEmpty(str)) { hql += " and type =:type"; args.put("type", str); } str = (String) param.get("referenceNumber"); if (StringUtils.isNotEmpty(str)) { hql += " and referenceNumber like:referenceNumber"; args.put("referenceNumber", "%" + str + "%"); } str = (String) param.get("name"); if (StringUtils.isNotEmpty(str)) { hql += " and name like:name"; args.put("name", "%" + str + "%"); } str = (String) param.get("key"); if (StringUtils.isNotEmpty(str)) { hql += " and (name like :name or referenceNumber like:referenceNumber) "; args.put("name", "%" + str + "%"); args.put("referenceNumber", "%" + str + "%"); } } String count = "select count(*) " + hql; hql += " order by createTime desc"; this.pagingQuery(page, hql, count, args); } public List listPlan() { String hql = " from " + InoutPlan.class.getName() + " where companyId =:companyId and deptId =:deptId"; Map args = new HashMap(); args.put("companyId", ContextUtil.getCompanyId()); args.put("deptId", ContextUtil.subDeptId(null)); return this.query(hql, args); } public List listPlanDetail(String planId) { String hql = " from " + InoutPlanDetail.class.getName(); if (null != planId) { hql += " where planId=:planId "; Map args = new HashMap(); args.put("planId", planId); return this.query(hql, args); } return this.query(hql); } @Transactional public String delPlan(InoutPlan data) { Session session = this.getSessionFactory().openSession(); try { if (null != data.getId()) { this.delPlanDetailByPlanId(data.getId(), session); session.delete(data); } } catch (Exception e) { e.printStackTrace(); } finally { session.flush(); session.close(); } return null; } public String delPlanDetail(InoutPlanDetail 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; } private void delPlanDetailByPlanId(String id, Session session) { String hql = " delete from " + InoutPlanDetail.class.getName() + " where planId=:planId"; session.createQuery(hql).setString("planId", id).executeUpdate(); } public void savePlan(InoutPlan 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()) { List list = getPlanId(data); data.setId(InoutManageUtil.createPlanId(data.getType(), data.getYear(), list)); data.setCreateUser(ContextUtil.getLoginUserCName()); data.setCreateTime(new Date()); session.save(data); } else { session.update(data); } savePlanDetail(data.getDetails(), data.getId(), session); } catch (Exception e) { e.printStackTrace(); } finally { session.flush(); session.close(); } } private void savePlanDetail(List details, String planId, Session session) { if (null == details || details.isEmpty()) return; for (InoutPlanDetail planDetail : details) { if (null == planDetail.getId()) { planDetail.setUpdateTime(new Date()); planDetail.setPlanId(planId); planDetail.setId(ContextUtil.getUUID()); session.save(planDetail); } else { planDetail.setUpdateTime(new Date()); session.update(planDetail); } } } public List getPlanId(InoutPlan plan) { String hql = " from " + InoutPlan.class.getName() + " where companyId =:companyId and year =:year"; Map args = new HashMap(); args.put("companyId", plan.getCompanyId()); args.put("year", plan.getYear()); hql += " order by createTime desc"; return this.query(hql, args); } public void pagePlanDetail(Page page, Map param) throws Exception { String hql = " from " + InoutPlanDetail.class.getName() + " where 1=1 "; Map args = new HashMap(); if (null != param) { String str = (String) param.get("key"); if (StringUtils.isNotEmpty(str)) { hql += " and year like :year "; args.put("year", "%" + str + "%"); } } String count = "select count(*) " + hql; hql += " order by updateTime desc"; this.pagingQuery(page, hql, count, args); } }