chenjiahe
2024-01-19 d83d729f80576bb4c7e73bcfff5c403a23f23b82
提交 | 用户 | age
37deae 1 package com.hx.phip.util.api;
Z 2
f9fd77 3 import com.alibaba.fastjson.JSON;
Z 4 import com.alibaba.fastjson.JSONArray;
5 import com.alibaba.fastjson.JSONObject;
37deae 6 import com.hx.common.service.CommonService;
f15332 7 import com.hx.exception.TipsException;
fa260e 8 import com.hx.guide.model.VisitRecord;
37deae 9 import com.hx.mybatisTool.SqlSentence;
253802 10 import com.hx.phiappt.common.*;
f9fd77 11 import com.hx.phiappt.constants.enums.GroupTypeEnum;
f15332 12 import com.hx.phiappt.constants.tool.order.OrderNewUtil;
185529 13 import com.hx.phiappt.constants.tool.order.OrderUtil;
a24ed7 14 import com.hx.phiappt.constants.tool.user.UserChannelTool;
eb25b2 15 import com.hx.phiappt.dao.mapper.*;
f9fd77 16 import com.hx.phiappt.dao.mapper.PromotionInfoMapper;
37deae 17 import com.hx.phiappt.model.*;
44d306 18 import com.hx.phiappt.model.activity.ActivityAction;
9f6958 19 import com.hx.phiappt.model.activity.ActivityRule;
932a23 20 import com.hx.phiappt.model.cardItem.CardEquity;
8c307c 21 import com.hx.phiappt.model.cardItem.CardItem;
f9fd77 22 import com.hx.phiappt.model.cardItem.CardItemInfo;
37deae 23 import com.hx.phiappt.model.channels.Channels;
8c307c 24 import com.hx.phiappt.model.consumables.Consumables;
253802 25 import com.hx.phiappt.model.consumables.ConsumablesAssoc;
a026ab 26 import com.hx.phiappt.model.limit.LimitTotal;
f9fd77 27 import com.hx.phiappt.model.order.*;
95d8e8 28 import com.hx.phiappt.model.project.ProjectInfo;
227b3e 29 import com.hx.phiappt.model.project.ProjectShopPrice;
04c26a 30 import com.hx.phiappt.model.project.ProjectSpecValue;
8c307c 31 import com.hx.phiappt.model.promotion.Promotion;
f9fd77 32 import com.hx.phiappt.model.promotion.PromotionInfo;
04c26a 33 import com.hx.phiappt.model.specification.Specification;
Z 34 import com.hx.phiappt.model.specification.SpecificationValue;
44d306 35 import com.hx.phiappt.model.system.ResendRecord;
f9fd77 36 import com.hx.phip.config.CustomParameter;
37deae 37 import com.hx.phip.dao.mapper.*;
eb25b2 38 import com.hx.phip.dao.mapper.ActivityActionMapper;
F 39 import com.hx.phip.dao.mapper.ActivityRuleMapper;
932a23 40 import com.hx.phip.dao.mapper.CardEquityMapper;
eb25b2 41 import com.hx.phip.dao.mapper.CardItemInfoMapper;
F 42 import com.hx.phip.dao.mapper.CardItemMapper;
43 import com.hx.phip.dao.mapper.ChannelsMapper;
44 import com.hx.phip.dao.mapper.ConsumablesMapper;
45 import com.hx.phip.dao.mapper.EmployeeMapper;
46 import com.hx.phip.dao.mapper.EmployeeRoleMapper;
47 import com.hx.phip.dao.mapper.OrderGoodsSpecsMapper;
48 import com.hx.phip.dao.mapper.OrderInfoMapper;
49 import com.hx.phip.dao.mapper.OrderItemMapper;
50 import com.hx.phip.dao.mapper.OrderItemSonMapper;
51 import com.hx.phip.dao.mapper.OrdersActivityInfoItemMapper;
52 import com.hx.phip.dao.mapper.OrdersActivityInfoMapper;
53 import com.hx.phip.dao.mapper.OrdersTotalMapper;
54 import com.hx.phip.dao.mapper.PlatformApplicationMapper;
55 import com.hx.phip.dao.mapper.ProjectInfoMapper;
56 import com.hx.phip.dao.mapper.ProjectMapper;
57 import com.hx.phip.dao.mapper.ProjectShopPriceMapper;
58 import com.hx.phip.dao.mapper.ProjectSpecValueMapper;
59 import com.hx.phip.dao.mapper.PromotionMapper;
60 import com.hx.phip.dao.mapper.ResendRecordMapper;
61 import com.hx.phip.dao.mapper.ShopMapper;
62 import com.hx.phip.dao.mapper.SpecificationMapper;
63 import com.hx.phip.dao.mapper.SpecificationValueMapper;
64 import com.hx.phip.dao.mapper.SysAdminMapper;
65 import com.hx.phip.dao.mapper.SystemParameterMapper;
66 import com.hx.phip.dao.mapper.UserMapper;
67 import com.hx.phip.dao.mapper.UserUnionHisMapper;
f58cfc 68 import com.hx.phip.service.CreateNoService;
8c307c 69 import com.hx.phip.service.OrdersTotalService;
f9fd77 70 import com.hx.phip.service.ProjectService;
e3aeb9 71 import com.hx.phip.tool.AppealTypeTool;
f9fd77 72 import com.hx.phip.tool.CreateNo;
932a23 73 import com.hx.phip.tool.user.UserCardTool;
80fecc 74 import com.hx.phip.vo.AppealTypeVo;
932a23 75 import com.hx.phip.vo.user.UserCardItemInfoVo;
a472d4 76 import com.hx.resultTool.Result;
9f6958 77 import com.hx.util.DateUtil;
f15332 78 import com.hx.util.SimpleTool;
37deae 79 import com.hx.util.StringUtils;
a472d4 80 import com.hz.his.dto.marketing.OrderDiscountDto;
314764 81 import com.hz.his.dto.marketing.OrderPayMarketingDto;
F 82 import com.hz.his.dto.marketing.RefundProjectDto;
5c0db7 83 import com.hz.his.dto.order.OrderPHisDto;
eb25b2 84 import com.hz.his.dto.order.OrderRiseDto;
a472d4 85 import com.hz.his.dto.order.OrdersTotalDto;
Z 86 import com.hz.his.feign.service.marketing.MOrderService;
f9fd77 87 import com.platform.constants.PlatformPattern;
37deae 88 import com.platform.exception.PlatTipsException;
Z 89 import com.platform.resultTool.PlatformCode;
90 import org.slf4j.Logger;
91 import org.slf4j.LoggerFactory;
92
93 import java.math.BigDecimal;
c5e876 94 import java.math.RoundingMode;
5cf0ec 95 import java.util.*;
141ad3 96 import java.util.stream.Collectors;
f9fd77 97
Z 98 import static com.hx.phiappt.constants.enums.GroupTypeEnum.PROJECT;
37deae 99
Z 100 /**
101  * 开单接口工具类
102  *
103  * @Author: zhouxiang
104  * @Date: 2022/09/19/11:57
105  * @Description:
106  */
107 public class OrderCreateUtil {
108     private static final Logger logger = LoggerFactory.getLogger(OrderCreateUtil.class);
109
110     /**
f9fd77 111      * 开单-总订单参数校验                   请求时
8c307c 112      *
Z 113      * @param orderTotal 总订单
37deae 114      * @return
Z 115      */
8c307c 116     public static OrdersTotal checkOrderTotalParam(OrdersTotal orderTotal) {
37deae 117
8c307c 118         if (StringUtils.isEmpty(orderTotal.getType())) {
Z 119             throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL, "订单类型为空");
37deae 120         }
Z 121
8c307c 122         if (StringUtils.isEmpty(orderTotal.getUserId())) {
Z 123             throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL, "用户Id为空");
37deae 124         }
8c307c 125         if (StringUtils.isEmpty(orderTotal.getDeveloperShopId())) {
Z 126             throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL, "开发人门店id为空");
37deae 127         }
1366e2 128         if (orderTotal.getOrderClassify() == null) {
C 129             orderTotal.setOrderClassify(OrderTotalConstants.ORDER_CLASSIFY_STANDARD);
130         }else{
131             if(!OrderTotalConstants.orderClassifyCheck(orderTotal.getOrderClassify())){
132                 throw new PlatTipsException(PlatformCode.ERROR_TIPS, "订单分类不存在!");
133             }
134         }
37deae 135         return orderTotal;
Z 136     }
137
138     /**
f9fd77 139      * 开单-默认总订单参数                   请求时
8c307c 140      *
Z 141      * @param orderTotal 总订单
37deae 142      */
c4e004 143     public static void setDefaultOrderTotal(OrdersTotal orderTotal, String appId, String code,User user, CommonService commonService) {
8c307c 144         SqlSentence sqlSentence = new SqlSentence();
Z 145         Map<String, Object> map = new HashMap<>();
37deae 146         sqlSentence.setM(map);
Z 147         map.put("isDel", BaseEntity.NO);
8c307c 148         map.put("isUp", BaseEntity.YES);
37deae 149         //处理默认销售平台名称
8c307c 150         if (StringUtils.noNull(orderTotal.getSourceCode()) && StringUtils.isEmpty(orderTotal.getSourceName())) {
Z 151             map.put("sourceCode", orderTotal.getSourceCode());
37deae 152             sqlSentence.setSqlSentence("select id,name from platform_application where code = #{m.sourceCode} and isUp=#{m.isUp} and isDel=#{m.isDel} ");
Z 153             List<PlatformApplication> platformApplications = commonService.selectList(PlatformApplicationMapper.class, sqlSentence);
8c307c 154             if (platformApplications == null || platformApplications.size() == 0) {
Z 155                 throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL, "销售平台编码错误,根据销售平台编码未找到平台信息!");
156             } else if (platformApplications.size() > 1) {
157                 throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL, "查找出多条平台信息,根据销售平台编码找到多条平台信息!");
158             } else if (platformApplications.size() == 1) {
37deae 159                 PlatformApplication platformApplication = platformApplications.get(0);
8c307c 160                 orderTotal.setSourceName(platformApplication == null ? "" : platformApplication.getName());
37deae 161             }
Z 162         }
163         //处理默认开单人名称
8c307c 164         if (StringUtils.noNull(orderTotal.getDeveloperId()) && StringUtils.isEmpty(orderTotal.getDeveloperName())) {
Z 165             Employee employee = commonService.selectOneByKeyBlob(EmployeeMapper.class, orderTotal.getDeveloperId());
166             if (employee == null) {
c4e004 167                 User developerUser = commonService.selectOneByKeyBlob(UserMapper.class, orderTotal.getDeveloperId());
Z 168                 if (developerUser == null) {
8c307c 169                     SysAdmin sysAdmin = commonService.selectOneByKey(SysAdminMapper.class, orderTotal.getDeveloperId());
Z 170                     if (sysAdmin == null) {
171                         logger.info("该开单人标识错误,未找到员工、用户、管理员信息,用户标识:{},开单人标识:{}", orderTotal.getUserId(), orderTotal.getDeveloperId());
172                     } else {
37deae 173                         orderTotal.setDeveloperName(sysAdmin.getName());
Z 174                     }
8c307c 175                 } else {
c4e004 176                     orderTotal.setDeveloperName(developerUser.getName());
37deae 177                 }
8c307c 178             } else {
37deae 179                 orderTotal.setDeveloperName(employee.getCnName());
Z 180             }
181         }
182         //处理默认订单渠道标识
8c307c 183         if (StringUtils.noNull(orderTotal.getCommonId()) && StringUtils.isEmpty(orderTotal.getCommonName())) {
Z 184             Channels channels = commonService.selectOneByKeyBlob(ChannelsMapper.class, orderTotal.getCommonId());
185             if (channels == null) {
186                 logger.info("订单渠道标识错误,未找到订单渠道标识信息,用户标识:{},开单人标识:{}", orderTotal.getUserId(), orderTotal.getCommonId());
187             } else {
37deae 188                 orderTotal.setCommonName(channels.getName());
Z 189             }
190         }
0c698a 191         // 处理默认订单推荐标识
A 192         if (StringUtils.noNull(orderTotal.getInviteeId())) {
193             // 用户推荐人处理
194             if (orderTotal.getInviteeType() == null || UserChannelType.CATEGORY_MEMBERS.equals(orderTotal.getInviteeType())) {
195                 orderTotal.setInviteeType(UserChannelType.CATEGORY_MEMBERS);
196                 User inviteeUser = commonService.selectOneByKeyBlob(UserMapper.class, orderTotal.getInviteeId());
197                 if (inviteeUser == null) {
198                     logger.info("订单推荐标识错误,未找到推荐人(用户)信息,用户标识:{},开单人标识:{}", orderTotal.getUserId(), orderTotal.getInviteeId());
199                 } else {
200                     orderTotal.setInviteeName(inviteeUser.getName());
201                 }
8c307c 202             } else {
0c698a 203                 // 员工推荐人处理
A 204                 orderTotal.setInviteeType(UserChannelType.CATEGORY_EMPLOYEE);
205                 Employee inviteeEmployee = commonService.selectOneByKeyBlob(EmployeeMapper.class, orderTotal.getInviteeId());
206                 if (inviteeEmployee == null) {
207                     logger.info("订单推荐标识错误,未找到推荐人(员工)信息,用户标识:{},开单人标识:{}", orderTotal.getUserId(), orderTotal.getInviteeId());
208                 } else {
209                     orderTotal.setInviteeName(inviteeEmployee.getCnName());
210                 }
37deae 211             }
Z 212         }
213         //处理默认订单其他信息
8c307c 214         if (orderTotal.getOrderInfo() == null) {
37deae 215             OrderInfo orderInfo = new OrderInfo();
Z 216             orderInfo.setIsEmployeeDiscount(BaseEntity.NO);
217             orderInfo.setIsWholeDiscount(BaseEntity.NO);
218             orderInfo.setDiscount(new BigDecimal(100));
219             orderTotal.setOrderInfo(orderInfo);
220         }
221         //设置平台标识和平台编码
222         orderTotal.setAppId(appId);
223         orderTotal.setAppIdCode(code);
5cf0ec 224         if(orderTotal.getActivityIds()==null || orderTotal.getActivityIds().size()==0){
Z 225             orderTotal.setActivityIds(new ArrayList<>());
c4e004 226         }
Z 227
228         //设置用户名称
229         orderTotal.setUserName(user.getName());
230
231         /**
232          * 判断是否是转疗
233          *      1、用户的所属门店和这张订单的门店不一致就是转疗,user:shopId字段,shopId为空不是转疗(禅道327)
234          */
235         if(StringUtils.noNull(user.getShopId()) && !user.getShopId().equals(orderTotal.getDeveloperShopId())){
236             orderTotal.setIsSwitch(BaseEntity.YES);
237         }
238
239         /**
240          * 客户成交标签(首次成交、复购),只有这两种情况,用string类型存文字形式(和爷说)
241          */
242         //判断该用户是否结账成功的订单数
243         map.clear();
244         map.put("userId",user.getId());
245         map.put("isDel", BaseEntity.NO);
246         map.put("payStatus", OrderTotalConstants.PAY_STATUS_SUC);
247         sqlSentence.setSqlSentence(" userId=#{m.userId} and payStatus=#{m.payStatus} and isDel=#{m.isDel}");
248         int paySucNum = commonService.selectCount(OrdersTotalMapper.class, sqlSentence);
249         if(paySucNum==0){
250             orderTotal.setCjLabel(OrderTotalConstants.LABEL_FIRST);
251         }else if(paySucNum >0){
252             orderTotal.setCjLabel(OrderTotalConstants.LABEL_AFTER);
5cf0ec 253         }
37deae 254     }
Z 255
256     /**
f9fd77 257      * 开单-子订单参数校验                   请求时
8c307c 258      *
Z 259      * @param orderItem 一级子订单
37deae 260      * @return
Z 261      */
8c307c 262     public static List<OrderItem> checkOrderItem(List<OrderItem> orderItem, String type) {
Z 263         if ((orderItem == null || orderItem.size() == 0) && !OrderItemConstants.TYPE_RECHARGE.equals(type)) {
264             throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL, "订单的子订单不能为空");
37deae 265         }
8c307c 266         orderItem.forEach(o -> {
Z 267             if (StringUtils.isEmpty(o.getType())) {
268                 throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL, "订单的子订单商品类型为空");
37deae 269             }
Z 270
8c307c 271             if (o.getBuyNum() == null) {
Z 272                 throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL, "订单的子订单购买数量为空");
37deae 273             }
Z 274
8c307c 275             if (StringUtils.isEmpty(o.getCommonId())) {
Z 276                 throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL, "订单的子订单项目id/商品id为空");
37deae 277             }
8c307c 278             if (o.getDiscount() == null) {
Z 279                 throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL, "折扣为空,默认:100");
37deae 280             }
Z 281         });
282         return orderItem;
283     }
f9fd77 284
8c307c 285     /**
Z 286      * 开单-业务逻辑判断(限制、订单重复等判断)
287      *
288      * @param ordersTotal
289      */
290     public static void businessJudge(OrdersTotal ordersTotal, List<OrderItem> items, User user, Shop shop, OrdersTotalService ordersTotalService, CommonService commonService) {
291         //判断应用来源appId和订单编号是否重复
292         List<OrdersTotal> ordersTotals = ordersTotalService.selectAppIdAndNumber(ordersTotal.getOrderNo(), ordersTotal.getAppId());
293         if (ordersTotals != null && ordersTotals.size() > 0) {
294             throw new PlatTipsException("997", "来源应用appId重复:" + ordersTotal.getAppId() + ",请检查");
295         }
296
1ba6d1 297         //判断第三方订单号是否为空,如果销售平台不是p-his、his、guide订单标号抛错
d1aa28 298         List<String> platformType = Arrays.asList(PlatformConstants.TYPE_PLATFORM_HIS, PlatformConstants.TYPE_PLATFORM_PHIS, PlatformConstants.TYPE_PLATFORM_GUIDE);
1ba6d1 299         if(!platformType.contains(ordersTotal.getAppIdCode()) && StringUtils.isEmpty(ordersTotal.getSourceCode())){
d1aa28 300             throw new PlatTipsException(PlatformCode.ERROR_TIPS, "创建订单:请传销售平台的订单编号!" );
Z 301         }
302
8c307c 303         //判断第三方订单号是否重复
Z 304         if (StringUtils.noNull(ordersTotal.getSourceOrderNo())) {
305             List<OrdersTotal> list = ordersTotalService.selectCheckSourceNo(ordersTotal);
306             if (list != null && list.size() > 0) {
307                 throw new PlatTipsException("998", "第三方订单号重复:" + ordersTotal.getSourceOrderNo() + ",请检查");
308             }
309         }
310
311         //判断康博佳订单是否重复
312         List<OrdersTotal> koapOrder = ordersTotalService.selectKoapOrder(ordersTotal.getKoapOrderId());
313         if (koapOrder != null && koapOrder.size() > 0) {
314             throw new PlatTipsException(PlatformCode.ERROR_TIPS, "创建订单:不能重复创建康博佳订单,康博佳订单标识:" + ordersTotal.getKoapOrderId());
315         }
316
d1aa28 317         //查询开发门店
Z 318         Shop developerShop = commonService.selectOneByKey(ShopMapper.class,ordersTotal.getDeveloperShopId());
319         if (developerShop ==null) {
320             throw new PlatTipsException(PlatformCode.ERROR_TIPS, "业务逻辑判断:未找到开单门店信息!" + ordersTotal.getDeveloperShopId());
321         }
322
8c307c 323         SqlSentence sqlSentence;
ea2d26 324         Map<String, Object> objectMap;
8c307c 325
Z 326         for (OrderItem vo : items) {
327             switch (vo.getType()){
328                 case OrderItemConstants.TYPE_PROJECT:
329                     sqlSentence = new SqlSentence();
ea2d26 330                     objectMap=new HashMap<>();
8c307c 331                     sqlSentence.setM(objectMap);
Z 332                     //限制项目
d1aa28 333                     limitProject(user, developerShop, commonService, sqlSentence, objectMap, vo);
8c307c 334                     break;
Z 335                 case OrderItemConstants.TYPE_RETAIL:
336                     sqlSentence = new SqlSentence();
ea2d26 337                     objectMap=new HashMap<>();
8c307c 338                     sqlSentence.setM(objectMap);
Z 339                     //限制商品
d1aa28 340                     limitConsumables(user, developerShop, commonService, sqlSentence, objectMap, vo);
8c307c 341                     break;
Z 342                 case OrderItemConstants.TYPE_CARD:
343                     sqlSentence = new SqlSentence();
ea2d26 344                     objectMap=new HashMap<>();
8c307c 345                     sqlSentence.setM(objectMap);
Z 346                     //限制卡项
d1aa28 347                     limitCard(user, developerShop, commonService, sqlSentence, objectMap, vo);
8c307c 348                     break;
Z 349                 case OrderItemConstants.TYPE_PROMOTION:
350                     sqlSentence = new SqlSentence();
ea2d26 351                     objectMap=new HashMap<>();
8c307c 352                     sqlSentence.setM(objectMap);
Z 353                     //限制促销
d1aa28 354                     limitPromotion(user, developerShop, commonService, sqlSentence, objectMap, vo);
8c307c 355                     break;
Z 356                 case OrderItemConstants.CARD_BAG:break;
357                 //限制卡包,暂时不处理
358                 case OrderItemConstants.TYPE_INTEGRAL:
359                     sqlSentence = new SqlSentence();
ea2d26 360                     objectMap=new HashMap<>();
8c307c 361                     sqlSentence.setM(objectMap);
Z 362                     //限制项目
d1aa28 363                     limitProject(user, developerShop, commonService, sqlSentence, objectMap, vo);
8c307c 364                     break;
Z 365                 default:break;
366             }
367         }
368     }
369
370     /**
371      * 开单-获取平台平台应用信息
372      *
373      * @param ordersTotal   总订单信息
374      * @param commonService
375      * @return
376      */
39db81 377     public static PlatformApplication getPlatformApplication(OrdersTotal ordersTotal, CommonService commonService) {
Z 378         SqlSentence sqlSentence=new SqlSentence();
379         Map<String, Object> appIdMap = new HashMap<>();
380         sqlSentence.setM(appIdMap);
381         PlatformApplication platformApplication ;
8c307c 382         if (StringUtils.isEmpty(ordersTotal.getSourceCode())) {
Z 383             appIdMap.put("appId", ordersTotal.getAppId());
384             sqlSentence.setSqlSentence("SELECT * FROM platform_application WHERE appId =#{m.appId} and isDel=0 and isUp=1");
385             platformApplication = commonService.selectOne(PlatformApplicationMapper.class, sqlSentence);
386         } else {
387             appIdMap.put("isDel", BaseEntity.NO);
388             appIdMap.put("isUp", BaseEntity.YES);
389             appIdMap.put("sourceCode", ordersTotal.getSourceCode());
374c64 390             sqlSentence.setSqlSentence("select * from platform_application where appId = #{m.sourceCode} and isUp=#{m.isUp} and isDel=#{m.isDel} ");
8c307c 391             platformApplication = commonService.selectOne(PlatformApplicationMapper.class, sqlSentence);
Z 392         }
393         return platformApplication;
394     }
395
396     /**
95d8e8 397      * 根据请求参数一级子订单,处理项目、商品、卡项、卡包、促销操作,生成一级子订单,计算总订单金额信息
Z 398      * @param ordersTotal                           总订单
399      * @param items                                 一级子订单
400      * @param totalPrice                            订单总金额
401      * @param shouldOrderTotal                      订单应付总金额
402      * @param discountTotal                         订单折扣总金额
ca03aa 403      * @param commonService 映射
C 404      * @param createNoService 映射
405      * @return 数据
8c307c 406      */
e2c918 407     public static Map<String,Object> handOrderItem(OrdersTotal ordersTotal,Shop shop,String opShopId, List<OrderItem> items,BigDecimal totalPrice,BigDecimal shouldOrderTotal,
ca03aa 408                                                    BigDecimal discountTotal,BigDecimal oriTotal,CommonService commonService,CreateNoService createNoService) {
8c307c 409         BigDecimal buyNum;
6b1639 410         //用来封装规格json集合
6166c5 411         JSONArray jsonObject ;
6b1639 412         //一级子订单规格的map
Z 413         Map<String,Object> map=new HashMap<>();
95d8e8 414
44d306 415         //根据请求参数一级子订单,处理项目、商品、卡项、卡包、促销等操作,生成一级子订单,计算总订单金额信息
8c307c 416         for(OrderItem vo:items){
6166c5 417             jsonObject = new JSONArray();
8c307c 418             buyNum=new BigDecimal(vo.getBuyNum());
ca03aa 419
95d8e8 420             //生成子订单编号,并设置值
ca03aa 421             String orderNum = createNoService.createOrderNo("SO", 16, "yyyyMMdd");//订单编号
8c307c 422             vo.setOrderNo(orderNum);
95d8e8 423
Z 424             //根据请求参数子订单根据类型做出对应处理
8c307c 425             if(OrderItemConstants.TYPE_PROJECT.equals(vo.getType())){
Z 426                 //处理项目
ea2d26 427                 Map<String, BigDecimal> bigDecimalMap =handleProject(jsonObject, map, vo, discountTotal, buyNum,commonService);
8c307c 428                 discountTotal=bigDecimalMap.get("discountTotal");
Z 429             }else if(OrderItemConstants.TYPE_RETAIL.equals(vo.getType())){
430                 //处理商品
e2c918 431                 Map<String, BigDecimal> bigDecimalMap =handleConsumables(shop,null,jsonObject, map, vo, discountTotal, buyNum,commonService);
8c307c 432                 discountTotal=bigDecimalMap.get("discountTotal");
44d306 433             }else if(OrderItemConstants.TYPE_CARD.equals(vo.getType()) ){
8c307c 434                 //处理卡项
ea2d26 435                 Map<String, BigDecimal> bigDecimalMap =handleCardItem(vo, discountTotal, buyNum,commonService);
8c307c 436                 discountTotal=bigDecimalMap.get("discountTotal");
44d306 437             }else if(OrderItemConstants.TYPE_PROMOTION.equals(vo.getType())){
8c307c 438                 //处理促销
ea2d26 439                 Map<String, BigDecimal> bigDecimalMap =handlePromotion(vo, discountTotal, buyNum, commonService);
8c307c 440                 discountTotal=bigDecimalMap.get("discountTotal");
44d306 441             }else if(OrderItemConstants.CARD_BAG.equals(vo.getType())){
8c307c 442                 //处理卡包
ea2d26 443                 Map<String, BigDecimal> bigDecimalMap =handleCardBag(vo, commonService);
8c307c 444                 discountTotal=bigDecimalMap.get("discountTotal");
44d306 445             }else if(OrderItemConstants.TYPE_INTEGRAL.equals(vo.getType())){
8c307c 446                 //处理积分
ea2d26 447                 Map<String, BigDecimal> bigDecimalMap =handleIntegral(jsonObject, map, vo, discountTotal, buyNum,commonService);
8c307c 448                 discountTotal=bigDecimalMap.get("discountTotal");
659adc 449             }else if(OrderItemConstants.TYPE_DRUG.equals(vo.getType())){
Z 450                 //处理药品
e2c918 451                 Map<String, BigDecimal> bigDecimalMap =handleConsumables(shop,opShopId,jsonObject, map, vo, discountTotal, buyNum,commonService);
659adc 452                 discountTotal=bigDecimalMap.get("discountTotal");
20d569 453             }else if(OrderItemConstants.TYPE_COUPON.equals(vo.getType())) {
F 454                 //处理优惠券商品
e2c918 455                 Map<String, BigDecimal> bigDecimalMap = handleConsumables(shop,null, jsonObject, map, vo, discountTotal, buyNum, commonService);
766aa8 456                 discountTotal = bigDecimalMap.get("discountTotal");
e21429 457             }else if(OrderItemConstants.TYPE_TRANSITION_GOODS.equals(vo.getType())) {
C 458                 //过渡商品,没有商品存储,只生成数据
459                 transitionGoodsHandle(vo);
8c307c 460             }
Z 461
e21429 462             //-----积分处理注意,这是第三方传参,如果有传值,那么就以这个为主
ca03aa 463             if(vo.getOrderItemIntegral() != null){
C 464                 if(vo.getOrderItemIntegral().getSaleIntegral() == null
465                         && vo.getOrderItemIntegral().getSaleIntegral().compareTo(BigDecimal.ZERO) < 0){
e21429 466                     throw new TipsException("售价积分必传或者数据有误!");
ca03aa 467                 }
C 468                 if(vo.getOrderItemIntegral().getOriginalIntegral() == null
469                         && vo.getOrderItemIntegral().getOriginalIntegral().compareTo(BigDecimal.ZERO) < 0){
e21429 470                     throw new TipsException("原价积分必传或者数据有误!");
ca03aa 471                 }
C 472                 if(vo.getOrderItemIntegral().getSaleTotal() == null
473                         && vo.getOrderItemIntegral().getSaleTotal().compareTo(BigDecimal.ZERO) < 0){
e21429 474                     throw new TipsException("售价总积分必传或者数据有误!");
ca03aa 475                 }
C 476                 if(vo.getOrderItemIntegral().getOriginalTotal() == null
477                         && vo.getOrderItemIntegral().getOriginalTotal().compareTo(BigDecimal.ZERO) < 0){
e21429 478                     throw new TipsException("原价总积分必传或者数据有误!");
ca03aa 479                 }
C 480
481                 vo.setOriginalIntegral(vo.getOrderItemIntegral().getOriginalIntegral());
482                 vo.setProIntegral(vo.getOrderItemIntegral().getSaleIntegral());
483                 vo.setSellPoints(vo.getOrderItemIntegral().getSaleIntegral());
484
485                 vo.setOriIntegralTotal(vo.getOrderItemIntegral().getOriginalTotal());
486                 vo.setProIntegralTotal(vo.getOrderItemIntegral().getSaleTotal());
487                 vo.setTotalPoints(vo.getOrderItemIntegral().getSaleTotal());
488                 vo.setShouldTotalPoints(vo.getOrderItemIntegral().getSaleTotal());
e05066 489                 vo.setActualTotalPoints(vo.getOrderItemIntegral().getSaleTotal());
ca03aa 490             }
C 491
e8809b 492             //累加总订单售价总金额
3d7887 493             totalPrice=totalPrice.add(vo.getTotal()).setScale(2,RoundingMode.HALF_UP);
e8809b 494             //订单原价总金额
3d7887 495             oriTotal= oriTotal.add(vo.getOriPrice().multiply(new BigDecimal(vo.getBuyNum()))).setScale(2,RoundingMode.HALF_UP);
95d8e8 496             //累加总订单实付金额
3d7887 497             shouldOrderTotal=shouldOrderTotal.add(vo.getActualTotal()).setScale(2,RoundingMode.HALF_UP);
6f4129 498
e21429 499             //----总订单的值计算
C 500             //--积分值赋值
501             ordersTotal.setDiscountIntegral(ordersTotal.getDiscountIntegral().add(vo.getDiscountIntegral()));
502             ordersTotal.setOriIntegralTotal(ordersTotal.getOriIntegralTotal().add(vo.getOriIntegralTotal()));
503             ordersTotal.setProIntegralTotal(ordersTotal.getProIntegralTotal().add(vo.getProIntegralTotal()));
504             ordersTotal.setTotalPoints(ordersTotal.getTotalPoints().add(vo.getTotalPoints()));
505             ordersTotal.setShouldTotalPoints(ordersTotal.getShouldTotalPoints().add(vo.getShouldTotalPoints()));
506             ordersTotal.setActualTotalPoints(ordersTotal.getActualTotalPoints().add(vo.getActualTotalPoints()));
8c307c 507         }
95d8e8 508
012323 509         ordersTotal.setTotal(totalPrice);
C 510
95d8e8 511         //返回数据
Z 512         map.clear();
e21429 513        // map.put("items",items);
95d8e8 514         map.put("totalPrice",totalPrice);
Z 515         map.put("shouldOrderTotal",shouldOrderTotal);
516         map.put("discountTotal",discountTotal);
e8809b 517         map.put("oriTotal",oriTotal);
95d8e8 518         return map;
6b1639 519     }
Z 520
521     /**
522      *  开单-处理项目方法
523      * @param jsonObject            规格json
524      * @param map                   参数集合
525      * @param vo                    一级子订单
526      * @param discountTotal         折扣总金额
527      * @param buyNum                购买次数
528      * @return
529      */
ea2d26 530     public static Map<String, BigDecimal> handleProject(JSONArray jsonObject, Map<String, Object> map, OrderItem vo, BigDecimal discountTotal, BigDecimal buyNum,
95d8e8 531                                                         CommonService commonService) {
Z 532         SqlSentence sqlSentence=new SqlSentence();
42f9be 533         Map<String,Object> objectMap=new HashMap<>();
Z 534         sqlSentence.setM(objectMap);
535         objectMap.put("commonId",vo.getCommonId());
536         objectMap.put("isDel",BaseEntity.NO);
537
95d8e8 538         Project project=commonService.selectOneByKeyBlob(ProjectMapper.class,vo.getCommonId());
Z 539         if(project==null){
540             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"找不到项目:"+vo.getCommonId());
541         }
542         //计算项目价格,如果总订单有康博佳标识代表是康博佳订单不需要任何处理直接拿去康博佳的总金额,每个门店价格不一样,如果没有设置门店价格就使用项目售价
543 //      BigDecimal price=priceJudge(ordersTotal, commonService,project.getId(),shopId);
544 //      project.setPrice(BigDecimal.ZERO.compareTo(price)==0?project.getPrice():price);
6b1639 545         if(project.getPrice()==null){
Z 546             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"项目:"+project.getName()+",没有销售价格,请填写销售单价再进行创建订单");
95d8e8 547         }
Z 548         if(project.getIsUp()==BaseEntity.NO){
549             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"项目:"+project.getName()+",该项目已下架");
550         }
551         if(project.getIsDel()==BaseEntity.YES){
552             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"项目:"+project.getName()+",该项目已删除");
6b1639 553         }
44d306 554     //计算订单金额信息
Z 555         //一级子订单:售价=项目售价*折扣
3d7887 556         BigDecimal actualTotal = project.getPrice().multiply(vo.getDiscount().divide(new BigDecimal(100))).setScale(2,RoundingMode.HALF_UP);
6b1639 557
Z 558
44d306 559         //总订单:订单优惠总金额=项目的销售单价*数量减去一级子订单售价;
3d7887 560         discountTotal=discountTotal.add(project.getPrice().multiply(buyNum).subtract(actualTotal.multiply(buyNum))).setScale(2,RoundingMode.HALF_UP);
95d8e8 561
Z 562     //一级子订单金额信息设值
24f2a1 563         //设置原价,原价=项目的销售价格
Z 564         vo.setOriPrice(project.getOriginalPrice());
fd269d 565         //设置项目、商品、促销、卡项现价的售价
24f2a1 566         vo.setProPrice(project.getPrice());
Z 567         //设置订单售价, 售价=项目的销售价格*折扣
95d8e8 568         vo.setCurPrice(actualTotal);
Z 569         //设置优惠前小计,优惠前小计=售价* 商品数量
570         vo.setTotal(vo.getCurPrice().multiply(buyNum));
571         //设置子订单优惠券抵扣金额,开单不知道是否使用优惠券,默认为0
572         vo.setDiscountPrice(BigDecimal.ZERO);
573         //设置子订单优惠后小计,优惠后小计=优惠前小计-优惠券抵扣金额
574         vo.setActualTotal(vo.getTotal().subtract(vo.getDiscountPrice()));
575         //设置用户已支付总金额,开单默认为0
576         vo.setUserPaidTotal(BigDecimal.ZERO);
06d6ca 577         //设置item总积分
Z 578         vo.setTotalPoints(BigDecimal.ZERO);
579         //设置售价积分
580         vo.setSellPoints(BigDecimal.ZERO);
44d306 581
42f9be 582         sqlSentence.setSqlSentence("select * from project_info where projectId=#{m.commonId} and isDel=#{m.isDel} ");
Z 583         ProjectInfo projectInfo=commonService.selectOne(ProjectInfoMapper.class,sqlSentence);
584
95d8e8 585     //一级子订单商品信息设值
44d306 586         //设置一级子订单规格,如果项目规格json没有值,则自动封装json规格(内容默认:规格值、单位、身体部位、医生资质),否则就采用项目规格json
04c26a 587         sqlSentence.setSqlSentence("select * from project_spec_value where projectId=#{m.commonId} and isDel=#{m.isDel} ");
Z 588         List<ProjectSpecValue> projectSpecValues = commonService.selectList(ProjectSpecValueMapper.class, sqlSentence);
589         if(projectSpecValues!=null && projectSpecValues.size()>0){
590             for (ProjectSpecValue projectSpecValue : projectSpecValues) {
a64c8b 591                 Map<String,Object> specMap=new HashMap<>();
04c26a 592                 SpecificationValue specificationValue=commonService.selectOneByKeyBlob(SpecificationValueMapper.class,projectSpecValue.getSpecValue());
Z 593                 if(specificationValue!=null){
594                     Specification specification=commonService.selectOneByKeyBlob(SpecificationMapper.class,specificationValue.getSpecificationId());
595                     if(specification!=null){
596                         specMap.put("specsTitle",specification.getSpecName());
597                     }
598                     specMap.put("specsValue",specificationValue.getSpecValueName());
599                     specMap.put("specsId",specificationValue.getId());
600                     specMap.put("unit","");
601                 }
602                 jsonObject.add(specMap);
603             }
604         }else{
44d306 605
04c26a 606             map.put("specsTitle","规格");
Z 607             map.put("specsValue",project==null?"":StringUtils.isEmpty(project.getSpecification())?"":project.getSpecification());
608             map.put("unit",project==null?"":StringUtils.isEmpty(project.getUnit())?"":project.getUnit());
609             map.put("bodyPartName",projectInfo==null?"":StringUtils.isEmpty(projectInfo.getBodyPartName())?"":projectInfo.getBodyPartName());
610             map.put("doctorQualification",projectInfo==null?"":StringUtils.isEmpty(projectInfo.getDoctorQualification())?"":projectInfo.getDoctorQualification());
611             jsonObject.add(map);
612         }
95d8e8 613         //设置一级子订单规格信息
04c26a 614         vo.setSpecs(jsonObject.isEmpty()?"[]":jsonObject.toJSONString());
42f9be 615         vo.setSpecsName(StringUtils.isEmpty(project.getSpecification())?"":project.getSpecification());
Z 616         if(projectInfo!=null){
617             if(StringUtils.noNull(projectInfo.getBodyPartId())){
618                 vo.setBodyPartsId(projectInfo.getBodyPartId());
619             }
620             if(StringUtils.noNull(projectInfo.getBodyPartName())){
621                 vo.setBodyPartsName(projectInfo.getBodyPartName());
622             }
623             if(StringUtils.noNull(projectInfo.getExecutiveQualificationCode())){
624                 vo.setAptitudeNo(projectInfo.getExecutiveQualificationCode());
625             }
626             if(StringUtils.noNull(projectInfo.getExecutiveQualificationName())){
627                 vo.setAptitudeName(projectInfo.getExecutiveQualificationName());
628             }
629             if(StringUtils.noNull(projectInfo.getDoctorQualification())){
630                 vo.setDoctorQualification(projectInfo.getDoctorQualification());
631             }
632         }
95d8e8 633         //设置项目编号
Z 634         vo.setGoodsNo(project.getCoding());
635         //设置项目名称
636         vo.setGoodsName(project.getName());
637         //设置项目主图,目前没有项目图片,默认为空字符串
638         vo.setGoodsImage("");
639     //一级子订单疗程次数设值
5fe472 640         //荣爷说项目疗程次数为空值,默认为1
95d8e8 641         //设置一级子订单单次疗程数量,该值获取的是项目的疗程次数(为了计算订单的总疗程次数)
5fe472 642         if(project.getIsExecutable()!=null && project.getIsExecutable()==BaseEntity.YES){
d83d72 643             Integer useredTotal= project.getTreatmentItemsNum() == null ?1: project.getTreatmentItemsNum() == 0 ?1:project.getTreatmentItemsNum();
5fe472 644             vo.setSingle(useredTotal);//订单单次疗程数量
Z 645         }else if(project.getIsExecutable()!=null && project.getIsExecutable()==BaseEntity.NO){
d83d72 646             Integer useredTotal= project.getTreatmentItemsNum() == null ?0:project.getTreatmentItemsNum();
5fe472 647             vo.setSingle(useredTotal);//订单单次疗程数量
Z 648         }else{
649             vo.setSingle(0);//订单单次疗程数量
650         }
651         //设置总次疗程次数,单次疗程次数*购买的总量(结账完成未执行划口剩余次数就是获取该总疗程次数字段),疗程总次数=订单单次疗程数量* 订单购买数量
95d8e8 652         vo.setUsedTotal(vo.getSingle()*vo.getBuyNum());
6b1639 653
Z 654         Map<String,BigDecimal> moneyMap=new HashMap<>();
655         moneyMap.put("discountTotal",discountTotal);
656         return moneyMap;
657     }
658
659     /**
660      *  开单-处理商品方法
661      * @param jsonObject            规格json
662      * @param map                   参数集合
663      * @param vo                    一级子订单
253802 664      * @param discountTotal         折扣总金额TYPE_VIRTUAL_GOODS
6b1639 665      * @param buyNum                购买次数
Z 666      * @return
667      */
e2c918 668     public static Map<String,BigDecimal> handleConsumables(Shop shop,String opShopId,JSONArray jsonObject, Map<String, Object> map, OrderItem vo,
ea2d26 669                                                            BigDecimal discountTotal,BigDecimal buyNum,
6b1639 670                                                            CommonService commonService) {
Z 671         Consumables consumables = commonService.selectOneByKey(ConsumablesMapper.class,vo.getCommonId());
672         if(consumables==null){
44d306 673             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"找不到商品:"+vo.getCommonId());
6b1639 674         }
Z 675         if(consumables.getPrice()==null){
227b3e 676             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"商品:["+consumables.getName()+"]没有销售价格,请填写销售单价在进行创建订单");
6b1639 677         }
44d306 678         if(consumables.getIsUp()==BaseEntity.NO){
227b3e 679             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"该商品:["+consumables.getName()+"]已下架");
44d306 680         }
Z 681         if(consumables.getIsDel()==BaseEntity.YES){
227b3e 682             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"该商品:["+consumables.getName()+"]已删除");
44d306 683         }
6b1639 684
227b3e 685         if(OrderItemConstants.TYPE_DRUG.equals(vo.getType())){
C 686             //获取门店价格
687             SqlSentence sqlSentence = new SqlSentence();
688             Map<String,Object> sqlMap = new HashMap<>();
689             StringBuilder sql = new StringBuilder();
690
f5acae 691             sqlMap.put("consumableId",vo.getCommonId());
e2c918 692             sqlMap.put("shopId",opShopId);
227b3e 693             sqlMap.put("isUp",ProjectShopPrice.YES);
C 694             sqlMap.put("isDel",ProjectShopPrice.NO);
695             sql.append(" SELECT id,price FROM project_shop_price WHERE isDel = #{m.isDel} ");
696             sql.append("  AND isUp = #{m.isUp} AND commonId = #{m.consumableId}  AND shopId = #{m.shopId} ");
697             sqlSentence.sqlSentence(sql.toString(), sqlMap);
698             List<ProjectShopPrice> priceList = commonService.selectList(ProjectShopPriceMapper.class, sqlSentence);
699             if (priceList != null && priceList.size() > 0){
700                 if (priceList.size() > 1){
701                     throw new PlatTipsException(PlatformCode.ERROR_TIPS,"当前处方药品列表中存在多个门店价格信息!");
702                 }
703                 ProjectShopPrice projectShopPrice = priceList.get(0);
704                 consumables.setPrice(projectShopPrice.getPrice());
705             }
706         }
707
708         //计算订单金额信息
44d306 709         //一级子订单:售价=商品售价*购买数量*折扣
Z 710         BigDecimal actualTotal = consumables.getPrice().multiply(vo.getDiscount().divide(new BigDecimal(100)));
6b1639 711
44d306 712         //总订单:订单优惠金额=商品售价*购买数量 减去 一级子订单售价
Z 713         discountTotal=discountTotal.add(consumables.getPrice().multiply(buyNum).subtract(actualTotal.multiply(buyNum)));
6b1639 714
227b3e 715         //一级子订单金额信息设值
fd269d 716         //设置原价,原价=商品的销售价格
ec5a03 717         vo.setOriPrice(consumables.getPrice());
fd269d 718         //设置项目、商品、促销、卡项现价的售价
Z 719         vo.setProPrice(consumables.getPrice());
44d306 720         //设置售价, 售价=项目的销售价格*购买数量*折扣
Z 721         vo.setCurPrice(actualTotal);
722         //设置优惠前小计,优惠前小计=售价* 商品数量
723         vo.setTotal(vo.getCurPrice().multiply(buyNum));
724         //设置子订单优惠券抵扣金额,开单不知道是否使用优惠券,默认为0
725         vo.setDiscountPrice(BigDecimal.ZERO);
726         //设置子订单优惠后小计,优惠后小计=优惠前小计-优惠券抵扣金额
727         vo.setActualTotal(vo.getTotal().subtract(vo.getDiscountPrice()));
728         //设置用户已支付总金额,开单默认为0
729         vo.setUserPaidTotal(BigDecimal.ZERO);
06d6ca 730         //设置item总积分
766aa8 731         //如果为空则使用商品售价积分乘以购买数量,不为空则使用已有值
F 732         if(vo.getTotalPoints() == null){
733             vo.setTotalPoints(consumables.getIntegral().multiply(buyNum));
734         }
6f4129 735         //应付积分 = 总积分
F 736         vo.setShouldTotalPoints(vo.getTotalPoints());
06d6ca 737         //设置售价积分
766aa8 738         vo.setSellPoints(consumables.getIntegral());
6b1639 739
44d306 740     //一级子订单商品信息设值
Z 741         //设置一级子订单商品规格
42f9be 742         if(StringUtils.noNull(consumables.getSpec())){
a64c8b 743             Map<String,Object> specMap=new HashMap<>();
42f9be 744             specMap.put("specsId","");
Z 745             specMap.put("specsTitle","规格");
746             specMap.put("specsValue",consumables.getSpec());
747             specMap.put("unit","");
a64c8b 748             jsonObject.add(specMap);
04c26a 749         }
42f9be 750         if(StringUtils.noNull(consumables.getPackSpec())){
a64c8b 751             Map<String,Object> packSpecMap=new HashMap<>();
Z 752             packSpecMap.put("specsId","");
753             packSpecMap.put("specsTitle","包装规格");
754             packSpecMap.put("specsValue",consumables.getPackSpec());
755             packSpecMap.put("unit","");
756             jsonObject.add(packSpecMap);
42f9be 757         }
Z 758         if(StringUtils.noNull(consumables.getzSpec())){
a64c8b 759             Map<String,Object> zSpecMap=new HashMap<>();
Z 760             zSpecMap.put("specsId","");
761             zSpecMap.put("specsTitle","中药规格");
762             zSpecMap.put("specsValue",consumables.getzSpec());
763             zSpecMap.put("unit","");
764             jsonObject.add(zSpecMap);
42f9be 765         }
Z 766         if(StringUtils.noNull(consumables.getUnitIdCode())){
a64c8b 767             Map<String,Object> unitIdCodeSpecMap=new HashMap<>();
Z 768             unitIdCodeSpecMap.put("specsId","");
769             unitIdCodeSpecMap.put("specsTitle","单位(字典编码)");
770             unitIdCodeSpecMap.put("specsValue",consumables.getUnitIdCode());
771             unitIdCodeSpecMap.put("unit","");
772             jsonObject.add(unitIdCodeSpecMap);
42f9be 773         }
Z 774         if(StringUtils.noNull(consumables.getUnitName())){
a64c8b 775             Map<String,Object> unitNameSpecMap=new HashMap<>();
Z 776             unitNameSpecMap.put("specsId","");
777             unitNameSpecMap.put("specsTitle","单位名称");
778             unitNameSpecMap.put("specsValue",consumables.getUnitName());
779             unitNameSpecMap.put("unit","");
780             jsonObject.add(unitNameSpecMap);
42f9be 781         }
a64c8b 782
42f9be 783         if(StringUtils.noNull(consumables.getzUnitName())){
a64c8b 784             Map<String,Object> zUnitNameSpecMap=new HashMap<>();
Z 785             zUnitNameSpecMap.put("specsId","");
786             zUnitNameSpecMap.put("specsTitle","中药单位");
787             zUnitNameSpecMap.put("specsValue",consumables.getzUnitName());
788             zUnitNameSpecMap.put("unit","");
789             jsonObject.add(zUnitNameSpecMap);
42f9be 790         }
Z 791         if(StringUtils.noNull(consumables.getPeruseUnit())){
a64c8b 792             Map<String,Object> peruseUnitSpecMap=new HashMap<>();
Z 793             peruseUnitSpecMap.put("specsId","");
794             peruseUnitSpecMap.put("specsTitle","每次用药单位");
795             peruseUnitSpecMap.put("specsValue",consumables.getPeruseUnit());
796             peruseUnitSpecMap.put("unit","");
797             jsonObject.add(peruseUnitSpecMap);
42f9be 798         }
Z 799         if(consumables.getPeruseCount()!=null){
a64c8b 800             Map<String,Object> peruseCountSpecMap=new HashMap<>();
Z 801             peruseCountSpecMap.put("specsId","");
802             peruseCountSpecMap.put("specsTitle","每次用药数量");
803             peruseCountSpecMap.put("specsValue",consumables.getPeruseCount());
804             peruseCountSpecMap.put("unit","");
805             jsonObject.add(peruseCountSpecMap);
42f9be 806         }
Z 807         if(consumables.getUsageDays()!=null){
a64c8b 808             Map<String,Object> usageDaysSpecMap=new HashMap<>();
Z 809             usageDaysSpecMap.put("specsId","");
810             usageDaysSpecMap.put("specsTitle","用药天数");
811             usageDaysSpecMap.put("specsValue",consumables.getUsageDays());
812             usageDaysSpecMap.put("unit","");
813             jsonObject.add(usageDaysSpecMap);
42f9be 814         }
Z 815         if(consumables.getTerm()!=null){
a64c8b 816             Map<String,Object> termSpecMap=new HashMap<>();
Z 817             termSpecMap.put("specsId","");
818             termSpecMap.put("specsTitle","保质期,单位月");
819             termSpecMap.put("specsValue",consumables.getTerm());
820             termSpecMap.put("unit","");
821             jsonObject.add(termSpecMap);
42f9be 822         }
04c26a 823         vo.setSpecs(jsonObject.isEmpty()?"[]":jsonObject.toJSONString());
42f9be 824         vo.setSpecsName(StringUtils.isEmpty(consumables.getSpec())?"":consumables.getSpec());
44d306 825         //设置一级子订单商品编号
Z 826         vo.setGoodsNo(consumables.getSerialNumber());
827         //设置一级子订单商品名称
828         vo.setGoodsName(consumables.getName());
829         //设置一级子订单商品项目主图
830         vo.setGoodsImage("");
831         //设置一级子订单商品单次疗程数量,默认为0,商品没有单次疗程数量
832
833     //一级子订单商品疗程次数信息设值
834         vo.setSingle(0);
835         //设置一级子订单商品总次疗程次数,单次疗程次数*购买的总量,商品没有单次疗程数量
836         vo.setUsedTotal(vo.getSingle()*vo.getBuyNum());
6b1639 837
Z 838         Map<String,BigDecimal> moneyMap=new HashMap<>();
839         moneyMap.put("discountTotal",discountTotal);
840         return moneyMap;
e21429 841     }
C 842
843     /**
844      *  开单-处理过渡商品方法
845      * @param orderItem 子订单信息
846      */
847     public static void transitionGoodsHandle(OrderItem orderItem) {
848         if(orderItem.getOrderTransitionGoods() == null){
849             throw new TipsException("过渡商品信息必填!");
850         }
851         if(StringUtils.isNull(orderItem.getOrderTransitionGoods().getGoodsId())){
852             throw new TipsException("过渡商品标识必填!");
853         }
854         if(StringUtils.isNull(orderItem.getOrderTransitionGoods().getGoodsNo())){
855             throw new TipsException("过渡商品编号必填!");
856         }
857         if(StringUtils.isNull(orderItem.getOrderTransitionGoods().getGoodsName())){
858             throw new TipsException("过渡商品名称必填!");
859         }
860         if(orderItem.getOrderTransitionGoods().getOriPrice() == null
861                 && orderItem.getOrderTransitionGoods().getOriPrice().compareTo(BigDecimal.ZERO) < 0){
862             throw new TipsException("过渡商品原价必填或者数据有误!");
863         }
864         if(orderItem.getOrderTransitionGoods().getCurPrice() == null
865                 && orderItem.getOrderTransitionGoods().getCurPrice().compareTo(BigDecimal.ZERO) < 0){
866             throw new TipsException("过渡商品售价必填或者数据有误!");
867         }
868         if(orderItem.getOrderTransitionGoods().getOriTotal() == null
869                 && orderItem.getOrderTransitionGoods().getOriTotal().compareTo(BigDecimal.ZERO) < 0){
870             throw new TipsException("过渡商品原价总价必填或者数据有误!");
871         }
872         if(orderItem.getOrderTransitionGoods().getCurTotal() == null
873                 && orderItem.getOrderTransitionGoods().getCurTotal().compareTo(BigDecimal.ZERO) < 0){
874             throw new TipsException("过渡商品售价总价必填或者数据有误!");
875         }
876
877         //一级子订单金额信息设值
878         //设置原价,原价=商品的销售价格
879         orderItem.setOriPrice(orderItem.getOrderTransitionGoods().getOriPrice());
880         //设置项目、商品、促销、卡项现价的售价
881         orderItem.setProPrice(orderItem.getOrderTransitionGoods().getCurPrice());
882         //设置售价, 售价=项目的销售价格*购买数量*折扣
883         orderItem.setCurPrice(orderItem.getOrderTransitionGoods().getCurPrice());
884         //设置优惠前小计,优惠前小计=售价* 商品数量
885         orderItem.setTotal(orderItem.getOrderTransitionGoods().getCurTotal());
886         //设置子订单优惠券抵扣金额,开单不知道是否使用优惠券,默认为0
887         orderItem.setDiscountPrice(BigDecimal.ZERO);
888         //设置子订单优惠后小计,优惠后小计=优惠前小计-优惠券抵扣金额
889         orderItem.setActualTotal(orderItem.getOrderTransitionGoods().getCurTotal());
890         //设置用户已支付总金额,开单默认为0
891         orderItem.setUserPaidTotal(BigDecimal.ZERO);
892
893         //设置一级子订单商品编号
894         orderItem.setGoodsNo(orderItem.getOrderTransitionGoods().getGoodsNo());
895         //设置一级子订单商品名称
896         orderItem.setGoodsName(orderItem.getOrderTransitionGoods().getGoodsName());
897         //设置一级子订单商品项目主图
898         orderItem.setGoodsImage("");
899         //设置一级子订单商品单次疗程数量,默认为0,商品没有单次疗程数量
900
901         //一级子订单商品疗程次数信息设值
902         orderItem.setSingle(0);
903         //设置一级子订单商品总次疗程次数,单次疗程次数*购买的总量,商品没有单次疗程数量
904         orderItem.setUsedTotal(orderItem.getSingle()*orderItem.getBuyNum());
8c307c 905     }
Z 906
907     /**
908      * 开单-计算促销金额
909      * @param vo                    一级子订单
910      * @param discountTotal         折扣总金额
911      * @param buyNum                购买次数
912      * @return
913      */
ea2d26 914     public static Map<String,BigDecimal> handlePromotion( OrderItem vo, BigDecimal discountTotal, BigDecimal buyNum, CommonService commonService) {
8c307c 915         SqlSentence sqlSentence = new SqlSentence();
Z 916         Map<String,Object> sqlMp=new HashMap<>();
917         sqlSentence.setM(sqlMp);
918         sqlMp.put("status",Promotion.IN_PROGRESS);
919         sqlMp.put("isUp",BaseEntity.YES);
920         sqlMp.put("isDel",BaseEntity.NO);
921         sqlMp.put("commonId",vo.getCommonId());
922         sqlSentence.setSqlSentence("select * from promotion where  id=#{m.commonId} and  status=#{m.status} and isUp=#{m.isUp} and isDel=#{m.isDel}");
923         Promotion promotion=commonService.selectOne(PromotionMapper.class,sqlSentence);
924         if(promotion==null){
925             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"找不到该促销,请确保促销状态必须是销售中并且是上架状态,促销标识:"+vo.getCommonId());
926         }
927         if(promotion.getTotal()==null){
928             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"促销:"+promotion.getName()+",没有销售价格,请填写销售单价在进行创建订单");
929         }
930
44d306 931     //计算订单金额信息
Z 932         //一级子订单:售价=促销销售价格*折扣
c5e876 933         BigDecimal actualTotal = promotion.getTotal().multiply(vo.getDiscount().divide(new BigDecimal(100),15,BigDecimal.ROUND_HALF_UP)).setScale(2,BigDecimal.ROUND_HALF_UP);
8c307c 934
Z 935
44d306 936         //总订单:订单优惠金额=商品的销售单价*数量 减去 一级子订单售价 * 购买数量;
Z 937         discountTotal=discountTotal.add(promotion.getTotal().multiply(buyNum).subtract(actualTotal.multiply(buyNum)));
8c307c 938
44d306 939     //一级子订单金额信息设值
fd269d 940         //设置原价,原价=促销的销售价格
44d306 941         vo.setOriPrice(promotion.getTotal());
fd269d 942         //设置项目、商品、促销、卡项现价的售价
Z 943         vo.setProPrice(promotion.getTotal());
44d306 944         //设置一级子订单售价, 售价=项目的销售价格*购买数量*折扣
Z 945         vo.setCurPrice(actualTotal);
946         //设置一级子订单优惠前小计,优惠前小计=售价* 商品数量
947         vo.setTotal(vo.getCurPrice().multiply(buyNum));
948         //设置一级子订单优惠券抵扣金额,开单不知道是否使用优惠券,默认为0
949         vo.setDiscountPrice(BigDecimal.ZERO);
950         //设置一级子订单优惠后小计,优惠后小计=优惠前小计-优惠券抵扣金额
951         vo.setActualTotal(vo.getTotal().subtract(vo.getDiscountPrice()));
952         //设置一级子订单用户已支付总金额,开单默认为0
953         vo.setUserPaidTotal(BigDecimal.ZERO);
06d6ca 954         //设置item总积分
Z 955         vo.setTotalPoints(BigDecimal.ZERO);
956         //设置售价积分
957         vo.setSellPoints(BigDecimal.ZERO);
44d306 958     //一级子订单促销规格信息设值
Z 959         //设置一级子订单促销规格
960         vo.setSpecs("[]");
961         //设置一级子订单促销编号
962         vo.setGoodsNo(promotion.getCode());
963         //设置一级子订单促销名称
964         vo.setGoodsName(promotion.getName());
965         //设置一级子订单促销主图
966         vo.setGoodsImage("");
8c307c 967
44d306 968     //一级子订单促销疗程次数设值
Z 969         //设置一级子订单促销单次疗程数量
970         vo.setSingle(0);
971         //设置一级子订单促销总次疗程次数,单次疗程次数*购买的总量
972         vo.setUsedTotal(vo.getSingle()*vo.getBuyNum());
8c307c 973
Z 974         Map<String,BigDecimal> moneyMap=new HashMap<>();
975         moneyMap.put("discountTotal",discountTotal);
976         return moneyMap;
977     }
978
979     /**
980      *  开单-计算卡项金额
981      * @param vo                    一级子订单
982      * @param discountTotal         折扣总金额
983      * @param buyNum                购买次数
984      * @return
985      */
ea2d26 986     public static Map<String,BigDecimal> handleCardItem(OrderItem vo, BigDecimal discountTotal,
44d306 987                                                         BigDecimal buyNum, CommonService commonService) {
8c307c 988         SqlSentence sqlSentence = new SqlSentence();
Z 989         Map<String,Object> sqlMp=new HashMap<>();
990         sqlSentence.setM(sqlMp);
991         sqlMp.put("status",CardItem.SALES);
992         sqlMp.put("isUp",BaseEntity.YES);
993         sqlMp.put("isDel",BaseEntity.NO);
994         sqlMp.put("commonId",vo.getCommonId());
a026ab 995         sqlSentence.setSqlSentence("select * from card_item where id = #{m.commonId} and status=#{m.status} and isDel=#{m.isDel}");
8c307c 996         CardItem cardItem=commonService.selectOne(CardItemMapper.class,sqlSentence);
Z 997         if(cardItem==null){
a026ab 998             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"购买的卡项不存在");
8c307c 999         }
a026ab 1000         //判断卡项是否下架
C 1001         if(cardItem.getIsUp().equals(CardItem.NO)){
1002             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"卡项:"+cardItem.getName()+"已下架!");
1003         }
c68c18 1004         //判断卡项是否销售
C 1005         if(!CardItem.SALES.equals(cardItem.getStatus())){
1006             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"卡项:"+cardItem.getName()+"未开放销售!");
1007         }
1008         if(cardItem.getTotal()==null){
1009             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"卡项:"+cardItem.getName()+"无法下单购买!");
1010         }
a026ab 1011
C 1012         //获取限制表
1013         sqlMp.clear();
1014         sqlMp.put("cardId", cardItem.getId());
1015         sqlSentence.setSqlSentence("select * from limit_total where isDel= 0 and foreignKey=#{m.cardId} order by createTime desc limit 1");
1016         LimitTotal limitTotal=commonService.selectOne(LimitTotalMapper.class,sqlSentence);
1017         if(limitTotal != null){
1018             Date date = new Date();
1019             if(limitTotal.getStartTime() != null){
1020                 //判断销售购买时间
1021                 if(date.before(limitTotal.getStartTime())){
1022                     throw new PlatTipsException(PlatformCode.ERROR_TIPS,"卡项:"+cardItem.getName()+"未到销售时间!");
1023                 }
1024             }
1025             if(limitTotal.getEndTime() != null){
1026                 //判断销售购买时间
1027                 if(date.after(limitTotal.getEndTime())){
1028                     throw new PlatTipsException(PlatformCode.ERROR_TIPS,"卡项:"+cardItem.getName()+"已过销售时间!");
1029                 }
1030             }
1031         }
1032
1033         //计算订单卡项金额信息
44d306 1034         //一级子订单:售价=卡项售价*折扣
78d1e8 1035         BigDecimal actualTotal = cardItem.getTotal().multiply(vo.getDiscount().divide(new BigDecimal(100))).setScale(2,RoundingMode.HALF_UP);
8c307c 1036
44d306 1037         //总订单:订单优惠金额=商品的售价*数量 减去 一级子订单售价*购买数量;
Z 1038         discountTotal=discountTotal.add(cardItem.getTotal().multiply(buyNum).subtract(actualTotal.multiply(buyNum)));
8c307c 1039
44d306 1040     //一级子订单卡项金额信息设值
fd269d 1041         //设置原价,原价=卡项的销售价格
44d306 1042         vo.setOriPrice(cardItem.getTotal());
fd269d 1043         //设置项目、商品、促销、卡项现价的售价
Z 1044         vo.setProPrice(cardItem.getTotal());
44d306 1045         //设置一级子订单售价
Z 1046         vo.setCurPrice(actualTotal);
1047         //设置一级子订单优惠前小计,优惠前小计=售价* 商品数量
1048         vo.setTotal(vo.getCurPrice().multiply(buyNum));
1049         //设置一级子订单优惠券抵扣金额,开单不知道是否使用优惠券,默认为0
1050         vo.setDiscountPrice(BigDecimal.ZERO);
1051         //设置一级子订单优惠后小计,优惠后小计=优惠前小计-优惠券抵扣金额
1052         vo.setActualTotal(vo.getTotal().subtract(vo.getDiscountPrice()));
1053         //设置一级子订单用户已支付总金额,开单默认为0
1054         vo.setUserPaidTotal(BigDecimal.ZERO);
06d6ca 1055         //设置item总积分
Z 1056         vo.setTotalPoints(BigDecimal.ZERO);
1057         //设置售价积分
1058         vo.setSellPoints(BigDecimal.ZERO);
44d306 1059     //一级子订单卡项规格信息设值
Z 1060         //设置一级子订单卡项编号
1061         vo.setGoodsNo(cardItem.getCode());
1062         //设置一级子订单卡项名称
1063         vo.setGoodsName(cardItem.getName());
1064         //设置一级子订单卡项规格(卡项没有规格,默认 [])
1065         vo.setSpecs("[]");
1066         //设置一级子订单卡项主图
1067         vo.setGoodsImage("");
8c307c 1068
44d306 1069     //一级子订单卡项疗程次数设值
Z 1070         //设置一级子订单卡项单次疗程数量
1071         vo.setSingle(0);
1072         //设置一级子订单卡项总次疗程次数,单次疗程次数*购买的总量
1073         vo.setUsedTotal(vo.getSingle()*vo.getBuyNum());
8c307c 1074
Z 1075         Map<String,BigDecimal> moneyMap=new HashMap<>();
1076         moneyMap.put("actualTotal",actualTotal);
1077         moneyMap.put("discountTotal",discountTotal);
1078         return moneyMap;
1079     }
1080
1081     /**
1082      * 开单-计算积分金额
1083      * @param jsonObject
1084      * @param map
1085      * @param vo
1086      * @param discountTotal
1087      * @param buyNum
1088      * @param commonService
1089      * @return
1090      */
ea2d26 1091     private static Map<String, BigDecimal> handleIntegral(JSONArray jsonObject, Map<String, Object> map, OrderItem vo, BigDecimal discountTotal,
Z 1092                                                           BigDecimal buyNum, CommonService commonService) {
95d8e8 1093         SqlSentence sqlSentence=new SqlSentence();
42f9be 1094         Map<String,Object> objectMap=new HashMap<>();
Z 1095         sqlSentence.setM(objectMap);
1096         objectMap.put("commonId",vo.getCommonId());
1097         objectMap.put("isDel",BaseEntity.NO);
1098
44d306 1099         Project project=commonService.selectOneByKeyBlob(ProjectMapper.class,vo.getCommonId());
95d8e8 1100         if(project==null){
Z 1101             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"找不到项目:"+vo.getCommonId());
1102         }
44d306 1103         //计算项目价格,如果总订单有康博佳标识代表是康博佳订单不需要任何处理直接拿去康博佳的总金额,每个门店价格不一样,如果没有设置门店价格就使用项目售价
Z 1104 //      BigDecimal price=priceJudge(ordersTotal, commonService,project.getId(),shopId);
1105 //      project.setPrice(BigDecimal.ZERO.compareTo(price)==0?project.getPrice():price);
95d8e8 1106         if(project.getPrice()==null){
44d306 1107             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"积分项目:"+project.getName()+",没有销售价格,请填写销售单价再进行创建订单");
95d8e8 1108         }
44d306 1109         if(project.getIsUp()==BaseEntity.NO){
Z 1110             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"积分项目:"+project.getName()+",该项目已下架");
1111         }
1112         if(project.getIsDel()==BaseEntity.YES){
1113             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"积分项目:"+project.getName()+",该项目已删除");
1114         }
42f9be 1115         sqlSentence.setSqlSentence("select * from project_info where projectId=#{m.commonId} and isDel=#{m.isDel} ");
Z 1116         ProjectInfo projectInfo=commonService.selectOne(ProjectInfoMapper.class,sqlSentence);
44d306 1117     //计算订单金额信息
Z 1118         //一级子订单:售价=项目售价*折扣
1119         BigDecimal actualTotal = project.getPrice().multiply(vo.getDiscount().divide(new BigDecimal(100)));
8c307c 1120
44d306 1121         //总订单:订单优惠总金额=项目的销售单价*数量减去一级子订单售价;
Z 1122         discountTotal=discountTotal.add(project.getPrice().multiply(buyNum).subtract(actualTotal.multiply(buyNum)));
8c307c 1123
44d306 1124     //一级子订单金额信息设值
fd269d 1125         //设置原价,原价=促销的销售价格
Z 1126         vo.setOriPrice(project.getOriginalPrice());
1127         //设置项目、商品、促销、卡项现价的售价
1128         vo.setProPrice(project.getPrice());
44d306 1129         //设置售价, 售价=项目的销售价格*购买数量*折扣
Z 1130         vo.setCurPrice(actualTotal);
1131         //设置优惠前小计,优惠前小计=售价* 商品数量
1132         vo.setTotal(vo.getCurPrice().multiply(buyNum));
1133         //设置子订单优惠券抵扣金额,开单不知道是否使用优惠券,默认为0
1134         vo.setDiscountPrice(BigDecimal.ZERO);
1135         //设置子订单优惠后小计,优惠后小计=优惠前小计-优惠券抵扣金额
1136         vo.setActualTotal(vo.getTotal().subtract(vo.getDiscountPrice()));
1137         //设置用户已支付总金额,开单默认为0
1138         vo.setUserPaidTotal(BigDecimal.ZERO);
06d6ca 1139         //设置item总积分
Z 1140         vo.setTotalPoints(BigDecimal.ZERO);
1141         //设置售价积分
1142         vo.setSellPoints(BigDecimal.ZERO);
8c307c 1143
44d306 1144     //一级子订单商品信息设值
Z 1145         //设置一级子订单规格,如果项目规格json没有值,则自动封装json规格(内容默认:规格值、单位、身体部位、医生资质),否则就采用项目规格json
04c26a 1146         sqlSentence.setSqlSentence("select * from project_spec_value where projectId=#{m.commonId} and isDel=#{m.isDel} ");
Z 1147         List<ProjectSpecValue> projectSpecValues = commonService.selectList(ProjectSpecValueMapper.class, sqlSentence);
1148         if(projectSpecValues!=null && projectSpecValues.size()>0){
1149             for (ProjectSpecValue projectSpecValue : projectSpecValues) {
a64c8b 1150                 Map<String,Object> specMap=new HashMap<>();
04c26a 1151                 SpecificationValue specificationValue=commonService.selectOneByKeyBlob(SpecificationValueMapper.class,projectSpecValue.getSpecValue());
Z 1152                 if(specificationValue!=null){
1153                     Specification specification=commonService.selectOneByKeyBlob(SpecificationMapper.class,specificationValue.getSpecificationId());
1154                     if(specification!=null){
1155                         specMap.put("specsTitle",specification.getSpecName());
1156                     }
1157                     specMap.put("specsValue",specificationValue.getSpecValueName());
1158                     specMap.put("specsId",specificationValue.getId());
1159                     specMap.put("unit","");
1160                 }
1161                 jsonObject.add(specMap);
1162             }
1163         }else{
1164             map.put("specsTitle","规格");
1165             map.put("specsValue",project==null?"":StringUtils.isEmpty(project.getSpecification())?"":project.getSpecification());
1166             map.put("unit",project==null?"":StringUtils.isEmpty(project.getUnit())?"":project.getUnit());
1167             map.put("bodyPartName",projectInfo==null?"":StringUtils.isEmpty(projectInfo.getBodyPartName())?"":projectInfo.getBodyPartName());
1168             map.put("doctorQualification",projectInfo==null?"":StringUtils.isEmpty(projectInfo.getDoctorQualification())?"":projectInfo.getDoctorQualification());
1169             jsonObject.add(map);
1170         }
44d306 1171         //设置一级子订单规格信息
04c26a 1172         vo.setSpecs(jsonObject.isEmpty()?"[]":jsonObject.toJSONString());
42f9be 1173         vo.setSpecsName(StringUtils.isEmpty(project.getSpecification())?"":project.getSpecification());
Z 1174         if(projectInfo!=null){
1175             if(StringUtils.noNull(projectInfo.getBodyPartId())){
1176                 vo.setBodyPartsId(projectInfo.getBodyPartId());
1177             }
1178             if(StringUtils.noNull(projectInfo.getBodyPartName())){
1179                 vo.setBodyPartsName(projectInfo.getBodyPartName());
1180             }
1181             if(StringUtils.noNull(projectInfo.getExecutiveQualificationCode())){
1182                 vo.setAptitudeNo(projectInfo.getExecutiveQualificationCode());
1183             }
1184             if(StringUtils.noNull(projectInfo.getExecutiveQualificationName())){
1185                 vo.setAptitudeName(projectInfo.getExecutiveQualificationName());
1186             }
1187             if(StringUtils.noNull(projectInfo.getDoctorQualification())){
1188                 vo.setDoctorQualification(projectInfo.getDoctorQualification());
1189             }
1190         }
8c307c 1191         vo.setGoodsNo(project.getCoding());//项目编号
Z 1192         vo.setGoodsName(project.getName());//项目名称
1193         vo.setGoodsImage("");//项目主图
1c40c5 1194     //一级子订单疗程次数设值OrderServiceImpl
5fe472 1195         //荣爷说项目疗程次数为空值,默认为1
44d306 1196         //设置一级子订单单次疗程数量,该值获取的是项目的疗程次数(为了计算订单的总疗程次数)
5fe472 1197         if(project.getIsExecutable()!=null && project.getIsExecutable()==BaseEntity.YES){
Z 1198             Integer useredTotal=project==null?0:project.getTreatmentItemsNum()==null?1:project.getTreatmentItemsNum()==0?1:project.getTreatmentItemsNum();
1199             vo.setSingle(useredTotal);//订单单次疗程数量
1200         }else if(project.getIsExecutable()!=null && project.getIsExecutable()==BaseEntity.NO){
1201             Integer useredTotal=project==null?0:project.getTreatmentItemsNum()==null?0:project.getTreatmentItemsNum();
1202             vo.setSingle(useredTotal);//订单单次疗程数量
1203         }else{
1204             vo.setSingle(0);//订单单次疗程数量
1205         }
1206         //设置总次疗程次数,单次疗程次数*购买的总量(结账完成未执行划口剩余次数就是获取该总疗程次数字段),疗程总次数=订单单次疗程数量* 订单购买数量
44d306 1207         vo.setUsedTotal(vo.getSingle()*vo.getBuyNum());
8c307c 1208
Z 1209         Map<String,BigDecimal> moneyMap=new HashMap<>();
1210         moneyMap.put("discountTotal",discountTotal);
1211         return moneyMap;
1212     }
f9fd77 1213
Z 1214     /**
6b1639 1215      *  开单-处理卡包方法
Z 1216      * @param vo                    一级子订单
1217      * @return
1218      */
ea2d26 1219     public static Map<String,BigDecimal> handleCardBag(OrderItem vo, CommonService commonService) {
95d8e8 1220         if(StringUtils.isEmpty(vo.getUserCardId())){
Z 1221             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"下单:开卡包订单需要卡包标识!");
1222         }
6b1639 1223         SqlSentence sqlSentence = new SqlSentence();
Z 1224         Map<String,Object> sqlMp=new HashMap<>();
1225         sqlSentence.setM(sqlMp);
1226         sqlMp.put("status",CardItem.SALES);
1227         sqlMp.put("isUp",BaseEntity.YES);
1228         sqlMp.put("isDel",BaseEntity.NO);
1229         sqlMp.put("commonId",vo.getCommonId());
1230         //已有卡项放开卡项是否上架判断,只要购买了卡项都能使用除非过期了,and isUp=#{m.isUp},and  status=#{m.status}
1231         sqlSentence.setSqlSentence("select * from card_item where  id=#{m.commonId} and isDel=#{m.isDel}");
1232         CardItem cardItem=commonService.selectOne(CardItemMapper.class,sqlSentence);
1233         if(cardItem==null){
1234             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"卡包:找不到该卡项,请确保卡项状态必须是销售中并且是上架状态,卡项标识:"+vo.getCommonId());
1235         }
1236         if(cardItem.getTotal()==null){
1237             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"卡包:"+cardItem.getName()+",没有销售价格,请填写销售单价在进行创建订单");
1238         }
44d306 1239
Z 1240     //计算一级子订单和总订单售价,卡包比较特殊,售价=未执行划扣金额,未执行划扣金额=(卡项明细分摊金额 除以 卡项明细分摊次数 )乘以 用户划扣次数
1241         //一级子订单未执行划扣金额
6b1639 1242         BigDecimal sumCardBagMoney =BigDecimal.ZERO;
415eef 1243         //单次划扣金额
C 1244         BigDecimal everySnapNum = null;
1245         //总次数
932a23 1246         Integer usedNum;
C 1247         CardEquity cardEquity;
1248         UserCardItemInfoVo userCardItemInfoVo;
4f41a7 1249         CardItemInfo cardItemInfo;
6b1639 1250         if(vo.getCardItemInfoIds()!=null && vo.getCardItemInfoIds().size()>0){
Z 1251             for(CardItemInfo info:vo.getCardItemInfoIds()){
415eef 1252                 if(info.getUserNum() == null || info.getUserNum() < 0){
C 1253                     throw new PlatTipsException(PlatformCode.ERROR_TIPS,"卡包开单提示:购买的卡项的条目数量错误!");
1254                 }
932a23 1255
4f41a7 1256                 cardItemInfo = commonService.selectOneByKey(CardItemInfoMapper.class,info.getId());
932a23 1257                 //-----校验抵扣次数
4f41a7 1258                 cardEquity = commonService.selectOneByKey(CardEquityMapper.class,cardItemInfo.getCardEquityId());
415eef 1259                 //获取当前需要抵扣的总次数
1a6a2e 1260                 usedNum = UserCardTool.countUsedNumber(info.getUserNum(),cardItemInfo,cardEquity);
4f41a7 1261                 userCardItemInfoVo = UserCardTool.getSurplusNumber(vo.getUserCardId(),cardItemInfo,cardEquity,commonService);
932a23 1262                 if(!userCardItemInfoVo.isNumByNum()){
C 1263                     throw new PlatTipsException(PlatformCode.ERROR_TIPS,"卡包开单提示:不符合N选M规则,请重试");
1264                 }
1265                 if(userCardItemInfoVo.getSurplusNum() <= 0 ||usedNum > userCardItemInfoVo.getSurplusNum()){
1266                     throw new PlatTipsException(PlatformCode.ERROR_TIPS,"卡包开单提示:卡项的次卡使用次数不够");
1267                 }
1268
415eef 1269                if(CardEquity.EQUITY_TYPE_TIME_CARD.equals(cardEquity.getEquityType())){
C 1270                     //-----次卡
6b1639 1271                     //每次划扣金额
415eef 1272                    if(cardItemInfo.getEveryDrawNum()>0){
C 1273                         everySnapNum = cardItemInfo.getEveryShareMoney().divide(BigDecimal.valueOf(cardItemInfo.getEveryDrawNum()),10,RoundingMode.HALF_UP);
1274                    }else{
1275                        everySnapNum = BigDecimal.ZERO;
1276                    }
1277                 }else{
1278                     //-----N选M/固定项目/其他
1279                     //每次划扣金额
1280                    if(cardItemInfo.getMaxNum()>0){
1281                         everySnapNum = cardItemInfo.getShareMoney().divide(BigDecimal.valueOf(cardItemInfo.getMaxNum()),10,RoundingMode.HALF_UP);
1282                    }else{
1283                        everySnapNum = BigDecimal.ZERO;
1284                    }
6b1639 1285                 }
415eef 1286                 sumCardBagMoney=sumCardBagMoney.add(everySnapNum.multiply(BigDecimal.valueOf(usedNum))).setScale(2,RoundingMode.HALF_UP);
6b1639 1287             }
Z 1288         }
1289
44d306 1290         //设置一级子订单原价(卡包:划扣项目的未执行划扣金额)
Z 1291         vo.setOriPrice(sumCardBagMoney);
1292         //设置一级子订单售价
415eef 1293         vo.setCurPrice(sumCardBagMoney);
44d306 1294         //设置一级子订单优惠前小计,优惠前小计=一级子订单未执行划扣金额
415eef 1295         vo.setTotal(BigDecimal.ZERO);
44d306 1296         //设置一级子订单优惠券抵扣金额,开单不知道是否使用优惠券,默认为0
Z 1297         vo.setDiscountPrice(BigDecimal.ZERO);
1298         //设置一级子订单优惠后小计,优惠后小计=一级子订单未执行划扣金额
415eef 1299         vo.setActualTotal(BigDecimal.ZERO);
44d306 1300         //设置一级子订单用户已支付总金额,开单默认为0
Z 1301         vo.setUserPaidTotal(BigDecimal.ZERO);
06d6ca 1302         //设置item总积分
Z 1303         vo.setTotalPoints(BigDecimal.ZERO);
1304         //设置售价积分
1305         vo.setSellPoints(BigDecimal.ZERO);
44d306 1306         //设置一级子订单规格信息
6b1639 1307         vo.setSpecs("[]");//规格
Z 1308         vo.setGoodsNo(cardItem.getCode());//卡项编号
1309         vo.setGoodsName(cardItem.getName());//卡项名称
1310         vo.setGoodsImage("");//卡项主图
44d306 1311         //设置一级子订单疗程信息
Z 1312         vo.setSingle(0);//单次疗程数量
1313         vo.setUsedTotal(vo.getSingle()*vo.getBuyNum());//总次疗程次数,单次疗程次数*购买的总量
6b1639 1314
Z 1315         Map<String,BigDecimal> moneyMap=new HashMap<>();
415eef 1316         moneyMap.put("actualTotal",vo.getActualTotal());
ea2d26 1317         moneyMap.put("discountTotal",BigDecimal.ZERO);
6b1639 1318         return moneyMap;
Z 1319     }
1320
1321     /**
f9fd77 1322      * 开单-构建总订单实体                   实现类
8c307c 1323      *
Z 1324      * @param ordersTotal         总订单
1325      * @param shop                门店
1326      * @param shouldOrderTotal    订单应付总金额
1327      * @param discountTotal       订单折扣金额
1328      * @param operatorId          操作人
1329      * @param operatorName        操作人名称
1330      * @param operatorType        操作类型
1331      * @param platformApplication 平台实体
f9fd77 1332      */
012323 1333     public static void setOrderTotal(OrdersTotal ordersTotal, Shop shop,BigDecimal shouldOrderTotal,
e21429 1334                                      BigDecimal discountTotal, String operatorId, String operatorName, Integer operatorType, BigDecimal oriTotal, Integer type,
f58cfc 1335                                      User user, CreateNoService createNoService, PlatformApplication platformApplication, CommonService commonService) {
f9fd77 1336         //type:0:草稿 1:提交 2:结账
8c307c 1337         if (type == 0) {
f9fd77 1338             ordersTotal.setStatus(OrderTotalConstants.STATUS_DRAFT);//订单状态:草稿
Z 1339         }
f58cfc 1340         String totalNum = createNoService.createOrderNo("S", 8, "yyyyMMdd");//总订单编号
f9fd77 1341         ordersTotal.setOrderNo(totalNum);//总订单编号
Z 1342         ordersTotal.setOrderNo(ordersTotal.getOrderNo());//订单编号
1343         ordersTotal.setOperatorType(operatorType);//操作类型(0:系统管理员,1:员工)
1344         ordersTotal.setOperatorId(operatorId);//下单操作人标识
1345         ordersTotal.setOperatorName(operatorName);//下单操作人名称
1346         ordersTotal.setPayTotal(BigDecimal.ZERO);//支付现金金额
1347         ordersTotal.setPayRecharge(BigDecimal.ZERO);//支付储值金金额
1348         ordersTotal.setPayIncrement(BigDecimal.ZERO);//支付增值金金额
1349         ordersTotal.setShouldTotal(shouldOrderTotal);//订单应付支付总金额
1350         ordersTotal.setDiscountTotal(discountTotal);//优惠金额,以元为存储
1351         ordersTotal.setShopId(shop.getId());//操作人门店标识
1352         ordersTotal.setShopName(shop.getName());//操作人门店名称
1353         ordersTotal.setActualTotal(BigDecimal.ZERO);//用户实际支付的总金额
1354         ordersTotal.setChannelType(OrderTotalConstants.CHANNEL_TYPE_NOTHING);//暂定用没有渠道类型
374c64 1355         ordersTotal.setSourceCode(platformApplication == null ? ordersTotal.getAppId() : platformApplication.getAppId());//没有传就默认是请求的appId
8c307c 1356         ordersTotal.setSourceName(platformApplication == null ? ordersTotal.getAppId() : platformApplication.getName());//没有传就默认是请求的appId
e8809b 1357         ordersTotal.setOriTotal(oriTotal);
1ba6d1 1358
Z 1359         //判断第三方订单号是否为空,如果是则用订单编号
1360         List<String> platformType = Arrays.asList(PlatformConstants.TYPE_PLATFORM_HIS, PlatformConstants.TYPE_PLATFORM_PHIS, PlatformConstants.TYPE_PLATFORM_GUIDE);
374c64 1361         if(platformType.contains(ordersTotal.getAppIdCode()) && StringUtils.isEmpty(ordersTotal.getSourceOrderNo())){
1ba6d1 1362             ordersTotal.setSourceOrderNo(ordersTotal.getOrderNo());
Z 1363         }
570761 1364
Z 1365         //所属美容师
c4e004 1366         if(StringUtils.noNull(user.getBeauticianCorpUserId())){
Z 1367             Employee employee=commonService.selectOneByKeyBlob(EmployeeMapper.class,user.getBeauticianCorpUserId());
1368             if(employee!=null){
1369                 //所属美容师:用户所属美容师
1370                 ordersTotal.setBeauticianCorpUserId(employee.getId());
1371                 ordersTotal.setBeauticianCorpUserName(StringUtils.isEmpty(employee.getCnName())?"":employee.getCnName());
1372             }
efcde5 1373         }
570761 1374
f9fd77 1375         logger.info("【设置总订单参数】ordersTotal: {}", JSON.toJSONString(ordersTotal));
Z 1376     }
1377
1378     /**
8c307c 1379      * 设置订单的开发人信息
Z 1380      * @param ordersTotal
1381      * @return
f9fd77 1382      */
8c307c 1383     public static void handUserDeveloper(OrdersTotal ordersTotal,User user,List<String> orderTypeList,CommonService commonService){
Z 1384         //如果开发人集合为空,那么默认用户的所属顾问
1385         SqlSentence sqlSentence = new SqlSentence();
1386         Map<String, Object> map = new HashMap<>();
1387         sqlSentence.setM(map);
1388         map.put("shopId", ordersTotal.getDeveloperShopId());
1389         if (StringUtils.noNull(user.getHisCorpUserId()) && !orderTypeList.contains(ordersTotal.getType()) && !PlatformConstants.TYPE_PLATFORM_CRM.equals(ordersTotal.getAppIdCode())) {
1390             map.put("hisCorpUserId", user.getHisCorpUserId());
1391 //            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}");
1392             //荣爷说放开所有权限,顾问可以开任意一个门店的单
1393             sqlSentence.setSqlSentence("SELECT id,name FROM  shop WHERE id=#{m.shopId}");
1394             Shop userShop = commonService.selectOne(ShopMapper.class, sqlSentence);
1395             if (userShop == null) {
1396                 throw new PlatTipsException(PlatformCode.ERROR_TIPS, "用户绑定的顾问不在该门店");
1397             }
1398             Employee userAdiver = commonService.selectOneByKey(EmployeeMapper.class, user.getHisCorpUserId());
1399             //开发人门店标识(用户绑定的销售顾问)
1400             ordersTotal.setDeveloperShopId(userShop.getId());
1401             //开发人门店名称
1402             ordersTotal.setDeveloperShopName(userShop.getName());
1403             ordersTotal.setDeveloperId(userAdiver.getId());
1404             ordersTotal.setDeveloperName(userAdiver.getCnName());
1405         } else if (StringUtils.noNull(user.getHisCorpUserId()) && !orderTypeList.contains(ordersTotal.getType()) && !PlatformConstants.TYPE_PLATFORM_CRM.equals(ordersTotal.getAppIdCode())) {
1406             //该操作支持crm开单
1407             sqlSentence.setSqlSentence("SELECT id,name FROM  shop WHERE id=#{m.shopId}");
1408             Shop userShop = commonService.selectOne(ShopMapper.class, sqlSentence);
1409             //开发人门店标识(用户绑定的销售顾问)
1410             ordersTotal.setDeveloperShopId(ordersTotal.getDeveloperShopId());
1411             //开发人门店名称
1412             ordersTotal.setDeveloperShopName(userShop == null ? "" : userShop.getName());
1413             ordersTotal.setDeveloperId(ordersTotal.getDeveloperId());
1414             ordersTotal.setDeveloperName(ordersTotal.getDeveloperName());
1415         }
1416         //荣爷说开发人必填此处不需要默认用户的咨询师  22-05-27
1417 //        else if(!orderTypeList.contains(ordersTotal.getType())){
1418 //            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"请填写好开发人或者用户绑定一个销售顾问在重试");
1419 //        }
1420     }
f9fd77 1421
8c307c 1422     /**
e7427f 1423      * 判断是否是老带新 用新工具类 OrderUtil 复制代码过去统一管理
8c307c 1424      *
e7427f 1425      * @param ordersTotal  总订单
A 1426      * @param commonService 公共方法
1427      * @return 返回
8c307c 1428      */
Z 1429     public static Integer checkOldBringNew(OrdersTotal ordersTotal, CommonService commonService) {
1430         User user = commonService.selectOneByKeyBlob(UserMapper.class, ordersTotal.getUserId());
1431         if (user != null) {
1432             if (StringUtils.isEmpty(user.getInviteeId())) {
1433                 //没有推荐人则不是老带新,说明客户是自来的
1434                 return BaseEntity.NO;
1435             } else {
1436                 SqlSentence sqlSentence = new SqlSentence();
1437                 Map<String, Object> map = new HashMap<>();
1438                 sqlSentence.setM(map);
1439                 //判断推荐人是否有支付记录
1440                 map.put("inviteeId", user.getInviteeId());
1441                 map.put("isDel", BaseEntity.NO);
1442                 map.put("status", OrderTotalConstants.STATUS_PAY);
1443                 sqlSentence.setSqlSentence(" userId=#{m.inviteeId} and status=#{m.status} and isDel=#{m.isDel} ");
1444                 int count = commonService.selectCount(OrdersTotalMapper.class, sqlSentence);
1445                 if (count > 0) {
1446                     map.clear();
1447                     map.put("userId", ordersTotal.getUserId());
1448                     map.put("isDel", BaseEntity.NO);
f75810 1449                     sqlSentence.setSqlSentence("SELECT * FROM v_visit_record WHERE userId =#{m.userId} and isDel=#{m.isDel} and arrivalTime is not NULL ORDER BY arrivalTime LIMIT 1");
fa260e 1450                     VisitRecord visitRecord = commonService.selectOne(VisitRecordMapper.class, sqlSentence);
Z 1451                     if (visitRecord != null) {
8c307c 1452                         //订单时间在到访时间之后则不算老带新
fa260e 1453                         if (DateUtil.timeEqual(visitRecord.getArrivalTime(), ordersTotal.getCreateTime())) {
153991 1454                             logger.info("老带新提示:该订单用户订单创建时间和到访时间是当天算老带新,用户标识:{},总订单标识:{}", user.getId(), ordersTotal.getId());
Z 1455                             return BaseEntity.YES;
fa260e 1456                         }else if (ordersTotal.getCreateTime().before(visitRecord.getArrivalTime())) {
8c307c 1457                             logger.info("老带新提示:该订单用户订单创建时间在到访时间之前算老带新,用户标识:{},总订单标识:{}", user.getId(), ordersTotal.getId());
Z 1458                             return BaseEntity.YES;
1459                             //订单时间和到访时间是同一天则算老带新
fa260e 1460                         }else if (ordersTotal.getCreateTime().after(visitRecord.getArrivalTime())) {
153991 1461                             logger.info("老带新提示:该订单用户订单创建时间在到访时间之后不算老带新,用户标识:{},总订单标识:{}", user.getId(), ordersTotal.getId());
Z 1462                             return BaseEntity.NO;
1463                             //订单时间在到访时间之前则算老带新
1464                         }else {
8c307c 1465                             logger.info("老带新提示:以上情况都不符合,默认不算老带新,用户标识:{},总订单标识:{}", user.getId(), ordersTotal.getId());
Z 1466                             //否则默认不算老带新
1467                             return BaseEntity.NO;
1468                         }
1469                     } else {
1470                         logger.info("老带新提示:该订单用户没有找到预约信息,算老带新,用户标识:{},总订单标识:{}", user.getId(), ordersTotal.getId());
1471                         //没有找到该用户的预约,该用户推荐人又不为空,那么算是老带新,老带新:在该用户到访前和到访当天开的单都是老带新
1472                         return BaseEntity.YES;
1473                     }
1474                 } else {
1475                     logger.info("老带新提示:该订单用户的推荐人没有支付订单不算老客,推荐人:{},总订单标识:{}", user.getInviteeId(), ordersTotal.getId());
1476                     return BaseEntity.NO;
1477                 }
1478             }
1479         } else {
1480             logger.info("老带新提示:未找到该用户信息!");
1481             return BaseEntity.NO;
1482         }
1483     }
1484
1485     /**
e7427f 1486      * 判断是否是初诊单 用新工具类 OrderUtil 复制代码过去统一管理
A 1487      * @param ordersTotal 总订单
1488      * @param commonService 公共方法
1489      * @return 返回
e1d05e 1490      */
Z 1491     public static Integer checkIsBothTheOne(OrdersTotal ordersTotal, CommonService commonService) {
1492         User user = commonService.selectOneByKeyBlob(UserMapper.class, ordersTotal.getUserId());
1493         if (user != null) {
1494             SqlSentence sqlSentence = new SqlSentence();
1495             Map<String, Object> map = new HashMap<>();
1496             sqlSentence.setM(map);
1497             //判断推荐人是否有支付记录
1498             map.put("userId", ordersTotal.getUserId());
1499             map.put("isDel", BaseEntity.NO);
e7427f 1500             sqlSentence.setSqlSentence("SELECT * FROM v_visit_record WHERE userId =#{m.userId} and isDel=#{m.isDel} and arrivalTime is not NULL ORDER BY arrivalTime LIMIT 1");
A 1501             VisitRecord visitRecord = commonService.selectOne(VisitRecordMapper.class, sqlSentence);
1502             if (visitRecord != null) {
e1d05e 1503                 //订单时间在到访时间之后则不算老带新
77b239 1504                 if (DateUtil.timeEqual(visitRecord.getArrivalTime(), ordersTotal.getCreateTime())) {
e33dcb 1505                     logger.info("初诊单提示:该订单用户订单创建时间和到访时间是当天算初诊单,用户标识:{},总订单标识:{}", user.getId(), ordersTotal.getId());
bf5cba 1506                     return BaseEntity.YES;
e1d05e 1507                     //订单时间在到访时间之前则算老带新
e7427f 1508                 } else if (ordersTotal.getCreateTime().before(visitRecord.getArrivalTime())) {
e1d05e 1509                     logger.info("初诊单提示:该订单用户订单创建时间在到访时间之前算初诊单,用户标识:{},总订单标识:{}", user.getId(), ordersTotal.getId());
Z 1510                     return BaseEntity.YES;
1511                     //订单时间和到访时间是同一天则算老带新
e7427f 1512                 } else if (ordersTotal.getCreateTime().after(visitRecord.getArrivalTime())) {
e33dcb 1513                     logger.info("初诊单提示:该订单用户订单创建时间在到访时间之后不算初诊单,用户标识:{},总订单标识:{}", user.getId(), ordersTotal.getId());
bf5cba 1514                     return BaseEntity.NO;
e1d05e 1515                 } else {
Z 1516                     logger.info("初诊单提示:以上情况都不符合,默认不算初诊单,用户标识:{},总订单标识:{}", user.getId(), ordersTotal.getId());
1517                     //否则默认不算老带新
1518                     return BaseEntity.NO;
1519                 }
1520             } else {
1521                 logger.info("初诊单提示:该订单用户没有找到预约信息,算初诊单,用户标识:{},总订单标识:{}", user.getId(), ordersTotal.getId());
1522                 //没有找到该用户的预约,该用户推荐人又不为空,那么算是老带新,老带新:在该用户到访前和到访当天开的单都是老带新
1523                 return BaseEntity.YES;
1524             }
1525         } else {
1526             logger.info("初诊单提示:未找到该用户信息!");
1527             return BaseEntity.NO;
1528         }
1529     }
1530
1531     /**
8c307c 1532      * 保存orderInfo信息
Z 1533      *
1534      * @param ordersTotal   总订单
1535      * @param user          用户信息
1536      * @param commonService
1537      * @param isOldBringNew 是否老带新(0:否,1:是)
1538      */
5c0db7 1539     public static String saveOrderInfo(OrdersTotal ordersTotal, User user, CustomParameter customParameter, Integer isOldBringNew, Integer isBothTheOne
C 1540             , OrderPHisDto orderPHisDto, CommonService commonService) {
8c307c 1541         String activityName = "";
Z 1542         OrderInfo orderInfo = ordersTotal.getOrderInfo();
59f4be 1543         logger.info("接收的orderInfo:"+JSON.toJSONString(orderInfo));
8c307c 1544         BigDecimal defaultDiscount = new BigDecimal(100);
Z 1545         if (ordersTotal.getOrderInfo() == null) {
26ba36 1546             orderInfo = new OrderInfo();
8c307c 1547             orderInfo.setIsWholeDiscount(0);
Z 1548             orderInfo.setIsEmployeeDiscount(0);
1549             orderInfo.setDiscount(defaultDiscount);
1550         } else {
cb7d81 1551             orderInfo.setIsEmployeeDiscount(StringUtils.noNull(ordersTotal.getKoapOrderId()) ? BaseEntity.NO : ordersTotal.getOrderInfo().getIsEmployeeDiscount());
F 1552             orderInfo.setIsWholeDiscount(StringUtils.noNull(ordersTotal.getKoapOrderId()) ? BaseEntity.NO : ordersTotal.getOrderInfo().getIsWholeDiscount());
8c307c 1553             if (StringUtils.noNull(ordersTotal.getKoapOrderId())) {
Z 1554                 orderInfo.setDiscount(defaultDiscount);
1555             } else {
c10144 1556 //                if (ordersTotal.getOrderInfo().getDiscount() == null) {
Z 1557 //                    throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL, "整单折扣/员工折扣参数为空,默认为100!");
1558 //                }
1559                 orderInfo.setDiscount(ordersTotal.getOrderInfo().getDiscount()==null?new BigDecimal(100):ordersTotal.getOrderInfo().getDiscount());
8c307c 1560             }
Z 1561         }
26ba36 1562         orderInfo.setOrderId(ordersTotal.getId());
F 1563         //如果有用活动规则将活动规则保存在orderInfo
1564         if (ordersTotal.getActivityIds() != null && ordersTotal.getActivityIds().size() > 0) {
1565             //查询活动规则基础信息
1566             ActivityRule activityRule = commonService.selectOneByKey(ActivityRuleMapper.class, ordersTotal.getActivityIds().get(0));
1567             if (activityRule != null) {
1568                 orderInfo.setActivityId(activityRule.getId());
1569                 orderInfo.setActivityName(activityRule.getName());
1570                 activityName = activityRule.getName();
1571             }
1572         }
64d5ae 1573         //用户推荐人处理
8c307c 1574         if (StringUtils.noNull(user.getInviteeId())) {
Z 1575             orderInfo.setInviteeId(user.getInviteeId());
1576             User userInviteeName = commonService.selectOneByKeyBlob(UserMapper.class, user.getInviteeId());
64d5ae 1577             SqlSentence sqlSentence = new SqlSentence();
C 1578             Map<String,Object> values = new HashMap<>();
1579             if(userInviteeName != null){
1580                 //推荐人是用户,获取其unionId
1581                 values.put("userId",userInviteeName.getId());
1582                 values.put("fromCode",customParameter.getCrmMpAppId());
29b738 1583                 sqlSentence.sqlSentence("SELECT * FROM user_union_his WHERE isDel = 0 AND userId = #{m.userId} AND fromCode = #{m.fromCode} ORDER BY createTime DESC LIMIT 1",values);
64d5ae 1584                 UserUnionHis userUnionHis = commonService.selectOne(UserUnionHisMapper.class,sqlSentence);
C 1585                 if(userUnionHis != null){
1586                     //查找员工
1587                     values.clear();
1588                     values.put("crmUnionId",userUnionHis.getUnionId());
4ba6e4 1589                     sqlSentence.sqlSentence("SELECT * FROM employee WHERE isDel = 0 AND isJob = 1 AND unionId = #{m.crmUnionId} ORDER BY createTime DESC LIMIT 1",values);
64d5ae 1590                     Employee comEmployee =  commonService.selectOne(EmployeeMapper.class,sqlSentence);
C 1591                     if(comEmployee != null){
1592                         //判断是不是医生
1593                         values.clear();
1594                         values.put("employeeId",comEmployee.getId());
1595                         values.put("roleUniqueStr",customParameter.getDoctorRoleCode());
1596                         sqlSentence.sqlSentence("SELECT * FROM employee_role WHERE isDel = 0 AND employeeId = #{m.employeeId} AND roleUniqueStr = #{m.roleUniqueStr}",values);
1597                         List<EmployeeRole> employeeRoleList = commonService.selectList(EmployeeRoleMapper.class,sqlSentence);
1598                         if(employeeRoleList.size() > 0){
1599                             orderInfo.setInviteeType(OrderInfo.INVITEE_TYPE_USER_DOCTOR);
1600                         }else{
1601                             orderInfo.setInviteeType(OrderInfo.INVITEE_TYPE_USER_STAFF);
1602                         }
1603                         orderInfo.setInviteeNo(comEmployee.getEmployeeNo());
1604                         orderInfo.setInviteeName(comEmployee.getCnName());
1605                     }else{
1606                         orderInfo.setInviteeType(OrderInfo.INVITEE_TYPE_USER);
1607                         orderInfo.setInviteeNo(userInviteeName.getMemberNO());
1608                         orderInfo.setInviteeName(userInviteeName.getName());
1609                     }
1610                 }else{
1611                     orderInfo.setInviteeType(OrderInfo.INVITEE_TYPE_USER);
1612                     orderInfo.setInviteeNo(userInviteeName.getMemberNO());
1613                     orderInfo.setInviteeName(userInviteeName.getName());
8c307c 1614                 }
64d5ae 1615                 orderInfo.setInviteeCIQ(userInviteeName.getCIQ());
C 1616             }else{
1617                 Employee employeeInviteeName = commonService.selectOneByKeyBlob(EmployeeMapper.class, user.getInviteeId());
1618                 if(employeeInviteeName != null){
1619                     //判断是不是医生
1620                     values.clear();
1621                     values.put("employeeId",employeeInviteeName.getId());
1622                     values.put("roleUniqueStr",customParameter.getDoctorRoleCode());
1623                     sqlSentence.sqlSentence("SELECT * FROM employee_role WHERE isDel = 0 AND employeeId = #{m.employeeId} AND roleUniqueStr = #{m.roleUniqueStr}",values);
1624                     List<EmployeeRole> employeeRoleList = commonService.selectList(EmployeeRoleMapper.class,sqlSentence);
1625                     if(employeeRoleList.size() > 0){
1626                         orderInfo.setInviteeType(OrderInfo.INVITEE_TYPE_STAFF_DOCTOR);
1627                     }else{
1628                         orderInfo.setInviteeType(OrderInfo.INVITEE_TYPE_STAFF_STAFF);
1629                     }
1630                     orderInfo.setInviteeName(employeeInviteeName.getCnName());
1631                     orderInfo.setInviteeNo(employeeInviteeName.getEmployeeNo());
833a5f 1632                 }else{
C 1633                     orderInfo.setInviteeType(OrderInfo.INVITEE_TYPE_UNKNOW);
64d5ae 1634                 }
8c307c 1635             }
Z 1636         } else {
64d5ae 1637             orderInfo.setInviteeType(OrderInfo.INVITEE_TYPE_UNKNOW);
8c307c 1638         }
e3aeb9 1639         orderInfo.setChannelId(user.getChannelId());
C 1640         orderInfo.setChannelName(user.getChannelType());
1641         orderInfo.setChannel2Id(user.getChannel2Id());
1642         orderInfo.setChannelName2(user.getChannelType2());
1643         orderInfo.setChannelCategory(user.getChannelCategory());
a24ed7 1644
F 1645         //副渠道信息
e3aeb9 1646         orderInfo.setChannelAssistId(user.getChannelAssistId());
C 1647         orderInfo.setChannelAssistName(user.getChannelAssistName());
1648         orderInfo.setChannelAssist2Id(user.getChannelAssist2Id());
1649         orderInfo.setChannelAssist2Name(user.getChannelAssist2Name());
1650         orderInfo.setChannelAssist2Json(UserChannelTool.getChannelJsonInfo(commonService, user.getChannelAssist2Id()));
a24ed7 1651
5c0db7 1652         //收件地址
C 1653         if(orderPHisDto.getDeliveryAddress() != null){
1654             orderInfo.setLinkMan(orderPHisDto.getDeliveryAddress().getLinkMan());
1655             orderInfo.setLinkTel(orderPHisDto.getDeliveryAddress().getLinkTel());
1656             orderInfo.setProvince(orderPHisDto.getDeliveryAddress().getProvince());
1657             orderInfo.setCity(orderPHisDto.getDeliveryAddress().getCity());
1658             orderInfo.setArea(orderPHisDto.getDeliveryAddress().getArea());
1659             orderInfo.setStreet(orderPHisDto.getDeliveryAddress().getStreet());
1660             orderInfo.setAddrDetail(orderPHisDto.getDeliveryAddress().getAddrDetail());
1661         }
1662
8c307c 1663         orderInfo.setSnapPayTotal(BigDecimal.ZERO);
Z 1664         orderInfo.setIsOldBringNew(isOldBringNew);
e1d05e 1665         orderInfo.setIsBothTheOne(isBothTheOne);
185529 1666         // 处理初复诊到店时间
A 1667         if (isBothTheOne != null && (OrderInfo.FIRST_VISIT == isBothTheOne || OrderInfo.CONSULTATION == isBothTheOne)){
1668             // 获取下单当天离下单时间最近的到访时间
1669             Date arrivalTime = OrderUtil.getNearOrderArrivalTime(commonService, VisitRecordMapper.class, ordersTotal.getCreateTime(), ordersTotal.getUserId());
1670             if (arrivalTime != null){
1671                 orderInfo.setBothTheOneTime(arrivalTime);
1672             }
1673         }
4a4134 1674         //设置用户状态
A 1675         orderInfo.setUserStatus(user.getUserStatus());
c4e004 1676         //设置用户所属顾问
Z 1677         if(StringUtils.noNull(user.getHisCorpUserId())){
1678             Employee hisCorpEmployee=commonService.selectOneByKeyBlob(EmployeeMapper.class,user.getHisCorpUserId());
1679             if(hisCorpEmployee!=null){
1680                 orderInfo.setHisCorpUserId(hisCorpEmployee.getId());
1681                 orderInfo.setHisCorpUserName(StringUtils.isEmpty(hisCorpEmployee.getCnName())?"":hisCorpEmployee.getCnName());
1682             }
1683         }
3ac7d2 1684         //设置用户所属门店
C 1685         if(StringUtils.noNull(user.getShopId())){
1686             Shop userShop=commonService.selectOneByKey(ShopMapper.class,user.getShopId());
1687             if(userShop!=null){
1688                 orderInfo.setUserShopId(userShop.getId());
1689                 orderInfo.setUserShopNo(userShop.getCode());
1690                 orderInfo.setUserShopName(userShop.getName());
1691             }
1692         }
c4e004 1693         //设置用户所属分诊医生:用户所属医生
Z 1694         if(StringUtils.noNull(user.getDoctorCorpUserId())){
1695             Employee doctorCorpEmployee=commonService.selectOneByKeyBlob(EmployeeMapper.class,user.getDoctorCorpUserId());
1696             if(doctorCorpEmployee!=null){
46a6ff 1697                 orderInfo.setDoctorCorpUserId(doctorCorpEmployee.getId());
Z 1698                 orderInfo.setDoctorCorpUserName(StringUtils.isEmpty(doctorCorpEmployee.getCnName())?"":doctorCorpEmployee.getCnName());
c4e004 1699             }
Z 1700         }
dd101a 1701         //设置用户所属护士:用户所属护士
Z 1702         if(StringUtils.noNull(user.getNurseCorpUserId())){
1703             Employee nurseCorpEmployee=commonService.selectOneByKeyBlob(EmployeeMapper.class,user.getNurseCorpUserId());
1704             if(nurseCorpEmployee!=null){
3ec621 1705                 orderInfo.setNurseCorpUserId(nurseCorpEmployee.getId());
Z 1706                 orderInfo.setNurseCorpUserName(StringUtils.isEmpty(nurseCorpEmployee.getCnName())?"":nurseCorpEmployee.getCnName());
dd101a 1707             }
Z 1708         }
1709         //设置用户所属电网咨询师(tmk):用户所属电网咨询师
1710         if(StringUtils.noNull(user.getInternetCorpUserId())){
1711             Employee internetCorpEmployee=commonService.selectOneByKeyBlob(EmployeeMapper.class,user.getInternetCorpUserId());
1712             if(internetCorpEmployee!=null){
3ec621 1713                 orderInfo.setInternetCorpUserId(internetCorpEmployee.getId());
Z 1714                 orderInfo.setInternetCorpUserName(StringUtils.isEmpty(internetCorpEmployee.getCnName())?"":internetCorpEmployee.getCnName());
dd101a 1715             }
Z 1716         }
db0b4d 1717         // 会员信息填充
A 1718         orderInfo.setMemberLevelId(user.getMemberLevelId());
1719         orderInfo.setMemberLevelName(user.getUserLevel());
dd101a 1720
8c307c 1721         commonService.insert(OrderInfoMapper.class, orderInfo);
Z 1722         return activityName;
1723     }
1724
1725     /**
1726      * 创建总订单
1727      *
1728      * @param ordersTotal   总订单信息
1729      * @param operator      操作人map
1730      * @param user          用户资料
1731      * @param userMoney     用户资金信息
406f60 1732      * @param commonService 公共方法
8c307c 1733      */
3043fe 1734     public static OrdersTotal createOrder(OrdersTotal ordersTotal, Map<String, String> operator, User user, UserMoney userMoney, CommonService commonService) {
406f60 1735         if (ordersTotal != null && StringUtils.noNull(ordersTotal.getInviteeId())) {
0c698a 1736             if (ordersTotal.getInviteeType() == null || UserChannelType.CATEGORY_MEMBERS.equals(ordersTotal.getInviteeType())) {
A 1737                 User users = commonService.selectOneByKeyBlob(UserMapper.class, ordersTotal.getInviteeId());
1738                 if (users != null) {
1739                     ordersTotal.setUserBelongingType(users.getUserBelongingType());
1740                 }
8c307c 1741             }
Z 1742         }
1743
1744         if (StringUtils.noNull(ordersTotal.getKoapOrderId())) {
1745             BigDecimal koapItemMoney = SynOrderUtil.koapItemMoney(ordersTotal.getKoapOrderId(), commonService);
1746
1747             ordersTotal.setShouldTotal(koapItemMoney);
1748             ordersTotal.setTotal(koapItemMoney);
1749         }
1750         ordersTotal.setUserLevel(user == null ? "" : user.getUserLevel());
1751         ordersTotal.setCIQ(user == null ? "" : user.getCIQ());
1752         ordersTotal.setStoredValueFund(userMoney == null ? "" : userMoney.getStoredValueFund());
1753         ordersTotal.setValueAddedFund(userMoney == null ? "" : userMoney.getValueAddedFund());
1754         ordersTotal.setIntegral(userMoney == null ? "" : userMoney.getIntegral());
cff937 1755
C 1756         //用户所属门店
1757         if(user != null && StringUtils.noNull(user.getShopId())){
1758             Shop shop = commonService.selectOneByKey(ShopMapper.class,user.getShopId());
1759             if(shop != null){
1760                 ordersTotal.setUserShopId(shop.getId());
1761                 ordersTotal.setUserShopNo(shop.getCode());
1762                 ordersTotal.setUserShopName(shop.getName());
1763             }
1764         }
1765
dbf2ab 1766         // 是否拼团
A 1767         if (ordersTotal.getIsGroupBuy() == null){
1768             ordersTotal.setIsGroupBuy(BaseEntity.NO);
1769         }
1770
8c307c 1771         //创建总订单
Z 1772         commonService.insert(OrdersTotalMapper.class, ordersTotal);
1773         return ordersTotal;
1774     }
1775
1776
1777     /**
1778      * 开单-创建一级子订单                       实现类
1779      *
1780      * @param ordersTotal                       总订单
1781      * @param items                             一级子订单集合
1782      */
1783     public static void createOrderItem(OrdersTotal ordersTotal, List<OrderItem> items, ProjectService projectService,
1784                                        CommonService commonService, SqlSentence sqlSentence, Map<String, Object> map) {
1785         map.clear();
1786         for (OrderItem vo : items) {
f9fd77 1787             //保存一级子订单
Z 1788             Map<String, Object> bodyPartMap = projectService.selectBodyPart(vo.getCommonId());
8c307c 1789             vo.setBodyPartsId(bodyPartMap == null ? "" : (String) bodyPartMap.get("bodyPartId"));
Z 1790             vo.setBodyPartsName(bodyPartMap == null ? "" : (String) bodyPartMap.get("bodyPartName"));
91de2f 1791             //计算折扣金额
444b3d 1792             vo.setDiscountOrderPrice(vo.getProPrice().multiply(BigDecimal.valueOf(vo.getBuyNum()).setScale(2,RoundingMode.HALF_UP)).subtract(vo.getTotal()));
f9fd77 1793             vo.setOrderId(ordersTotal.getId());
Z 1794
80fecc 1795             //诉求分类
F 1796             List<AppealTypeVo> appealTypeVoList = null;
e3aeb9 1797             if (OrderItemConstants.TYPE_PROJECT.equals(vo.getType())) {
80fecc 1798                 //获取诉求分类列表
F 1799                 appealTypeVoList = AppealTypeTool.getAppealTypeListByIdList(vo.getAppealTypeIdList(), commonService);
1800             }
1801             if (appealTypeVoList != null && appealTypeVoList.size() > 0) {
1802                 String str = AppealTypeTool.getStr(appealTypeVoList);
c9270b 1803                 logger.info("拼接的诉求分类:"+str);
80fecc 1804                 if (StringUtils.noNull(str)) {
F 1805                     String[] split = str.split("-");
1806                     for (int i = 0; i < split.length; i++) {
1807                         if (i == 0) {
1808                             vo.setAppealSecond(split[i]);
1809                         } else if (i == 1) {
1810                             vo.setAppealThird(split[i]);
1811                         }
1812                     }
1813                 }
1814             }
1815             commonService.insert(OrderItemMapper.class, vo);
1816             //述求分类生成,目前项目
1817             if (appealTypeVoList != null && appealTypeVoList.size() > 0){
1818                 //生成关联关系
1819                 AppealTypeTool.orderProjectInsert(ordersTotal.getId(), vo.getId(), appealTypeVoList, commonService);
e3aeb9 1820             }
C 1821
8c307c 1822             map.put("commonId", vo.getCommonId());
Z 1823             map.put("isDel", BaseEntity.NO);
f9fd77 1824
Z 1825             //处理一级子订单卡项
8c307c 1826             if (OrderItemConstants.TYPE_CARD.equals(vo.getType())) {
f9fd77 1827                 sqlSentence.setSqlSentence("select * from card_item_info where cardItemId=#{m.commonId} and isDel=#{m.isDel}");
8c307c 1828                 List<CardItemInfo> cardItemInfoList = commonService.selectList(CardItemInfoMapper.class, sqlSentence);
f9fd77 1829                 for (CardItemInfo cardItemInfo : cardItemInfoList) {
Z 1830                     //生成卡项二级子订单
bcd046 1831                     OrderCreateUtil.createOrderItemSonCard(ordersTotal.getId(), vo, cardItemInfo, 0, commonService);
f9fd77 1832                 }
8c307c 1833                 //处理一级子订单促销
Z 1834             } else if (OrderItemConstants.TYPE_PROMOTION.equals(vo.getType())) {
f9fd77 1835                 sqlSentence.setSqlSentence("select * from promotion_info where promotionId=#{m.commonId} and isDel=#{m.isDel}");
8c307c 1836                 List<PromotionInfo> promotionInfoList = commonService.selectList(PromotionInfoMapper.class, sqlSentence);
f9fd77 1837                 //生成促销二级子订单
141ad3 1838                 OrderCreateUtil.createOrderItemProm(ordersTotal, vo,promotionInfoList, commonService);
8c307c 1839                 //处理一级子订单卡包
Z 1840             } else if (OrderItemConstants.CARD_BAG.equals(vo.getType())) {
1841                 if (vo.getCardItemInfoIds() != null && vo.getCardItemInfoIds().size() > 0) {
1842                     for (CardItemInfo info : vo.getCardItemInfoIds()) {
f9fd77 1843                         map.clear();
8c307c 1844                         map.put("id", info.getId());
Z 1845                         map.put("isDel", BaseEntity.NO);
f9fd77 1846                         sqlSentence.setSqlSentence("select * from card_item_info where  id =#{m.id} and isDel=#{m.isDel}  ");
Z 1847                         CardItemInfo cardItemInfo = commonService.selectOne(CardItemInfoMapper.class, sqlSentence);
8c307c 1848                         if (cardItemInfo != null) {
f9fd77 1849                             cardItemInfo.setUserNum(info.getUserNum());
Z 1850                             //生成卡包二级子订单
bcd046 1851                             OrderCreateUtil.createOrderItemSonCard(ordersTotal.getId(), vo, cardItemInfo, 1, commonService);
f9fd77 1852                         }
Z 1853                     }
1854
1855                 }
1856             }
253802 1857             //虚拟商品
20d569 1858             else if (OrderItemConstants.TYPE_COUPON.equals(vo.getType())){
253802 1859                 //查询关联中间表数据
F 1860                 map.clear();
20d569 1861                 map.put("consumablesId", vo.getCommonId());
F 1862                 sqlSentence.setSqlSentence("select * from consumables_assoc where isDel = 0 and consumablesId = #{m.consumablesId} ");
253802 1863                 List<ConsumablesAssoc> assocList = commonService.selectList(ConsumablesAssocMapper.class, sqlSentence);
F 1864                 createOrderItemAssocToVirtualGoods(commonService, vo, assocList);
f9fd77 1865             }
Z 1866
1867             //保存子订单商品规格表
1868             OrderGoodsSpecs orderGoodsSpecs;
1869             JSONArray objects = JSONArray.parseArray(vo.getSpecs());
8c307c 1870             for (int i = 0; i < objects.size(); i++) {
f9fd77 1871                 JSONObject object = objects.getJSONObject(i);
8c307c 1872                 orderGoodsSpecs = new OrderGoodsSpecs();
f9fd77 1873                 setOrderGoodsSpecs(ordersTotal, orderGoodsSpecs, vo, object);
8c307c 1874                 commonService.insert(OrderGoodsSpecsMapper.class, orderGoodsSpecs);
Z 1875             }
1876         }
1877     }
1878
1879     /**
bcd046 1880      * 开单-创建卡项的二级子订单
Z 1881      * @param ordersId    总订单标识
1882      * @param vo
1883      * @param
1884      */
e065e7 1885     public static void createOrderItemSonCard(String ordersId, OrderItem vo,CardItemInfo cardItemInfo,Integer type,CommonService commonService) {
bcd046 1886         if(cardItemInfo.getPrice()==null){
Z 1887             throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"开单:卡项组合项目价格为空,组合项标识:"+cardItemInfo.getId());
1888         }
1889         if(cardItemInfo.getMaxNum()==null && cardItemInfo.getEveryDrawNum()==null){
1890             throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"开单:卡项组合项目最大次数或者每次划扣的扣减次数为空,组合项标识:"+cardItemInfo.getId());
1891         }
415eef 1892         CardEquity cardEquity = commonService.selectOneByKey(CardEquityMapper.class,cardItemInfo.getCardEquityId());
C 1893
bcd046 1894         //卡项明细分摊金额
Z 1895         BigDecimal shareMoney=cardItemInfo.getShareMoney()==null?cardItemInfo.getEveryShareMoney():cardItemInfo.getShareMoney();
415eef 1896
C 1897         BigDecimal pro2Price;
1898         BigDecimal curPrice;
1899
bcd046 1900         //每次划扣金额
415eef 1901         BigDecimal everySnapMoney;
154469 1902         OrderItemSon orderItemSon=new OrderItemSon();
bcd046 1903
Z 1904         //卡项明细最大次数或者每次划扣次数
1905         Integer cardNum=cardItemInfo.getMaxNum() ==null ?cardItemInfo.getEveryDrawNum():cardItemInfo.getMaxNum();
1c40c5 1906
Z 1907         //数量
1908         orderItemSon.setBuyNum(OrderItemConstants.CARD_BAG.equals(vo.getType())?cardItemInfo.getUserNum():cardNum);
bcd046 1909         if(OrderItemConstants.CARD_BAG.equals(vo.getType())){
415eef 1910             if(CardEquity.EQUITY_TYPE_TIME_CARD.equals(cardEquity.getEquityType())){
C 1911                 //-----次卡
1912                 //每次划扣金额
1913                 if(cardItemInfo.getEveryDrawNum()>0){
1914                     everySnapMoney = cardItemInfo.getEveryShareMoney().divide(BigDecimal.valueOf(cardItemInfo.getEveryDrawNum()),10,RoundingMode.HALF_UP);
1915                 }else{
1916                     everySnapMoney = BigDecimal.ZERO;
1917                 }
1918                 pro2Price = cardItemInfo.getEveryShareMoney();
1919             }else{
1920                 //-----N选M/固定项目/其他
1921                 //每次划扣金额
1922                 if(cardItemInfo.getMaxNum()>0){
1923                     everySnapMoney = cardItemInfo.getShareMoney().divide(BigDecimal.valueOf(cardItemInfo.getMaxNum()),2,RoundingMode.HALF_UP);
1924                 }else{
1925                     everySnapMoney = BigDecimal.ZERO;
1926                 }
1927                 pro2Price = everySnapMoney;
1928             }
1929             curPrice = pro2Price;
bcd046 1930         }else{
415eef 1931             pro2Price = shareMoney;
C 1932             curPrice = pro2Price;
bcd046 1933         }
415eef 1934
bcd046 1935         BigDecimal originalPrice=BigDecimal.ZERO;
154469 1936         if(OrderTotalConstants.TYPE_PROJECT.equals(cardItemInfo.getCommonType())){
Z 1937             Project project=commonService.selectOneByKeyBlob(ProjectMapper.class,cardItemInfo.getCommonId());
1938             BigDecimal proPrice=BigDecimal.ZERO;
1939             if(project!=null&&project.getOriginalPrice()!=null){
1940                 originalPrice=project.getOriginalPrice();
04c26a 1941             }
154469 1942             if(project!=null&&project.getPrice()!=null){
Z 1943                 proPrice=project.getPrice();
1944             }
1945             SqlSentence sqlSentence = new SqlSentence();
1946             Map<String,Object> objectMap=new HashMap<>();
1947             sqlSentence.setM(objectMap);
1948
1949             objectMap.put("commonId",vo.getCommonId());
1950             objectMap.put("isDel",BaseEntity.NO);
1951             sqlSentence.setSqlSentence("select * from project_info where projectId=#{m.commonId} and isDel=#{m.isDel} ");
1952             ProjectInfo projectInfo=commonService.selectOne(ProjectInfoMapper.class,sqlSentence);
1953
1954             JSONArray jsonObject=new JSONArray();
1955             sqlSentence.setSqlSentence("select * from project_spec_value where projectId=#{m.commonId} and isDel=#{m.isDel} ");
1956             List<ProjectSpecValue> projectSpecValues = commonService.selectList(ProjectSpecValueMapper.class, sqlSentence);
1957             if(projectSpecValues!=null && projectSpecValues.size()>0){
1958                 for (ProjectSpecValue projectSpecValue : projectSpecValues) {
1959                     Map<String,Object> specMap=new HashMap<>();
1960                     SpecificationValue specificationValue=commonService.selectOneByKeyBlob(SpecificationValueMapper.class,projectSpecValue.getSpecValue());
1961                     if(specificationValue!=null){
1962                         Specification specification=commonService.selectOneByKeyBlob(SpecificationMapper.class,specificationValue.getSpecificationId());
1963                         if(specification!=null){
1964                             specMap.put("specsTitle",specification.getSpecName());
1965                         }
1966                         specMap.put("specsValue",specificationValue.getSpecValueName());
1967                         specMap.put("specsId",specificationValue.getId());
1968                         specMap.put("unit","");
1969                     }
1970                     jsonObject.add(specMap);
1971                 }
1972             }else{
1973                 Map<String,Object> map=new HashMap<>();
1974                 map.put("specsTitle","规格");
1975                 map.put("specsValue",project==null?"":StringUtils.isEmpty(project.getSpecification())?"":project.getSpecification());
1976                 map.put("unit",project==null?"":StringUtils.isEmpty(project.getUnit())?"":project.getUnit());
1977                 map.put("bodyPartName",projectInfo==null?"":StringUtils.isEmpty(projectInfo.getBodyPartName())?"":projectInfo.getBodyPartName());
1978                 map.put("doctorQualification",projectInfo==null?"":StringUtils.isEmpty(projectInfo.getDoctorQualification())?"":projectInfo.getDoctorQualification());
1979                 jsonObject.add(map);
1980             }
1c40c5 1981             //荣爷说项目疗程次数为空值,默认为1
5fe472 1982             if(project.getIsExecutable()!=null && project.getIsExecutable()==BaseEntity.YES){
Z 1983                 Integer useredTotal=project==null?0:project.getTreatmentItemsNum()==null?1:project.getTreatmentItemsNum()==0?1:project.getTreatmentItemsNum();
1984                 orderItemSon.setUsedOne(useredTotal);//订单单次疗程数量
1985             }else if(project.getIsExecutable()!=null && project.getIsExecutable()==BaseEntity.NO){
1986                 Integer useredTotal=project==null?0:project.getTreatmentItemsNum()==null?0:project.getTreatmentItemsNum();
1987                 orderItemSon.setUsedOne(useredTotal);//订单单次疗程数量
1988             }else{
1989                 orderItemSon.setUsedOne(0);//订单单次疗程数量
1990             }
1991             //疗程总次数=订单单次疗程数量* 订单购买数量
1992             orderItemSon.setUsedTotal(orderItemSon.getUsedOne()*orderItemSon.getBuyNum());
154469 1993             orderItemSon.setSpecs(jsonObject.isEmpty()?"[]":jsonObject.toJSONString());
Z 1994             orderItemSon.setSpecsName(StringUtils.isEmpty(project.getSpecification())?"":project.getSpecification());
1995             if(projectInfo!=null){
1996                 if(StringUtils.noNull(projectInfo.getBodyPartId())){
1997                     orderItemSon.setBodyPartsId(projectInfo.getBodyPartId());
1998                 }
1999                 if(StringUtils.noNull(projectInfo.getBodyPartName())){
2000                     orderItemSon.setBodyPartsName(projectInfo.getBodyPartName());
2001                 }
2002                 if(StringUtils.noNull(projectInfo.getExecutiveQualificationCode())){
2003                     orderItemSon.setAptitudeNo(projectInfo.getExecutiveQualificationCode());
2004                 }
2005                 if(StringUtils.noNull(projectInfo.getExecutiveQualificationName())){
2006                     orderItemSon.setAptitudeName(projectInfo.getExecutiveQualificationName());
2007                 }
2008                 if(StringUtils.noNull(projectInfo.getDoctorQualification())){
2009                     orderItemSon.setDoctorQualification(projectInfo.getDoctorQualification());
2010                 }
2011             }
5be488 2012         }else if(OrderTotalConstants.TYPE_RETAIL.equals(cardItemInfo.getCommonType())){
154469 2013             JSONArray jsonArray = new JSONArray();
Z 2014             Consumables consumables=commonService.selectOneByKeyBlob(ConsumablesMapper.class,cardItemInfo.getCommonId());
2015             if(consumables!=null){
2016                 if(consumables.getBuyPrice()!=null){
2017                     originalPrice=consumables.getBuyPrice();
2018                 }
2019                 if(StringUtils.noNull(consumables.getSpec())){
2020                     Map<String,Object> specMap=new HashMap<>();
2021                     specMap.put("specsId","");
2022                     specMap.put("specsTitle","规格");
2023                     specMap.put("specsValue",consumables.getSpec());
2024                     specMap.put("unit","");
2025                     jsonArray.add(specMap);
2026                 }
2027                 if(StringUtils.noNull(consumables.getPackSpec())){
2028                     Map<String,Object> packSpecMap=new HashMap<>();
2029                     packSpecMap.put("specsId","");
2030                     packSpecMap.put("specsTitle","包装规格");
2031                     packSpecMap.put("specsValue",consumables.getPackSpec());
2032                     packSpecMap.put("unit","");
2033                     jsonArray.add(packSpecMap);
2034                 }
2035                 if(StringUtils.noNull(consumables.getzSpec())){
2036                     Map<String,Object> zSpecMap=new HashMap<>();
2037                     zSpecMap.put("specsId","");
2038                     zSpecMap.put("specsTitle","中药规格");
2039                     zSpecMap.put("specsValue",consumables.getzSpec());
2040                     zSpecMap.put("unit","");
2041                     jsonArray.add(zSpecMap);
2042                 }
2043                 if(StringUtils.noNull(consumables.getUnitIdCode())){
2044                     Map<String,Object> unitIdCodeSpecMap=new HashMap<>();
2045                     unitIdCodeSpecMap.put("specsId","");
2046                     unitIdCodeSpecMap.put("specsTitle","单位(字典编码)");
2047                     unitIdCodeSpecMap.put("specsValue",consumables.getUnitIdCode());
2048                     unitIdCodeSpecMap.put("unit","");
2049                     jsonArray.add(unitIdCodeSpecMap);
2050                 }
2051                 if(StringUtils.noNull(consumables.getUnitName())){
2052                     Map<String,Object> unitNameSpecMap=new HashMap<>();
2053                     unitNameSpecMap.put("specsId","");
2054                     unitNameSpecMap.put("specsTitle","单位名称");
2055                     unitNameSpecMap.put("specsValue",consumables.getUnitName());
2056                     unitNameSpecMap.put("unit","");
2057                     jsonArray.add(unitNameSpecMap);
2058                 }
2059
2060                 if(StringUtils.noNull(consumables.getzUnitName())){
2061                     Map<String,Object> zUnitNameSpecMap=new HashMap<>();
2062                     zUnitNameSpecMap.put("specsId","");
2063                     zUnitNameSpecMap.put("specsTitle","中药单位");
2064                     zUnitNameSpecMap.put("specsValue",consumables.getzUnitName());
2065                     zUnitNameSpecMap.put("unit","");
2066                     jsonArray.add(zUnitNameSpecMap);
2067                 }
2068                 if(StringUtils.noNull(consumables.getPeruseUnit())){
2069                     Map<String,Object> peruseUnitSpecMap=new HashMap<>();
2070                     peruseUnitSpecMap.put("specsId","");
2071                     peruseUnitSpecMap.put("specsTitle","每次用药单位");
2072                     peruseUnitSpecMap.put("specsValue",consumables.getPeruseUnit());
2073                     peruseUnitSpecMap.put("unit","");
2074                     jsonArray.add(peruseUnitSpecMap);
2075                 }
2076                 if(consumables.getPeruseCount()!=null){
2077                     Map<String,Object> peruseCountSpecMap=new HashMap<>();
2078                     peruseCountSpecMap.put("specsId","");
2079                     peruseCountSpecMap.put("specsTitle","每次用药数量");
2080                     peruseCountSpecMap.put("specsValue",consumables.getPeruseCount());
2081                     peruseCountSpecMap.put("unit","");
2082                     jsonArray.add(peruseCountSpecMap);
2083                 }
2084                 if(consumables.getUsageDays()!=null){
2085                     Map<String,Object> usageDaysSpecMap=new HashMap<>();
2086                     usageDaysSpecMap.put("specsId","");
2087                     usageDaysSpecMap.put("specsTitle","用药天数");
2088                     usageDaysSpecMap.put("specsValue",consumables.getUsageDays());
2089                     usageDaysSpecMap.put("unit","");
2090                     jsonArray.add(usageDaysSpecMap);
2091                 }
2092                 if(consumables.getTerm()!=null){
2093                     Map<String,Object> termSpecMap=new HashMap<>();
2094                     termSpecMap.put("specsId","");
2095                     termSpecMap.put("specsTitle","保质期,单位月");
2096                     termSpecMap.put("specsValue",consumables.getTerm());
2097                     termSpecMap.put("unit","");
2098                     jsonArray.add(termSpecMap);
2099                 }
2100                 orderItemSon.setSpecsName(StringUtils.isEmpty(consumables.getSpec())?"":consumables.getSpec());
1c40c5 2101                 //荣爷说商品没有疗程次数,默认为0
154469 2102                 orderItemSon.setUsedTotal(0);//商品没有疗程次数
Z 2103                 orderItemSon.setUsedOne(0);//商品没有疗程次数
5be488 2104             }else{
Z 2105                 orderItemSon.setUsedTotal(0);//暂定为0
2106                 orderItemSon.setUsedOne(0);//暂定为0
154469 2107             }
04c26a 2108         }
bcd046 2109
154469 2110
bcd046 2111         orderItemSon.setType(cardItemInfo.getCommonType());
Z 2112         orderItemSon.setGoodsNo(cardItemInfo.getCommonCode());
2113         orderItemSon.setGoodsId(cardItemInfo.getCommonId());
2114         orderItemSon.setGoodsName(cardItemInfo.getCommonName());
2115         orderItemSon.setGoodsImage("");//暂定为空
2116         //项目原价
2117         orderItemSon.setOriPrice(originalPrice);
647489 2118         //项目售价(卡项的分摊金额/卡包就是卡项分摊金额除以使用次数)
415eef 2119         orderItemSon.setProPrice(pro2Price);
C 2120         //售价
2121         orderItemSon.setCurPrice(curPrice);
bcd046 2122         //折扣=上级item填写的商品折扣
Z 2123         orderItemSon.setDiscount(vo.getDiscount());
647489 2124         //订单售价=项目售价*订单折扣
415eef 2125         if(orderItemSon.getDiscount().compareTo(BigDecimal.ZERO)> 0){
C 2126             orderItemSon.setCurPrice(orderItemSon.getCurPrice().multiply(orderItemSon.getDiscount().divide(new BigDecimal(100),10,RoundingMode.HALF_UP)).setScale(2,RoundingMode.HALF_UP));
2127         }else{
2128             orderItemSon.setCurPrice(BigDecimal.ZERO);
2129         }
2130         //总金额
2131         BigDecimal total = BigDecimal.ZERO;
2132
2133         if(!OrderItemConstants.CARD_BAG.equals(vo.getType())){
2134             total = orderItemSon.getCurPrice().multiply(new BigDecimal(orderItemSon.getBuyNum()));
2135         }
1c40c5 2136
bcd046 2137         //优惠前小计=订单售价*数量
415eef 2138         orderItemSon.setTotal(total);
bcd046 2139         //优惠券抵扣金额
Z 2140         orderItemSon.setDiscountPrice(BigDecimal.ZERO);
2141         //优惠后小计=优惠前小计-优惠券抵扣金额
2142         orderItemSon.setActualTotal(orderItemSon.getTotal().subtract(orderItemSon.getDiscountPrice()));
154469 2143
bcd046 2144         orderItemSon.setOrderId(ordersId);
Z 2145         orderItemSon.setOrderItemId(vo.getId());
2146         orderItemSon.setCardItemInfoId(cardItemInfo.getId());
2147         orderItemSon.setCardEquityId(cardItemInfo.getCardEquityId());
2148         orderItemSon.setIsCardBag(type==0?0:1);
2149         commonService.insert(OrderItemSonMapper.class,orderItemSon);
2150     }
2151
2152     /**
2153      * 开单-创建二级子订单
2154      * @param ordersTotal                       总订单
2155      * @param vo                                一级子订单
2156      * @param promotionInfoList                 促销详情集合
2157      */
141ad3 2158     public static void createOrderItemProm(OrdersTotal ordersTotal, OrderItem vo, List<PromotionInfo> promotionInfoList, CommonService commonService) {
C 2159
2160         //升序排序,因为有按照比例算积分
2161         promotionInfoList = promotionInfoList.stream().sorted(Comparator.comparing(PromotionInfo::getDiscountAfterPrice))
2162                 .collect(Collectors.toList());
2163
bcd046 2164         OrderItemSon orderItemSon;
ee9602 2165         SqlSentence sqlSentence;
Z 2166         JSONArray jsonArray;
e065e7 2167         //已付积分
C 2168         BigDecimal integralTotal = vo.getShouldTotalPoints();
2169         //比例
2170         BigDecimal proportion;
2171         PromotionInfo promotionInfo;
2172         for(int i= 0;i<promotionInfoList.size();i++){
2173             promotionInfo = promotionInfoList.get(i);
ee9602 2174             jsonArray=new JSONArray();
42f9be 2175             orderItemSon=new OrderItemSon();
bcd046 2176             Integer usedTotal=0;
Z 2177             Integer usedOne=0;
baf2c0 2178             //数量
097e4f 2179             orderItemSon.setBuyNum(promotionInfo.getNum()*vo.getBuyNum());
bcd046 2180             if(PROJECT.equals(GroupTypeEnum.getCode(promotionInfo.getType()))){
Z 2181                 Project project=commonService.selectOneByKeyBlob(ProjectMapper.class,promotionInfo.getCommonId());
2182                 if(project!=null){
42f9be 2183                     sqlSentence=new SqlSentence();
Z 2184                     Map<String,Object> objectMap=new HashMap<>();
2185                     sqlSentence.setM(objectMap);
2186                     objectMap.put("commonId",vo.getCommonId());
2187                     objectMap.put("isDel",BaseEntity.NO);
2188                     sqlSentence.setSqlSentence("select * from project_info where projectId=#{m.commonId} and isDel=#{m.isDel} ");
2189                     ProjectInfo projectInfo=commonService.selectOne(ProjectInfoMapper.class,sqlSentence);
baf2c0 2190                     Integer useredTotal=0;
5fe472 2191                     //荣爷说项目疗程次数为空值,默认为1
Z 2192                     if(project.getIsExecutable()!=null && project.getIsExecutable()==BaseEntity.YES){
baf2c0 2193                         useredTotal=project==null?0:project.getTreatmentItemsNum()==null?1:project.getTreatmentItemsNum()==0?1:project.getTreatmentItemsNum();
5fe472 2194                     }else if(project.getIsExecutable()!=null && project.getIsExecutable()==BaseEntity.NO){
baf2c0 2195                         useredTotal=project==null?0:project.getTreatmentItemsNum()==null?0:project.getTreatmentItemsNum();
5fe472 2196                     }
baf2c0 2197                     orderItemSon.setUsedOne(useredTotal);//订单单次疗程数量
Z 2198                     logger.info("useredTotal:{},orderUsedOne:{},bueNum:{}",useredTotal,orderItemSon.getUsedOne(),orderItemSon.getBuyNum());
f23843 2199                     //疗程总次数=订单单次疗程数量* 订单购买数量*促销项目数量
097e4f 2200                     orderItemSon.setUsedTotal(orderItemSon.getUsedOne()*orderItemSon.getBuyNum());
ee9602 2201
91de2f 2202                     sqlSentence.setSqlSentence("select * from project_spec_value where projectId = #{m.commonId} and isDel=#{m.isDel} ");
04c26a 2203                     List<ProjectSpecValue> projectSpecValues = commonService.selectList(ProjectSpecValueMapper.class, sqlSentence);
Z 2204                     if(projectSpecValues!=null && projectSpecValues.size()>0){
2205                         for (ProjectSpecValue projectSpecValue : projectSpecValues) {
a64c8b 2206                             Map<String,Object> specMap=new HashMap<>();
04c26a 2207                             SpecificationValue specificationValue=commonService.selectOneByKeyBlob(SpecificationValueMapper.class,projectSpecValue.getSpecValue());
Z 2208                             if(specificationValue!=null){
2209                                 Specification specification=commonService.selectOneByKeyBlob(SpecificationMapper.class,specificationValue.getSpecificationId());
2210                                 if(specification!=null){
2211                                     specMap.put("specsTitle",specification.getSpecName());
2212                                 }
2213                                 specMap.put("specsValue",specificationValue.getSpecValueName());
2214                                 specMap.put("specsId",specificationValue.getId());
2215                                 specMap.put("unit","");
2216                             }
2217                             jsonArray.add(specMap);
2218                         }
2219                     }else{
a64c8b 2220                         Map<String,Object> map=new HashMap<>();
04c26a 2221                         map.put("specsTitle","规格");
Z 2222                         map.put("specsValue",project==null?"":StringUtils.isEmpty(project.getSpecification())?"":project.getSpecification());
2223                         map.put("unit",project==null?"":StringUtils.isEmpty(project.getUnit())?"":project.getUnit());
2224                         map.put("bodyPartName",projectInfo==null?"":StringUtils.isEmpty(projectInfo.getBodyPartName())?"":projectInfo.getBodyPartName());
2225                         map.put("doctorQualification",projectInfo==null?"":StringUtils.isEmpty(projectInfo.getDoctorQualification())?"":projectInfo.getDoctorQualification());
2226                         jsonArray.add(map);
2227                     }
42f9be 2228
154469 2229                     orderItemSon.setSpecsName(StringUtils.isEmpty(project.getSpecification())?"":project.getSpecification());
42f9be 2230                     if(projectInfo!=null){
Z 2231                         if(StringUtils.noNull(projectInfo.getBodyPartId())){
154469 2232                             orderItemSon.setBodyPartsId(projectInfo.getBodyPartId());
42f9be 2233                         }
Z 2234                         if(StringUtils.noNull(projectInfo.getBodyPartName())){
154469 2235                             orderItemSon.setBodyPartsName(projectInfo.getBodyPartName());
42f9be 2236                         }
Z 2237                         if(StringUtils.noNull(projectInfo.getExecutiveQualificationCode())){
154469 2238                             orderItemSon.setAptitudeNo(projectInfo.getExecutiveQualificationCode());
42f9be 2239                         }
Z 2240                         if(StringUtils.noNull(projectInfo.getExecutiveQualificationName())){
154469 2241                             orderItemSon.setAptitudeName(projectInfo.getExecutiveQualificationName());
42f9be 2242                         }
Z 2243                         if(StringUtils.noNull(projectInfo.getDoctorQualification())){
154469 2244                             orderItemSon.setDoctorQualification(projectInfo.getDoctorQualification());
42f9be 2245                         }
Z 2246                     }
04c26a 2247                 }
Z 2248             }else if(GroupTypeEnum.RETAIL.equals(GroupTypeEnum.getCode(promotionInfo.getType()))){
2249                 Consumables consumables=commonService.selectOneByKeyBlob(ConsumablesMapper.class,promotionInfo.getCommonId());
2250                 if(consumables!=null){
42f9be 2251                     if(StringUtils.noNull(consumables.getSpec())){
a64c8b 2252                         Map<String,Object> specMap=new HashMap<>();
42f9be 2253                         specMap.put("specsId","");
Z 2254                         specMap.put("specsTitle","规格");
2255                         specMap.put("specsValue",consumables.getSpec());
2256                         specMap.put("unit","");
a64c8b 2257                         jsonArray.add(specMap);
04c26a 2258                     }
42f9be 2259                     if(StringUtils.noNull(consumables.getPackSpec())){
a64c8b 2260                         Map<String,Object> packSpecMap=new HashMap<>();
Z 2261                         packSpecMap.put("specsId","");
2262                         packSpecMap.put("specsTitle","包装规格");
2263                         packSpecMap.put("specsValue",consumables.getPackSpec());
2264                         packSpecMap.put("unit","");
2265                         jsonArray.add(packSpecMap);
42f9be 2266                     }
Z 2267                     if(StringUtils.noNull(consumables.getzSpec())){
a64c8b 2268                         Map<String,Object> zSpecMap=new HashMap<>();
Z 2269                         zSpecMap.put("specsId","");
2270                         zSpecMap.put("specsTitle","中药规格");
2271                         zSpecMap.put("specsValue",consumables.getzSpec());
2272                         zSpecMap.put("unit","");
2273                         jsonArray.add(zSpecMap);
42f9be 2274                     }
Z 2275                     if(StringUtils.noNull(consumables.getUnitIdCode())){
a64c8b 2276                         Map<String,Object> unitIdCodeSpecMap=new HashMap<>();
Z 2277                         unitIdCodeSpecMap.put("specsId","");
2278                         unitIdCodeSpecMap.put("specsTitle","单位(字典编码)");
2279                         unitIdCodeSpecMap.put("specsValue",consumables.getUnitIdCode());
2280                         unitIdCodeSpecMap.put("unit","");
2281                         jsonArray.add(unitIdCodeSpecMap);
42f9be 2282                     }
Z 2283                     if(StringUtils.noNull(consumables.getUnitName())){
a64c8b 2284                         Map<String,Object> unitNameSpecMap=new HashMap<>();
Z 2285                         unitNameSpecMap.put("specsId","");
2286                         unitNameSpecMap.put("specsTitle","单位名称");
2287                         unitNameSpecMap.put("specsValue",consumables.getUnitName());
2288                         unitNameSpecMap.put("unit","");
2289                         jsonArray.add(unitNameSpecMap);
42f9be 2290                     }
a64c8b 2291
42f9be 2292                     if(StringUtils.noNull(consumables.getzUnitName())){
a64c8b 2293                         Map<String,Object> zUnitNameSpecMap=new HashMap<>();
Z 2294                         zUnitNameSpecMap.put("specsId","");
2295                         zUnitNameSpecMap.put("specsTitle","中药单位");
2296                         zUnitNameSpecMap.put("specsValue",consumables.getzUnitName());
2297                         zUnitNameSpecMap.put("unit","");
2298                         jsonArray.add(zUnitNameSpecMap);
42f9be 2299                     }
Z 2300                     if(StringUtils.noNull(consumables.getPeruseUnit())){
a64c8b 2301                         Map<String,Object> peruseUnitSpecMap=new HashMap<>();
Z 2302                         peruseUnitSpecMap.put("specsId","");
2303                         peruseUnitSpecMap.put("specsTitle","每次用药单位");
2304                         peruseUnitSpecMap.put("specsValue",consumables.getPeruseUnit());
2305                         peruseUnitSpecMap.put("unit","");
2306                         jsonArray.add(peruseUnitSpecMap);
42f9be 2307                     }
Z 2308                     if(consumables.getPeruseCount()!=null){
a64c8b 2309                         Map<String,Object> peruseCountSpecMap=new HashMap<>();
Z 2310                         peruseCountSpecMap.put("specsId","");
2311                         peruseCountSpecMap.put("specsTitle","每次用药数量");
2312                         peruseCountSpecMap.put("specsValue",consumables.getPeruseCount());
2313                         peruseCountSpecMap.put("unit","");
2314                         jsonArray.add(peruseCountSpecMap);
42f9be 2315                     }
Z 2316                     if(consumables.getUsageDays()!=null){
a64c8b 2317                         Map<String,Object> usageDaysSpecMap=new HashMap<>();
Z 2318                         usageDaysSpecMap.put("specsId","");
2319                         usageDaysSpecMap.put("specsTitle","用药天数");
2320                         usageDaysSpecMap.put("specsValue",consumables.getUsageDays());
2321                         usageDaysSpecMap.put("unit","");
2322                         jsonArray.add(usageDaysSpecMap);
42f9be 2323                     }
Z 2324                     if(consumables.getTerm()!=null){
a64c8b 2325                         Map<String,Object> termSpecMap=new HashMap<>();
Z 2326                         termSpecMap.put("specsId","");
2327                         termSpecMap.put("specsTitle","保质期,单位月");
2328                         termSpecMap.put("specsValue",consumables.getTerm());
2329                         termSpecMap.put("unit","");
2330                         jsonArray.add(termSpecMap);
42f9be 2331                     }
Z 2332                     orderItemSon.setSpecsName(StringUtils.isEmpty(consumables.getSpec())?"":consumables.getSpec());
f23843 2333                     orderItemSon.setUsedTotal(usedTotal);//暂定为0
Z 2334                     orderItemSon.setUsedOne(usedOne);//暂定为0
bcd046 2335                 }
5be488 2336             }else{
Z 2337                 orderItemSon.setUsedTotal(usedTotal);//暂定为0
2338                 orderItemSon.setUsedOne(usedOne);//暂定为0
bcd046 2339             }
Z 2340
5be488 2341
bcd046 2342             orderItemSon.setType(promotionInfo.getType());
Z 2343             orderItemSon.setGoodsNo(promotionInfo.getCommonCode());
2344             orderItemSon.setGoodsId(promotionInfo.getCommonId());
2345             orderItemSon.setGoodsName(promotionInfo.getCommonName());
2346             orderItemSon.setGoodsImage("");//暂定为空
04c26a 2347             orderItemSon.setSpecs(jsonArray.isEmpty()?"[]":jsonArray.toJSONString());
bcd046 2348             //项目单价
Z 2349             orderItemSon.setOriPrice(promotionInfo.getPrice());
2350             //促销单价
2351             orderItemSon.setProPrice(promotionInfo.getDiscountPrice());
2352             //上级item填写的商品折扣
2353             orderItemSon.setDiscount(vo.getDiscount());
2354             //订单售价=促销单价*订单折扣
91de2f 2355             orderItemSon.setCurPrice(orderItemSon.getProPrice().multiply(orderItemSon.getDiscount().divide(new BigDecimal(100),15,RoundingMode.HALF_UP)).setScale(2,RoundingMode.HALF_UP));
baf2c0 2356
91de2f 2357             BigDecimal total = promotionInfo.getDiscountAfterPrice().multiply(new BigDecimal(vo.getBuyNum())).setScale(2,RoundingMode.HALF_UP);
bcd046 2358             //优惠前小计=订单售价*数量
91de2f 2359             orderItemSon.setTotal(total.multiply(orderItemSon.getDiscount().divide(new BigDecimal(100),15,RoundingMode.HALF_UP)).setScale(2,RoundingMode.HALF_UP));
C 2360             //折扣金额
2361             orderItemSon.setDiscountOrderPrice(total.subtract(orderItemSon.getTotal()));
bcd046 2362             //优惠券抵扣金额
Z 2363             orderItemSon.setDiscountPrice(BigDecimal.ZERO);
2364             //优惠后小计=优惠前小计-优惠券抵扣金额
c5e876 2365             orderItemSon.setActualTotal(orderItemSon.getTotal().subtract(orderItemSon.getDiscountPrice()).setScale(2, RoundingMode.HALF_UP));
bcd046 2366
e065e7 2367
C 2368             //可算积分
2369             if(integralTotal.compareTo(BigDecimal.ZERO) > 0){
2370
2371                 if(i == promotionInfoList.size()-1){
2372                     orderItemSon.setTotalPoints(integralTotal);
2373                 }else{
2374                     //计算金额比例
141ad3 2375                     if(vo.getTotal().compareTo(BigDecimal.ZERO) > 0){
C 2376                         proportion = orderItemSon.getTotal().divide(vo.getTotal(),15,RoundingMode.HALF_UP);
e065e7 2377                     }else{
C 2378                         proportion = BigDecimal.ZERO;
2379                     }
2380                     //根据比例算出积分
2381                     orderItemSon.setTotalPoints(vo.getShouldTotalPoints().multiply(proportion).setScale(0,RoundingMode.HALF_UP));
2382                     if(orderItemSon.getTotalPoints().compareTo(integralTotal) > 0){
2383                         orderItemSon.setTotalPoints(integralTotal);
2384                     }
2385                 }
2386
2387                 orderItemSon.setShouldTotalPoints(orderItemSon.getTotalPoints());
2388                 if(orderItemSon.getBuyNum() > 0){
bd9b3c 2389                     //这里保留两位小数,可能有除不尽的问题
e065e7 2390                     orderItemSon.setOriginalIntegral(orderItemSon.getTotalPoints().divide(BigDecimal.valueOf(orderItemSon.getBuyNum()),2,RoundingMode.HALF_UP));
C 2391                 }
2392                 orderItemSon.setProIntegral(orderItemSon.getOriginalIntegral());
2393                 orderItemSon.setSellPoints(orderItemSon.getOriginalIntegral());
2394
2395                 orderItemSon.setOriIntegralTotal(orderItemSon.getTotalPoints());
2396                 orderItemSon.setProIntegralTotal(orderItemSon.getTotalPoints());
2397
2398                 //减去已经分配的
2399                 integralTotal = integralTotal.subtract(orderItemSon.getTotalPoints());
2400             }
2401
bcd046 2402             orderItemSon.setOrderId(ordersTotal.getId());
Z 2403             orderItemSon.setOrderItemId(vo.getId());
2404             commonService.insert(OrderItemSonMapper.class,orderItemSon);
2405         }
2406     }
2407
2408     /**
8c307c 2409      * 处理整单折扣或者员工折扣
Z 2410      *
2411      * @param ordersTotal
2412      * @param commonService
2413      * @param operator
2414      * @param sqlSentence
2415      * @param appIdMap
2416      * @param user
2417      */
2418     public static void handAllAndEmployee(OrdersTotal ordersTotal, CommonService commonService, Map<String, String> operator,
2419                                           SqlSentence sqlSentence, Map<String, Object> appIdMap, User user, CustomParameter customParameter) {
2420         if (PlatformPattern.TEST.equals(customParameter.getPlatformPattern()) && StringUtils.isEmpty(ordersTotal.getKoapOrderId()) && ordersTotal.getOrderInfo() != null) {
2421             if (ordersTotal.getOrderInfo().getIsEmployeeDiscount() == BaseEntity.YES && ordersTotal.getOrderInfo().getIsWholeDiscount() == BaseEntity.YES) {
2422                 appIdMap.put("tel", user.getTel());
2423                 appIdMap.put("isDel", BaseEntity.NO);
2424                 appIdMap.put("isJob", BaseEntity.YES);
2425                 sqlSentence.setSqlSentence(" tel=#{m.tel} and isDel=#{m.isDel} and isJob=#{m.isJob}");
2426                 int count = commonService.selectCount(EmployeeMapper.class, sqlSentence);
d63b45 2427 //                logger.info("员工折扣:sql:{},m:{},结果(count):{}", sqlSentence.getSqlSentence(), sqlSentence.getM(), count);
8c307c 2428                 if (count <= 0) {
Z 2429                     throw new PlatTipsException(PlatformCode.ERROR_TIPS, "创建订单:该用户不是员工,不能享受员工折扣!");
2430                 } else {
2431                     JbpmParamUtil.wholeEmployeeApproval(ordersTotal, operator, sqlSentence, appIdMap, ordersTotal.getDiscountTotal(), commonService, customParameter, 1);
2432                 }
2433             }
2434             if (ordersTotal.getOrderInfo().getIsWholeDiscount() == BaseEntity.YES) {
2435                 JbpmParamUtil.wholeEmployeeApproval(ordersTotal, operator, sqlSentence, appIdMap, ordersTotal.getDiscountTotal(), commonService, customParameter, 0);
f9fd77 2436             }
Z 2437         }
2438     }
2439
2440     /**
2441      * 开单-创建领建订单                        实现类
8c307c 2442      *
Z 2443      * @param ordersTotal     总订单标识
f9fd77 2444      * @param customParameter
Z 2445      * @param commonService
2446      * @return
2447      */
4e171e 2448     public static String syncOrder(OrdersTotal ordersTotal, Map<String, String> operator,String orderNo, CustomParameter customParameter, CommonService commonService) {
8c307c 2449         String hisOrderId = null;
Z 2450         if (ordersTotal != null) {
2451             if (OrderTotalConstants.TYPE_RECHARGE.equals(ordersTotal.getType())) {
f9fd77 2452                 //创建领建储值金订单
4e171e 2453                 hisOrderId = SynOrderUtil.synPrepaidOrder(operator, ordersTotal,orderNo, customParameter, commonService);
8c307c 2454             } else {
f9fd77 2455                 //创建领建销售订单
4e171e 2456                 hisOrderId = SynOrderUtil.synSalesOrder(operator, ordersTotal,orderNo, customParameter, commonService);
f9fd77 2457             }
8c307c 2458         } else {
Z 2459             throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL, "同步订单操作未找到订单信息");
f9fd77 2460         }
Z 2461         return hisOrderId;
2462     }
2463
2464     /**
2465      * 开单-构建订单商品规格
8c307c 2466      *
Z 2467      * @param ordersTotal     总订单实体
2468      * @param orderGoodsSpecs 订单商品规格标识
2469      * @param vo              一级子订单
f9fd77 2470      * @param object
Z 2471      */
2472     public static void setOrderGoodsSpecs(OrdersTotal ordersTotal, OrderGoodsSpecs orderGoodsSpecs, OrderItem vo, JSONObject object) {
2473         orderGoodsSpecs.setType(vo.getType());
2474         orderGoodsSpecs.setGoodsId(vo.getCommonId());
2475         orderGoodsSpecs.setGoodsNo(vo.getGoodsNo());
2476         orderGoodsSpecs.setOrderId(ordersTotal.getId());
2477         orderGoodsSpecs.setOrderItemId(vo.getId());
2478         orderGoodsSpecs.setSpecsTitle("规格");
a64c8b 2479         orderGoodsSpecs.setSpecsValue(StringUtils.isEmpty(object.getString("specsValue"))?"":object.getString("specsValue"));
f9fd77 2480         orderGoodsSpecs.setSpecsId(vo.getCommonId());
Z 2481         orderGoodsSpecs.setUnit(object.getString("unit"));
2482     }
2483
2484     /**
8c307c 2485      * 开单-限制项目
Z 2486      * @param user                          用户信息
2487      * @param shop                          操作人门店信息
f9fd77 2488      * @param commonService
Z 2489      * @param sqlSentence
8c307c 2490      * @param objectMap
Z 2491      * @param vo                            一级子订单
f9fd77 2492      */
cd904a 2493     public static void limitProject(User user, Shop shop, CommonService commonService, SqlSentence sqlSentence, Map<String, Object> objectMap, OrderItem vo) {
8c307c 2494         objectMap.clear();
Z 2495         objectMap.put("commonId", vo.getCommonId());
4698e2 2496         sqlSentence.setSqlSentence("SELECT p.* FROM project p WHERE p.id=#{m.commonId} and p.isDel =0 and p.isUp =1");
8c307c 2497         Project project = commonService.selectOne(ProjectMapper.class, sqlSentence);
Z 2498         if (project == null) {
2499             throw new PlatTipsException(PlatformCode.ERROR_TIPS, "找不到项目:" + vo.getCommonId());
f9fd77 2500         }
8c307c 2501         if (project.getPrice() == null) {
Z 2502             throw new PlatTipsException(PlatformCode.ERROR_TIPS, "项目:" + project.getName() + ",没有销售价格,请填写销售单价再进行创建订单");
2503         }
2504         objectMap.clear();
2505         objectMap.put("isDel", BaseEntity.NO);
2506         //限制会员等级购买
2507         objectMap.put("foreignKey", project.getId());
2508         OrderAddUtil.checkMemberLevel(sqlSentence, user, OrderItemConstants.TYPE_CARD, commonService);
2509
2510         JSONObject param = new JSONObject();
2511         param.put("commonId", project.getId());
ea9cfd 2512         param.put("commonName",project.getName());
8c307c 2513         param.put("userId", user.getId());
Z 2514         param.put("shopId", shop.getId());
2515         //限制项目
2516         LimitPlatformUtil.limitMethod(commonService, param.toJSONString());
f9fd77 2517     }
Z 2518
2519     /**
8c307c 2520      * 限制商品
Z 2521      * @param user                              用户信息
2522      * @param shop                              操作人门店信息
f9fd77 2523      * @param commonService
Z 2524      * @param sqlSentence
8c307c 2525      * @param objectMap
Z 2526      * @param vo                                一级子订单
f9fd77 2527      */
8c307c 2528     public static void limitConsumables(User user, Shop shop, CommonService commonService, SqlSentence sqlSentence, Map<String, Object> objectMap, OrderItem vo) {
Z 2529         Consumables consumables = commonService.selectOneByKey(ConsumablesMapper.class, vo.getCommonId());
2530         if (consumables == null) {
2531             throw new PlatTipsException(PlatformCode.ERROR_TIPS, "找不到项目:" + vo.getCommonId());
f9fd77 2532         }
8c307c 2533         if (consumables.getPrice() == null) {
Z 2534             throw new PlatTipsException(PlatformCode.ERROR_TIPS, "商品:" + consumables.getName() + ",没有销售价格,请填写销售单价在进行创建订单");
2535         }
2536         objectMap.clear();
2537         objectMap.put("isDel", BaseEntity.NO);
2538         //限制会员等级购买
2539         objectMap.put("foreignKey", consumables.getId());
2540         OrderAddUtil.checkMemberLevel(sqlSentence, user, OrderItemConstants.TYPE_CARD, commonService);
2541
2542         JSONObject param = new JSONObject();
2543         param.put("commonId", consumables.getId());
ea9cfd 2544         param.put("commonName",consumables.getName());
8c307c 2545         param.put("userId", user.getId());
Z 2546         param.put("shopId", shop.getId());
2547         //限制商品
2548         LimitPlatformUtil.limitMethod(commonService, param.toJSONString());
f9fd77 2549     }
9f6958 2550
Z 2551     /**
8c307c 2552      * 限制卡项
Z 2553      * @param user                           用户信息
2554      * @param shop                          操作人门店信息
9f6958 2555      * @param commonService
8c307c 2556      * @param sqlSentence
Z 2557      * @param objectMap
2558      * @param vo                            一级子订单
9f6958 2559      */
8c307c 2560     public static void limitCard(User user, Shop shop, CommonService commonService, SqlSentence sqlSentence, Map<String, Object> objectMap, OrderItem vo) {
Z 2561         //处理卡项
2562         objectMap.clear();
2563         objectMap.put("status", CardItem.SALES);
2564         objectMap.put("isUp", BaseEntity.YES);
2565         objectMap.put("isDel", BaseEntity.NO);
2566         objectMap.put("commonId", vo.getCommonId());
2567         sqlSentence.setSqlSentence("select * from card_item where  id=#{m.commonId} and  status=#{m.status} and isUp=#{m.isUp} and isDel=#{m.isDel}");
2568         CardItem cardItem = commonService.selectOne(CardItemMapper.class, sqlSentence);
2569         if (cardItem == null) {
2570             throw new PlatTipsException(PlatformCode.ERROR_TIPS, "找不到该卡项,请确保卡项状态必须是销售中并且是上架状态,卡项标识:" + vo.getCommonId());
9f6958 2571         }
8c307c 2572         if (cardItem.getTotal() == null) {
Z 2573             throw new PlatTipsException(PlatformCode.ERROR_TIPS, "卡项:" + cardItem.getName() + ",没有销售价格,请填写销售单价在进行创建订单");
2574         }
2575         objectMap.clear();
2576         objectMap.put("isDel", BaseEntity.NO);
2577         //限制会员等级购买
2578         objectMap.put("foreignKey", cardItem.getId());
2579         OrderAddUtil.checkMemberLevel(sqlSentence, user, OrderItemConstants.TYPE_CARD, commonService);
9f6958 2580
8c307c 2581         JSONObject param = new JSONObject();
Z 2582         param.put("commonId", cardItem.getId());
ea9cfd 2583         param.put("commonName",cardItem.getName());
8c307c 2584         param.put("userId", user.getId());
Z 2585         param.put("shopId", shop.getId());
2586         //限制卡项
2587         LimitPlatformUtil.limitMethod(commonService, param.toJSONString());
9f6958 2588     }
Z 2589
2590     /**
8c307c 2591      * 限制促销
Z 2592      * @param user                          用户信息
2593      * @param shop                          操作人门店信息
9f6958 2594      * @param commonService
8c307c 2595      * @param sqlSentence
Z 2596      * @param objectMap
2597      * @param vo                            一级子订单
9f6958 2598      */
8c307c 2599     public static void limitPromotion(User user, Shop shop, CommonService commonService, SqlSentence sqlSentence, Map<String, Object> objectMap, OrderItem vo) {
Z 2600         objectMap.clear();
2601         objectMap.put("status", Promotion.IN_PROGRESS);
2602         objectMap.put("isUp", BaseEntity.YES);
2603         objectMap.put("isDel", BaseEntity.NO);
2604         objectMap.put("commonId", vo.getCommonId());
2605         sqlSentence.setSqlSentence("select * from promotion where  id=#{m.commonId} and  status=#{m.status} and isUp=#{m.isUp} and isDel=#{m.isDel}");
2606         Promotion promotion = commonService.selectOne(PromotionMapper.class, sqlSentence);
2607         if (promotion == null) {
2608             throw new PlatTipsException(PlatformCode.ERROR_TIPS, "找不到该促销,请确保促销状态必须是销售中并且是上架状态,促销标识:" + vo.getCommonId());
9f6958 2609         }
8c307c 2610         if (promotion.getTotal() == null) {
Z 2611             throw new PlatTipsException(PlatformCode.ERROR_TIPS, "促销:" + promotion.getName() + ",没有销售价格,请填写销售单价在进行创建订单");
2612         }
2613         objectMap.clear();
2614         objectMap.put("isDel", BaseEntity.NO);
2615         objectMap.put("foreignKey", promotion.getId());
2616         //限制会员等级购买
2617         OrderAddUtil.checkMemberLevel(sqlSentence, user, OrderItemConstants.TYPE_CARD, commonService);
2618
2619         JSONObject param = new JSONObject();
2620         param.put("commonId", promotion.getId());
ea9cfd 2621         param.put("commonName",promotion.getName());
8c307c 2622         param.put("userId", user.getId());
Z 2623         param.put("shopId", shop.getId());
2624         //限制促销
2625         LimitPlatformUtil.limitMethod(commonService, param.toJSONString());
9f6958 2626     }
44d306 2627
Z 2628     /**
2629      * 保存订单的活动规则
2630      * @param activityIds
2631      */
2632     public static void insertActivity(List<String> activityIds,OrdersTotal ordersTotal,CommonService commonService) {
2633         OrdersActivityInfo ordersActivityInfo;
2634         OrdersActivityInfoItem ordersActivityInfoItem;
2635         SqlSentence sqlSentence;
2636         Map<String,Object> map;
2637         for (String activityId : activityIds) {
2638
2639             //查询活动规则基础信息
2640             ActivityRule activityRule=commonService.selectOneByKey(ActivityRuleMapper.class,activityId);
2641             if(activityRule!=null){
2642                 //保存订单活动规则表
2643                 ordersActivityInfo=new OrdersActivityInfo();
2644                 ordersActivityInfo.setOrderId(ordersTotal.getId());
2645                 ordersActivityInfo.setActivityId(activityRule.getId());
2646                 ordersActivityInfo.setActivityName(activityRule.getName());
2647                 commonService.insert(OrdersActivityInfoMapper.class,ordersActivityInfo);
2648
2649                 //保存订单活动规则子表
2650                 sqlSentence = new SqlSentence();
2651                 map=new HashMap<>();
2652                 sqlSentence.setM(map);
2653                 map.put("isDel",BaseEntity.NO);
2654                 map.put("activityRuleId",activityRule.getId());
143537 2655                 sqlSentence.setSqlSentence("select * from activity_action where activityRuleId=#{m.activityRuleId} and isDel=#{m.isDel}");
44d306 2656                 //根据活动组来查询活动的条件集合,同时满足才能进行满减金额或者满减折扣
Z 2657                 List<ActivityAction> activityActions = commonService.selectList(ActivityActionMapper.class, sqlSentence);
2658                 if(activityActions!=null && activityActions.size()>0){
2659                     for (ActivityAction activityAction : activityActions) {
2660                         ordersActivityInfoItem=new OrdersActivityInfoItem();
2661                         ordersActivityInfoItem.setOrderId(ordersTotal.getId());
4df37f 2662                         ordersActivityInfoItem.setActivityInfoId(ordersActivityInfo.getId());
44d306 2663                         ordersActivityInfoItem.setActivityRuleId(activityRule.getId());
Z 2664                         ordersActivityInfoItem.setType(activityAction.getType());
2665                         ordersActivityInfoItem.setWorth(activityAction.getWorth());
2666                         ordersActivityInfoItem.setWorthName(activityAction.getWorthName());
2667                         ordersActivityInfoItem.setActivityGroupId(activityAction.getActivityGroupId());
2668                         ordersActivityInfoItem.setActivityRuleId(activityAction.getActivityRuleId());
f5e288 2669                         ordersActivityInfoItem.setActivityActionId(activityAction.getId());
44d306 2670                         ordersActivityInfoItem.setCrmCouponId(activityAction.getCrmCouponId());
Z 2671                         ordersActivityInfoItem.setCrmCouponName(activityAction.getCrmCouponName());
2672                         ordersActivityInfoItem.setPlatformType(activityAction.getPlatformType());
160c33 2673                         ordersActivityInfoItem.setPerformStatus(OrdersActivityInfoItem.STATUS_PERFORM_NONE);
C 2674
2675                         if(ActivityAction.TYPE_FULL_REDUCE.equals(ordersActivityInfoItem.getType())
2676                                 || ActivityAction.TYPE_DISCOUNT.equals(ordersActivityInfoItem.getType())){
2677                             if(ordersTotal.getActivityTotal() != null && ordersTotal.getActivityTotal().compareTo(BigDecimal.ZERO) > 0){
2678                                 ordersActivityInfoItem.setPerformStatus(OrdersActivityInfoItem.STATUS_PERFORM_SEND_OUT);
2679                                 ordersActivityInfoItem.setHandleStatus(OrdersActivityInfoItem.HANDLE_STATUS_SUCCESS);
2680                             }
44d306 2681                         }
160c33 2682
44d306 2683                         commonService.insert(OrdersActivityInfoItemMapper.class,ordersActivityInfoItem);
Z 2684                     }
2685                 }
2686             }
2687         }
2688     }
2689
2690     /**
2691      * 结账-订单异常发送企业通知和保存重发记录
2692      * @param ordersTotal                   总订单
2693      * @param e                             异常
2694      * @param object                        请求参数
2695      * @param interfaceName                 接口名称
2696      * @param remarks                       备注
2697      * @param commonService
2698      * @param customParameter
2699      */
fd269d 2700     public static void orderSendNoticeResendRecord(OrdersTotal ordersTotal, Exception e, JSONObject object,String interfaceName,String remarks,Integer synHisOrderType,
44d306 2701                                                    CommonService commonService,CustomParameter customParameter) {
Z 2702         //构建重发实体对象
2703         ResendRecord resendRecord = ResendUtil.saveOrResend(JSONObject.toJSONString(object), "", e.getMessage(),
2704                 interfaceName, StringUtils.isEmpty(ordersTotal.getKoapOrderId())?ResendRecord.TYPE_SOURCE_ORDER_SYN_PHISKIN:ResendRecord.TYPE_SOURCE_ORDER_SYN_KOAP,
2705                 ResendRecord.TYPE_SYN,remarks ,ordersTotal.getId(),ResendRecord.TYPE_COMMONTYPE_ORDER,
2706                 null,ordersTotal.getAppIdCode(),commonService);
2707         resendRecord.setIsResend(BaseEntity.YES);
2708         resendRecord.setIsSuccess(BaseEntity.NO);
fd269d 2709         resendRecord.setNodeType(synHisOrderType);
44d306 2710         //新增或更新重发记录
Z 2711         commonService.insert(ResendRecordMapper.class,resendRecord);
2712     }
2713
a472d4 2714     /**
Z 2715      * 折扣审批-营销助手审批
2716      * @param ordersTotal               总订单
2717      * @param mOrderService             营销助手service
2718      * @param commonService
2719      */
2720     public static void approvalMarketing(OrdersTotal ordersTotal, MOrderService mOrderService, CommonService commonService) {
2721         //荣爷说订单是crm那边的订单不走营销助手审批
2722         if(!PlatformConstants.TYPE_PLATFORM_CRM.equals(ordersTotal.getAppIdCode()) && ordersTotal.getOrderInfo() !=null &&ordersTotal.getOrderInfo().getDiscount()!=null &&
2723                 ordersTotal.getOrderInfo().getDiscount().compareTo(new BigDecimal(100))==-1){
2724             //查看订单操作人信息
2725             Employee employee = commonService.selectOneByKeyBlob(EmployeeMapper.class, ordersTotal.getOperatorId());
2726             if(employee==null){
2727                 throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"营销助手审批:未找到操作人员工信息!");
2728             }
eb6ecc 2729             SqlSentence sqlSentence = new SqlSentence();
F 2730             Map<String,Object> sqlMap = new HashMap<>();
2731             sqlMap.put("id",employee.getRoleId());
2732             sqlSentence.sqlSentence(" SELECT er.*,s.name AS shopName FROM employee_role AS er " +
2733                     " LEFT JOIN shop AS s ON s.id = er.shopId WHERE er.id = #{m.id}",sqlMap);
2734             EmployeeRole employeeRole = commonService.selectOne(EmployeeRoleMapper.class,sqlSentence);
2735
a472d4 2736
Z 2737             OrdersTotalDto ordersTotalDto=new OrdersTotalDto();
ab068d 2738             ordersTotalDto.setTotal(ordersTotal.getShouldTotal().add(ordersTotal.getDiscountTotal()));
a472d4 2739             ordersTotalDto.setOrderNo(ordersTotal.getOrderNo());
Z 2740             ordersTotalDto.setId(ordersTotal.getId());
2741             ordersTotalDto.setDisTotal(ordersTotal.getShouldTotal());
2742
2743             //走审批流程,封装对象
2744             OrderDiscountDto orderDiscountDto = new OrderDiscountDto();
2745             orderDiscountDto.setDiscountNum(ordersTotal.getOrderInfo().getDiscount());
2746             orderDiscountDto.setOrdersTotalDto(ordersTotalDto);
2747             orderDiscountDto.setOperatorId(ordersTotal.getOperatorId());
2748             orderDiscountDto.setOpName(ordersTotal.getOperatorName());
eb6ecc 2749             orderDiscountDto.setShopId(employeeRole.getShopId());
F 2750             orderDiscountDto.setShopName(employeeRole.getShopName());
a472d4 2751             orderDiscountDto.setRoleId(employeeRole.getRoleTypeId());
Z 2752             orderDiscountDto.setRoleUniqueStr(employeeRole.getRoleUniqueStr());
2753             orderDiscountDto.setUserId(ordersTotal.getUserId());
2754             orderDiscountDto.setRemark(ordersTotal.getRemarks());
2755
2756             //调用营销助手审批
314764 2757             logger.info("请求营销助手审批接口,参数:{}", JSONObject.toJSONString(orderDiscountDto));
a472d4 2758             Result result = mOrderService.applyOrderDiscount(orderDiscountDto);
314764 2759             logger.info("营销助手审批接口,返回:{}",JSONObject.toJSONString(result));
a472d4 2760             if (result != null) {
Z 2761                 result.checkTips();
2762                 JSONObject object = JSONObject.parseObject(JSONObject.toJSONString(result.getData()));
2763                 if(!object.isEmpty()){
2764                     String applyId = object.getString("applyId");
2765
2766                     SqlSentence sql = new SqlSentence();
2767                     Map<String,Object> objectMap=new HashMap<>();
2768                     sql.setM(objectMap);
2769
2770                     //保存营销助手的审批标识
2771                     objectMap.put("applyId",applyId);
2772                     objectMap.put("id",ordersTotal.getOrderInfo().getId());
2773                     sql.setSqlSentence(" applyId=#{m.applyId} where id=#{m.id}");
2774                     commonService.updateWhere(OrderInfoMapper.class,sql);
2775
2776                     //更新总订单状态
2777                     objectMap.clear();
2778                     objectMap.put("status", OrderTotalConstants.STATUS_APPLE_PENDING);
2779                     objectMap.put("oldStatus", OrderTotalConstants.STATUS_APPLE_NONE);
2780                     objectMap.put("id",ordersTotal.getId());
2781                     sql.setSqlSentence(" applyStatus=#{m.status} where id=#{m.id} AND applyStatus=#{m.oldStatus}");
2782                     commonService.updateWhere(OrdersTotalMapper.class,sql);
2783                 }else{
314764 2784                     throw new PlatTipsException(PlatformCode.ERROR_TIPS,"请求营销助手审批接口报错,data返回为空!");
a472d4 2785                 }
Z 2786             } else {
314764 2787                 throw new PlatTipsException(PlatformCode.ERROR_TIPS,"请求营销助手审批接口报错,返回为空!");
F 2788             }
2789         }
2790     }
2791
2792     /**
2793      * 订单支付审批-营销助手审批
2794      * @param ordersTotal               总订单
2795      * @param mOrderService             营销助手service
2796      * @param commonService
2797      */
2798     public static void approvalPayMarketing(OrdersTotal ordersTotal,List<OrderItem> orderItemList, MOrderService mOrderService, CommonService commonService) {
2799         //荣爷说订单是crm那边的订单不走营销助手审批
2800         if(!PlatformConstants.TYPE_PLATFORM_CRM.equals(ordersTotal.getAppIdCode())
2801                 && ordersTotal.getOrderInfo() !=null && orderItemList != null && orderItemList.size() > 0){
2802             //查看订单操作人信息
2803             Employee employee = commonService.selectOneByKeyBlob(EmployeeMapper.class, ordersTotal.getOperatorId());
2804             if(employee==null){
2805                 throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"营销助手审批:未找到操作人员工信息!");
2806             }
2807             EmployeeRole employeeRole = commonService.selectOneByKeyBlob(EmployeeRoleMapper.class, employee.getRoleId());
2808
2809             OrdersTotalDto ordersTotalDto=new OrdersTotalDto();
2810             ordersTotalDto.setTotal(ordersTotal.getShouldTotal().add(ordersTotal.getDiscountTotal()));
2811             ordersTotalDto.setOrderNo(ordersTotal.getOrderNo());
2812             ordersTotalDto.setId(ordersTotal.getId());
2813             ordersTotalDto.setDisTotal(ordersTotal.getShouldTotal());
2814
2815             //走审批流程,封装对象
2816             OrderPayMarketingDto orderPayMarketingDto = new OrderPayMarketingDto();
2817             orderPayMarketingDto.setOrdersTotalDto(ordersTotalDto);
2818             orderPayMarketingDto.setDiscountNum(ordersTotal.getOrderInfo().getDiscount());
2819
2820             orderPayMarketingDto.setOperatorId(ordersTotal.getOperatorId());
2821             orderPayMarketingDto.setOpName(ordersTotal.getOperatorName());
2822             orderPayMarketingDto.setShopId(ordersTotal.getShopId());
2823             orderPayMarketingDto.setShopName(ordersTotal.getShopName());
2824             orderPayMarketingDto.setRoleId(employeeRole.getRoleTypeId());
2825             orderPayMarketingDto.setRoleUniqueStr(employeeRole.getRoleUniqueStr());
2826             orderPayMarketingDto.setUserId(ordersTotal.getUserId());
2827             orderPayMarketingDto.setRemark(ordersTotal.getRemarks());
2828
2829             List<RefundProjectDto> projectDtoList = new ArrayList<>();
2830             for (OrderItem orderItem : orderItemList) {
2831                 RefundProjectDto refundProjectDto = new RefundProjectDto();
2832                 refundProjectDto.setProjectId(orderItem.getCommonId());
2833                 refundProjectDto.setProjectNo(orderItem.getGoodsNo());
2834                 refundProjectDto.setProjectName(orderItem.getGoodsName());
2835                 //传到退款数量字段
2836                 refundProjectDto.setBuyNum(orderItem.getBuyNum());
dea5d5 2837                 projectDtoList.add(refundProjectDto);
314764 2838             }
F 2839             orderPayMarketingDto.setProjectDtoList(projectDtoList);
2840
2841             //调用营销助手审批
2842             logger.info("请求营销助手审批接口,参数:{}", JSONObject.toJSONString(orderPayMarketingDto));
2843             Result result = mOrderService.orderPay(orderPayMarketingDto);
2844             logger.info("营销助手审批接口,返回:{}",JSONObject.toJSONString(result));
2845             if (result != null) {
2846                 result.checkTips();
2847                 JSONObject object = JSONObject.parseObject(JSONObject.toJSONString(result.getData()));
2848                 if(!object.isEmpty()){
2849                     String applyId = object.getString("applyId");
2850
2851                     SqlSentence sql = new SqlSentence();
2852                     Map<String,Object> objectMap=new HashMap<>();
2853                     sql.setM(objectMap);
2854
2855                     //保存营销助手的审批标识
2856                     objectMap.put("applyId",applyId);
2857                     objectMap.put("id",ordersTotal.getOrderInfo().getId());
2858                     sql.setSqlSentence(" applyId=#{m.applyId} where id=#{m.id}");
2859                     commonService.updateWhere(OrderInfoMapper.class,sql);
2860
2861                     //更新总订单状态
2862                     objectMap.clear();
2863                     objectMap.put("status", OrderTotalConstants.STATUS_APPLE_PENDING);
2864                     objectMap.put("oldStatus", OrderTotalConstants.STATUS_APPLE_NONE);
2865                     objectMap.put("id",ordersTotal.getId());
2866                     sql.setSqlSentence(" applyStatus=#{m.status} where id=#{m.id} AND applyStatus=#{m.oldStatus}");
2867                     commonService.updateWhere(OrdersTotalMapper.class,sql);
2868                 }else{
2869                     throw new PlatTipsException(PlatformCode.ERROR_TIPS,"请求营销助手审批接口报错,data返回为空!");
2870                 }
2871             } else {
2872                 throw new PlatTipsException(PlatformCode.ERROR_TIPS,"请求营销助手审批接口报错,返回为空!");
a472d4 2873             }
Z 2874         }
2875     }
e1d05e 2876
253802 2877     /** 创建一级订单关联表数据 to 虚拟商品 */
F 2878     public static void createOrderItemAssocToVirtualGoods(CommonService commonService, OrderItem orderItem, List<ConsumablesAssoc> assocList){
2879         if(assocList != null && assocList.size() > 0){
2880             OrderItemAssoc orderItemAssoc;
2881             for(ConsumablesAssoc assoc : assocList){
2882                 orderItemAssoc = new OrderItemAssoc();
2883                 orderItemAssoc.setOrderItemId(orderItem.getId());
2884                 orderItemAssoc.setQuantity(assoc.getQuantity());
2885                 orderItemAssoc.setSourceId(assoc.getId());
2886                 orderItemAssoc.setSourceType(OrderItemAssocConstants.SOURCE_TYPE_VIRTUAL_GOODS);
2887                 commonService.insert(OrderItemAssocMapper.class, orderItemAssoc);
2888             }
2889
2890         }
2891     }
eb25b2 2892
F 2893     /** 开单-校验升单关联信息参数 */
f15332 2894     public static List<OrderRise> checkOrderRise(CommonService commonService, List<OrderRiseDto> orderRiseList){
eb25b2 2895         if(orderRiseList == null || orderRiseList.size() < 1){
F 2896             return null;
2897         }
f15332 2898
F 2899         //遍历升单关联的订单
eb25b2 2900         List<OrderRise> riseList = new ArrayList<>();
f15332 2901         OrdersTotal ordersTotal;
F 2902         OrderRise orderRise;
2903         for(OrderRiseDto orderRiseDto : orderRiseList){
2904             if(StringUtils.isEmpty(orderRiseDto.getAssocOrderId())){
aa39d6 2905                 throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL, "升单关联的订单标识不能为空!");
eb25b2 2906             }
f15332 2907             try{
F 2908                 ordersTotal = OrderNewUtil.checkOrderCanRise(commonService, orderRiseDto.getAssocOrderId());
2909             } catch (TipsException e){
2910                 throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"开单失败,检查升单逻辑时:" + e.getMessage());
2911             } catch (Exception e){
2912                 logger.error("开单失败,检查升单异常!{}", SimpleTool.getExceptionMsg(e));
2913                 throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL, "开单失败,检查升单异常!");
2914             }
2915             orderRise = new OrderRise(orderRiseDto.getAssocOrderId());
2916             orderRise.setShouldTotal(ordersTotal.getShouldTotal());
2917             riseList.add(orderRise);
eb25b2 2918         }
F 2919         return riseList;
2920     }
2921
2922     /** 创建订单升单关联的信息 */
2923     public static void createOrderRiseInfo(CommonService commonService, OrdersTotal ordersTotal, List<OrderRise> orderRiseList){
2924         if(orderRiseList == null || orderRiseList.size() < 1){
2925             return;
2926         }
2927
f15332 2928         //汇总原订单应付金额,判断是否可以升单
F 2929         BigDecimal oriShouldTotal = orderRiseList.stream().map(s->s.getShouldTotal()).reduce(BigDecimal.ZERO,BigDecimal::add);
2930         if(ordersTotal.getShouldTotal().compareTo(oriShouldTotal) == -1){
2931             throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL, "开单失败,升单订单的应付金额不能小于原订单应付金额!");
2932         }
2933
eb25b2 2934         String orderId = ordersTotal.getId();
F 2935         for(OrderRise orderRise : orderRiseList){
2936             orderRise.setOrderId(orderId);
2937             commonService.insert(OrderRiseMapper.class, orderRise);
2938         }
2939     }
cafa53 2940
6fec68 2941     /**获取用户所属顾问的门店
C 2942      * @param employeeId 员工标识
2943      * @param commonService 映射
2944      * @return 门店
2945      */
2946     public static Shop getUserConsultShop(String employeeId,CommonService commonService){
2947
2948         SqlSentence sqlSentence = new SqlSentence();
2949         Map<String,Object> values = new HashMap<>();
2950
2951         values.put("employeeId",employeeId);
2952         sqlSentence.sqlSentence("SELECT s.* FROM employee_role er" +
2953                 " JOIN shop s ON s.id = er.shopId" +
2954                 " WHERE er.isDel = 0 AND er.isMainProfessional = 1 AND er.employeeId = #{m.employeeId} LIMIT 1",values);
2955         return commonService.selectOne(ShopMapper.class,sqlSentence);
2956     }
2957
8c307c 2958 }