xiaogang
2022-08-18 146bd184f86648f21603d7d6698cdbe0a2df7cff
service-order/src/main/java/com/java110/order/smo/impl/OIdServiceSMOImpl.java
old mode 100644 new mode 100755
@@ -3,17 +3,23 @@
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.core.client.RestTemplate;
import com.java110.core.context.Environment;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.core.log.LoggerFactory;
import com.java110.dto.app.AppDto;
import com.java110.dto.businessTableHis.BusinessTableHisDto;
import com.java110.dto.order.OrderDto;
import com.java110.dto.order.OrderItemDto;
import com.java110.order.dao.ICenterServiceDAO;
import com.java110.order.smo.IAsynNotifySubService;
import com.java110.order.smo.IOIdServiceSMO;
import com.java110.utils.cache.BusinessTableHisCache;
import com.java110.utils.constant.StatusConstant;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.DateUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
@@ -36,6 +42,8 @@
    public static final String FALLBACK_URL = "http://SERVICE_NAME/businessApi/fallBack";
    public static final String BOOT_FALLBACK_URL = "http://127.0.0.1:8008/businessApi/fallBack";
    public static final String SERVICE_NAME = "SERVICE_NAME";
@@ -43,7 +51,13 @@
    private ICenterServiceDAO centerServiceDAOImpl;
    @Autowired
    private RestTemplate outRestTemplate;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private IAsynNotifySubService asynNotifySubServiceImpl;
    @Override
@@ -51,23 +65,24 @@
        orderDto.setoId(GenerateCodeFactory.getOId());
        if (StringUtil.isEmpty(orderDto.getAppId())) {
            throw new IllegalArgumentException("未包含appId");
            orderDto.setAppId(AppDto.WEB_APP_ID);
        }
        if (StringUtil.isEmpty(orderDto.getExtTransactionId())) {
            throw new IllegalArgumentException("未包含交互日志");
            orderDto.setExtTransactionId(GenerateCodeFactory.getTransactionId());
        }
        if (StringUtil.isEmpty(orderDto.getRequestTime())) {
            throw new IllegalArgumentException("未包含请求时间");
            orderDto.setRequestTime(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_DEFAULT));
        }
        if (StringUtil.isEmpty(orderDto.getUserId())) {
            throw new IllegalArgumentException("未包含用户ID");
            orderDto.setUserId("-1");
        }
        //保存订单信息
        centerServiceDAOImpl.saveOrder(BeanConvertUtil.beanCovertMap(orderDto));
        return new ResponseEntity<String>(JSONObject.toJSONString(orderDto), HttpStatus.OK);
    }
@@ -100,7 +115,12 @@
            try {
                JSONArray params = generateParam(orderItemDto);
                httpEntity = new HttpEntity<String>(params.toJSONString(), header);
                restTemplate.exchange(FALLBACK_URL.replace(SERVICE_NAME, orderItemDto.getServiceName()), HttpMethod.POST, httpEntity, String.class);
                if (Environment.isStartBootWay()) {
                    outRestTemplate.exchange(BOOT_FALLBACK_URL, HttpMethod.POST, httpEntity, String.class);
                } else if (!StringUtil.isEmpty(orderItemDto.getActionObj()) && !orderItemDto.getActionObj().equals("meter_water")) {
                    restTemplate.exchange(FALLBACK_URL.replace(SERVICE_NAME, orderItemDto.getServiceName()), HttpMethod.POST, httpEntity, String.class);
                }
                //标记为订单项失败
                Map info = new HashMap();
@@ -156,6 +176,7 @@
        return params;
    }
    /**
     * 生成insert语句
     *
@@ -174,6 +195,9 @@
            sql = "insert into " + orderItemDto.getActionObj() + " ";
            param = new JSONObject();
            JSONObject keyValue = preValues.getJSONObject(preValueIndex);
            if (keyValue.isEmpty()) {
                continue;
            }
            String keySql = "( ";
            String valueSql = " values (";
            for (String key : keyValue.keySet()) {
@@ -209,13 +233,23 @@
            param = new JSONObject();
            JSONObject keyValue = preValues.getJSONObject(preValueIndex);
            JSONObject afterKeyValue = afterValues.getJSONObject(preValueIndex);
            if (keyValue.isEmpty() || afterKeyValue.isEmpty()) {
                continue;
            }
            String whereSql = " where 1=1 ";
            for (String key : keyValue.keySet()) {
                sql += (key + "=" + keyValue.getString(key) + ",");
                if ("''".equals(afterKeyValue.getString(key))) { //条件中不拼写 为空的结果
                    continue;
                }
                whereSql += (" and " + key + " = " + afterKeyValue.getString(key));
            }
            if (sql.endsWith(",")) {
                sql = sql.substring(0, sql.length() - 1);
            }
            if (sql.endsWith(whereSql)) { // 说明没有条件 不做回退 回退整个表是有问题的
                continue;
            }
            sql += whereSql;
@@ -240,15 +274,23 @@
        JSONObject logTextObj = JSONObject.parseObject(logText);
        JSONArray afterValues = logTextObj.getJSONArray("afterValue");
        String whereSql = " where 1=1 ";
        for (int preValueIndex = 0; preValueIndex < afterValues.size(); preValueIndex++) {
            sql = "delete from " + orderItemDto.getActionObj() + " where 1=1 ";
            sql = "delete from " + orderItemDto.getActionObj() + whereSql;
            param = new JSONObject();
            JSONObject keyValue = afterValues.getJSONObject(preValueIndex);
            if (keyValue.isEmpty()) {
                continue;
            }
            for (String key : keyValue.keySet()) {
                if (!StringUtil.isEmpty(keyValue.getString(key))) {
                    sql += (" and " + key + "=" + keyValue.getString(key));
                }
            }
            if (sql.endsWith(whereSql)) { // 说明没有条件 不做回退 回退整个表是有问题的
                continue;
            }
            sql += " limit 1";//防止程序异常删除 尴尬 根据业务场景 没有需要删除多余 1条的场景
            param.put("fallBackSql", sql);
            params.add(param);
        }
@@ -288,7 +330,73 @@
        }
        centerServiceDAOImpl.saveOrderItem(BeanConvertUtil.beanCovertMap(orderItemDto));
        //获取action
        String action = getOrderItemAction(orderItemDto);
        //判断是否配置了 轨迹
        BusinessTableHisDto businessTableHisDto = BusinessTableHisCache.getBusinessTableHisDto(action, orderItemDto.getActionObj());
        if (businessTableHisDto == null) {
            return ResultVo.createResponseEntity(ResultVo.CODE_OK, ResultVo.MSG_OK);
        }
        //补充 c_business  #{bId},#{oId},#{businessTypeCd},#{remark},#{statusCd}
        Map business = new HashMap();
        business.put("oId", orderItemDto.getoId());
        business.put("businessTypeCd", businessTableHisDto.getBusinessTypeCd());
        business.put("remark", "");
        business.put("statusCd", StatusConstant.STATUS_CD_SAVE);
        business.put("bId", orderItemDto.getbId());
        centerServiceDAOImpl.saveBusiness(business);
        //通知子服务生成 business 数据,如果配置NO 不通知生成 business 数据
        if (BusinessTableHisDto.ACTION_OBJ_HIS_NO.equals(businessTableHisDto.getActionObjHis())) {
            return ResultVo.createResponseEntity(ResultVo.CODE_OK, ResultVo.MSG_OK);
        }
        doNoticeServiceGeneratorBusiness(orderItemDto, businessTableHisDto);
        return ResultVo.createResponseEntity(ResultVo.CODE_OK, ResultVo.MSG_OK);
    }
    /**
     * 这里 兼容性处理
     * 因为我们不涉及 物理删除 都是逻辑删除 所以 status_cd 为 1 时强行设置为DEL 为逻辑删除
     *
     * @param orderItemDto
     * @return
     */
    private String getOrderItemAction(OrderItemDto orderItemDto) {
        if (StringUtil.isEmpty(orderItemDto.getLogText())) {
            return orderItemDto.getAction();
        }
        if ("ADD".equals(orderItemDto.getAction()) || "DEL".equals(orderItemDto.getAction())) {
            return orderItemDto.getAction();
        }
        if (!StringUtil.isJsonObject(orderItemDto.getLogText())) {
            return orderItemDto.getAction();
        }
        JSONObject logTextObj = JSONObject.parseObject(orderItemDto.getLogText());
        if (!logTextObj.containsKey("afterValue")) {
            return orderItemDto.getAction();
        }
        JSONArray afterValues = logTextObj.getJSONArray("afterValue");
        if (afterValues == null || afterValues.size() < 1) {
            return orderItemDto.getAction();
        }
        for (int afterValueIndex = 0; afterValueIndex < afterValues.size(); afterValueIndex++) {
            JSONObject keyValue = afterValues.getJSONObject(afterValueIndex);
            if (keyValue.containsKey("status_cd") && "'1'".equals(keyValue.getString("status_cd"))) {
                return "DEL";
            }
        }
        return orderItemDto.getAction();
    }
    private void doNoticeServiceGeneratorBusiness(OrderItemDto orderItemDto, BusinessTableHisDto businessTableHisDto) {
        asynNotifySubServiceImpl.notifySubService(orderItemDto, businessTableHisDto);
    }
    /**
@@ -310,9 +418,6 @@
        info.put("oId", orderDto.getoId());
        centerServiceDAOImpl.updateOrderItem(info);
        //删除 事务日志
        //centerServiceDAOImpl.deleteUnItemLog(info);
        //完成订单
        info = new HashMap();
        info.put("finishTime", DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_A));
@@ -320,6 +425,27 @@
        info.put("oId", orderDto.getoId());
        centerServiceDAOImpl.updateOrder(info);
        //将c_business 修改为完成
        //完成订单项
        info = new HashMap();
        info.put("finishTime", DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_A));
        info.put("statusCd", "C");
        info.put("oId", orderDto.getoId());
        centerServiceDAOImpl.updateBusiness(info);
        //触发databug
        //查询 事务项
        Map orderItem = new HashMap();
        orderItem.put("oId", orderDto.getoId());
        List<Map> orderItemMaps = centerServiceDAOImpl.getOrderItems(orderItem);
        //删除 事务日志
        //centerServiceDAOImpl.deleteUnItemLog(info);
        asynNotifySubServiceImpl.notifyDatabus(orderItemMaps, orderDto);
        return ResultVo.createResponseEntity(ResultVo.CODE_OK, ResultVo.MSG_OK);
    }
}