zhouxiang
2023-02-23 bec88badaeb0341a1d5429b31357eb37c0b16fb0
提交 | 用户 | age
37deae 1 package com.hx.phip.controller.order;
Z 2
3 /**
4  * 订单控制器
5  *
6  * @Author: zhouxiang
7  * @Date: 2022/09/19/11:53
8  * @Description:
9  */
10
f886af 11 import com.alibaba.fastjson.JSONArray;
37deae 12 import com.alibaba.fastjson.JSONObject;
Z 13 import com.hx.common.BaseController;
fbde78 14 import com.hx.common.service.CommonService;
37deae 15 import com.hx.mybatisTool.SqlSentence;
957141 16 import com.hx.phiappt.common.*;
5c395d 17 import com.hx.phiappt.common.PlatformConstants;
f8f3c5 18 import com.hx.phiappt.constants.tool.ConsultantCashReportTool;
Z 19 import com.hx.phiappt.constants.tool.PerformanceInfoTool;
236a02 20 import com.hx.phiappt.constants.tool.UserStatusTool;
6a4f14 21 import com.hx.phiappt.dao.mapper.CouponNumberMapper;
39db81 22 import com.hx.phiappt.model.*;
6a4f14 23 import com.hx.phiappt.model.coupon.Coupon;
Z 24 import com.hx.phiappt.model.coupon.CouponNumber;
236a02 25 import com.hx.phiappt.model.order.*;
071df8 26 import com.hx.phiappt.model.performance.PerformanceInfo;
236a02 27 import com.hx.phiappt.model.userStatus.UserStatusLog;
f886af 28 import com.hx.phip.config.CustomParameter;
6a4f14 29 import com.hx.phip.dao.mapper.*;
8c307c 30 import com.hx.phip.service.ShopService;
Z 31 import com.hx.phip.service.UserMoneyService;
823513 32 import com.hx.phip.service.UserService;
f3226c 33 import com.hx.phip.service.order.OrderService;
a24ca9 34 import com.hx.phip.service.order.OrderServiceV2;
236a02 35 import com.hx.phip.util.api.*;
37deae 36 import com.hx.util.StringUtils;
Z 37 import com.hz.his.dto.order.OrderPHisDto;
f886af 38 import com.hz.his.dto.order.PayDto;
448b78 39 import com.hz.his.feign.service.sync.SyncOrderService;
37deae 40 import com.platform.constants.LoginConstant;
Z 41 import com.platform.entity.ThirtApplication;
42 import com.platform.exception.PlatTipsException;
43 import com.platform.resultTool.PlatformCode;
44 import com.platform.resultTool.PlatformResult;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.springframework.web.bind.annotation.RequestBody;
48 import org.springframework.web.bind.annotation.RequestMapping;
49 import org.springframework.web.bind.annotation.RequestMethod;
50 import org.springframework.web.bind.annotation.RestController;
51
a24ca9 52 import javax.annotation.Resource;
37deae 53 import javax.servlet.http.HttpServletRequest;
957141 54 import java.math.BigDecimal;
236a02 55 import java.util.*;
f886af 56 import java.util.concurrent.ExecutorService;
Z 57 import java.util.concurrent.Executors;
37deae 58
Z 59 /**
60  * 订单控制器
61  * @USER: zhouxiang
62  * @DATE: 2021/12/03
63  */
64 @RestController
65 @RequestMapping("/order")
66 public class OrderControllerV2 extends BaseController {
67
68     //log4j日志
69     private static Logger logger = LoggerFactory.getLogger(OrderControllerV2.class.getName());
f8f3c5 70     // 固定线程池
99af69 71     //ExecutorService threadPool = Executors.newFixedThreadPool(10);
d64999 72
6942c5 73     //订单类型集合(充值,转增充值)
Z 74     private static List<String> orderTypeList= Arrays.asList(OrderTotalConstants.TYPE_RECHARGE,OrderTotalConstants.TYPE_TURN_RECHARGE);
a24ca9 75     @Resource
Z 76     private OrderServiceV2 orderServiceV2;
77     @Resource
f3226c 78     private OrderService orderService;
8c307c 79     @Resource
Z 80     private ShopService shopService;
81     @Resource
82     private UserMoneyService userMoneyService;
f886af 83     @Resource
Z 84     private CustomParameter customParameter;
823513 85     @Resource
Z 86     private UserService userService;
448b78 87     @Resource
Z 88     private SyncOrderService syncOrderService;
37deae 89     /**
Z 90      * 开单接口
91      */
92     @RequestMapping(value = "/create",method = RequestMethod.POST)
ea2d26 93     public PlatformResult orderCreate(HttpServletRequest request, @RequestBody OrderPHisDto dto) {
Z 94         //校验请求参数是否为空
37deae 95         if(dto==null){
Z 96             throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"请传订单参数");
97         }
ea2d26 98         //校验签名
37deae 99         ThirtApplication thirtApplication= (ThirtApplication) request.getSession().getAttribute(LoginConstant.LOGIN_APPLY);
Z 100         if(thirtApplication==null){
101             throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"签名失败,请检查签名!");
102         }
ea2d26 103
37deae 104         //打印请求参数
14d3a0 105         logger.info("前打印新开单接口请求所有参数:{}", JSONObject.toJSONString(dto));
37deae 106
ea2d26 107         //解析请求参数,并校验参数必填值,赋予默认值等操作
Z 108         JSONObject object = JSONObject.parseObject(JSONObject.toJSONString(dto));
37deae 109         //解析总订单对象
Z 110         OrdersTotal orderTotal = JSONObject.parseObject(object.getString("orderTotal"), OrdersTotal.class);
111         //校验总订单参数
112         orderTotal = OrderCreateUtil.checkOrderTotalParam(orderTotal);
c4e004 113
37deae 114         //获取订单的子订单
Z 115         List<OrderItem> orderItem = JSONObject.parseArray(object.getString("orderItem"), OrderItem.class);
116         //校验子订单参数
117         orderItem=OrderCreateUtil.checkOrderItem(orderItem,orderTotal.getType());
118         //获取开发人员集合
119         List<OrderDeveloper> orderDeveloperList = JSONObject.parseArray(object.getString("orderDeveloper"), OrderDeveloper.class);
120         //操作人id,没有默认填订单用户标识
121         String operatorId = StringUtils.isEmpty(object.getString("operatorId"))?orderTotal.getUserId():object.getString("operatorId");
122         //操作类型,0:草稿 1:创建订单 2:创建订单+结账(目前结账和创建是分开的),默认1
123         Integer type =object.getInteger("type")==null?1:object.getInteger("type");
124         //获取操作人门店标识
125         String shopId = object.getString("shopId");//操作人的门店id
126
ea2d26 127         //查询需求要的信息,并且判空(例如查询门店、用户、用户资金等信息)
8c307c 128         //查询操作人门店信息
Z 129         Shop shop = shopService.selectOneByKey(shopId);
130         if(shop==null){
131             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到门店信息,操作人门店标识:{}",shopId);
132         }
133         //获取用户资金信息,用于订单快照存储用户资金信息
134         UserMoney userMoney = userMoneyService.selectUserId(orderTotal.getUserId());
135         if(userMoney==null){
136             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找用户资金信息,订单用户标识:{}"+orderTotal.getUserId());
137         }
138         //获取用户信息
139         User user = commonService.selectOneByKey(UserMapper.class,orderTotal.getUserId());
140         if(user==null){
141             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找用户信息,订单用户标识:{}"+orderTotal.getUserId());
142         }
39db81 143         //获取订单销售平台信息
Z 144         PlatformApplication platformApplication = OrderCreateUtil.getPlatformApplication(orderTotal, commonService);
8c307c 145         //获取操作人信息
Z 146         Map<String, String> operator = ApiOrderUtil.getOperator(commonService, operatorId);
147
c4e004 148         //默认总订单参数
Z 149         OrderCreateUtil.setDefaultOrderTotal(orderTotal,thirtApplication.getAppId(),thirtApplication.getAppIdCode(),user,commonService);
efcde5 150
Z 151
ea2d26 152         //执行开单方法
Z 153         Map<String, Object> map = orderServiceV2.orderCreate(orderTotal, orderItem, type, orderDeveloperList, operator, shop,userMoney,user,platformApplication,commonService);
154
155         //返回phis订单号、phis订单id
a24ca9 156         return  PlatformResult.success(map);
Z 157     }
158
159     /**
f886af 160      * 结账
Z 161      */
162     @RequestMapping(value = "/payMoney",method = RequestMethod.POST)
ea2d26 163     public synchronized PlatformResult orderPayMoney(HttpServletRequest request,@RequestBody PayDto dto) {
Z 164         //校验请求参数是否为空
f886af 165         if(dto==null){
Z 166             throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"请传订单参数");
167         }
ea2d26 168         //校验签名
Z 169         ThirtApplication thirtApplication= (ThirtApplication) request.getSession().getAttribute(LoginConstant.LOGIN_APPLY);
170         if(thirtApplication==null){
171             throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"签名失败,请检查签名!");
172         }
173
174         //打印请求参数
14d3a0 175         logger.info("前打印新结账订单请求所有参数:{}",JSONObject.toJSONString(dto));
6942c5 176
ea2d26 177         //解析请求参数,并校验参数必填值,赋予默认值等操作
f886af 178         JSONObject object = JSONObject.parseObject(JSONObject.toJSONString(dto));
Z 179         //是否是按项目付款,0:否,1:是
180         Integer type = object.getInteger("type");
181         //订单id
182         String orderId = object.getString("orderId");
183         //应付的总金额
184         String total = object.getString("total");
185         //用户真正支付的金额集合
186         JSONArray amountList = object.getJSONArray("amount");
187         //操作人员id
188         String operatorId = object.getString("operatorId");
fbde78 189
b071b7 190         //优惠券id集合
5c395d 191         JSONArray userCouponIds = new JSONArray();
f886af 192         String userCouponId = object.getString("userCouponId");
59a39b 193         JSONArray couponIds = object.getJSONArray("userCouponIds");
Z 194
195         if(StringUtils.noNull(userCouponId)){
196             userCouponIds.add(userCouponId);
5c395d 197         }
59a39b 198         if(couponIds!=null && couponIds.size()>0){
Z 199             userCouponIds.addAll(couponIds);
200         }
f886af 201
ea2d26 202         //查询需求要的信息,并且判空(例如查询门店、订单信息、一级子订单、用户资金等信息)
f886af 203         OrdersTotal ordersTotal=commonService.selectOneByKeyBlob(OrdersTotalMapper.class,orderId);
6942c5 204         User user = userService.selectOneByKey(ordersTotal.getUserId());
Z 205         UserMoney userMoney=userMoneyService.selectUserId(ordersTotal.getUserId());
206         Shop shop = shopService.selectOneByKey(ordersTotal.getDeveloperShopId());
207         //判断订单状态是否正确
823513 208         if(OrderTotalConstants.STATUS_WAIT_PAY!=ordersTotal.getStatus()){
Z 209             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"订单总状态不正确,只有待支付才能进行结账!");
210         }
c4e004 211
Z 212
6942c5 213         SqlSentence sqlSentence = new SqlSentence();
Z 214         Map<String,Object> objectMap=new HashMap<>();
215         sqlSentence.setM(objectMap);
216         objectMap.put("id",orderId);
217         //获取订单其他信息(orderInfo)
218         sqlSentence.setSqlSentence("SELECT * FROM orders_info where orderId=#{m.id}");
219         OrderInfo info = commonService.selectOne(OrderInfoMapper.class,sqlSentence);
220         //获取一级子订单信息,注意:充值和转增充值订单没有一级子订单集合
221         sqlSentence.setSqlSentence("select * from order_item where isDel=0 and orderId=#{m.id}");
222         List<OrderItem> orderItemList=commonService.selectList(OrderItemMapper.class,sqlSentence);
223         //获取操作人信息map
224         Map<String, String> operator = ApiOrderUtil.getOperator(commonService, operatorId);
fbde78 225         //判断优惠券叠加使用,项目券或者现金券+生日券
b071b7 226         if(userCouponIds!=null && userCouponIds.size()>0){
fbde78 227             checkCoupon(userCouponIds,commonService);
6a4f14 228         }
6942c5 229
ea2d26 230         //判断是否非空,订单、用户、用户资金、门店、订单其他信息(orderInfo)、订单标识、应付总金额、用户支付金额、操作人标识信息是否为空
Z 231         checkIsNull(ordersTotal, user, userMoney, shop,info,orderItemList,orderId,total,amountList,operatorId);
232         ordersTotal.setOrderInfo(info);
464466 233         //收银员id
Z 234         String cashier = object.getString("cashier");
235         if(PlatformConstants.TYPE_PLATFORM_CRM.equals(ordersTotal.getAppIdCode())){
236             cashier=ordersTotal.getUserId();
237         }
238         if(StringUtils.noNull(cashier)){
239             Employee employee=commonService.selectOneByKeyBlob(EmployeeMapper.class,cashier);
240             if(employee!=null){
241                 ordersTotal.setCashierId(employee.getId());
242                 ordersTotal.setCashierName(StringUtils.isEmpty(employee.getCnName())?"":employee.getCnName());
243             }else{
244                 User cashierUser=commonService.selectOneByKeyBlob(UserMapper.class,cashier);
245                 if(cashierUser!=null){
246                     ordersTotal.setCashierId(cashierUser.getId());
247                     ordersTotal.setCashierName(StringUtils.isEmpty(cashierUser.getName())?"":cashierUser.getName());
248                 }
249             }
250         }
251         //收银员门店
252         String cashierShopId = object.getString("cashierShopId");
253         if(PlatformConstants.TYPE_PLATFORM_CRM.equals(ordersTotal.getAppIdCode())){
254             cashierShopId=ordersTotal.getDeveloperShopId();
255         }
256         if(StringUtils.noNull(cashierShopId)){
257             Shop cashierShop=commonService.selectOneByKeyBlob(ShopMapper.class,cashierShopId);
258             if(cashierShop!=null){
259                 ordersTotal.setCashierShopId(cashierShop.getId());
260                 ordersTotal.setCashierShopName(StringUtils.isEmpty(cashierShop.getName())?"":cashierShop.getName());
261             }
262         }
ea2d26 263
56f07c 264         User payUser = null;
C 265         //支付用户
266         if(StringUtils.noNull(dto.getPayUserId())){
01a106 267             payUser = userService.selectOneByKey(dto.getPayUserId());
56f07c 268             if(payUser == null){
C 269                 throw new PlatTipsException(PlatformCode.ERROR_TIPS,"支付人标识错误!");
270             }
271         }
272         if(payUser == null){
273             payUser = user;
274         }
275
ea2d26 276         //执行结账方法
56f07c 277         Map<String, Object> map = orderServiceV2.orderPayMoney(type, ordersTotal,user,payUser,shop,info,orderItemList,operator,userMoney,total, amountList,userCouponIds,null);
ea2d26 278
739451 279         if(map!=null && map.containsKey("orderId")){
4ab44f 280             OrdersTotal sucOrdersTotal=commonService.selectOneByKey(OrdersTotalMapper.class,map.get("orderId"));
15b29e 281             if(OrderTotalConstants.PAY_STATUS_SUC==sucOrdersTotal.getPayStatus()){
Z 282                 //处理老带新
283                 JSONObject commissionObject = handOrderOldNew(amountList, ordersTotal, user, map);
236a02 284
15b29e 285                 //更新用户最后下单时间
Z 286                 updateUserLastTime(ordersTotal, user);
236a02 287
15b29e 288                 //需求: 1.HIS助手结账成功:要通知客户所属顾问,发送企业微信通知 ,如果没有所属顾问发送给门店运营客服(三少说目前不处理crm已处理) -异步
Z 289                 try {
290                     orderServiceV2.sendUserConsultant(ordersTotal.getUserId(),ordersTotal.getOrderNo(),ordersTotal.getId(),commissionObject,commonService);
291                 }catch (Exception e){
292                     logger.info("要通知客户所属顾问,发送企业微信通知异常 -异步:{}",e.getMessage(),e);
293                 }
a85bdc 294                 //记录收入确认表
Z 295                 try{
296                     orderServiceV2.statementPerformanceInfo((String) map.get("orderId"));
297                 }catch (Exception e) {
298                     logger.info("记录收入确认表,异常:{}", e.getMessage(), e);
299                     // 添加错误日志
300                     PerformanceInfoTool.addErrorLog(commonService, map.get("orderId").toString(), PerformanceInfo.ASSOCIATION_TYPE_GOODS_ORDER, e.getMessage());
301                 }
302                 //咨询师现金业绩报表
303                 try{
304                     orderServiceV2.statementConsultantCash((String) map.get("orderId"));
305                 }catch (Exception e){
306                     logger.info("咨询师现金业绩报表,异常:{}",e.getMessage(),e);
307                 }
236a02 308             }
Z 309
1c40c5 310             //异步获取未执行划扣信息,测试环境先放开,荣爷说,记得写重发
350069 311 //            if(user!=null && StringUtils.noNull(user.getApiId())){
Z 312 //                try {
313 //                    orderService.getExecution(map.get("orderId"),user);
314 //                }catch (Exception e){
315 //                    logger.info("异步获取未执行划扣信信失败!订单标识:{},用户信息:{}",map.get("orderId"),JSONObject.toJSONString(user));
316 //                }
317 //            }
448b78 318             //获取领建订单号
350069 319 //            if(map.containsKey("hisOrderId")){
Z 320 //                try {
321 //                    Map<String,Object> paramMap=new HashMap<>();
322 //                    sqlSentence.setM(paramMap);
323 //                    paramMap.put("orderId",map.get("orderId"));
324 //                    paramMap.put("isDel",BaseEntity.NO);
325 //                    String hisOrderId = orderService.getHisOrderId((String) map.get("hisOrderId"), (String) map.get("orderId"), sqlSentence, paramMap, syncOrderService, commonService);
326 //                    map.put("hisOrderNo", hisOrderId);
327 //                }catch (Exception e){
328 //                    logger.info("获取领建订单号失败!订单标识:{}",map.get("orderId"));
329 //                }
330 //            }
56021c 331 //            OrdersTotal orderSum=commonService.selectOneByKey(OrdersTotalMapper.class,map.get("orderId"));
Z 332 //            if(orderSum!=null && StringUtils.noNull(orderSum.getHisOrderId())){
333 //                //同步卡包标识,暂时由同步中心处理,以后切掉领建由预约处理新增卡包
334 //                if(StringUtils.isEmpty(ordersTotal.getKoapOrderId())){
335 //                    SynOrderUtil.synGetUserCard(orderSum,customParameter,commonService);
336 //                }
337 //            }
739451 338             //根据订单用户标识同步当前用户所有未执行划扣数据,弃用
Z 339 //            if(user!=null && StringUtils.noNull(user.getApiId())){
340 //                JSONObject data = new JSONObject();
341 //                data.put("endTime", DateUtil.formatDate_3(new Date()));
342 //                data.put("startTime",DateUtil.formatDate_3(new Date()));
343 //                data.put("customerId",user.getApiId());
344 //                ApiPlatformUtil.sysnExecution(customParameter,data.toJSONString());
345 //            }
13ce60 346
739451 347
Z 348         }
fbde78 349 //        //优惠券id  单张使用优惠券废弃
Z 350 //        String userCouponId = object.getString("userCouponId");
351 //        //查询优惠券信息
352 //        if(StringUtils.noNull(userCouponId)){
353 //            //查看用户是否有优惠券
354 //            couponNumber = commonService.selectOneByKey(CouponNumberMapper.class, userCouponId);
355 //            if(couponNumber==null){
356 //                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"优惠卷:该用户没有该优惠券");
357 //            }
358 //            //查看用户是否有优惠券
359 //            coupon = commonService.selectOneByKey(CouponMapper.class, couponNumber.getCouponId());
360 //            if(coupon==null){
361 //                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"优惠卷:未找到优惠券信息");
362 //            }
363 //        }
739451 364
d63b45 365 //        //结账成功发送公众号通知给用户
Z 366 //        threadPool.execute(() -> orderServiceV2.sendPublicNo(ordersTotal.getId(),commonService));
367 //        //需求: 1.HIS助手结账成功:要通知客户所属顾问,发送企业微信通知
368 //        threadPool.execute(() -> orderService.sendUserConsultant(ordersTotal.getUserId(),ordersTotal.getOrderNo(),ordersTotal.getId(),commonService));
f886af 369
ea2d26 370         //返回phis订单号、phis订单id、领建订单号、领建订单id
Z 371         return  PlatformResult.success(map);
37deae 372     }
6942c5 373
Z 374     /**
375      * 结账判断是否为空
376      * @param ordersTotal                       总订单标识
377      * @param user                              用户信息
378      * @param userMoney                         用户资金
379      * @param shop                              门店信息
380      * @param info                              订单其他信息(orderInfo)
381      * @param orderItemList                     一级子订单集合
382      * @param orderId                           订单标识
383      * @param total                             应付总金额
384      * @param amountList                        用户支付集合
385      * @param operatorId                        操作人标识
386      */
6a4f14 387     private void checkIsNull(OrdersTotal ordersTotal, User user, UserMoney userMoney, Shop shop,OrderInfo info,List<OrderItem> orderItemList,
Z 388                              String orderId,String total,JSONArray amountList,String operatorId) {
6942c5 389         if(StringUtils.isEmpty(orderId)){
Z 390             throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"请传订单id");
391         }
392
393         if(StringUtils.isEmpty(total)){
394             throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"请传应付的总金额");
395         }
396
397         if(amountList ==null || amountList.size()==0){
398             throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"请传用户真实付款金额");
399         }
400
401         if(StringUtils.isEmpty(operatorId)){
402             throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"请传用户操作人标识");
403         }
404
405         if(ordersTotal!=null){
043443 406 //            //同步用户资金信息
Z 407 //            UserMoneyUtil.syncHisUserMoney(commonService,customParameter,ordersTotal.getUserId());
6942c5 408         }else{
Z 409             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"订单不存在");
410         }
411
412         if(user==null){
413             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"结账:未找到该用户信息");
414         }
415
416         if(userMoney ==null ){
417             //没有用户资金信息,抛错,资金全部交给用户资金全权处理
418             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到用户资金信息!");
419         }
420
421         if(shop==null){
422             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"结账时:订单门店不存在");
423         }
424
425         if(info==null){
426             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"根据总订单未找到订单其他信息");
427         }
428
429         if(!orderTypeList.contains(ordersTotal.getType()) && (orderItemList ==null || orderItemList.size()==0)){
430             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"没有对应的子订单");
431         }
432     }
b0bef0 433
fbde78 434     /**
Z 435      * 检查优惠券是否符合一张项目券或者一张现金券+生日优惠券
436      * 判断思路说明:
437      *      1、只要统计用户使用优惠券集合的项目券或者现金券的张数
438      *      2、如果项目或者现金券大于1张肯定是不给使用的
439      *      3、如果都没有项目券或者现金券,代表专门使用生日优惠券,生日优惠券不进行判断限制,无限使用
440      @param userCouponIds
441      */
442     public static void checkCoupon(JSONArray userCouponIds, CommonService commonService) {
443         CouponNumber couponNumber=null;
444         Coupon coupon=null;
445         //统计项目券张数
446         Integer projectCoupon=0;
447         //统计现金券张数
448         Integer cashCoupon=0;
449         for(int i=0;i<userCouponIds.size();i++){
450             String couponId = userCouponIds.getString(i);
451             //查看用户是否有优惠券
452             couponNumber = commonService.selectOneByKey(CouponNumberMapper.class, couponId);
453             if(couponNumber==null){
454                 throw new PlatTipsException(PlatformCode.ERROR_TIPS,"优惠卷:该用户没有该优惠券");
455             }
456             //查看用户是否有优惠券
457             coupon = commonService.selectOneByKey(CouponMapper.class, couponNumber.getCouponId());
458             if(coupon==null){
459                 throw new PlatTipsException(PlatformCode.ERROR_TIPS,"优惠卷:未找到优惠券信息");
460             }
461             if(coupon.getIsBirthday()== BaseEntity.NO && (coupon.getUseProjectType()==Coupon.USE_TYPE_PROJECT || coupon.getUseProjectType()==Coupon.USE_TYPE_PROJECT_CLASSIFY)){
462                 projectCoupon=projectCoupon+1;
463             }else if(coupon.getIsBirthday()==BaseEntity.NO &&(coupon.getUseGoodsType()==Coupon.USE_TYPE_GOODS || coupon.getUseGoodsType()==Coupon.USE_TYPE_GOODS_CLASSIFY)){
464                 projectCoupon=projectCoupon+1;
465             }else if(coupon.getIsBirthday()==BaseEntity.NO &&(coupon.getUsePromotionType()==Coupon.USE_TYPE_PROMOTION || coupon.getUsePromotionType()==Coupon.USE_TYPE_PROMOTION_CLASSIFY)){
466                 projectCoupon=projectCoupon+1;
467             }else if(coupon.getIsBirthday()==BaseEntity.NO &&(coupon.getUseCardType()==Coupon.USE_TYPE_CARD || coupon.getUseCardType()==Coupon.USE_TYPE_CARD_CLASSIFY)){
468                 projectCoupon=projectCoupon+1;
469             }else if(coupon.getIsBirthday()==BaseEntity.NO && coupon.getCommodityType()==Coupon.USE_TYPE_TYPE_COMMODITY){
470
471             }else if(coupon.getIsBirthday()==BaseEntity.NO && coupon.getIntegralType()==Coupon.USE_TYPE_CRM_GOODS){
472
473             }else if(coupon.getIsBirthday()==BaseEntity.NO &&(coupon.getUseProjectType()==Coupon.USE_TYPE_PROJECT_ALL || coupon.getUseGoodsType()==Coupon.USE_TYPE_GOODS_ALL||
474                     coupon.getUsePromotionType()==Coupon.USE_TYPE_PROMOTION_ALL || coupon.getUseCardType()==Coupon.USE_TYPE_CARD_ALL || coupon.getCommodityType()==Coupon.USE_TYPE_COMMODITY_ALL ||
475                     coupon.getIntegralType()==Coupon.USE_TYPE_CRM_GOODS_ALL)){
476                 cashCoupon=cashCoupon+1;
477             }
478         }
479         if(cashCoupon>1){
480             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"叠加优惠卷提示:一张订单只能使用一张现金券");
481         }
482         if(projectCoupon>1){
483             throw new PlatTipsException(PlatformCode.ERROR_TIPS,"叠加优惠卷提示:一张订单只能使用一张项目券");
484         }
485     }
b0bef0 486
236a02 487     /**
Z 488      * 更新用户最后一次下单时间
489      * @param ordersTotal
490      * @param user
491      */
492     public void updateUserLastTime(OrdersTotal ordersTotal, User user) {
493         try {
494             SqlSentence sqlSentence = new SqlSentence();
495             Map<String, Object> sqlValues = new HashMap<>();
496             sqlValues.put("lastPayOrderTime", new Date());
497             sqlValues.put("editTime", new Date());
498             sqlValues.put("isLoss", BaseEntity.NO);
499             sqlValues.put("isDel", BaseEntity.NO);
500             sqlValues.put("id", user.getId());
501             StringBuilder sql = new StringBuilder();
502             sql.append(" lastPayOrderTime=#{m.lastPayOrderTime},editTime=#{m.editTime},isLoss=#{m.isLoss}");
503             // 会员有消费和执行就是活跃会员
504             String status = null;
505             if (!UserStatusConstants.USER_STATUS_NON_MEMBER.equals(user.getUserStatus())) {
506                 status = UserStatusConstants.USER_STATUS_ACTIVE_MEMBER;
507                 sqlValues.put("userStatus", status);
508                 sql.append(",userStatus=#{m.userStatus}");
509             }
510             sql.append(" WHERE id=#{m.id} AND isDel=#{m.isDel}");
511             sqlSentence.sqlSentence(sql.toString(), sqlValues);
512             commonService.updateWhere(UserMapper.class, sqlSentence);
513             // 添加调整用户状态日志 调整了用户等级在写入日志
514             if (!StringUtils.isEmpty(status)) {
515                 UserStatusTool.addUserStatusLog(commonService, user, UserStatusLog.CHANGE_STATUS_TYPE_ORDER, ordersTotal.getId(), UserStatusConstants.USER_STATUS_ACTIVE_MEMBER);
516             }
517         } catch (Exception e) {
518             logger.info("更新用户最后下单时间,异常:{}", e.getMessage(), e);
519         }
520     }
521
522     /**
523      * 处理老带新
524      * @param amountList
525      * @param ordersTotal
526      * @param user
527      * @param map
528      * @return
529      */
530     private JSONObject handOrderOldNew(JSONArray amountList, OrdersTotal ordersTotal, User user, Map<String, Object> map) {
531         /**
532          * 处理老带新(禅道为准,禅道号176)
533          *      1.准会员、普卡、银卡:
534          *          邀请人增值金百分比:8%
535          *          被邀请人增值金百分比:2%
536          *     2.金卡、钻卡、黑卡:
537          *          邀请人增值金百分比:10%
538          *          被邀请人增值金百分比:5%
539          */
957141 540         SqlSentence sql = new SqlSentence();
Z 541         Map<String,Object> paramMap=new HashMap<>();
542         sql.setM(paramMap);
236a02 543         JSONObject commissionObject =new JSONObject();
957141 544         JSONArray hisPayList=new JSONArray();
236a02 545         StringBuilder orderNodeBuilder = new StringBuilder();
Z 546         if(ordersTotal.getOrderInfo().getIsOldBringNew() !=null && ordersTotal.getOrderInfo().getIsOldBringNew()== BaseEntity.YES){
1f5e38 547             JSONObject hisPay ;
957141 548             for(int i=0;i<amountList.size();i++) {
Z 549                 JSONObject jsonObject = amountList.getJSONObject(i);
550                 String amount = jsonObject.getString("amount");//用户实际支付金额
551                 String method = jsonObject.getString("method");//用户支付编码
552
553                 hisPay = new JSONObject();
554
555                 //根据支付编码,上架的支付方式,并判空
556                 paramMap.put("method", method);
557                 paramMap.put("isDel", BaseEntity.NO);
558                 paramMap.put("isUp", BaseEntity.YES);
559                 sql.setSqlSentence("select * from payment_method where numberNo=#{m.method} and isDel=#{m.isDel} and isUp=#{m.isUp}");
560                 PaymentMethod paymentMethod = commonService.selectOne(PaymentMethodMapper.class, sql);
561                 if (paymentMethod != null) {
562                     //添加用户支付方式金额、以及方法,用于同步到his系统
563                     hisPay.put("amount",amount);
564                     hisPay.put("methodCode",paymentMethod.getNumberNo());
565                     hisPay.put("isMoneyPay",paymentMethod.getIsMoneyPay());
566                     hisPayList.add(hisPay);
567                 }
568             }
569                 try {
236a02 570                 orderNodeBuilder.append("-开始处理老带新");
957141 571                 commissionObject = OrderPayMoneyUtil.handPayOldNew(ordersTotal, user, hisPayList, commonService);
236a02 572                 orderNodeBuilder.append("-结束处理老带新");
Z 573             }catch (Exception e){
574                 String snapshot="处理老带新失败";
575                 orderNodeBuilder.append("-处理老带新失败,异常原因:"+e.getMessage());
576                 logger.error("处理老带新失败:" + e.getMessage(),e);
577                 //发送企业微信通知给工作人员
578                 SendNoticeUtil.failOrderSendNotice(ordersTotal,e.getMessage(),snapshot,commonService,customParameter);
579             }
957141 580
236a02 581             paramMap.put("orderId",map.get("orderId"));
Z 582             sql.setSqlSentence("select * from orders_node_log where orderId=#{m.orderId} and isDel=0 order by createTime desc LIMIT 1");
583             OrdersNodeLog ordersNodeLog=commonService.selectOne(OrdersNodeLogMapper.class,sql);
584             if(ordersNodeLog!=null ){
585                 String concat = ordersNodeLog.getContent().concat(orderNodeBuilder.toString());
586                 paramMap.put("concat",concat);
587                 paramMap.put("id",ordersNodeLog.getId());
588                 sql.setSqlSentence(" content=#{m.concat} where id=#{m.id} ");
589                 commonService.updateWhere(OrdersNodeLogMapper.class,sql);
590             }
591         }
592         return commissionObject;
593     }
37deae 594 }