本文共 2728 字,大约阅读时间需要 9 分钟。
@Override//把查询到的结果集与查询的字段一一对应组成map,并设置产品的有效期后, 把map放入到list集合中去... public List > queryOrderProductInfo(Integer orderId) { String[] fields = {"productCat", "productName", "size", "quantity", "lotNo", "localPrice"}; Query query = entityManager.createNativeQuery("SELECT " + StringUtils.join(fields, ",") + "\n from selleck.selleck_orderproduct" + "\n where orderId=:orderId" + "\n order by indexId" ); query.setParameter("orderId", orderId); List list = query.getResultList(); if (list.isEmpty()) { return new ArrayList<>(); } else { List > result = new ArrayList<>(list.size()); list.forEach(v ->{ Map valueMap = new HashMap<>(); for (int i = 0; i < fields.length; i++){ valueMap.put(fields[i], v[i]); } fillExpDate(valueMap); //根据产品的cat号和批次号lotNo,设置产品的有效期 result.add(valueMap); }); return result; } } private void fillExpDate(Map product) { // 根据产品的cat号和批次号lotNo,设置产品的有效期! S打头的; B打头的! try{ String cat = ((String)product.get("productCat")).toUpperCase(); String lotNo = (String)product.get("lotNo"); if (StringUtils.isNotBlank(lotNo)) { if (cat.startsWith("S")){ // S通过cat+lotNo去查询!添加日期,如果在201511之前统统改成2018 11 ,之后加三年 Query query = entityManager.createNativeQuery("select addDate from selleck.craft where lotNo=:lotNo"); query.setParameter("lotNo", cat + lotNo); List list = query.getResultList(); if (list.size() > 0) { Date dbDate = (Date) list.get(0); LocalDate craftAddDate = LocalDate.parse(new SimpleDateFormat("yyyy-MM-dd").format(dbDate)); LocalDate barrierDate = LocalDate.of(2015, 1, 1); // 临界日期 ---2015 年 1 月 1日 if (craftAddDate.isBefore(barrierDate)) { craftAddDate = craftAddDate.withYear(2018); //2015 1 1 之前的有效期自动改为 2018 年 1月 1日 } else { craftAddDate = craftAddDate.plusYears(3); } String expDate = craftAddDate.getMonth() + " " + craftAddDate.getYear(); product.put("expDate", expDate); } } else if (cat.startsWith("B")) { //B 通过cat和lotNo去查询! B打头的没有加3年一说!直接拿到即可 Query query = entityManager.createNativeQuery("select expDate from selleck.selleck_size_batch where cat=:cat and lotNo=:lotNo order by expDate desc"); query.setParameter("cat", cat); query.setParameter("lotNo", lotNo); List list = query.getResultList(); if (list.size() > 0) { String dbDate = (String) list.get(0); if (dbDate.length() == 6) { LocalDate date = LocalDate.parse(dbDate.substring(0, 4) + "-" + dbDate.substring(4) + "-15"); String expDate = date.getMonth() + " " + date.getYear(); product.put("expDate", expDate); } } } } }catch (Exception ignore){} }}
转载于:https://www.cnblogs.com/bravolove/p/6169935.html