package abacus.springboot.example.esb; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.alibaba.fastjson.JSONObject; import abacus.commons.context.interceptor.UserContext; import abacus.commons.exception.BusinessException; import abacus.config.ext.AbacusExtConfig; import abacus.springboot.example.dao.JcBill; import abacus.springboot.example.dao.JcBillHg; import abacus.springboot.example.dao.JcBillInItem; import abacus.springboot.example.dao.JcBillPhoto; import abacus.springboot.example.dao.JcBillSourceItem; import abacus.springboot.example.dao.JcSourceItem; import abacus.springboot.example.pi.JcBillAccessIF; import abacus.springboot.example.repository.JcBillHgRepository; import abacus.springboot.example.repository.JcBillInItemRepository; import abacus.springboot.example.repository.JcBillPhotoRepository; import abacus.springboot.example.repository.JcBillRepository; import abacus.springboot.example.repository.JcBillSourceItemRepository; import abacus.springboot.example.repository.JcSourceItemRepository; import abacus.springboot.example.util.RequestUtil; import abacus.springboot.example.vo.PMSConfigKey; import abacus.springboot.example.vo.SiDepositlocation; import abacus.springboot.example.wsi.JcBillServiceIF; @Repository public class JcBillService implements JcBillServiceIF{ private static final Logger LOG = LoggerFactory.getLogger(JcBillService.class); @Autowired private JcBillAccessIF jcBillAccess; @Autowired private JcBillRepository jcBillRepository; @Autowired private JcBillSourceItemRepository jcBillSourceItemRepository; @Autowired private JcBillHgRepository jcBillHgRepository; @Autowired private JcSourceItemRepository jcSourceItemRepository; @Autowired private JcBillInItemRepository jcBillInItemRepository; @Autowired private JcBillPhotoRepository jcBillPhotoRepository; @Autowired private AbacusExtConfig abacusExtConfig; @Override public Page queryJcBill(String fromDate, String thruDate, String id, String applicant, String goods, String billin, String goodsid, String product, String status, int typ, Pageable pageable) throws RuntimeException { try { return this.jcBillAccess.queryJcBill(fromDate, thruDate, applicant, goods, id, billin, goodsid, product, status, typ, pageable); } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Override public JcBill queryJcBillById(String billId) throws RuntimeException { try { Optional optional = jcBillRepository.findById(billId); return optional.isPresent() ? optional.get() : null; } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Override public List queryJcBillSourceItemByBillId(String billId) throws RuntimeException { try { List list = jcBillSourceItemRepository.findByBillid(billId); return (list != null && list.size() >= 1) ? list : null; } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Override public JcBillHg queryJcBillHg(String billHg) throws RuntimeException { try { Optional optional = jcBillHgRepository.findById(billHg); return optional.isPresent() ? optional.get() : null; } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Override public List queryJcSourceItemByBillId(String billId) throws RuntimeException { try { return jcBillAccess.queryJcSourceItemByBillId(billId); } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Override public Map> queryJcBillPhoto(String srcid) throws RuntimeException { try { List billPhotos = jcBillAccess.queryJcBillPhoto(srcid, null, null); if(billPhotos == null) return new HashMap>(); Map> map = billPhotos.stream() .collect(Collectors.groupingBy(e -> { return e.getTypephoto() + "#" + e.getSrcitemid(); }, Collectors.collectingAndThen(Collectors.toList(), value -> value) )); return map; } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Override public List queryJcSourceItemByBillId(String billId, String srcitemid) throws RuntimeException { try { return jcSourceItemRepository.findByBillidAndSrcitemid(billId, srcitemid); } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Override public JcBillSourceItem queryJcBillSourceItemById(Long id) throws RuntimeException { try { Optional optional = jcBillSourceItemRepository.findById(id); return optional.isPresent() ? optional.get() : null; } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Override public JcSourceItem queryJcSourceItemById(Long id) throws RuntimeException { try { Optional optional = jcSourceItemRepository.findById(id); return optional.isPresent() ? optional.get() : null; } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}) @Override public void saveQuerenBillSourceItem(JcBillSourceItem billItem, JcSourceItem sourceItem) throws RuntimeException { try { jcBillSourceItemRepository.save(billItem); jcSourceItemRepository.save(sourceItem); } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}) @Override public void saveRejectJcSourceItems(JcBillSourceItem billItem, JcBill jcBill) throws RuntimeException { try { jcBillSourceItemRepository.save(billItem); jcBillRepository.save(jcBill); } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}) @Override public void saveCompleteJcBill(List inItems, JcBill jcBill) throws RuntimeException { try { jcBillInItemRepository.saveAll(inItems); jcBillRepository.save(jcBill); } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}) @Override public void updateWarehouse(JcBill jcBill) throws RuntimeException { try { jcBillRepository.save(jcBill); } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Override public List queryJcBillInItemByBillId(String billId, int status) throws RuntimeException { try { List list = jcBillInItemRepository.findByBillidAndStatus(billId, status); return (list != null && list.size() >= 1) ? list : null; } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Override public List queryJcBillInItemByBillId(String billId) throws RuntimeException { try { List list = jcBillInItemRepository.findByBillid(billId); return (list != null && list.size() >= 1) ? list : null; } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}) @Override public void createStockIn(JcBill jcBill) throws RuntimeException { try { jcBillRepository.save(jcBill); jcBillAccess.saveEvent(jcBill.getId(), "稽查收货", "其他入库"); } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Override public JcBillInItem queryJcBillInItemById(Long id) { try { Optional optional = jcBillInItemRepository.findById(id); return optional.isPresent() ? optional.get() : null; } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}) @Override public void saveTerminateHgInItem(JcBillInItem inItem) throws RuntimeException { try { jcBillInItemRepository.save(inItem); } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Override public List findAllById(List ids) throws RuntimeException { try { return jcBillInItemRepository.findAllById(ids); } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}) @Override public void saveInformHg(List inItems, JcBill jcBill, JcBillHg jcBillHg, String photoUrl) throws RuntimeException { try { JcBillPhoto photo = new JcBillPhoto(); photo.setSrcid(jcBillHg.getId()); photo.setSrcitemid(jcBillHg.getId()); photo.setTypephoto(JcBillPhoto.HG_TZH); photo.setUrlphoto(photoUrl); jcBillPhotoRepository.save(photo); jcBillInItemRepository.saveAll(inItems); jcBillRepository.save(jcBill); jcBillHgRepository.save(jcBillHg); jcBillAccess.saveEvent(jcBillHg.getId(), "回购订单", "回购通知"); } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Override public Page queryJcBillHg(String fromDate, String thruDate, String account, String salearea, String id, String billId, String status, String product, String manager, int typ, Pageable pageable) throws RuntimeException { try { return jcBillAccess.queryJcBillHg(fromDate, thruDate, account, salearea, id, billId, status, product, manager, typ, pageable); } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Override public List queryJcBillInItemByBillHg(String billHgId) throws RuntimeException { try { List list = jcBillInItemRepository.findByBillhg(billHgId); return (list != null && list.size() >= 1) ? list : null; } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}) @Override public void saveRejectBillHg(int status, int typ, JcBill jcBill, JcBillHg billHg, List inItems) throws RuntimeException { try { if(JcBillHg.STATUS_ONE == status) { jcBillRepository.save(jcBill); jcBillHgRepository.save(billHg); jcBillInItemRepository.saveAll(inItems); } else if(JcBillHg.STATUS_TWO == status) { jcBillHgRepository.save(billHg); } else if(JcBillHg.STATUS_THREE == status || JcBillHg.STATUS_FOUR == status) { jcBillHgRepository.save(billHg); } else if (JcBillHg.STATUS_FIVE == status){ // 删除对应的事件 jcBillAccess.deleteEvent(billHg.getId(), "回购分仓", "回购分仓"); } else { throw new BusinessException("未知状态不能驳回!"); } } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}) @Override public void saveQuerenSk(JcBillHg billHg, boolean fcFlag) throws RuntimeException { try { billHg.setAudit(UserContext.getUserId()); billHg.setAuditname(UserContext.getUserName()); billHg.setAuditdt(new Date()); jcBillHgRepository.save(billHg); if(fcFlag) { jcBillAccess.saveEvent(billHg.getId(), "回购分仓", "回购分仓"); } } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}) @Override public void savecreateFc(JcBillHg billHg) throws RuntimeException { try { jcBillHgRepository.save(billHg); jcBillAccess.saveEvent(billHg.getId(), "回购分仓", "回购分仓"); } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}) @Override public void updateFcWarehouse(String oldJsonStr, JcBillHg billHg) throws RuntimeException { try { jcBillHgRepository.save(billHg); } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}) @Override public JcSourceItem saveJcSourceItem(JcSourceItem item) throws RuntimeException { try { JcSourceItem tempItem = jcSourceItemRepository.save(item); return tempItem; } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}) @Override public void deleteJcSourceItem(List ids) throws RuntimeException { try { jcBillAccess.deleteJcSourceItem(ids); // /*操作日志*/ // OperationLog log = new OperationLog("jc_bill", item.getBillid(), "删除溯源确认信息", "", "", JSON.toJSONString(item)); // log.setOperator(UserContext.getUserId()); // log.setOperatorName(UserContext.getUserName()); // BusinessESUtil.checkService(this.operationLogService).saveOperationLog(log); } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}) @Override public void updateHgprice(JcBillInItem inItem) throws RuntimeException { try { jcBillInItemRepository.save(inItem); } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}) @Override public void updateRemark(String oldJsonStr, JcBillHg billHg) throws RuntimeException { try { jcBillHgRepository.save(billHg); } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}) @Override public void updatePac(String oldJsonStr, JcBillHg billHg) throws RuntimeException { try { jcBillHgRepository.save(billHg); } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}) @Override public void updateOperatorname(JcBillInItem inItem, String oldStr) throws RuntimeException { try { jcBillInItemRepository.save(inItem); } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Override public void saveJcBillInItem(List items) throws RuntimeException { jcBillInItemRepository.saveAll(items); } @Override public List queryJcBillSourceItemInBillId(List billIds) throws RuntimeException { try { return jcBillAccess.queryJcBillSourceItemInBillId(billIds); } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}) @Override public void saveRejectBill(int status, JcBill jcBill, List inItems, List sourceItems) throws RuntimeException { try { jcBillRepository.save(jcBill); jcBillSourceItemRepository.saveAll(sourceItems); if(inItems != null) jcBillInItemRepository.deleteAll(inItems); if(JcBill.STATUS_FOUR == status) {// 入库待审核 进行撤回 // 进行事件删除 jcBillAccess.deleteEvent(jcBill.getId(), "稽查收货", "其他入库"); } } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Override public List queryJcSourceItemByBillId(String billId, String srcitemid, int status) throws RuntimeException { try { return jcSourceItemRepository.findByBillidAndSrcitemidAndStatus(billId, srcitemid, status); } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}) @Override public void updateSourceItemAccount(String sourceItemLogJson, String billSourceItemLogJson, String billSourceItemNewLogJson, JcSourceItem sourceItem, List billSourceItems, List oldInItems, List inItems) throws RuntimeException { try { String biillId = sourceItem.getBillid(); jcSourceItemRepository.save(sourceItem); jcBillSourceItemRepository.saveAll(billSourceItems); jcBillInItemRepository.deleteAll(oldInItems); jcBillInItemRepository.saveAll(inItems); } catch (RuntimeException e) { e.printStackTrace(); LOG.error("服务异常", e.getMessage()); throw new RuntimeException("服务异常:", e); } } @Override public SiDepositlocation querySiDepositlocation(String srcid) throws Exception { return jcBillAccess.querySiDepositlocation(srcid); } @Override public SiDepositlocation querySiDepositlocationHGfc(String srcid) throws Exception { return jcBillAccess.querySiDepositlocationHGfc(srcid); } @Override public JSONObject gwisSubCancelOrder(String orderCode, String orderType) throws Exception { try { Map configValues = this.abacusExtConfig.getAbacusConfigSetByKeyGroupByKey( PMSConfigKey.KEY_GROUP, PMSConfigKey.PMS_WMSCONF); if (configValues == null || configValues.isEmpty()) throw new Exception(PMSConfigKey.PMS_WMSCONF + "为空!"); // 判断key是否存在 if (!configValues.containsKey(PMSConfigKey.PMS_WMSCONF_URL)) throw new Exception(PMSConfigKey.PMS_WMSCONF + "配置中" + PMSConfigKey.PMS_WMSCONF_URL + "不存在!"); if (!configValues.containsKey(PMSConfigKey.PMS_WMSCONF_APPKEY)) throw new Exception(PMSConfigKey.PMS_WMSCONF + "配置中" + PMSConfigKey.PMS_WMSCONF_APPKEY + "不存在!"); if (!configValues.containsKey(PMSConfigKey.PMS_WMSCONF_SESSIONKEY)) throw new Exception(PMSConfigKey.PMS_WMSCONF + "配置中" + PMSConfigKey.PMS_WMSCONF_SESSIONKEY + "不存在!"); // 判断值是否为空 String url = configValues.get(PMSConfigKey.PMS_WMSCONF_URL); if (StringUtils.isBlank(url)) throw new Exception(PMSConfigKey.PMS_WMSCONF + "配置中" + PMSConfigKey.PMS_WMSCONF_URL + "为空!"); String appkey = configValues.get(PMSConfigKey.PMS_WMSCONF_APPKEY); if (StringUtils.isBlank(appkey)) throw new Exception(PMSConfigKey.PMS_WMSCONF + "配置中" + PMSConfigKey.PMS_WMSCONF_APPKEY + "为空!"); String sessionKey = configValues.get(PMSConfigKey.PMS_WMSCONF_SESSIONKEY); if (StringUtils.isBlank(sessionKey)) throw new Exception(PMSConfigKey.PMS_WMSCONF + "配置中" + PMSConfigKey.PMS_WMSCONF_SESSIONKEY + "为空!"); String goodsOwner = configValues.get(PMSConfigKey.PMS_WMSCONF_GOODSOWNER); if (StringUtils.isBlank(goodsOwner)) throw new Exception(PMSConfigKey.PMS_WMSCONF + "配置中" + PMSConfigKey.PMS_WMSCONF_GOODSOWNER + "为空!"); JSONObject head = new JSONObject(); head.put("orderCode", orderCode); // 唯一订单号 head.put("orderType", orderType); head.put("goodsOwner", goodsOwner); // 2. 计算签名 secret = MD5(contentJson + sessionKey) String secret = md5(head.toJSONString() + sessionKey); // 3. 拼接完整URL(带上method、appkey、secret) String fullUrl = url + "?method=gwisSubCancelOrder" + "&appkey=" + appkey + "&secret=" + secret; LOG.info("WMS取消订单>>>orderCode>>>{}>>>orderType>>>{}>>>请求参数>>>{}", orderCode, orderType, head.toJSONString()); String returnJson = RequestUtil.send(fullUrl, head.toJSONString(), orderCode); LOG.info("WMS取消订单>>>orderCode>>>{}>>>orderType>>>{}>>>响应参数>>>{}", orderCode, orderType, returnJson); JSONObject jsonObj = JSONObject.parseObject(returnJson); return jsonObj; } catch (RuntimeException e) { e.printStackTrace(); LOG.error("WMS取消订单接口服务异常", e.getMessage()); throw new RuntimeException("WMS取消订单接口服务异常:", e); } } private static String md5(String input) { try { java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5"); byte[] digest = md.digest(input.getBytes("UTF-8")); StringBuilder sb = new StringBuilder(); for (byte b : digest) { sb.append(String.format("%02X", b)); // 大写十六进制 } return sb.toString(); } catch (Exception e) { throw new RuntimeException("MD5计算失败", e); } } }