zhouxiang
2023-02-23 bec88badaeb0341a1d5429b31357eb37c0b16fb0
phi_platform_user/src/main/java/com/hx/phip/controller/order/OrderControllerV2.java
@@ -13,20 +13,18 @@
import com.hx.common.BaseController;
import com.hx.common.service.CommonService;
import com.hx.mybatisTool.SqlSentence;
import com.hx.phiappt.common.OrderTotalConstants;
import com.hx.phiappt.common.PlatformConstants;
import com.hx.phiappt.common.*;
import com.hx.phiappt.common.PlatformConstants;
import com.hx.phiappt.constants.tool.ConsultantCashReportTool;
import com.hx.phiappt.constants.tool.PerformanceInfoTool;
import com.hx.phiappt.constants.tool.UserStatusTool;
import com.hx.phiappt.dao.mapper.CouponNumberMapper;
import com.hx.phiappt.model.*;
import com.hx.phiappt.model.coupon.Coupon;
import com.hx.phiappt.model.coupon.CouponNumber;
import com.hx.phiappt.model.order.OrderDeveloper;
import com.hx.phiappt.model.order.OrderInfo;
import com.hx.phiappt.model.order.OrderItem;
import com.hx.phiappt.model.order.OrdersTotal;
import com.hx.phiappt.model.order.*;
import com.hx.phiappt.model.performance.PerformanceInfo;
import com.hx.phiappt.model.userStatus.UserStatusLog;
import com.hx.phip.config.CustomParameter;
import com.hx.phip.dao.mapper.*;
import com.hx.phip.service.ShopService;
@@ -34,9 +32,7 @@
import com.hx.phip.service.UserService;
import com.hx.phip.service.order.OrderService;
import com.hx.phip.service.order.OrderServiceV2;
import com.hx.phip.util.api.ApiOrderUtil;
import com.hx.phip.util.api.OrderCreateUtil;
import com.hx.phip.util.api.UserMoneyUtil;
import com.hx.phip.util.api.*;
import com.hx.util.StringUtils;
import com.hz.his.dto.order.OrderPHisDto;
import com.hz.his.dto.order.PayDto;
@@ -55,10 +51,8 @@
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@@ -283,20 +277,36 @@
        Map<String, Object> map = orderServiceV2.orderPayMoney(type, ordersTotal,user,payUser,shop,info,orderItemList,operator,userMoney,total, amountList,userCouponIds,null);
        if(map!=null && map.containsKey("orderId")){
            //记录收入确认表
            try{
                orderServiceV2.statementPerformanceInfo((String) map.get("orderId"));
            }catch (Exception e) {
                logger.info("记录收入确认表,异常:{}", e.getMessage(), e);
                // 添加错误日志
                PerformanceInfoTool.addErrorLog(commonService, map.get("orderId").toString(), PerformanceInfo.ASSOCIATION_TYPE_GOODS_ORDER, e.getMessage());
            OrdersTotal sucOrdersTotal=commonService.selectOneByKey(OrdersTotalMapper.class,map.get("orderId"));
            if(OrderTotalConstants.PAY_STATUS_SUC==sucOrdersTotal.getPayStatus()){
                //处理老带新
                JSONObject commissionObject = handOrderOldNew(amountList, ordersTotal, user, map);
                //更新用户最后下单时间
                updateUserLastTime(ordersTotal, user);
                //需求: 1.HIS助手结账成功:要通知客户所属顾问,发送企业微信通知 ,如果没有所属顾问发送给门店运营客服(三少说目前不处理crm已处理) -异步
                try {
                    orderServiceV2.sendUserConsultant(ordersTotal.getUserId(),ordersTotal.getOrderNo(),ordersTotal.getId(),commissionObject,commonService);
                }catch (Exception e){
                    logger.info("要通知客户所属顾问,发送企业微信通知异常 -异步:{}",e.getMessage(),e);
                }
                //记录收入确认表
                try{
                    orderServiceV2.statementPerformanceInfo((String) map.get("orderId"));
                }catch (Exception e) {
                    logger.info("记录收入确认表,异常:{}", e.getMessage(), e);
                    // 添加错误日志
                    PerformanceInfoTool.addErrorLog(commonService, map.get("orderId").toString(), PerformanceInfo.ASSOCIATION_TYPE_GOODS_ORDER, e.getMessage());
                }
                //咨询师现金业绩报表
                try{
                    orderServiceV2.statementConsultantCash((String) map.get("orderId"));
                }catch (Exception e){
                    logger.info("咨询师现金业绩报表,异常:{}",e.getMessage(),e);
                }
            }
            //咨询师现金业绩报表
            try{
                orderServiceV2.statementConsultantCash((String) map.get("orderId"));
            }catch (Exception e){
                logger.info("咨询师现金业绩报表,异常:{}",e.getMessage(),e);
            }
            //异步获取未执行划扣信息,测试环境先放开,荣爷说,记得写重发
//            if(user!=null && StringUtils.noNull(user.getApiId())){
//                try {
@@ -474,4 +484,111 @@
        }
    }
    /**
     * 更新用户最后一次下单时间
     * @param ordersTotal
     * @param user
     */
    public void updateUserLastTime(OrdersTotal ordersTotal, User user) {
        try {
            SqlSentence sqlSentence = new SqlSentence();
            Map<String, Object> sqlValues = new HashMap<>();
            sqlValues.put("lastPayOrderTime", new Date());
            sqlValues.put("editTime", new Date());
            sqlValues.put("isLoss", BaseEntity.NO);
            sqlValues.put("isDel", BaseEntity.NO);
            sqlValues.put("id", user.getId());
            StringBuilder sql = new StringBuilder();
            sql.append(" lastPayOrderTime=#{m.lastPayOrderTime},editTime=#{m.editTime},isLoss=#{m.isLoss}");
            // 会员有消费和执行就是活跃会员
            String status = null;
            if (!UserStatusConstants.USER_STATUS_NON_MEMBER.equals(user.getUserStatus())) {
                status = UserStatusConstants.USER_STATUS_ACTIVE_MEMBER;
                sqlValues.put("userStatus", status);
                sql.append(",userStatus=#{m.userStatus}");
            }
            sql.append(" WHERE id=#{m.id} AND isDel=#{m.isDel}");
            sqlSentence.sqlSentence(sql.toString(), sqlValues);
            commonService.updateWhere(UserMapper.class, sqlSentence);
            // 添加调整用户状态日志 调整了用户等级在写入日志
            if (!StringUtils.isEmpty(status)) {
                UserStatusTool.addUserStatusLog(commonService, user, UserStatusLog.CHANGE_STATUS_TYPE_ORDER, ordersTotal.getId(), UserStatusConstants.USER_STATUS_ACTIVE_MEMBER);
            }
        } catch (Exception e) {
            logger.info("更新用户最后下单时间,异常:{}", e.getMessage(), e);
        }
    }
    /**
     * 处理老带新
     * @param amountList
     * @param ordersTotal
     * @param user
     * @param map
     * @return
     */
    private JSONObject handOrderOldNew(JSONArray amountList, OrdersTotal ordersTotal, User user, Map<String, Object> map) {
        /**
         * 处理老带新(禅道为准,禅道号176)
         *      1.准会员、普卡、银卡:
         *          邀请人增值金百分比:8%
         *          被邀请人增值金百分比:2%
         *     2.金卡、钻卡、黑卡:
         *          邀请人增值金百分比:10%
         *          被邀请人增值金百分比:5%
         */
        SqlSentence sql = new SqlSentence();
        Map<String,Object> paramMap=new HashMap<>();
        sql.setM(paramMap);
        JSONObject commissionObject =new JSONObject();
        JSONArray hisPayList=new JSONArray();
        StringBuilder orderNodeBuilder = new StringBuilder();
        if(ordersTotal.getOrderInfo().getIsOldBringNew() !=null && ordersTotal.getOrderInfo().getIsOldBringNew()== BaseEntity.YES){
            JSONObject hisPay ;
            for(int i=0;i<amountList.size();i++) {
                JSONObject jsonObject = amountList.getJSONObject(i);
                String amount = jsonObject.getString("amount");//用户实际支付金额
                String method = jsonObject.getString("method");//用户支付编码
                hisPay = new JSONObject();
                //根据支付编码,上架的支付方式,并判空
                paramMap.put("method", method);
                paramMap.put("isDel", BaseEntity.NO);
                paramMap.put("isUp", BaseEntity.YES);
                sql.setSqlSentence("select * from payment_method where numberNo=#{m.method} and isDel=#{m.isDel} and isUp=#{m.isUp}");
                PaymentMethod paymentMethod = commonService.selectOne(PaymentMethodMapper.class, sql);
                if (paymentMethod != null) {
                    //添加用户支付方式金额、以及方法,用于同步到his系统
                    hisPay.put("amount",amount);
                    hisPay.put("methodCode",paymentMethod.getNumberNo());
                    hisPay.put("isMoneyPay",paymentMethod.getIsMoneyPay());
                    hisPayList.add(hisPay);
                }
            }
                try {
                orderNodeBuilder.append("-开始处理老带新");
                commissionObject = OrderPayMoneyUtil.handPayOldNew(ordersTotal, user, hisPayList, commonService);
                orderNodeBuilder.append("-结束处理老带新");
            }catch (Exception e){
                String snapshot="处理老带新失败";
                orderNodeBuilder.append("-处理老带新失败,异常原因:"+e.getMessage());
                logger.error("处理老带新失败:" + e.getMessage(),e);
                //发送企业微信通知给工作人员
                SendNoticeUtil.failOrderSendNotice(ordersTotal,e.getMessage(),snapshot,commonService,customParameter);
            }
            paramMap.put("orderId",map.get("orderId"));
            sql.setSqlSentence("select * from orders_node_log where orderId=#{m.orderId} and isDel=0 order by createTime desc LIMIT 1");
            OrdersNodeLog ordersNodeLog=commonService.selectOne(OrdersNodeLogMapper.class,sql);
            if(ordersNodeLog!=null ){
                String concat = ordersNodeLog.getContent().concat(orderNodeBuilder.toString());
                paramMap.put("concat",concat);
                paramMap.put("id",ordersNodeLog.getId());
                sql.setSqlSentence(" content=#{m.concat} where id=#{m.id} ");
                commonService.updateWhere(OrdersNodeLogMapper.class,sql);
            }
        }
        return commissionObject;
    }
}