package com.hx.phip.order.service.impl; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.hx.phiappt.common.*; import com.hx.phiappt.model.*; import com.hx.phiappt.model.order.*; import com.hx.phiappt.model.user.UserProject; import com.hx.phiappt.model.user.UserProjectItem; import com.hx.phip.dao.mapper.*; import com.hx.phip.order.service.OrderService; import com.hx.phip.service.*; import com.hx.phip.tool.CreateNo; import com.hx.phip.user.service.UserProjectItemService; import com.hx.phip.user.service.UserProjectService; import com.hx.util.SimpleEncrypt; import com.hx.util.StringUtils; import com.platform.exception.PlatTipsException; import com.platform.resultTool.PlatformCode; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.math.BigDecimal; import java.util.*; @Transactional @Service public class OrderServiceImpl implements OrderService { @Resource private SystemParameterMapper systemParameterMapper; @Resource private OrdersTotalService ordersTotalService; @Resource private OrderItemService orderItemService; @Resource private ProjectService projectService; @Resource private ConsumablesService consumablesService; @Resource private UserService userService; @Resource private OrderDeveloperService orderDeveloperService; @Resource private SysAdminService sysAdminService; @Resource private PaymentMethodTypeService paymentMethodTypeService; @Resource private PaymentMethodService paymentMethodService; @Resource private UserMoneyService userMoneyService; @Resource private UserMoneyLogService userMoneyLogService; @Resource private UserProjectService userProjectService; @Resource private UserProjectItemService userProjectItemService; @Resource private ShopService shopService; @Resource private OrderLogService orderLogService; @Resource private OrderGoodsSpecsService orderGoodsSpecsService; @Resource private EmployeeService employeeService; @Override public void add(OrdersTotal ordersTotal, List items, Integer type, List orderDeveloperList,String operatorId,String shopId) { SysAdmin sysAdmin = sysAdminService.selectOneByKey(operatorId); Employee employee = employeeService.selectOneByKey(operatorId); if(sysAdmin==null && employee==null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到操作人的信息"+operatorId); } operatorId=sysAdmin==null?employee.getId():sysAdmin.getId(); String operatorName=sysAdmin==null?employee.getCnName():sysAdmin.getName(); Integer operatorType=sysAdmin==null?1:0; Shop shop = shopService.selectOneByKey(shopId);//查询门店,创建订单人的门店 if(shop==null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到门店信息"+shopId); } List packageTypes = Arrays.asList("meal", "card");//套餐和卡项类型集合 //type:0:草稿 1:提交 2:结账 if(type== 0){ ordersTotal.setStatus(OrderTotalConstants.STATUS_DRAFT);//订单状态:草稿 } BigDecimal totalPrice=BigDecimal.ZERO;//订单总金额 BigDecimal shouldOrderTotal=BigDecimal.ZERO;//订单应付支付总金额 BigDecimal buyNum=BigDecimal.ZERO;//购买数量 BigDecimal actualTotal =BigDecimal.ZERO;//子订单实付金额 BigDecimal sonTotalPrice =BigDecimal.ZERO;//子订单订单总额 List packageIds=new ArrayList<>();//用来装套餐或者卡项的id(标识) JSONArray jsonObject = new JSONArray();//用来封装规格json集合 Map map=new HashMap<>();//用来存储规格的map BigDecimal discountTotal=BigDecimal.ZERO;//总订单的优惠总金额 OrderGoodsSpecs orderGoodsSpecs=null;//订单子订单商品规格 //计算总订单金额信息,填补子订单信息 for(OrderItem vo:items){ buyNum=new BigDecimal(vo.getBuyNum()); //子订单编号 String orderNum = CreateNo.createTimeNo(systemParameterMapper, "SO", 6, "yyyyMMdd");//订单编号 vo.setOrderNo(orderNum); map.put("specsValue",""); map.put("unit",""); map.put("specsId",""); if(vo.getType().equals(OrderItemConstants.TYPE_PROJECT)){ Project project = projectService.selectOneByKey(vo.getCommonId()); if(project==null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"找不到项目:"+vo.getCommonId()); } if(project.getPrice()==null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"项目:"+project.getName()+",没有销售价格,请填写销售单价在进行创建订单"); } //项目实付金额=项目的销售价格*购买数量*折扣 actualTotal = project.getPrice().multiply(buyNum).multiply(vo.getDiscount().divide(new BigDecimal(100))); //项目订单总额=项目原价*购买数量,如果项目没有原价就用现价 sonTotalPrice=sonTotalPrice.add(project.getOriginalPrice()==null?project.getPrice().multiply(buyNum):project.getOriginalPrice().multiply(buyNum)); //优惠金额=项目的销售单价*数量-用户实付金额; discountTotal=discountTotal.add(project.getPrice().multiply(buyNum).subtract(actualTotal)); vo.setSingle(project.getTreatmentItemsNum()==null?1:project.getTreatmentItemsNum());//单次疗程数量 vo.setUsedTotal(vo.getSingle()*vo.getBuyNum()); //总次疗程次数,单次疗程次数*购买的总量 vo.setOriPrice(project.getOriginalPrice());//原价,项目的原价 vo.setCurPrice(project.getPrice());//现价,项目的销售单价格 map.put("specsTitle",project.getSpecification()); map.put("unit",""); jsonObject.add(map); vo.setSpecs(StringUtils.isEmpty(project.getSpecification())?"[]":jsonObject.toString());//规格 vo.setGoodsNo(project.getCoding());//项目编号 vo.setGoodsName(project.getName());//项目名称 vo.setGoodsImage("");//项目主图 }else if(vo.getType().equals(OrderItemConstants.TYPE_RETAIL)){ Consumables consumables = consumablesService.selectOneByKey(vo.getCommonId()); if(consumables==null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"找不到项目:"+vo.getCommonId()); } if(consumables.getPrice()==null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"商品:"+consumables.getName()+",没有销售价格,请填写销售单价在进行创建订单"); } //商品实付金额=商品的销售价格*购买数量*折扣 actualTotal = consumables.getPrice().multiply(buyNum).multiply(vo.getDiscount().divide(new BigDecimal(100))); //商品订单总额=项目销售价*购买数量 sonTotalPrice=sonTotalPrice.add(consumables.getPrice().multiply(buyNum)); //优惠金额=商品的销售单价*数量-用户实付金额; discountTotal=discountTotal.add(consumables.getPrice().multiply(buyNum).subtract(actualTotal)); vo.setSingle(0);//单次疗程数量 vo.setUsedTotal(vo.getSingle()*vo.getBuyNum());//总次疗程次数,单次疗程次数*购买的总量 vo.setOriPrice(consumables.getPrice());//原价,项目的销售价 vo.setCurPrice(consumables.getPrice());//现价 map.put("specsTitle",consumables.getSpec()); map.put("unit",consumables.getUnitName()); vo.setSpecs(StringUtils.isEmpty(consumables.getSpec())?"[]":jsonObject.toString());//规格 vo.setGoodsNo(consumables.getSerialNumber());//项目编号 vo.setGoodsName(consumables.getName());//项目名称 vo.setGoodsImage("");//项目主图 }else if(vo.getType().equals(OrderItemConstants.TYPE_MEAL)){//套餐 暂定 }else if(vo.getType().equals(OrderItemConstants.TYPE_CARD)){//卡项 暂定 } //子订单实付金额 vo.setActualTotal(actualTotal); //子订单总额 vo.setTotal(sonTotalPrice); totalPrice=totalPrice.add(vo.getTotal());//订单总金额 shouldOrderTotal=shouldOrderTotal.add(vo.getActualTotal());//订单应付支付总金额 sonTotalPrice=BigDecimal.ZERO; } //如果开发人集合为空,那么默认用户的所属顾问 // if(orderDeveloperList==null || orderDeveloperList.size()==0){ User user = userService.selectOneByKey(ordersTotal.getUserId()); if(StringUtils.noNull(user.getHisCorpUserId())){ Shop userShop = shopService.selectAdiver(user.getHisCorpUserId()); Employee userAdiver = employeeService.selectOneByKey(user.getHisCorpUserId()); //开发人门店标识(用户绑定的销售顾问) ordersTotal.setDeveloperShopId(userShop.getId()); //开发人门店名称 ordersTotal.setDeveloperShopName(userShop.getName()); ordersTotal.setDeveloperId(userAdiver.getId()); ordersTotal.setDeveloperName(userAdiver.getCnName()); }else{ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"请填写好开发人或者用户绑定一个销售顾问在重试"); } // } setOrderTotal(ordersTotal, shop, totalPrice, shouldOrderTotal, discountTotal,operatorId,operatorName,operatorType); //创建总订单 ordersTotalService.insert(ordersTotal); orderLogService.insert(setCreateOrderTotalUserLog(ordersTotal,operatorId,operatorName)); //总订单绑定开发人 if(orderDeveloperList !=null && orderDeveloperList.size()>0){ for(OrderDeveloper vo:orderDeveloperList){ vo.setOrderId(ordersTotal.getId());//总订单id orderDeveloperService.insert(vo); } } //创建子订单 for(OrderItem vo:items){ vo.setOrderId(ordersTotal.getId());//总订单id orderItemService.insert(vo);//保存订单的子订单 if(packageTypes.contains(vo.getType())){ packageIds.add(vo.getCommonId());//套餐/卡项id集合 } //如果是套餐也需要保存,暂时没有套餐和卡项 JSONArray objects = JSONArray.parseArray(vo.getSpecs()); for(int i=0;i itemMap=new HashMap<>();//用于存一级子订单商品对应的金额集合 for(int i=0;i orderItemList= orderItemService.selectItemList(ordersTotal.getId()); if(orderItemList ==null || orderItemList.size()==0){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"没有对应的子订单"); } UserProject userProject = null; UserProjectItem userProjectItem = null; Date date = new Date(); BigDecimal itemMoneyBig=null; for(OrderItem vo:orderItemList){ date = new Date(); userProject=new UserProject(); userProjectItem = new UserProjectItem(); if(vo.getType().equals(OrderItemConstants.TYPE_PROJECT)){ setUserProject(ordersTotal, userProject, vo); userProjectService.insert(userProject);//新增用户项目记录 setUserProjectItem(ordersTotal, userProject, userProjectItem, date, vo); userProjectItemService.insert(userProjectItem);//新增用户项目子项记录 } //暂定,如果是套餐还需要根据套餐找出子订单的子项 String itemMoney = itemMap.get(vo.getCommonId()); if(StringUtils.noNull(itemMoney)){ vo.setPayStatus(ordersTotal.getPayStatus());//支付状态,先暂定总订单支付状态 orderItemService.updatePayStatus(vo);//更改子订单的支付状态 } } } private OrderLog setPayOrderTotalUserLog(OrdersTotal ordersTotal, String opName, String opId) { return new OrderLog(ordersTotal.getId(),OrderLogConstants.LOG_TYPE_PAY, "操作人:"+opName+",操作订单类型:"+ordersTotal.getType()+",订单编号:"+ordersTotal.getOrderNo()+",应付总金额:"+ordersTotal.getShouldTotal() +",已支付总金额:"+ordersTotal.getActualTotal()+",优惠总金额:"+ordersTotal.getDiscountTotal(), opName,opId,ordersTotal.getStatus()); } @Override public void test(String value, String storeValue,String userId) { System.out.println("增值金"+value); System.out.println("储值金"+storeValue); if(StringUtils.noNull(value)){ try { UserMoney userMoney1 = userMoneyService.selectUserId(userId); if(userMoney1==null){ UserMoney userMoney = new UserMoney(); userMoney.setValueAddedFund(SimpleEncrypt.simpleEnCode(value)); userMoney.setIntegral(SimpleEncrypt.simpleEnCode("0")); userMoney.setStoredValueFund(SimpleEncrypt.simpleEnCode(storeValue)); userMoney.setUserId(userId); userMoneyService.insert(userMoney); }else { if(StringUtils.noNull(value)){ userMoney1.setValueAddedFund(SimpleEncrypt.simpleEnCode(value)); userMoneyService.updateValueAddedFund(userMoney1); } if(StringUtils.noNull(storeValue)){ userMoney1.setStoredValueFund(SimpleEncrypt.simpleEnCode(storeValue)); userMoneyService.updateStoredValueFund(userMoney1); } } } catch (Exception e) { e.printStackTrace(); } } } private void setUserProjectItem(OrdersTotal ordersTotal, UserProject userProject, UserProjectItem userProjectItem, Date date, OrderItem vo) { userProjectItem.setTotal(vo.getTotal());//子项总金额 userProjectItem.setActualTotal(vo.getActualTotal());//子项支付总金额 userProjectItem.setOriPrice(vo.getOriPrice());//原价 userProjectItem.setCurPrice(vo.getCurPrice());//现价 userProjectItem.setBuyNum(vo.getBuyNum());//数量 userProjectItem.setUsedTotal(vo.getUsedTotal());//总次数 userProjectItem.setNotUsedNum(vo.getUsedTotal());//未使用次数(户口的时候需要相应扣减) userProjectItem.setUsedNum(0);//已使用次数 userProjectItem.setEffectiveStatus(UserProjectConstants.EFF_STATUS_YES);//是否有效,结账默认项目和商品有效,套餐卡项暂定 userProjectItem.setTimeLimit(0);//是否有效时间限制,目前项目和商品没有时间限制,套餐和卡项暂定 userProjectItem.setLimitStartTime(date);//有效开始时间,系统当前时间 userProjectItem.setLimitEndTime(date);//有效结束时间,系统当前时间,项目和商品是没有时间限制的,所以默认系统当前时间,如果是套餐和卡项就填套餐和卡项的有效时间 userProjectItem.setCommonType(OrderSourceConstans.TYPE_PROJECT);//项目和商品是来源于一级子订单,套餐和卡项即使二级子订单 userProjectItem.setCommonId(vo.getId());//类型是一级子订单,该字段就是一级子订单标识,二级也是如此 userProjectItem.setUsedOne(vo.getSingle());//单次疗程数量 userProjectItem.setUserProjectId(userProject.getId());//用户项目id userProjectItem.setUserId(ordersTotal.getUserId());//用户id userProjectItem.setOrderItemId(vo.getId()); // 存OrderItemId userProjectItem.setDepartmentCode(ordersTotal.getDepartmentCode());//科室编码 userProjectItem.setDepartmentName(ordersTotal.getDepartmentName());//科室名称 userProjectItem.setShopId(ordersTotal.getDeveloperShopId());//开单人门店id userProjectItem.setShopName(ordersTotal.getShopName());//开单人门店名称 } private void setUserProject(OrdersTotal ordersTotal, UserProject userProject, OrderItem vo) { userProject.setType(vo.getType()); userProject.setGoodsNo(vo.getGoodsNo()); userProject.setGoodsId(vo.getCommonId()); userProject.setEffectiveStatus(UserProjectConstants.EFF_STATUS_YES); userProject.setUsedTotal(vo.getUsedTotal()); userProject.setNotUsedNum(vo.getUsedTotal()); userProject.setUsedNum(0); userProject.setOverdueNum(0); userProject.setTransferNum(0); userProject.setUserId(ordersTotal.getUserId()); } private void setUserLog(String operatorId,String operatorName, String userId, BigDecimal money, BigDecimal valueAddedFundBig, UserMoneyLog userMoneyLog, BigDecimal leftMoney,Integer type,Integer opType) { userMoneyLog.setUserId(userId); userMoneyLog.setType(type); userMoneyLog.setOpNumber(money); userMoneyLog.setOpBeforeNumber(valueAddedFundBig); userMoneyLog.setOpAfterNumber(leftMoney); userMoneyLog.setOpAdminId(operatorId); userMoneyLog.setOpAdminName(operatorName); userMoneyLog.setRemarks(""); userMoneyLog.setOpType(opType); } private OrderLog setCreateOrderTotalUserLog(OrdersTotal ordersTotal,String opId,String opName){ return new OrderLog(ordersTotal.getId(),OrderLogConstants.LOG_TYPE_CREATE,"操作人:"+opName+",创建订单类型:"+ordersTotal.getType()+",订单编号:"+ordersTotal.getOrderNo() +",应付总金额:"+ordersTotal.getShouldTotal(),opName,opId,ordersTotal.getStatus()); } }