Authored by Lixiaodi

商品增加销量信息

package com.yohoufo.dal.product;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.yohoufo.dal.product.model.ProductSales;
public interface ProductSalesMapper {
ProductSales selectUfoSalesByProductId(Integer productId);
List<ProductSales> selectAmountByProductIdList(@Param("productIdList")List<Integer> productIdList);
int updateUfoSales(Integer productId);
int addOrUpdateUfoSales(ProductSales record);
}
\ No newline at end of file
... ...
package com.yohoufo.dal.product.model;
import lombok.Data;
import lombok.ToString;
@Data
@ToString
public class ProductSales {
private Integer id;
private Integer productId;
private Byte companyId;
private Integer amount;
private Integer updateTime;
private Integer editUid;
}
\ No newline at end of file
... ...
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.yohoufo.dal.product.ProductSalesMapper" >
<resultMap id="BaseResultMap" type="com.yohoufo.dal.product.model.ProductSales" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="product_id" property="productId" jdbcType="INTEGER" />
<result column="company_id" property="companyId" jdbcType="TINYINT" />
<result column="amount" property="amount" jdbcType="INTEGER" />
<result column="update_time" property="updateTime" jdbcType="INTEGER" />
<result column="edit_uid" property="editUid" jdbcType="INTEGER" />
</resultMap>
<sql id="Base_Column_List" >
id, product_id, company_id, amount, update_time, edit_uid
</sql>
<select id="selectUfoSalesByProductId" resultMap="BaseResultMap">
select
*
from product_sales
where product_id = #{productId,jdbcType=INTEGER} and company_id=0
</select>
<select id="selectAmountByProductIdList" resultMap="BaseResultMap">
select
product_id,sum(amount) as amount
from product_sales
where product_id in
<foreach item="item" index="index" collection="productIdList" open="(" separator="," close=")">
#{item}
</foreach>
group by product_id
</select>
<update id="updateUfoSales" parameterType="com.yohoufo.dal.product.model.ProductSales" >
update product_sales
set amount = amount + 1
where product_id = #{productId,jdbcType=INTEGER} and company_id=0
</update>
<insert id="addOrUpdateUfoSales" parameterType="com.yohoufo.dal.product.model.ProductSales" >
insert into product_sales (id, product_id, company_id,
amount, update_time, edit_uid
)
values (#{id,jdbcType=INTEGER}, #{productId,jdbcType=INTEGER}, #{companyId,jdbcType=TINYINT},
#{amount,jdbcType=INTEGER}, #{updateTime,jdbcType=INTEGER}, #{editUid,jdbcType=INTEGER}
) ON DUPLICATE KEY UPDATE amount = amount + 1
</insert>
</mapper>
\ No newline at end of file
... ...
... ... @@ -319,6 +319,7 @@ public class PaymentServiceImpl implements IPaymentService {
}
addPayBuyRecord(orderInfo.getUid(), orderInfo.getSellerUid(), goods.getSkup(), orderCode, recordPayment,
new BigDecimal(paymentData.getTotalFee()));
addSalesInfo(goods.getSkup());
} else if (codeMeta.getType() == OrderCodeType.SELLER_TYPE.getType()) {
addPayEnsureRecord(orderInfo.getUid(), orderInfo.getSkup(), orderCode, recordPayment,
new BigDecimal(paymentData.getTotalFee()));
... ... @@ -330,7 +331,13 @@ public class PaymentServiceImpl implements IPaymentService {
abstractOrderService.processAfterPay(orderInfo);
}
private OrdersPay saveOrdersPay(PaymentData paymentData, long orderType, OrderInfo orderInfo) {
private void addSalesInfo(Integer skup) {
logger.info("paySuccess finished. 增加销量数据,skup= {}", skup);
ufoServiceCaller.call("ufo.product.addSales", skup);
}
private OrdersPay saveOrdersPay(PaymentData paymentData, long orderType, OrderInfo orderInfo) {
long orderCode = Long.valueOf(paymentData.getOrderCode());
... ...
... ... @@ -3,6 +3,7 @@ package com.yohoufo.product.controller;
import com.alibaba.fastjson.JSONObject;
import com.yoho.core.config.ConfigReader;
import com.yoho.error.exception.ServiceException;
import com.yoho.tools.common.beans.BaseRequestBO;
import com.yoho.tools.docs.ApiOperation;
import com.yohobuy.ufo.model.request.StoragePriceBo;
import com.yohobuy.ufo.model.request.product.ProductImportTranItemBo;
... ... @@ -563,5 +564,17 @@ public class ProductController {
return new ApiResponse.ApiResponseBuilder().data(result).code(200).message("productLimitInfo").build();
}
@RequestMapping(params = "method=ufo.product.addSales")
public ApiResponse addSales(Integer skup) {
LOG.info("in method=ufo.product.addSales skup = {}", skup);
try {
productService.addSales(skup);
return new ApiResponse(200, "添加成功!", Boolean.TRUE);
} catch (Exception e) {
LOG.error("addSales失败!", e);
int code = (e instanceof ServiceException) ? ((ServiceException) e).getCode() : 500;
return new ApiResponse(code, e.getMessage(), Boolean.FALSE);
}
}
}
\ No newline at end of file
... ...
... ... @@ -89,4 +89,6 @@ public interface ProductService {
SkupDetailForScreenResp getSkuPDetailForShopsScreen(Integer skuP);
void changeSkupHideStatus(Integer uid, int status);
void addSales(Integer skup);
}
... ...
... ... @@ -8,6 +8,7 @@ import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.apache.commons.collections.MapUtils;
... ... @@ -31,8 +32,10 @@ import com.yohoufo.common.caller.UfoServiceCaller;
import com.yohoufo.common.helper.ImageUrlAssist;
import com.yohoufo.common.utils.UfoStringUtils;
import com.yohoufo.dal.product.ProductMapper;
import com.yohoufo.dal.product.ProductSalesMapper;
import com.yohoufo.dal.product.ProductSortMapper;
import com.yohoufo.dal.product.model.Product;
import com.yohoufo.dal.product.model.ProductSales;
import com.yohoufo.dal.product.model.ProductSort;
import com.yohoufo.product.helper.SearchParam;
import com.yohoufo.product.model.FilterItem;
... ... @@ -62,6 +65,9 @@ public class ProductSearchServiceImpl implements ProductSearchService {
@Autowired
private ProductSortMapper productSortMapper;
@Autowired
private ProductSalesMapper productSalesMapper;
@Autowired
private ProductMapper productMapper;
... ... @@ -144,6 +150,7 @@ public class ProductSearchServiceImpl implements ProductSearchService {
// 将图片的相对路径转成绝对路径
if (null != data) {
processProductList(data.getJSONArray("product_list"));
processProductSales(data.getJSONArray("product_list"));
}
return data;
... ... @@ -173,6 +180,36 @@ public class ProductSearchServiceImpl implements ProductSearchService {
}
}
protected void processProductSales(JSONArray productList) {
if(CollectionUtils.isEmpty(productList)){
return;
}
List<Integer> productIdList = new ArrayList<>();
// 遍历商品列表
for (int i = 0; i < productList.size(); i++) {
JSONObject product = productList.getJSONObject(i);
if(null == product){
continue;
}
productIdList.add(product.getInteger("id"));
}
List<ProductSales> salesList = productSalesMapper.selectAmountByProductIdList(productIdList);
Map<Integer, ProductSales> salesMap = salesList.stream()
.collect(Collectors.toMap(ProductSales::getProductId, Function.identity()));
// 遍历商品列表
for (int i = 0; i < productList.size(); i++) {
JSONObject product = productList.getJSONObject(i);
if (null == product) {
continue;
}
ProductSales sale = salesMap.get(product.getInteger("id"));
if (null == sale || null == sale.getAmount()) {
continue;
}
product.put("sales", sale.getAmount());
}
}
@SuppressWarnings("unchecked")
@Override
public void processUserFavoriteProductList(JSONObject productJSON, Integer uid) {
... ...
... ... @@ -82,6 +82,9 @@ public class ProductServiceImpl implements ProductService {
@Autowired
private PriceTrendDayMapper priceTrendDayMapper;
@Autowired
private ProductSalesMapper productSalesMapper;
@Autowired
private SellerStoreMapUtil sellerStoreMapUtil;
... ... @@ -966,6 +969,29 @@ public class ProductServiceImpl implements ProductService {
result.setSkupDetail(innerResult);
return result;
}
@Override
public void addSales(Integer skup) {
StoragePrice sp = storagePriceMapper.selectBySkup(skup);
if (sp == null) {
LOGGER.error("in addSales skup = {}, find no record", skup);
}
Integer productId = sp.getProductId();
if (productSalesMapper.selectUfoSalesByProductId(productId) == null) {
ProductSales sale = new ProductSales();
sale.setAmount(1);
sale.setCompanyId((byte) 0);
sale.setEditUid(0);
sale.setProductId(productId);
sale.setUpdateTime((int) (System.currentTimeMillis() / 1000));
LOGGER.info("in addSales productId = {}, first sale bean={}", productId, sale);
productSalesMapper.addOrUpdateUfoSales(sale);
} else {
LOGGER.info("in addSales productId = {}, sale add 1", productId);
productSalesMapper.updateUfoSales(productId);
}
LOGGER.info("in addSales productId = {}, add success", productId);
}
/**
* 获取价格走势
... ... @@ -1077,4 +1103,5 @@ public class ProductServiceImpl implements ProductService {
innerResult.setSkupPrice(storagePrice.getPrice());
}
}
... ...
... ... @@ -38,6 +38,7 @@ datasources:
- com.yohoufo.dal.product.ProductImportTranItemMapper
- com.yohoufo.dal.product.TransferRecordsMapper
- com.yohoufo.dal.product.TransferRecordsHistoryMapper
- com.yohoufo.dal.product.ProductSalesMapper
ufo_order:
... ...
... ... @@ -38,6 +38,7 @@ datasources:
- com.yohoufo.dal.product.ProductImportTranItemMapper
- com.yohoufo.dal.product.TransferRecordsMapper
- com.yohoufo.dal.product.TransferRecordsHistoryMapper
- com.yohoufo.dal.product.ProductSalesMapper
ufo_order:
servers:
... ...