package com.hx.phip.util.api; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.hx.common.service.CommonService; import com.hx.mybatisTool.SqlSentence; import com.hx.phiappt.common.OperationReasonConstants; import com.hx.phiappt.common.OrderItemConstants; import com.hx.phiappt.common.OrderLogConstants; import com.hx.phiappt.constants.tool.CouponTool; import com.hx.phiappt.constants.tool.SqlSplicingTool; //import com.hx.phiappt.constants.tool.ValueAddedFundTool; import com.hx.phiappt.dao.mapper.*; import com.hx.phiappt.model.*; import com.hx.phiappt.model.activity.ActivityAction; import com.hx.phiappt.model.activity.ActivityCondition; import com.hx.phiappt.model.activity.ActivityGroup; import com.hx.phiappt.model.activity.ActivityRule; import com.hx.phiappt.model.cardItem.CardEquity; import com.hx.phiappt.model.cardItem.CardItem; import com.hx.phiappt.model.cardItem.CardItemInfo; import com.hx.phiappt.model.consumables.Consumables; import com.hx.phiappt.model.coupon.CouponReleaseRecordItem; import com.hx.phiappt.model.limit.MemberLimit; import com.hx.phiappt.model.memberLevel.MemberLevel; import com.hx.phiappt.model.order.*; import com.hx.phiappt.model.project.ProjectShopPrice; import com.hx.phiappt.model.promotion.Promotion; import com.hx.phiappt.model.userMoney.UserMoneyUnclaimed; import com.hx.phip.config.CustomParameter; import com.hx.phip.dao.mapper.*; import com.hx.phip.dao.mapper.CardEquityMapper; import com.hx.phip.dao.mapper.CardItemInfoMapper; import com.hx.phip.dao.mapper.CardItemMapper; import com.hx.phip.dao.mapper.ConsumablesMapper; import com.hx.phip.dao.mapper.EmployeeMapper; import com.hx.phip.dao.mapper.KoapOrderItemMapper; import com.hx.phip.dao.mapper.MemberLevelMapper; import com.hx.phip.dao.mapper.MemberLimitMapper; import com.hx.phip.dao.mapper.OrderDeveloperMapper; import com.hx.phip.dao.mapper.OrderInfoMapper; import com.hx.phip.dao.mapper.OrderItemSonMapper; import com.hx.phip.dao.mapper.OrderLogMapper; import com.hx.phip.dao.mapper.OrdersTotalMapper; import com.hx.phip.dao.mapper.ProjectMapper; import com.hx.phip.dao.mapper.PromotionMapper; import com.hx.phip.dao.mapper.ShopMapper; import com.hx.phip.dao.mapper.SystemParameterMapper; import com.hx.phip.dao.mapper.UserMapper; import com.hx.phip.dto.OperatorDto; import com.hx.phip.tool.CreateNo; import com.hx.util.StringUtils; import com.platform.constants.PlatformPattern; import com.platform.exception.PlatTipsException; import com.platform.resultTool.PlatformCode; import org.apache.commons.collections.map.HashedMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.Serializable; import java.math.BigDecimal; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** * 订单-开单工具类 */ public class OrderAddUtil { private static final Logger logger = LoggerFactory.getLogger(OrderAddUtil.class); /** * 处理一级子订单信息 * @param ordersTotal * @param items * @param shopId * @param jsonObject * @param map */ public static Map handOrderItem(OrdersTotal ordersTotal, List items, String shopId, JSONArray jsonObject, Map map,BigDecimal totalPrice,BigDecimal shouldOrderTotal, BigDecimal discountTotal,CommonService commonService,SystemParameterMapper systemParameterMapper) { BigDecimal buyNum; BigDecimal actualTotal =BigDecimal.ZERO;//子订单实付金额 BigDecimal sonTotalPrice =BigDecimal.ZERO;//子订单订单总额 SqlSentence sqlSentence; Map objectMap=new HashMap<>();; //计算总订单金额信息,填补子订单信息 for(OrderItem vo:items){ buyNum=new BigDecimal(vo.getBuyNum()); //子订单编号 String orderNum = CreateNo.createTimeNo(systemParameterMapper, "SO", 16, "yyyyMMdd");//订单编号 vo.setOrderNo(orderNum); map.put("specsValue",""); map.put("unit",""); map.put("specsId",""); map.put("shopId",shopId); if(vo.getType().equals(OrderItemConstants.TYPE_PROJECT)){ //处理项目 sqlSentence=new SqlSentence(); sqlSentence.setM(objectMap); objectMap.put("commonId",vo.getCommonId()); sqlSentence.setSqlSentence("SELECT p.* FROM project p LEFT JOIN syn_project sp ON p.apiId=sp.projectId WHERE (p.id=#{m.commonId} or sp.id=#{m.commonId}) and p.isDel =0 and p.isUp =1"); Project project=commonService.selectOne(ProjectMapper.class,sqlSentence); if(project==null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"找不到项目:"+vo.getCommonId()); } // //计算项目价格,如果总订单有康博佳标识代表是康博佳订单不需要任何处理直接拿去康博佳的总金额,每个门店价格不一样,如果没有设置门店价格就使用项目售价 // BigDecimal price=priceJudge(ordersTotal, commonService,project.getId(),shopId); // project.setPrice(BigDecimal.ZERO.compareTo(price)==0?project.getPrice():price); Map bigDecimalMap = handleProject(jsonObject, map, vo, actualTotal, sonTotalPrice, discountTotal, buyNum,commonService,project); actualTotal=bigDecimalMap.get("actualTotal"); sonTotalPrice=bigDecimalMap.get("sonTotalPrice"); discountTotal=bigDecimalMap.get("discountTotal"); }else if(vo.getType().equals(OrderItemConstants.TYPE_RETAIL)){ //处理商品 Map bigDecimalMap =handleConsumables(jsonObject, map, vo, actualTotal, sonTotalPrice, discountTotal, buyNum,commonService); actualTotal=bigDecimalMap.get("actualTotal"); sonTotalPrice=bigDecimalMap.get("sonTotalPrice"); discountTotal=bigDecimalMap.get("discountTotal"); }else if(vo.getType().equals(OrderItemConstants.TYPE_MEAL)){//套餐 暂定 }else if(vo.getType().equals(OrderItemConstants.TYPE_CARD) ){//卡项 暂定 //处理卡项 Map bigDecimalMap =handleCardItem(jsonObject, map, vo, actualTotal, sonTotalPrice, discountTotal, buyNum, ordersTotal.getUserId(),commonService); actualTotal=bigDecimalMap.get("actualTotal"); sonTotalPrice=bigDecimalMap.get("sonTotalPrice"); discountTotal=bigDecimalMap.get("discountTotal"); }else if(vo.getType().equals(OrderItemConstants.TYPE_PROMOTION)){//促销 暂定 //处理卡项 Map bigDecimalMap =handlePromotion(jsonObject, map, vo, actualTotal, sonTotalPrice, discountTotal, buyNum, ordersTotal.getUserId(),commonService); actualTotal=bigDecimalMap.get("actualTotal"); sonTotalPrice=bigDecimalMap.get("sonTotalPrice"); discountTotal=bigDecimalMap.get("discountTotal"); }else if(vo.getType().equals(OrderItemConstants.CARD_BAG)){//卡包 if(StringUtils.isEmpty(vo.getUserCardId())){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"下单:开卡包订单需要卡包标识!"); } //处理卡项 Map bigDecimalMap =handleCardBag(jsonObject, map, vo, actualTotal, sonTotalPrice, discountTotal, buyNum, ordersTotal.getUserId(),commonService); actualTotal=bigDecimalMap.get("actualTotal"); sonTotalPrice=bigDecimalMap.get("sonTotalPrice"); discountTotal=bigDecimalMap.get("discountTotal"); } //子订单实付金额 vo.setActualTotal(actualTotal); vo.setUserPaidTotal(BigDecimal.ZERO); //子订单总额 vo.setTotal(sonTotalPrice); totalPrice=totalPrice.add(vo.getTotal());//订单总金额 shouldOrderTotal=shouldOrderTotal.add(vo.getActualTotal());//订单应付支付总金额 sonTotalPrice=BigDecimal.ZERO; } objectMap.clear(); objectMap.put("items",items); objectMap.put("totalPrice",totalPrice); objectMap.put("shouldOrderTotal",shouldOrderTotal); objectMap.put("discountTotal",discountTotal); return objectMap; } /** * 创建总订单 * @param ordersTotal 总订单信息 * @param operator 操作人map * @param user 用户资料 * @param userMoney 用户资金信息 * @param commonService */ public static OrdersTotal createOrder(OrdersTotal ordersTotal, Map operator, User user, UserMoney userMoney, CustomParameter customParameter,CommonService commonService) { if(ordersTotal!=null && StringUtils.noNull(ordersTotal.getIntegral())){ User users=commonService.selectOneByKeyBlob(UserMapper.class,ordersTotal.getInviteeId()); if(users!=null){ ordersTotal.setUserBelongingType(users.getUserBelongingType()); } } //活动规则金额判断 if( ordersTotal.getActivityIds() !=null && ordersTotal.getActivityIds().size()>0){ BigDecimal price=activityRule(ordersTotal,commonService,operator,userMoney,customParameter,user,0); if(price !=BigDecimal.ZERO){ ordersTotal.setActivityTotal(ordersTotal.getShouldTotal().subtract(price)); ordersTotal.setShouldTotal(price); } } if(StringUtils.noNull(ordersTotal.getKoapOrderId())){ BigDecimal koapItemMoney = SynOrderUtil.koapItemMoney(ordersTotal.getKoapOrderId(), commonService); ordersTotal.setShouldTotal(koapItemMoney); ordersTotal.setTotal(koapItemMoney); } ordersTotal.setUserLevel(user.getUserLevel()); ordersTotal.setCIQ(user.getCIQ()); ordersTotal.setStoredValueFund(userMoney ==null ?"":userMoney.getStoredValueFund()); ordersTotal.setValueAddedFund(userMoney ==null ?"":userMoney.getValueAddedFund()); ordersTotal.setIntegral(userMoney ==null ?"":userMoney.getIntegral()); //创建总订单 commonService.insert(OrdersTotalMapper.class,ordersTotal); //如果有用活动规则将活动规则保存在orderInfo if( ordersTotal.getActivityIds()!=null && ordersTotal.getActivityIds().size()>0){ //查询活动规则基础信息 ActivityRule activityRule=commonService.selectOneByKey(ActivityRuleMapper.class,ordersTotal.getActivityIds().get(0)); if(activityRule!=null){ OrderInfo orderInfo = new OrderInfo(); orderInfo.setOrderId(ordersTotal.getId()); orderInfo.setActivityId(activityRule.getId()); orderInfo.setActivityName(activityRule.getName()); commonService.insert(OrderInfoMapper.class,orderInfo); } } //生成订单日志 commonService.insert(OrderLogMapper.class,ApiOrderUtil.setOrderLog(ordersTotal,null,operator.get("operatorId"),operator.get("operatorName"),"",0, OrderLogConstants.LOG_TYPE_CREATE)); return ordersTotal; } /** * 创建总订单 * @param ordersTotal 总订单信息 * @param operator 操作人 * @param user 用户资料 * @param userMoney 用户资金信息 * @param commonService */ public static OrdersTotal createOrderNew(OrdersTotal ordersTotal, OperatorDto operator, User user, UserMoney userMoney, CommonService commonService) { if(ordersTotal!=null && StringUtils.noNull(ordersTotal.getIntegral())){ User users=commonService.selectOneByKeyBlob(UserMapper.class,ordersTotal.getInviteeId()); if(users!=null){ ordersTotal.setUserBelongingType(users.getUserBelongingType()); } } ordersTotal.setUserLevel(user.getUserLevel()); ordersTotal.setCIQ(user.getCIQ()); ordersTotal.setStoredValueFund(userMoney ==null ?"":userMoney.getStoredValueFund()); ordersTotal.setValueAddedFund(userMoney ==null ?"":userMoney.getValueAddedFund()); ordersTotal.setIntegral(userMoney ==null ?"":userMoney.getIntegral()); //创建总订单 commonService.insert(OrdersTotalMapper.class,ordersTotal); //生成订单日志 commonService.insert(OrderLogMapper.class,ApiOrderUtil.setOrderLog(ordersTotal,null,operator.getOperatorId(),operator.getOperatorName(),"",0, OrderLogConstants.LOG_TYPE_CREATE)); return ordersTotal; } /** * 开单-处理项目方法 * @param jsonObject 规格json * @param map 参数集合 * @param vo 一级子订单 * @param actualTotal 商品实付金额 * @param sonTotalPrice 商品订单总额 * @param discountTotal 折扣总金额 * @param buyNum 购买次数 * @param project 项目对象 * @return */ public static Map handleProject(JSONArray jsonObject, Map map, OrderItem vo, BigDecimal actualTotal, BigDecimal sonTotalPrice, BigDecimal discountTotal, BigDecimal buyNum, CommonService commonService,Project project) { 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 || BigDecimal.ZERO.compareTo(project.getOriginalPrice())==0?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("");//项目主图 Map moneyMap=new HashMap<>(); moneyMap.put("actualTotal",actualTotal); moneyMap.put("sonTotalPrice",sonTotalPrice); moneyMap.put("discountTotal",discountTotal); return moneyMap; } /** * 开单-处理商品方法 * @param jsonObject 规格json * @param map 参数集合 * @param vo 一级子订单 * @param actualTotal 商品实付金额 * @param sonTotalPrice 商品订单总额 * @param discountTotal 折扣总金额 * @param buyNum 购买次数 * @return */ public static Map handleConsumables(JSONArray jsonObject, Map map, OrderItem vo, BigDecimal actualTotal,BigDecimal sonTotalPrice,BigDecimal discountTotal,BigDecimal buyNum, CommonService commonService) { Consumables consumables = commonService.selectOneByKey(ConsumablesMapper.class,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()); jsonObject.add(map); vo.setSpecs(StringUtils.isEmpty(consumables.getSpec())?"[]":jsonObject.toString());//规格 vo.setGoodsNo(consumables.getSerialNumber());//项目编号 vo.setGoodsName(consumables.getName());//项目名称 vo.setGoodsImage("");//项目主图 Map moneyMap=new HashMap<>(); moneyMap.put("actualTotal",actualTotal); moneyMap.put("sonTotalPrice",sonTotalPrice); moneyMap.put("discountTotal",discountTotal); return moneyMap; } /** * 开单-处理卡项方法 * @param jsonObject 规格json * @param map 参数集合 * @param vo 一级子订单 * @param actualTotal 商品实付金额 * @param sonTotalPrice 商品订单总额 * @param discountTotal 折扣总金额 * @param buyNum 购买次数 * @param userId 用户id * @return */ public static Map handleCardItem(JSONArray jsonObject, Map map, OrderItem vo, BigDecimal actualTotal,BigDecimal sonTotalPrice,BigDecimal discountTotal, BigDecimal buyNum,String userId, CommonService commonService) { SqlSentence sqlSentence = new SqlSentence(); Map sqlMp=new HashMap<>(); sqlSentence.setM(sqlMp); sqlMp.put("status",CardItem.SALES); sqlMp.put("isUp",BaseEntity.YES); sqlMp.put("isDel",BaseEntity.NO); sqlMp.put("commonId",vo.getCommonId()); sqlSentence.setSqlSentence("select * from card_item where id=#{m.commonId} and status=#{m.status} and isUp=#{m.isUp} and isDel=#{m.isDel}"); CardItem cardItem=commonService.selectOne(CardItemMapper.class,sqlSentence); if(cardItem==null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"找不到该卡项,请确保卡项状态必须是销售中并且是上架状态,卡项标识:"+vo.getCommonId()); } if(cardItem.getTotal()==null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"促销:"+cardItem.getName()+",没有销售价格,请填写销售单价在进行创建订单"); } User user=commonService.selectOneByKey(UserMapper.class,userId); if(user==null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"处理促销操作:未找到该用户信息"); } sqlMp.clear(); sqlMp.put("isDel", BaseEntity.NO); //限制会员等级购买 sqlMp.put("foreignKey",cardItem.getId()); checkMemberLevel(sqlSentence, user, OrderItemConstants.TYPE_CARD,commonService); JSONObject param = new JSONObject(); param.put("commonId",cardItem.getId()); param.put("userId",userId); param.put("shopId",map.get("shopId")); //卡项/促销/项目购买限制 LimitPlatformUtil.limitMethod(commonService,param.toJSONString()); //商品实付金额=卡项的销售价格*购买数量*折扣 actualTotal = cardItem.getTotal().multiply(buyNum).multiply(vo.getDiscount().divide(new BigDecimal(100))); //商品订单总额=项目销售价*购买数量 sonTotalPrice=sonTotalPrice.add(cardItem.getTotal().multiply(buyNum)); //优惠金额=商品的销售单价*数量-用户实付金额; discountTotal=discountTotal.add(cardItem.getTotal().multiply(buyNum).subtract(actualTotal)); vo.setSingle(0);//单次疗程数量 vo.setUsedTotal(vo.getSingle()*vo.getBuyNum());//总次疗程次数,单次疗程次数*购买的总量 vo.setOriPrice(cardItem.getTotal());//原价,项目的销售价 vo.setCurPrice(cardItem.getTotal());//现价 vo.setSpecs("[]");//规格 vo.setGoodsNo(cardItem.getCode());//卡项编号 vo.setGoodsName(cardItem.getName());//卡项名称 vo.setGoodsImage("");//卡项主图 Map moneyMap=new HashMap<>(); moneyMap.put("actualTotal",actualTotal); moneyMap.put("sonTotalPrice",sonTotalPrice); moneyMap.put("discountTotal",discountTotal); return moneyMap; } /** * 开单-处理促销方法 * @param jsonObject 规格json * @param map 参数集合 * @param vo 一级子订单 * @param actualTotal 商品实付金额 * @param sonTotalPrice 商品订单总额 * @param discountTotal 折扣总金额 * @param buyNum 购买次数 * @param userId 用户id * @return */ public static Map handlePromotion(JSONArray jsonObject, Map map, OrderItem vo, BigDecimal actualTotal, BigDecimal sonTotalPrice, BigDecimal discountTotal, BigDecimal buyNum, String userId, CommonService commonService) { SqlSentence sqlSentence = new SqlSentence(); Map sqlMp=new HashMap<>(); sqlSentence.setM(sqlMp); sqlMp.put("status",Promotion.IN_PROGRESS); sqlMp.put("isUp",BaseEntity.YES); sqlMp.put("isDel",BaseEntity.NO); sqlMp.put("commonId",vo.getCommonId()); sqlSentence.setSqlSentence("select * from promotion where id=#{m.commonId} and status=#{m.status} and isUp=#{m.isUp} and isDel=#{m.isDel}"); Promotion promotion=commonService.selectOne(PromotionMapper.class,sqlSentence); if(promotion==null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"找不到该促销,请确保促销状态必须是销售中并且是上架状态,促销标识:"+vo.getCommonId()); } if(promotion.getTotal()==null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"促销:"+promotion.getName()+",没有销售价格,请填写销售单价在进行创建订单"); } User user=commonService.selectOneByKey(UserMapper.class,userId); if(user==null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"处理促销操作:未找到该用户信息"); } //限制会员等级购买 sqlMp.clear(); sqlSentence.setM(sqlMp); sqlMp.put("isDel",BaseEntity.NO); sqlMp.put("foreignKey",promotion.getId()); checkMemberLevel(sqlSentence, user, OrderItemConstants.TYPE_PROMOTION,commonService); JSONObject param = new JSONObject(); param.put("commonId",promotion.getId()); param.put("userId",userId); param.put("shopId",map.get("shopId")); //卡项/促销/项目购买限制 LimitPlatformUtil.limitMethod(commonService,param.toJSONString()); //商品实付金额=促销的销售价格*购买数量*折扣 actualTotal = promotion.getTotal().multiply(buyNum).multiply(vo.getDiscount().divide(new BigDecimal(100))); //商品订单总额=项目销售价*购买数量 sonTotalPrice=sonTotalPrice.add(promotion.getTotal().multiply(buyNum)); //优惠金额=商品的销售单价*数量-用户实付金额; discountTotal=discountTotal.add(promotion.getTotal().multiply(buyNum).subtract(actualTotal)); vo.setSingle(0);//单次疗程数量 vo.setUsedTotal(vo.getSingle()*vo.getBuyNum());//总次疗程次数,单次疗程次数*购买的总量 vo.setOriPrice(promotion.getTotal());//原价,项目的销售价 vo.setCurPrice(promotion.getTotal());//现价 vo.setSpecs("[]");//规格 vo.setGoodsNo(promotion.getCode());//促销编号 vo.setGoodsName(promotion.getName());//促销名称 vo.setGoodsImage("");//促销主图 Map moneyMap=new HashMap<>(); moneyMap.put("actualTotal",actualTotal); moneyMap.put("sonTotalPrice",sonTotalPrice); moneyMap.put("discountTotal",discountTotal); return moneyMap; } /** * 检查会员等级限制 * @param sqlSentence * @param user * @param type * @param commonService */ public static void checkMemberLevel(SqlSentence sqlSentence, User user,String type,CommonService commonService) { sqlSentence.setSqlSentence("select * from member_limit where isDel=#{m.isDel} and foreignKey=#{m.foreignKey}"); List list=commonService.selectList(MemberLimitMapper.class,sqlSentence); StringBuffer stringBuffer = null; if(list!=null && list.size()>0){ List collect = list.stream().map(o -> o.getMemberId()).collect(Collectors.toList()); stringBuffer=new StringBuffer(); stringBuffer.append("select hisName from member_level where isDel=#{m.isDel} and id in( "); SqlSplicingTool.selectIn(stringBuffer,collect); sqlSentence.setSqlSentence(stringBuffer.toString()); List memberList=commonService.selectList(MemberLevelMapper.class,sqlSentence); if(memberList!=null){ List memberNameList = memberList.stream().map(o -> o.getHisName()).collect(Collectors.toList()); if(!memberNameList.contains(user.getUserLevel())){ String name=OrderItemConstants.TYPE_CARD.equals(type)?"卡项":"促销"; throw new PlatTipsException(PlatformCode.ERROR_TIPS,"该"+name+"不能购买,该用户等级不在"+name+"范围之内"); } } } } /** * 设置用户默认的开发人信息 * @param ordersTotal * @return */ public static User handUserDeveloper(OrdersTotal ordersTotal,CommonService commonService,List orderTypeList) { //如果开发人集合为空,那么默认用户的所属顾问 SqlSentence sqlSentence = new SqlSentence(); Map map=new HashMap<>(); sqlSentence.setM(map); map.put("shopId",ordersTotal.getDeveloperShopId()); User user = commonService.selectOneByKey(UserMapper.class,ordersTotal.getUserId()); if(StringUtils.noNull(user.getHisCorpUserId()) && !orderTypeList.contains(ordersTotal.getType())) { map.put("hisCorpUserId",user.getHisCorpUserId()); sqlSentence.setSqlSentence("SELECT s.* FROM employee_role er LEFT JOIN shop s ON er.shopId = s.id WHERE er.isDel=0 and er.employeeId = #{m.hisCorpUserId} AND er.roleUniqueStr ='adviser_leader' and s.id=#{m.shopId}"); Shop userShop = commonService.selectOne(ShopMapper.class,sqlSentence); if (userShop == null) { throw new PlatTipsException(PlatformCode.ERROR_TIPS, "用户绑定的顾问不在该门店"); } Employee userAdiver =commonService.selectOneByKey(EmployeeMapper.class,user.getHisCorpUserId()); //开发人门店标识(用户绑定的销售顾问) ordersTotal.setDeveloperShopId(userShop.getId()); //开发人门店名称 ordersTotal.setDeveloperShopName(userShop.getName()); ordersTotal.setDeveloperId(userAdiver.getId()); ordersTotal.setDeveloperName(userAdiver.getCnName()); } //荣爷说开发人必填此处不需要默认用户的咨询师 22-05-27 // else if(!orderTypeList.contains(ordersTotal.getType())){ // throw new PlatTipsException(PlatformCode.ERROR_TIPS,"请填写好开发人或者用户绑定一个销售顾问在重试"); // } return user; } /** * 创建开发人集合记录 * @param ordersTotal * @param orderDeveloperList */ public static void creatDeveloperList(OrdersTotal ordersTotal, List orderDeveloperList,CommonService commonService) { if(orderDeveloperList !=null && orderDeveloperList.size()>0){ for(OrderDeveloper vo:orderDeveloperList){ vo.setOrderId(ordersTotal.getId());//总订单id commonService.insert(OrderDeveloperMapper.class,vo); } } } /** * 价格判断 * @param ordersTotal 总订单 * @param commonService * @param commonId 公共标识,/项目/商品/卡项/促销/ * @return */ public static BigDecimal priceJudge(OrdersTotal ordersTotal, CommonService commonService, String commonId,String shopId) { BigDecimal price=BigDecimal.ZERO; SqlSentence sqlSentence=new SqlSentence(); Map objectMap=new HashMap<>(); sqlSentence.setM(objectMap); if(StringUtils.noNull(ordersTotal.getKoapOrderId())){ objectMap.put("koapId",ordersTotal.getKoapOrderId()); sqlSentence.setSqlSentence("SELECT * FROM koap_order_item WHERE orderId=#{m.koapId}"); List koapOrderItemList=commonService.selectList(KoapOrderItemMapper.class,sqlSentence); if(koapOrderItemList==null || koapOrderItemList.size()==0){ throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"结账:未找到康博佳子订单"); } //康博佳子订单的应收金额之和(这个金额要等于paymentList里面的所有之和) price=koapOrderItemList.stream().map(o -> o.getChargeAmount()).reduce(BigDecimal.ZERO, BigDecimal::add); }else{ objectMap.put("id",commonId); objectMap.put("shopId",shopId); objectMap.put("isUp",BaseEntity.YES); sqlSentence.setSqlSentence("SELECT * FROM project_shop_price WHERE commonId=#{m.id} and shopId=#{m.shopId} and isUp=#{m.isUp}"); ProjectShopPrice shopPrice=commonService.selectOne(ProjectShopPriceMapper.class,sqlSentence); if(shopPrice!=null){ price=shopPrice.getPrice(); } } return price; } /** * 计算活动规则价格,如果是折扣或者满减就需要在创建订单计算好价格(荣爷说),其他动作分别都在结账处理 * @param ordersTotal * @param type 0:创建订单,1:结账,类型区分来源,如果是创建订单则只需要判断是否有满减或者折扣进行口减总订单的金额,如果是结账则只需要进行将积分、增值金额、优惠券 * @return */ public static BigDecimal activityRule(OrdersTotal ordersTotal,CommonService commonService,Map operator,UserMoney userMoney, CustomParameter customParameter,User user,Integer type) { BigDecimal price=BigDecimal.ZERO; if(ordersTotal.getActivityIds() !=null && ordersTotal.getActivityIds().size()>0){ //查询活动规则基础信息 ActivityRule activityRule=commonService.selectOneByKey(ActivityRuleMapper.class,ordersTotal.getActivityIds().get(0)); if(activityRule!=null){ SqlSentence sqlSentence = new SqlSentence(); Map map=new HashedMap(); sqlSentence.setM(map); map.put("isDel",BaseEntity.NO); map.put("ruleId",activityRule.getId()); sqlSentence.setSqlSentence("select * from activity_group where isDel=#{m.isDel} and activityRuleId=#{m.ruleId}"); //查找活动规则的分组 List activityGroup = commonService.selectList(ActivityGroupMapper.class, sqlSentence); if(activityGroup!=null && activityGroup.size()>0){ BigDecimal convert=BigDecimal.ZERO; for (ActivityGroup group : activityGroup) { map.put("activityGroupId",group.getId()); //type=0,代表是创建订单,需要将活动规则的满减或者满减(折扣)进行价格计算 if(type==0){ price=fullReduce(convert,ordersTotal,sqlSentence,commonService); }else if(type==1){ //如果是结账需要将活动规则的增值金、积分、优惠券到账 otherAction(convert,ordersTotal,operator,userMoney,customParameter,sqlSentence,user,commonService); } } } } } return price; } /** * 活动规则-处理满减(金额/折扣)动作方法 * @param commonService * @param convert * @param ordersTotal * @return */ public static BigDecimal fullReduce(BigDecimal convert,OrdersTotal ordersTotal,SqlSentence sqlSentence,CommonService commonService){ sqlSentence.setSqlSentence("select * from activity_action where activityGroupId=#{m.activityGroupId} and (type='full_reduce' or type='discount') "); //查找活动组的满减金额或者满减(折扣)的动作,满减金额或者满减折扣一个组中只能存在一个 List activityActions = commonService.selectList(ActivityActionMapper.class, sqlSentence); if(activityActions!=null && activityActions.size()==1){ sqlSentence.setSqlSentence("select * from activity_condition where activityGroupId=#{m.activityGroupId}"); //根据活动组来查询活动的条件集合,同时满足才能进行满减金额或者满减折扣 List activityConditions = commonService.selectList(ActivityConditionMapper.class, sqlSentence); if(activityConditions!=null && activityConditions.size()>0){ boolean flag=false; for(ActivityCondition activityCondition:activityConditions){ convert=new BigDecimal(activityCondition.getWorth()); boolean isFull = checkCondition(ordersTotal, commonService, convert, activityCondition); //只要有一条条件不满足就直接返回活动价格,一个组里面所有条件都需要同时满足 if(!isFull){ throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"活动规则:该订单不满足活动规则条件,请仔细检查活动,活动规则条件标识:"+activityCondition.getId()); // return BigDecimal.ZERO; } logger.info("开单:isFull:{}",isFull); flag=isFull; } if(flag){ logger.info("开单:isFull:{}",flag); for (ActivityAction activityAction : activityActions) { convert=new BigDecimal(activityAction.getWorth()); if(ActivityAction.TYPE_DISCOUNT.equals(activityAction.getType())){ return ordersTotal.getShouldTotal().multiply(convert).divide(new BigDecimal(100)); }else if(ActivityAction.TYPE_FULL_REDUCE.equals(activityAction.getType())){ return ordersTotal.getShouldTotal().subtract(convert); } } } } }else if(activityActions!=null && activityActions.size()>1){ throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"活动规则:创建订单时找到一个组中同时存在折扣和满减"); } return BigDecimal.ZERO; } /** * 活动规则-其他动作(增值金、积分、优惠券) 结账使用 * @param convert * @param ordersTotal 总订单 * @param operator 操作人map * @param userMoney 用户资金信息 * @param customParameter * @param sqlSentence * @param commonService */ private static void otherAction(BigDecimal convert, OrdersTotal ordersTotal,Map operator,UserMoney userMoney, CustomParameter customParameter,SqlSentence sqlSentence,User user,CommonService commonService) { List fullTypeList = Arrays.asList(ActivityAction.TYPE_FULL_REDUCE, ActivityAction.TYPE_DISCOUNT); sqlSentence.setSqlSentence("select * from activity_action where activityGroupId=#{m.activityGroupId} "); //查找活动组的满减金额或者满减(折扣)的动作,满减金额或者满减折扣一个组中只能存在一个 List activityActions = commonService.selectList(ActivityActionMapper.class, sqlSentence); if(activityActions!=null && activityActions.size()>0){ activityActions= activityActions.stream().filter(o -> !fullTypeList.contains(o.getType())).collect(Collectors.toList()); sqlSentence.setSqlSentence("select * from activity_condition where activityGroupId=#{m.activityGroupId}"); //根据活动组来查询活动的条件集合,同时满足才能进行满减金额或者满减折扣 List activityConditions = commonService.selectList(ActivityConditionMapper.class, sqlSentence); if(activityConditions!=null && activityConditions.size()>0){ boolean flag=false; for(ActivityCondition activityCondition:activityConditions){ convert=new BigDecimal(activityCondition.getWorth()); if(ordersTotal.getActivityTotal()!=null){ convert=convert.subtract(ordersTotal.getActivityTotal()); } boolean isFull = checkCondition(ordersTotal, commonService, convert, activityCondition); //只要有一条条件不满足就结束 if(!isFull){ throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"活动规则:该订单结账不满足活动规则条件,请仔细检查活动,活动规则条件标识:"+activityCondition.getId()); } logger.info("结账:isFull:{}",isFull); flag=isFull; } if(flag){ logger.info("结账:isFull:{}",flag); //用户信息集合 net.sf.json.JSONArray array; net.sf.json.JSONObject object; CouponReleaseRecordItem couponReleaseRecordItem; BigDecimal worth; for (ActivityAction activityAction : activityActions) { switch (activityAction.getType()){ case ActivityAction.TYPE_VALUEADDEDFUND: worth=new BigDecimal(activityAction.getWorth()); //增加增值金 // ValueAddedFundTool.add(ordersTotal.getUserId(),ordersTotal.getId(),convert); // UserMoneyUtil.dealValueAddFund(operator,ordersTotal.getUserId(),userMoney,convert,customParameter,commonService,1); UserMoneyUtil.setUserMoneyUnclaimed(ordersTotal, activityAction.getId(),worth,UserMoneyUnclaimed.FUND_TYPE_VALUE_ADDED_FUND,OperationReasonConstants.OP_REASON_ACTIVITY_RULE_GIFT,commonService,UserMoneyUnclaimed.YES); break; case ActivityAction.TYPE_INTEGRAL: worth=new BigDecimal(activityAction.getWorth()); //增加积分,积分先暂时不做是否过期 // ValueAddedFundTool.add(ordersTotal.getUserId(),ordersTotal.getId(),convert); // UserMoneyUtil.dealIntegral(operator,ordersTotal.getUserId(),userMoney,convert,customParameter,commonService,1); UserMoneyUtil.setUserMoneyUnclaimed(ordersTotal, activityAction.getId(),worth,UserMoneyUnclaimed.FUND_TYPE_INTEGRAL,OperationReasonConstants.OP_REASON_ACTIVITY_RULE_GIFT,commonService,UserMoneyUnclaimed.YES); break; case ActivityAction.TYPE_COUPON: if(ActivityAction.TYPE_PLATFORM_CRM.equals(activityAction.getPlatformType())){ SynOrderUtil.sendCrmCoupon(customParameter, user.getTel(),user.getApiId(), activityAction.getCrmCouponId()); }else if(ActivityAction.TYPE_PLATFORM_PHIS.equals(activityAction.getPlatformType())){ object=new net.sf.json.JSONObject(); object.put("apiId",user.getApiId()); object.put("createTime",user.getCreateTime()); object.put("gender",user.getGender()); object.put("id",user.getId()); object.put("imgUrl",user.getImgUrl()); object.put("name",user.getName()); object.put("tel",user.getTel()); array=new net.sf.json.JSONArray(); array.add(object); couponReleaseRecordItem=new CouponReleaseRecordItem(); couponReleaseRecordItem.setCouponId(activityAction.getWorth()); couponReleaseRecordItem.setRecordName("系统赠送"); //赠送优惠券给用户 CouponTool.sendCoupon(couponReleaseRecordItem,operator.get("operatorId"),operator.get("operatorName"),operator.get("roleId"),array,commonService); } break; } } } } } } /** * 活动规则-判断条件是否符合 * @param ordersTotal 总订单 * @param commonService * @param convert 条件值 * @param activityCondition 条件对象 * @return */ private static boolean checkCondition(OrdersTotal ordersTotal, CommonService commonService, BigDecimal convert, ActivityCondition activityCondition) { boolean flag=false; if(ActivityCondition.TYPE_ORDER.equals(activityCondition.getGenre())){ switch (activityCondition.getSymbol()){ case "<": if(ordersTotal.getShouldTotal().compareTo(convert)<0){ flag=true; } break; case ">": if(ordersTotal.getShouldTotal().compareTo(convert)>0){ flag=true; } break; case "<=": if(ordersTotal.getShouldTotal().compareTo(convert)<=0){ flag=true; } break; case ">=": if(ordersTotal.getShouldTotal().compareTo(convert)>=0){ flag=true; } case "=": if(ordersTotal.getShouldTotal().compareTo(convert)==0){ flag=true; } break; default: break; } }else if(ActivityCondition.TYPE_USER.equals(activityCondition.getGenre())){ switch (activityCondition.getSymbol()){ case "=": if(ActivityCondition.CONDITION_USER_LEVEL.equals(activityCondition.getPrerequisite())){ User user=commonService.selectOneByKey(UserMapper.class,ordersTotal.getUserId()); if(user==null){ throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"活动规则:条件为用户条件时根据订单未找到该用户信息"); } if(activityCondition.getWorth().equals(user.getUserLevel())){ flag=true; } }else if(ActivityCondition.CONDITION_USER_CLINCH_TAG.equals(activityCondition.getPrerequisite())){ } break; case "in": if(ActivityCondition.CONDITION_USER_LEVEL.equals(activityCondition.getPrerequisite())){ List worths = Arrays.asList(activityCondition.getWorth().split(",")); User user=commonService.selectOneByKey(UserMapper.class,ordersTotal.getUserId()); if(user==null){ throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"活动规则:条件为用户条件时根据订单未找到该用户信息"); } if(worths.contains(user.getUserLevel())){ flag=true; } }else if(ActivityCondition.CONDITION_USER_CLINCH_TAG.equals(activityCondition.getPrerequisite())){ } break; default: break; } } return flag; } /** * 开单-处理卡包方法 * @param jsonObject 规格json * @param map 参数集合 * @param vo 一级子订单 * @param actualTotal 商品实付金额 * @param sonTotalPrice 商品订单总额 * @param discountTotal 折扣总金额 * @param buyNum 购买次数 * @param userId 用户id * @return */ public static Map handleCardBag(JSONArray jsonObject, Map map, OrderItem vo, BigDecimal actualTotal,BigDecimal sonTotalPrice,BigDecimal discountTotal, BigDecimal buyNum,String userId, CommonService commonService) { SqlSentence sqlSentence = new SqlSentence(); Map sqlMp=new HashMap<>(); sqlSentence.setM(sqlMp); sqlMp.put("status",CardItem.SALES); sqlMp.put("isUp",BaseEntity.YES); sqlMp.put("isDel",BaseEntity.NO); sqlMp.put("commonId",vo.getCommonId()); sqlSentence.setSqlSentence("select * from card_item where id=#{m.commonId} and status=#{m.status} and isUp=#{m.isUp} and isDel=#{m.isDel}"); CardItem cardItem=commonService.selectOne(CardItemMapper.class,sqlSentence); if(cardItem==null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"卡包:找不到该卡项,请确保卡项状态必须是销售中并且是上架状态,卡项标识:"+vo.getCommonId()); } if(cardItem.getTotal()==null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"卡包:"+cardItem.getName()+",没有销售价格,请填写销售单价在进行创建订单"); } User user=commonService.selectOneByKey(UserMapper.class,userId); if(user==null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"卡包:处理促销操作:未找到该用户信息"); } sqlSentence.setSqlSentence("select * from card_equity where cardItemId=#{m.commonId} and isDel=#{m.isDel}"); List cardEquities=commonService.selectList(CardEquityMapper.class,sqlSentence); if(cardEquities==null || cardEquities.size()==0){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"卡包:"+cardItem.getName()+",没有找到权益信息!"); } BigDecimal sumCardBagMoney =BigDecimal.ZERO; BigDecimal userNum =null; if(vo.getCardItemInfoIds()!=null && vo.getCardItemInfoIds().size()>0){ for(CardItemInfo info:vo.getCardItemInfoIds()){ userNum=new BigDecimal(info.getUserNum()); sqlMp.clear(); sqlMp.put("id",info.getId()); sqlSentence.setSqlSentence("select * from card_item_info where id =#{m.id} and isDel=0 "); CardItemInfo cardItemInfo = commonService.selectOne(CardItemInfoMapper.class, sqlSentence); if(cardItemInfo!=null){ BigDecimal shareMoney=cardItemInfo.getShareMoney()==null?cardItemInfo.getEveryShareMoney():cardItemInfo.getShareMoney(); sumCardBagMoney=sumCardBagMoney.add(shareMoney.multiply(userNum)); } } } sqlMp.clear(); sqlMp.put("isDel", BaseEntity.NO); //限制会员等级购买 sqlMp.put("foreignKey",cardItem.getId()); checkMemberLevel(sqlSentence, user, OrderItemConstants.TYPE_CARD,commonService); JSONObject param = new JSONObject(); param.put("commonId",cardItem.getId()); param.put("userId",userId); param.put("shopId",map.get("shopId")); //卡项/促销/项目购买限制 LimitPlatformUtil.limitMethod(commonService,param.toJSONString()); //商品实付金额=卡项的销售价格*购买数量*折扣 actualTotal = BigDecimal.ZERO; //商品订单总额=项目销售价*购买数量 sonTotalPrice=sumCardBagMoney; //优惠金额=商品的销售单价*数量-用户实付金额; discountTotal=discountTotal.add(sumCardBagMoney.multiply(buyNum).subtract(actualTotal)); vo.setSingle(0);//单次疗程数量 vo.setUsedTotal(vo.getSingle()*vo.getBuyNum());//总次疗程次数,单次疗程次数*购买的总量 vo.setOriPrice(sumCardBagMoney);//原价,项目的销售价 vo.setCurPrice(sumCardBagMoney);//现价 vo.setSpecs("[]");//规格 vo.setGoodsNo(cardItem.getCode());//卡项编号 vo.setGoodsName(cardItem.getName());//卡项名称 vo.setGoodsImage("");//卡项主图 Map moneyMap=new HashMap<>(); moneyMap.put("actualTotal",actualTotal); moneyMap.put("sonTotalPrice",sonTotalPrice); moneyMap.put("discountTotal",discountTotal); return moneyMap; } /** * 开单-创建卡项的二级子订单 * @param ordersId 总订单标识 * @param vo * @param */ public static void createOrderItemSonCard(String ordersId, OrderItem vo, CardItemInfo cardItemInfo,Integer type,CommonService commonService) { // for(CardItemInfo cardItemInfo:cardItemInfos){ if(cardItemInfo.getPrice()==null){ throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"开单:卡项组合项目价格为空,组合项标识:"+cardItemInfo.getId()); } if(cardItemInfo.getMaxNum()==null && cardItemInfo.getEveryDrawNum()==null){ throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"开单:卡项组合项目最大次数或者每次划扣的扣减次数为空,组合项标识:"+cardItemInfo.getId()); } BigDecimal total = cardItemInfo.getPrice().multiply(new BigDecimal(cardItemInfo.getMaxNum() ==null ?cardItemInfo.getEveryDrawNum():cardItemInfo.getMaxNum())); OrderItemSon orderItemSon=new OrderItemSon(); orderItemSon.setType(cardItemInfo.getCommonType()); orderItemSon.setGoodsNo(cardItemInfo.getCommonCode()); orderItemSon.setGoodsId(cardItemInfo.getCommonId()); orderItemSon.setGoodsName(cardItemInfo.getCommonName()); orderItemSon.setGoodsImage("");//暂定为空 orderItemSon.setSpecs(cardItemInfo.getSpecJson()); orderItemSon.setTotal(total); orderItemSon.setActualTotal(total); orderItemSon.setOriPrice(cardItemInfo.getShareMoney()==null?cardItemInfo.getEveryShareMoney():cardItemInfo.getShareMoney()); orderItemSon.setCurPrice(cardItemInfo.getShareMoney()==null?cardItemInfo.getEveryShareMoney():cardItemInfo.getShareMoney()); orderItemSon.setUsedTotal(0);//暂定为0 orderItemSon.setUsedOne(0);//暂定为0 orderItemSon.setBuyNum(OrderItemConstants.CARD_BAG.equals(vo.getType())?cardItemInfo.getUserNum():cardItemInfo.getMaxNum() ==null ?cardItemInfo.getEveryDrawNum():cardItemInfo.getMaxNum()); orderItemSon.setOrderId(ordersId); orderItemSon.setOrderItemId(vo.getId()); orderItemSon.setCardItemInfoId(cardItemInfo.getId()); orderItemSon.setCardEquityId(cardItemInfo.getCardEquityId()); orderItemSon.setIsCardBag(type==0?0:1); commonService.insert(OrderItemSonMapper.class,orderItemSon); // } } }