Authored by mali

批量操作性能优化

package com.yoho.ufo.util;
import com.google.common.collect.Lists;
import java.util.List;
/**
* Created by li.ma on 2019/6/25.
*/
public class ListUtil {
public static List<Long> convertList(String str, String split) {
List<Long> result = Lists.newArrayList();
String[] strCodeArr = str.split(",");
for (String item : strCodeArr) {
result.add(Long.valueOf(item));
}
return result;
}
}
... ...
... ... @@ -37,4 +37,6 @@ public interface BuyerOrderMapper {
// 根据sellerUid 和状态查询订单数目
int selectCountByUidStatus(@Param("statusList")List<Integer> statusList, @Param("sellerUid")int sellerUid);
List<BuyerOrder> selectBatchByOrderCode(@Param("orderCodes") List<Long> orderCodes);
}
... ...
... ... @@ -297,4 +297,12 @@
</foreach>
</if>
</select>
<select id="selectBatchByOrderCode" resultMap="BaseResultMap">
select <include refid="Base_Column_List" />
from buyer_order where order_code in
<foreach collection="orderCodes" item="item" open="(" close=")" separator=",">
#{item, jdbcType=BIGINT}
</foreach>
</select>
</mapper>
\ No newline at end of file
... ...
package com.yoho.ufo.order.request;
import com.yoho.order.dal.OrderOperateRecordMapper;
import com.yoho.order.model.OrderOperateRecord;
import com.yoho.ufo.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.concurrent.Callable;
/**
* 异步处理订单操作的日志
*/
public class OrderOperateTask implements Callable<Integer> {
private static final Logger LOGGER = LoggerFactory.getLogger(OrderOperateTask.class);
private String orderCode;
private Integer type;
private Integer uid;
private String userName;
private String oldInfo;
private Integer updateTime;
private OrderOperateRecordMapper orderOperateRecordMapper;
public OrderOperateTask(String orderCode, Integer type, Integer uid, String userName, String oldInfo, Integer updateTime) {
this.uid = uid;
this.orderCode = orderCode;
this.type = type;
this.userName = userName;
this.oldInfo = oldInfo;
this.updateTime = updateTime;
}
@Override
public Integer call() throws Exception {
try {
LOGGER.info("handle OrderOperateTask orderCode:{}, type:{}, uid:{}, userName :{}, oldInfo:{}, updateTime:{}",
orderCode, type, uid, userName, oldInfo, updateTime);
OrderOperateRecord optRecord = new OrderOperateRecord();
optRecord.setOrderCode(orderCode);
optRecord.setType(type);
optRecord.setUid(uid);
optRecord.setUserName(userName);
optRecord.setUpdateTime(updateTime);
optRecord.setOldInfo(oldInfo);
orderOperateRecordMapper.insert(optRecord);
return null;
} catch (Throwable t) {
LOGGER.error("handle OrderOperateTask orderCode:" + orderCode + ";type is :" + type, t);
return null;
}
}
}
\ No newline at end of file
... ...
... ... @@ -9,11 +9,15 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import com.yoho.ufo.order.request.OrderOperateTask;
import com.yoho.ufo.util.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.common.collect.Lists;
... ... @@ -100,11 +104,6 @@ import com.yoho.ufo.order.service.IBuyerOrderService;
import com.yoho.ufo.service.impl.UfoServiceCaller;
import com.yoho.ufo.service.impl.UserHelper;
import com.yoho.ufo.service.model.PageResponseBO;
import com.yoho.ufo.util.DateUtil;
import com.yoho.ufo.util.ImagesConstant;
import com.yoho.ufo.util.ImagesHelper;
import com.yoho.ufo.util.MobileHelper;
import com.yoho.ufo.util.OrderVideoUrlUtil;
import com.yohobuy.ufo.model.order.bo.AppraiseExpressInfoBo;
import com.yohobuy.ufo.model.order.common.EnumExpressType;
import com.yohobuy.ufo.model.order.common.EnumQualityCheckStatus;
... ... @@ -130,6 +129,8 @@ public class BuyerOrderServiceImpl implements IBuyerOrderService {
private static final Logger LOGGER = LoggerFactory.getLogger(BuyerOrderServiceImpl.class);
private final static ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);
@Autowired
private BuyerOrderMapper buyerOrderMapper;
... ... @@ -859,9 +860,16 @@ public class BuyerOrderServiceImpl implements IBuyerOrderService {
}
String[] orderCodeArr = orderCodes.split(",");
UserHelper userHelper = new UserHelper();
int operateType =OperateTypeEnum.OPERATE_TYPE_CONFIRM_RECEIVE.getCode();
List<Long> orderCodeList = ListUtil.convertList(orderCodes, ",");
List<BuyerOrder> buyerOrders = buyerOrderMapper.selectBatchByOrderCode(orderCodeList);
Map<String, BuyerOrder> orderMap = buyerOrders.stream().collect(Collectors.toMap(BuyerOrder::getOrderCode, item -> item));
for(int i=0; i<orderCodeArr.length; i++) {
String orderCode = orderCodeArr[i];
BuyerOrder buyerOrder = buyerOrderMapper.selectByOrderCode(orderCode);
BuyerOrder buyerOrder = orderMap.get(orderCode);//buyerOrderMapper.selectByOrderCode(orderCode);
if( Constant.BUYER_ORDER_STATUS_ALLOCATING.getByteVal()!=buyerOrder.getStatus().byteValue()){
throw new ServiceException(400,"错误:订单"+orderCode +"状态变化,不允许确认收货,请重新刷新列表");
}
... ... @@ -869,14 +877,10 @@ public class BuyerOrderServiceImpl implements IBuyerOrderService {
String args = "ufo-gateway.confirmReceive";
JSONObject jsonObject = asyncCallConfirmReceive(args, buyerOrder.getOrderCode());
if(jsonObject!=null&&jsonObject.getIntValue("code") == 200) {
if(jsonObject == null || jsonObject.getIntValue("code") != 200) {
throw new ServiceException(400,"错误:订单"+orderCode +"确认收货失败");
}
//记录操作日志
int operateType =OperateTypeEnum.OPERATE_TYPE_CONFIRM_RECEIVE.getCode();
UserHelper userHelper = new UserHelper();
saveOrderOperateRecord(buyerOrder.getOrderCode(),userHelper, operateType, "");
saveOrderOperateRecord(buyerOrder.getOrderCode(),userHelper, operateType, ""); //记录操作日志
LOGGER.info("confirmReceive saveOrderOperateRecord operateType={} ,order code ={} ,userHelper = {} ,result json {}",operateType,buyerOrder.getOrderCode() ,userHelper,jsonObject);
}
}
... ... @@ -2523,14 +2527,15 @@ public class BuyerOrderServiceImpl implements IBuyerOrderService {
}
private void saveOrderOperateRecord(String orderCode, UserHelper userInfo, int operateType, String oldInfo) {
OrderOperateRecord optRecord = new OrderOperateRecord();
executorService.submit(new OrderOperateTask(orderCode, operateType, userInfo.getUserId(), userInfo.getUserName(), oldInfo, DateUtil.getCurrentTimeSeconds()));
/*OrderOperateRecord optRecord = new OrderOperateRecord();
optRecord.setOrderCode(orderCode);
optRecord.setType(operateType);
optRecord.setUid(userInfo.getUserId());
optRecord.setUserName(userInfo.getUserName());
optRecord.setUpdateTime(DateUtil.getCurrentTimeSeconds());
optRecord.setOldInfo(oldInfo);
orderOperateRecordMapper.insert(optRecord);
orderOperateRecordMapper.insert(optRecord);*/
}
/*private String getAddressInfo(String areaCode) {
... ...