package com.hx.phip.util.api; import com.alibaba.fastjson.JSONObject; import com.hx.common.service.CommonService; import com.hx.mybatisTool.SqlSentence; import com.hx.phiappt.common.OrderGoodsConstants; import com.hx.phiappt.common.OrderItemConstants; import com.hx.phiappt.common.OrderTotalConstants; import com.hx.phiappt.common.UserProjectConstants; import com.hx.phiappt.constants.tool.SqlSplicingTool; import com.hx.phiappt.dao.mapper.UserCardItemMapper; import com.hx.phiappt.model.BaseEntity; import com.hx.phiappt.model.order.OrderItem; import com.hx.phiappt.model.order.OrderItemSon; import com.hx.phiappt.model.order.OrdersTotal; import com.hx.phiappt.model.refund.RefundRecord; import com.hx.phiappt.model.refund.RefundRecordItem; import com.hx.phiappt.model.refund.RefundRecordItemSource; import com.hx.phiappt.model.user.UserCard; import com.hx.phiappt.model.user.UserCardItem; import com.hx.phiappt.model.user.UserProjectItem; import com.hx.phip.config.CustomParameter; import com.hx.phiappt.dao.mapper.*; import com.hx.util.StringUtils; import com.hz.his.dto.sync.SyncOrderDto; import com.hz.his.dto.sync.SyncUserCradDto; import com.hz.his.dto.sync.SyncUserProjectItem; import com.hz.his.feign.service.sync.SyncExecutionRecordService; import com.hz.his.feign.service.sync.SyncOrderService; import com.hz.his.feign.service.sync.SyncUserCardService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** * @Author */ public class OrderCancelUtil { private static final Logger logger = LoggerFactory.getLogger(OrderCancelUtil.class); public static void hisCancelOrder(CommonService commonService,SyncOrderService syncOrderService, SyncExecutionRecordService syncExecutionRecordService, OrdersTotal ordersTotal, SyncUserCardService syncUserCardService,CustomParameter customParameter ){ if(StringUtils.isEmpty(ordersTotal.getHisOrderId())){ //领建订单id为空则不处理下面逻辑 return; } //更改领健订单为退款状态 SyncOrderDto syncOrderDto=new SyncOrderDto(); syncOrderDto.setOrderId(ordersTotal.getHisOrderId()); syncOrderService.refundOrder(syncOrderDto); //处理用户拥有项目 SqlSentence sqlSentence = new SqlSentence(); Map map=new HashMap<>(); sqlSentence.setM(map); map.put("orderId",ordersTotal.getId()); map.put("isDel", BaseEntity.NO); sqlSentence.setSqlSentence("select id,buyNum,actualTotal,type from order_item where orderId=#{m.orderId} and isDel=#{m.isDel}"); List orderItems=commonService.selectList(OrderItemMapper.class,sqlSentence); for (OrderItem orderItem : orderItems) { switch (orderItem.getType()){ case OrderItemConstants.TYPE_PROJECT: //项目 handleTypeProject(commonService,orderItem,syncExecutionRecordService); break; case OrderItemConstants.TYPE_PROMOTION: handleTypePromotion(commonService,orderItem,syncExecutionRecordService); break; case OrderItemConstants.TYPE_CARD: handleTypeCard(commonService,ordersTotal,syncUserCardService); break; case OrderItemConstants.CARD_BAG: handleTypeCardBag(commonService,orderItem,syncExecutionRecordService); break; default: break; } } if(OrderTotalConstants.TYPE_CARD_BAG.equals(ordersTotal.getType())){ //处理已有卡项次数退回同步到领建 handHisCardBag(commonService, customParameter, ordersTotal,1,null); } } public static void hisPartOrder(CommonService commonService, SyncOrderService syncOrderService, SyncExecutionRecordService syncExecutionRecordService, OrdersTotal ordersTotal, SyncUserCardService syncUserCardService, CustomParameter customParameter, StringBuilder orderNodeBuilder, RefundRecord refundRecord){ if(StringUtils.isEmpty(ordersTotal.getHisOrderId())){ //领建订单id为空则不处理下面逻辑 return; } //更改领健订单为退款状态 SyncOrderDto syncOrderDto=new SyncOrderDto(); syncOrderDto.setOrderId(ordersTotal.getHisOrderId()); orderNodeBuilder.append("-开始处理his订单状态:"); if(OrderTotalConstants.STATUS_REFUND_PART==ordersTotal.getRefundStatus()){ orderNodeBuilder.append("部分退款"); //部分退款 syncOrderService.partialRefund(syncOrderDto); }else if(OrderTotalConstants.STATUS_REFUND_FINSH==ordersTotal.getRefundStatus()){ orderNodeBuilder.append("全部退款"); //全部退款完成 syncOrderService.refundOrder(syncOrderDto); } //处理用户拥有项目 SqlSentence sqlSentence = new SqlSentence(); Map map=new HashMap<>(); sqlSentence.setM(map); map.put("orderId",ordersTotal.getId()); map.put("isDel", BaseEntity.NO); sqlSentence.setSqlSentence("select id,buyNum,actualTotal,type,goodsName from order_item where orderId=#{m.orderId} and isDel=#{m.isDel}"); List orderItems=commonService.selectList(OrderItemMapper.class,sqlSentence); for (OrderItem orderItem : orderItems) { switch (orderItem.getType()){ case OrderItemConstants.TYPE_PROJECT: //项目 handleTypeProject(commonService,orderItem,syncExecutionRecordService); orderNodeBuilder.append("-处理his退款项目:"+orderItem.getGoodsName()+",成功"); break; case OrderItemConstants.TYPE_PROMOTION: handleTypePromotion(commonService,orderItem,syncExecutionRecordService); orderNodeBuilder.append("-处理his退款促销:"+orderItem.getGoodsName()+",成功"); break; case OrderItemConstants.TYPE_CARD: handleTypeCard(commonService,ordersTotal,syncUserCardService); break; case OrderItemConstants.CARD_BAG: handleTypeCardBag(commonService,orderItem,syncExecutionRecordService); break; default: break; } } if(OrderTotalConstants.TYPE_CARD_BAG.equals(ordersTotal.getType())){ //处理已有卡项次数退回同步到领建 handHisCardBag(commonService, customParameter, ordersTotal,1,refundRecord); orderNodeBuilder.append("-处理his退款已有卡包成功"); } } //处理一级是项目的 public static void handleTypeProject(CommonService commonService,OrderItem orderItem,SyncExecutionRecordService syncExecutionRecordService){ SqlSentence sqlSentence = new SqlSentence(); Map map=new HashMap<>(); sqlSentence.setM(map); map.put("commonId",orderItem.getId()); sqlSentence.setSqlSentence("select id,itemId,cancelNum from user_project_item where commonId=#{m.commonId} AND cancelNum>0"); List userProjectItems=commonService.selectList(UserProjectItemMapper.class, sqlSentence); SyncUserProjectItem syncUserProjectItem; for (UserProjectItem userProjectItem : userProjectItems) { syncUserProjectItem=new SyncUserProjectItem(); if(StringUtils.isEmpty(userProjectItem.getItemId())) continue; syncUserProjectItem.setOrderItemId(userProjectItem.getItemId()); syncUserProjectItem.setRefunded(userProjectItem.getCancelNum()); //调用同步中心取消用户带执行记录 syncExecutionRecordService.updateUserProjectItm(syncUserProjectItem); } } //处理一级是促销的 public static void handleTypePromotion(CommonService commonService,OrderItem orderItem,SyncExecutionRecordService syncExecutionRecordService){ SqlSentence sqlSentence = new SqlSentence(); Map map=new HashMap<>(); sqlSentence.setM(map); map.put("orderItemId",orderItem.getId()); sqlSentence.setSqlSentence("select id,type from order_item_source where orderItemId=#{m.orderItemId}"); List orderItemSonList=commonService.selectList(OrderItemSonMapper.class, sqlSentence); for (OrderItemSon orderItemSon : orderItemSonList) { if (OrderGoodsConstants.TYPE_PROJECT.equals(orderItemSon.getType())){ handleSonTypeProject(commonService,orderItemSon,syncExecutionRecordService); } } } //处理一级是卡项的 public static void handleTypeCard(CommonService commonService,OrdersTotal ordersTotal,SyncUserCardService syncUserCardService ){ SqlSentence sqlSentence = new SqlSentence(); Map map=new HashMap<>(); sqlSentence.setM(map); map.put("orderId",ordersTotal.getId()); map.put("hisOrderId",ordersTotal.getHisOrderId()); map.put("effectiveStatus", UserProjectConstants.EFF_STATUS_CANCEL); sqlSentence.setSqlSentence("select hisId from user_card where (orderId=#{m.orderId} or hisOrderId=#{m.hisOrderId}) and effectiveStatus=#{m.effectiveStatus}"); List userCardList=commonService.selectList(UserCardMapper.class, sqlSentence); SyncUserCradDto syncUserCradDto=null; for (UserCard userCard : userCardList) { if(StringUtils.isEmpty(userCard.getHisId())){ continue; } syncUserCradDto=new SyncUserCradDto(); syncUserCradDto.setHisId(userCard.getHisId()); syncUserCardService.updateUserCard(syncUserCradDto); } } //处理一级是卡包的 public static void handleTypeCardBag(CommonService commonService,OrderItem orderItem, SyncExecutionRecordService syncExecutionRecordService){ SqlSentence sqlSentence = new SqlSentence(); Map map=new HashMap<>(); sqlSentence.setM(map); map.put("orderItemId",orderItem.getId()); sqlSentence.setSqlSentence("select id,type from order_item_source where orderItemId=#{m.orderItemId}"); List orderItemSonList=commonService.selectList(OrderItemSonMapper.class, sqlSentence); for (OrderItemSon orderItemSon : orderItemSonList) { if (OrderGoodsConstants.TYPE_PROJECT.equals(orderItemSon.getType())){ handleSonTypeProject(commonService,orderItemSon,syncExecutionRecordService); } } } //处理二级级是项目的 public static void handleSonTypeProject(CommonService commonService,OrderItemSon orderItemSon,SyncExecutionRecordService syncExecutionRecordService){ SqlSentence sqlSentence = new SqlSentence(); Map map=new HashMap<>(); sqlSentence.setM(map); map.put("commonId",orderItemSon.getId()); sqlSentence.setSqlSentence("select id,itemId,cancelNum from user_project_item where commonId=#{m.commonId} AND cancelNum>0"); List userProjectItems=commonService.selectList(UserProjectItemMapper.class, sqlSentence); SyncUserProjectItem syncUserProjectItem; for (UserProjectItem userProjectItem : userProjectItems) { syncUserProjectItem=new SyncUserProjectItem(); if(StringUtils.isEmpty(userProjectItem.getItemId())) continue; syncUserProjectItem.setOrderItemId(userProjectItem.getItemId()); syncUserProjectItem.setRefunded(userProjectItem.getCancelNum()); //调用同步中心取消用户带执行记录 syncExecutionRecordService.updateUserProjectItm(syncUserProjectItem); } } /** * 处理领建卡包次数加回 * @param commonService * @param customParameter * @param ordersTotal * @param type 0:结账扣减卡项次数,1:退款加回卡项次数 */ public static void handHisCardBag(CommonService commonService, CustomParameter customParameter, OrdersTotal ordersTotal, Integer type,RefundRecord refundRecord) { SqlSentence sqlSentence = new SqlSentence(); Map map=new HashMap<>(); sqlSentence.setM(map); map.put("isDel", BaseEntity.NO); map.put("refundRecordId", refundRecord.getId()); sqlSentence.setSqlSentence("SELECT id,orderItemId FROM refund_record_item WHERE refundRecordId=#{m.refundRecordId} and isDel=#{m.isDel}"); List refundRecordItems=commonService.selectList(RefundRecordItemMapper.class,sqlSentence); List itemIds = refundRecordItems.stream().map(o -> o.getOrderItemId()).collect(Collectors.toList()); sqlSentence.setSqlSentence("SELECT id,orderItemSonId FROM refund_record_item_source WHERE refundRecordId=#{m.refundRecordId} and isDel=#{m.isDel}"); List refundRecordItemSources=commonService.selectList(RefundRecordItemSourceMapper.class,sqlSentence); List itemSonIds = refundRecordItemSources.stream().map(o -> o.getOrderItemSonId()).collect(Collectors.toList()); map.put("orderId", ordersTotal.getId()); StringBuffer itemBuffer=new StringBuffer(); itemBuffer.append("SELECT id,type,userCardId FROM order_item WHERE id in ( "); SqlSplicingTool.selectIn(itemBuffer,itemIds); sqlSentence.setSqlSentence(itemBuffer.toString()); //查询该订单选中的二级子订获取订单购买数量 // sqlSentence.setSqlSentence("SELECT id,type,userCardId FROM order_item WHERE orderId=#{m.orderId} and isDel=#{m.isDel}"); List orderitemList=commonService.selectList(OrderItemMapper.class,sqlSentence); if(orderitemList!=null && orderitemList.size()>0){ for (OrderItem orderItem : orderitemList) { if(OrderItemConstants.CARD_BAG.equals(orderItem.getType())){ StringBuffer itemSonBuffer=new StringBuffer(); //查询该订单选中的二级子订获取订单购买数量 map.put("orderItemId",orderItem.getId()); itemSonBuffer.append("SELECT id,goodsId,cardEquityId,buyNum,cardItemInfoId FROM order_item_source WHERE orderItemId=#{m.orderItemId} and id in ( "); SqlSplicingTool.selectIn(itemSonBuffer,itemSonIds); sqlSentence.setSqlSentence(itemSonBuffer.toString()); // sqlSentence.setSqlSentence("SELECT id,goodsId,cardEquityId,buyNum,cardItemInfoId FROM order_item_source WHERE orderItemId=#{m.orderItemId} and isDel=#{m.isDel} AND hasReNum>0 "); List orderItemSons=commonService.selectList(OrderItemSonMapper.class,sqlSentence); if(orderItemSons==null|| orderItemSons.size()==0){ logger.info("退款已有卡项提示:根据该总订单未找到该二级子订单信息,订单标识:{}",ordersTotal.getId()); }else{ map.put("cardId",orderItem.getUserCardId()); for (OrderItemSon orderItemSon : orderItemSons) { map.put("commonId", orderItemSon.getCardItemInfoId()); map.put("cardEquityId",orderItemSon.getCardEquityId()); sqlSentence.setSqlSentence("select usedNum,commonId,id,hisId FROM user_card_item WHERE cardEquityId=#{m.cardEquityId} and userCardId=#{m.cardId} and cardItemInfoId=#{m.commonId} and isDel=#{m.isDel}"); List userCardItems=commonService.selectList(UserCardItemMapper.class,sqlSentence); if(userCardItems!=null && userCardItems.size()>0) { List userCardItemList = userCardItems.stream().filter(o -> o.getCommonId().equals(orderItemSon.getGoodsId())).collect(Collectors.toList()); logger.info("userCardItemList.size:{}", JSONObject.toJSONString(userCardItemList)); if (userCardItemList != null && userCardItemList.size() == 1) { Integer num=type==0?orderItemSon.getBuyNum():-orderItemSon.getBuyNum(); SynOrderUtil.updateUserCard(userCardItemList.get(0).getHisId(), num, customParameter); } else { logger.info("卡项结账提示:找到多条或者未找到领建使用记录,请排查!,cardEquityId:{},cardId:{},订单标识:{}",orderItemSon.getCardEquityId(),orderItemSon.getGoodsNo(),ordersTotal.getId()); } } } } } } } } }