Authored by caoyan

Merge branch 'dev_suggestPrice_20181119' into test6.8.2

# Conflicts:
#	dal/src/main/java/com/yoho/ufo/dal/StorageMapper.java
#	dal/src/main/java/com/yoho/ufo/dal/UfoSizeMapper.java
#	dal/src/main/resources/META-INF/mybatis/StorageMapper.xml
#	dal/src/main/resources/META-INF/mybatis/UfoSizeMapper.xml
#	web/src/main/resources/META-INF/spring/spring-web-context.xml
package com.yoho.ufo.restapi;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.yoho.error.exception.ServiceException;
import com.yoho.ufo.service.impl.BatchService;
import com.yoho.ufo.service.model.ExportParam;
/**
* 批量导出服务接口
* Created by xueyin on 2016/2/2.
*/
@Controller
@RequestMapping("/batch")
public class BatchExportController {
private static final Logger LOGGER = LoggerFactory.getLogger(BatchExportController.class);
@Autowired
private BatchService batchService;
/**
* 下载批量操作结果
*
* @param path 批量导出的业务类型
* @return
*/
@RequestMapping(value = "/download")
public String download(HttpServletResponse response,@RequestParam(name = "path",required = true) String path) {
if (path.startsWith("http://")) { // 如果是绝对路径,则需要通过远程下载文件流
try {
return writeFile(response, "cert.pdf", batchService.downNetFile(path), "multipart/form-data");
} catch (Exception e) {
LOGGER.warn("writeFile find wrong.", e);
}
return writeErrorInfo(response, "文件不存在");
} else { // 本地下载
File file = batchService.localDownload(path);
try {
return writeFile(response, file.getName(), new FileInputStream(file), "multipart/form-data");
} catch (Exception e) {
LOGGER.warn("writeFile find wrong.", e);
return writeErrorInfo(response, "文件不存在");
}
}
}
/**
* 导出表格的excel表格
*
* @param param 批量导出的业务类型
* @return
*/
@RequestMapping(value = "/export")
public String export(HttpServletResponse response, @RequestBody ExportParam param) {
File file;
try {
file = batchService.batchExport(param);
} catch (ServiceException e) {
LOGGER.warn("batchExport find wrong.", e);
return writeErrorInfo(response, e.getMessage());
} catch (Exception e) {
LOGGER.warn("batchExport find wrong.", e);
return writeErrorInfo(response, "<p>导出记录条数太多</p>");
}
try {
return writeFile(response, file.getName(), new FileInputStream(file), "application/vnd.ms-excel");
} catch (FileNotFoundException e) {
LOGGER.warn("writeFile find wrong.", e);;
return writeErrorInfo(response, "文件不存在");
}
}
@RequestMapping(value = "/export.do")
public String exportForGet(HttpServletResponse response, ExportParam param) {
File file;
try {
file = batchService.batchExport(param);
return writeFile(response, file.getName(), new FileInputStream(file), "application/vnd.ms-excel");
} catch (ServiceException e) {
LOGGER.warn("exportForGet find wrong.", e);
return writeErrorInfo(response, e.getMessage());
} catch (Exception e) {
LOGGER.warn("exportForGet find wrong.", e);
return writeErrorInfo(response, "<p>导出记录条数太多</p>");
}
}
private String writeErrorInfo(HttpServletResponse response, String string) {
PrintWriter writer = null;
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-type", "text/html;charset=UTF-8");
try {
writer = response.getWriter();
writer.print(new String(string.getBytes("UTF-8"), "UTF-8"));
writer.flush();
} catch (IOException e) {
LOGGER.warn("os.write find wrong.", e);
} finally {
if (null != writer) {
try {
writer.close();
} catch (Exception e) {
LOGGER.warn("os.close find wrong.", e);
}
}
}
return null;
}
/**
* 向 response 中,写入文件
*/
private String writeFile(HttpServletResponse response, File file, String contentType) {
checkNotNull(file, "file is null!");
String msg = null;
response.setCharacterEncoding("utf-8");
response.setContentType(contentType);
response.setHeader("Content-Disposition", "attachment;fileName=" + file.getName());
try {
InputStream inputStream = new FileInputStream(file);
OutputStream os = response.getOutputStream();
byte[] b = new byte[2048];
int length;
while ((length = inputStream.read(b)) > 0) {
os.write(b, 0, length);
}
os.close();
inputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
msg = "File not found";
} catch (IOException e) {
e.printStackTrace();
msg = "Read file error";
}
return msg;
}
/**
* 向 response 中,写入文件
*/
private String writeFile(HttpServletResponse response, String fileName, InputStream inputStream, String contentType) {
checkNotNull(inputStream, "inputStream is null!");
String msg = null;
response.setCharacterEncoding("utf-8");
response.setContentType(contentType);
response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
try {
OutputStream os = response.getOutputStream();
byte[] b = new byte[2048];
int length;
while ((length = inputStream.read(b)) > 0) {
os.write(b, 0, length);
}
os.close();
inputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
msg = "File not found";
} catch (IOException e) {
e.printStackTrace();
msg = "Read file error";
}
return msg;
}
}
... ...
... ... @@ -29,14 +29,4 @@ public interface IBusinessExportService {
default List<? extends Object> batchExport(String confStr) throws PlatformException {
return Lists.newArrayList();
}
/**
* 批量导出数据
* @param confStr 条件字符串,json格式
* @return 导出的数据集
*/
default List<? extends Object> batchExportEx(String confStr, int startIndex, int size) throws PlatformException {
return Lists.newArrayList();
}
}
... ...
package com.yoho.ufo.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.yoho.ufo.cache.CommonLocalCacheService;
import com.yoho.ufo.cache.LocalCacheKeyBuilder;
import com.yoho.ufo.constants.Constant;
import com.yoho.ufo.exception.PlatformException;
import com.yoho.ufo.service.IBusinessExportService;
import com.yoho.ufo.service.IBusinessImportService;
import com.yoho.ufo.service.model.ExportParam;
import com.yoho.ufo.util.XlsxUtils;
import org.apache.commons.collections.CollectionUtils;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
... ... @@ -26,16 +27,16 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.yoho.ufo.cache.CommonLocalCacheService;
import com.yoho.ufo.cache.LocalCacheKeyBuilder;
import com.yoho.ufo.constants.Constant;
import com.yoho.ufo.exception.PlatformException;
import com.yoho.ufo.service.IBusinessExportService;
import com.yoho.ufo.service.IBusinessImportService;
import com.yoho.ufo.service.model.ExportParam;
import com.yoho.ufo.util.XlsxUtils;
/**
* 批量操作服务
... ... @@ -224,38 +225,6 @@ public class BatchService {
return localFile;
}
// 分批查询出导出列表,追加的方式写到表格中
@SuppressWarnings("unchecked")
public File batchExportEx(ExportParam param) throws Exception {
logger.info("batchExportEx req is {}", param);
if (!exportServiceMap.containsKey(param.getType())) {
logger.error("invalid export type: {}", param.getType());
throw new PlatformException("无效的导出类型:" + param.getType(), 400);
}
IBusinessExportService service = (IBusinessExportService)exportServiceMap.get(param.getType());
// 生成需要保存的临时文件
File localFile = createLocalFileEx();
logger.info("batch batchExportEx path is {}", localFile.getAbsolutePath());
int startIndex = 0;
// 生成Excel表格
XlsxUtils.writeEx(localFile, service.batchExportEx(param.getQueryConf(), startIndex , TIME_PROCESS), service.getDataClass());
List<? extends Object> batchExportEx;
while (true) {
startIndex += TIME_PROCESS;
batchExportEx = service.batchExportEx(param.getQueryConf(), startIndex , TIME_PROCESS);
if (CollectionUtils.isEmpty(batchExportEx)) {
break;
}
XlsxUtils.appendExcel(localFile, batchExportEx, service.getDataClass());
logger.info("appendExcel index is {}", startIndex);
}
return localFile;
}
private File createLocalFileEx() {
... ...
package com.yoho.ufo.dal;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.yoho.ufo.model.ChannelSkuCompare;
import com.yoho.ufo.model.ChannelSkuCompareReq;
/**
* Created by caoyan.
*/
public interface ChannelSkuCompareMapper {
int selectTotalByCondition(@Param("channelSkuCompareReq") ChannelSkuCompareReq req);
List<ChannelSkuCompare> selectByCondition(@Param("channelSkuCompareReq") ChannelSkuCompareReq req);
ChannelSkuCompare selectById(@Param("id") Integer id);
int updateChangeStatusById(@Param("id")Integer id, @Param("status")Integer status, @Param("changeStatusTime") Integer changeStatusTime,
@Param("changeStatusUid")Integer changeStatusUid);
}
... ...
... ... @@ -2,7 +2,6 @@ package com.yoho.ufo.dal;
import java.util.List;
import com.yoho.ufo.model.brand.Brand;
import org.apache.ibatis.annotations.Param;
import com.yoho.ufo.dal.model.Product;
... ... @@ -72,4 +71,6 @@ public interface ProductMapper {
int selectProductStorageCount(@Param("product")Product product);
List<Product> selectProductStorageList(@Param("product")Product product, @Param("start")int start, @Param("rows")Integer rows);
List<Product> selectByProductName(@Param("productName") String productName);
}
\ No newline at end of file
... ...
package com.yoho.ufo.dal;
import com.yoho.ufo.dal.model.Storage;
import java.math.BigDecimal;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.yoho.ufo.dal.model.Storage;
public interface StorageMapper {
int deleteByPrimaryKey(Integer id);
... ... @@ -25,4 +27,7 @@ public interface StorageMapper {
List<Storage> selectByIds(@Param("storageIdList")List<Integer> storageIdList);
int updateSizeId(@Param("newSizeId")Integer newSizeId, @Param("oldSizeIdList")List<Integer> oldSizeIdList);
int updateSuggestPriceById(@Param("id") Integer id, @Param("suggestLowPrice") BigDecimal suggestLowPrice,
@Param("suggestHighPrice") BigDecimal suggestHighPrice);
}
\ No newline at end of file
... ...
... ... @@ -2,7 +2,6 @@ package com.yoho.ufo.dal;
import java.util.List;
import com.yoho.ufo.dal.model.Product;
import org.apache.ibatis.annotations.Param;
import com.yoho.ufo.dal.model.StoragePrice;
... ... @@ -47,4 +46,6 @@ public interface StoragePriceMapper {
// 根据SKup等信息查询其所属productId列表 最多查询出最新的300 productId
List<Integer> selectProductIdsBySkupInfo(@Param("storagePrice")StoragePrice storagePrice);
List<StoragePrice> selectMinPriceByStorageIdList(@Param("list") List<Integer> storageIdList);
}
\ No newline at end of file
... ...
... ... @@ -98,4 +98,10 @@ public interface UfoSizeMapper {
int getBindCount(@Param("size") Size size, @Param("sortIdList")List<Integer> sortIdList);
/**
* 根据尺码名称查询尺码对象
* @param sizeName
* @return
*/
List<Size> selectBySizeName(@Param("sizeName") String sizeName);
}
... ...
package com.yoho.ufo.dal.model;
import java.math.BigDecimal;
public class Storage {
private Integer id;
... ... @@ -14,6 +16,10 @@ public class Storage {
private Integer updateTime;
private Integer createTime;
private BigDecimal suggestLowPrice;
private BigDecimal suggestHighPrice;
public Integer getId() {
return id;
... ... @@ -70,4 +76,21 @@ public class Storage {
public void setCreateTime(Integer createTime) {
this.createTime = createTime;
}
public BigDecimal getSuggestLowPrice() {
return suggestLowPrice;
}
public void setSuggestLowPrice(BigDecimal suggestLowPrice) {
this.suggestLowPrice = suggestLowPrice;
}
public BigDecimal getSuggestHighPrice() {
return suggestHighPrice;
}
public void setSuggestHighPrice(BigDecimal suggestHighPrice) {
this.suggestHighPrice = suggestHighPrice;
}
}
\ No newline at end of file
... ...
package com.yoho.ufo.model;
import java.io.Serializable;
import java.math.BigDecimal;
/**
* 爬虫跟毒价
*/
public class ChannelSkuCompare implements Serializable {
private static final long serialVersionUID = 1613330087543482073L;
/**
* 主键
*/
private Integer id;
/**
* 商品ID
*/
private Integer productId;
private Integer sku;
private Integer sizeId;
private BigDecimal channelPrice;
private BigDecimal lowRate;
private BigDecimal highRate;
private Integer updateTime;
private String channelUrl;
private Integer status;
private Integer changeStatusTime;
private Integer changeStatusUid;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getProductId() {
return productId;
}
public void setProductId(Integer productId) {
this.productId = productId;
}
public Integer getSku() {
return sku;
}
public void setSku(Integer sku) {
this.sku = sku;
}
public Integer getSizeId() {
return sizeId;
}
public void setSizeId(Integer sizeId) {
this.sizeId = sizeId;
}
public BigDecimal getChannelPrice() {
return channelPrice;
}
public void setChannelPrice(BigDecimal channelPrice) {
this.channelPrice = channelPrice;
}
public BigDecimal getLowRate() {
return lowRate;
}
public void setLowRate(BigDecimal lowRate) {
this.lowRate = lowRate;
}
public BigDecimal getHighRate() {
return highRate;
}
public void setHighRate(BigDecimal highRate) {
this.highRate = highRate;
}
public Integer getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Integer updateTime) {
this.updateTime = updateTime;
}
public String getChannelUrl() {
return channelUrl;
}
public void setChannelUrl(String channelUrl) {
this.channelUrl = channelUrl;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Integer getChangeStatusTime() {
return changeStatusTime;
}
public void setChangeStatusTime(Integer changeStatusTime) {
this.changeStatusTime = changeStatusTime;
}
public Integer getChangeStatusUid() {
return changeStatusUid;
}
public void setChangeStatusUid(Integer changeStatusUid) {
this.changeStatusUid = changeStatusUid;
}
@Override
public String toString() {
return "ProductPool{" +
"id=" + id +
", productId='" + productId +
", sku=" + sku +
", sizeId=" + sizeId +
", channelPrice=" + channelPrice +
", lowRate=" + lowRate +
", highRate=" + highRate +
", updateTime=" + updateTime +
", channelUrl=" + channelUrl +
", status=" + status +
", changeStatusTime=" + changeStatusTime +
", changeStatusUid=" + changeStatusUid +
'}';
}
}
... ...
package com.yoho.ufo.model;
import java.util.List;
import com.alibaba.fastjson.JSONObject;
import com.yoho.ufo.service.model.PageRequestBO;
public class ChannelSkuCompareReq extends PageRequestBO {
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer id;
private Integer sku;
private Integer productId;
private Integer status;
private String productName;
private String sizeName;
private List<Integer> productIdList;
private List<Integer> sizeIdList;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getSku() {
return sku;
}
public void setSku(Integer sku) {
this.sku = sku;
}
public Integer getProductId() {
return productId;
}
public void setProductId(Integer productId) {
this.productId = productId;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getSizeName() {
return sizeName;
}
public void setSizeName(String sizeName) {
this.sizeName = sizeName;
}
public List<Integer> getProductIdList() {
return productIdList;
}
public void setProductIdList(List<Integer> productIdList) {
this.productIdList = productIdList;
}
public List<Integer> getSizeIdList() {
return sizeIdList;
}
public void setSizeIdList(List<Integer> sizeIdList) {
this.sizeIdList = sizeIdList;
}
public String toString() {
return JSONObject.toJSONString(this);
}
}
... ...
package com.yoho.ufo.model;
import com.yoho.ufo.annotation.BatchExportField;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
@Data
@EqualsAndHashCode(callSuper=false)
@ToString
public class ChannelSkuCompareRspBo {
private Integer id;
@BatchExportField(name = "商品编码")
private Integer productId;
@BatchExportField(name = "SKU")
private Integer sku;
@BatchExportField(name = "商品名称")
private String productName;
@BatchExportField(name = "尺码")
private String sizeName;
@BatchExportField(name = "毒当前价(A)")
private String channelPrice;
@BatchExportField(name = "价格低于(a%)")
private String lowRate;
@BatchExportField(name = "价格高于(b%)")
private String highRate;
@BatchExportField(name = "对标毒的价格区间A(1-a%)~A(1+b%)")
private String channelPriceRange;
@BatchExportField(name = "平台建议售价")
private String suggestPriceRange;
@BatchExportField(name = "UFO当前价")
private String ufoCurrentPrice;
@BatchExportField(name = "UFO价格红线")
private String ufoMinPrice;
@BatchExportField(name = "状态")
private String statusStr;
private Integer status;
private String channelUrl;
}
... ...
<?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.yoho.ufo.dal.ChannelSkuCompareMapper">
<resultMap id="BaseResultMap" type="com.yoho.ufo.model.ChannelSkuCompare">
<result column="id" property="id" jdbcType="INTEGER" />
<result column="product_id" property="productId" jdbcType="INTEGER" />
<result column="sku" property="sku" jdbcType="INTEGER" />
<result column="size_id" property="sizeId" jdbcType="INTEGER" />
<result column="channel_price" property="channelPrice" jdbcType="DECIMAL" />
<result column="low_rate" property="lowRate" jdbcType="DECIMAL" />
<result column="high_rate" property="highRate" jdbcType="DECIMAL" />
<result column="update_time" property="updateTime" jdbcType="INTEGER" />
<result column="channel_url" property="channelUrl" jdbcType="VARCHAR" />
<result column="status" property="status" jdbcType="INTEGER" />
<result column="change_status_time" property="changeStatusTime" jdbcType="INTEGER" />
<result column="change_status_uid" property="changeStatusUid" jdbcType="INTEGER" />
</resultMap>
<sql id="Base_Column_List">
id, product_id, sku, size_id, channel_price, low_rate, high_rate, update_time, channel_url,
status, change_status_time, change_status_uid
</sql>
<select id="selectById" resultMap="BaseResultMap">
select <include refid="Base_Column_List" />
from channel_sku_compare
where id=#{id}
</select>
<sql id="Query_Sql" >
<if test="channelSkuCompareReq.sku != null and channelSkuCompareReq.sku != 0 ">
and a.sku = #{channelSkuCompareReq.sku}
</if>
<if test="channelSkuCompareReq.productId != null and channelSkuCompareReq.productId != 0 ">
and a.product_id = #{channelSkuCompareReq.productId}
</if>
<if test="channelSkuCompareReq.productIdList != null and channelSkuCompareReq.productIdList.size()>0">
and a.product_id in
<foreach collection="channelSkuCompareReq.productIdList" item="productId" open="(" close=")" separator=",">
#{productId}
</foreach>
</if>
<if test="channelSkuCompareReq.sizeIdList != null and channelSkuCompareReq.sizeIdList.size()>0">
and a.size_id in
<foreach collection="channelSkuCompareReq.sizeIdList" item="sizeId" open="(" close=")" separator=",">
#{sizeId}
</foreach>
</if>
<if test="channelSkuCompareReq.status != null">
and a.status = #{channelSkuCompareReq.status}
</if>
</sql>
<select id="selectTotalByCondition" resultType="java.lang.Integer" parameterType="com.yoho.ufo.model.ChannelSkuCompareReq">
select count(a.id)
from channel_sku_compare a
where 1=1
<include refid="Query_Sql" />
</select>
<select id="selectByCondition" resultMap="BaseResultMap" parameterType="com.yoho.ufo.model.ChannelSkuCompareReq">
select a.*
from channel_sku_compare a
where 1=1
<include refid="Query_Sql" />
order by a.product_id desc, a.sku asc
<if test="channelSkuCompareReq.start!=null and channelSkuCompareReq.size != null">
limit #{channelSkuCompareReq.start},#{channelSkuCompareReq.size}
</if>
</select>
<update id="updateChangeStatusById">
update channel_sku_compare set status=#{status}, change_status_time=#{changeStatusTime}, change_status_uid=#{changeStatusUid}
where id=#{id}
</update>
</mapper>
\ No newline at end of file
... ...
... ... @@ -224,4 +224,7 @@
</foreach>
</if>
</sql>
<select id="selectByProductName" resultMap="BaseResultMap">
select id, product_name from product where product_name like "%"#{productName}"%"
</select>
</mapper>
\ No newline at end of file
... ...
... ... @@ -9,6 +9,8 @@
<result column="storage_num" jdbcType="INTEGER" property="storageNum" />
<result column="update_time" jdbcType="INTEGER" property="updateTime" />
<result column="create_time" jdbcType="INTEGER" property="createTime" />
<result column="suggest_low_price" jdbcType="DECIMAL" property="suggestLowPrice" />
<result column="suggest_high_price" jdbcType="DECIMAL" property="suggestHighPrice" />
</resultMap>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from storage
... ... @@ -58,7 +60,7 @@
where id = #{storageId,jdbcType=INTEGER} and storage_num = #{oldStorageNum,jdbcType=INTEGER}
</update>
<select id="selectByIds" resultMap="BaseResultMap">
select id, product_id, goods_id, size_id, storage_num, update_time, create_time
select id, product_id, goods_id, size_id, storage_num, update_time, create_time, suggest_low_price, suggest_high_price
from storage where id in
<foreach item="item" index="index" collection="storageIdList" open="(" separator="," close=")">
#{item}
... ... @@ -70,5 +72,7 @@
<foreach item="item" index="index" collection="oldSizeIdList" open="(" separator="," close=")">
#{item}
</foreach>
<update id="updateSuggestPriceById">
update storage set suggest_low_price=#{suggestLowPrice}, suggest_high_price=#{suggestHighPrice} where id=#{id}
</update>
</mapper>
\ No newline at end of file
... ...
... ... @@ -161,4 +161,13 @@
</if>
order by create_time desc limit 300
</select>
<select id="selectMinPriceByStorageIdList" resultMap="BaseResultMap">
select storage_id, min(price) as price
from storage_price where storage_id in
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
#{item}
</foreach>
and status = 1 group by storage_id
</select>
</mapper>
\ No newline at end of file
... ...
... ... @@ -192,4 +192,7 @@
select * from sort_size ss where id = #{id}
</select>
<select id="selectBySizeName" resultMap="sizeMap">
select id, size_name from size where size_name = #{sizeName}
</select>
</mapper>
\ No newline at end of file
... ...
package com.yoho.ufo.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yoho.ufo.model.ChannelSkuCompareReq;
import com.yoho.ufo.model.ChannelSkuCompareRspBo;
import com.yoho.ufo.service.IChannelSkuCompareService;
import com.yoho.ufo.service.model.ApiResponse;
import com.yoho.ufo.service.model.PageResponseBO;
@RestController
@RequestMapping(value = "/channelSkuCompare")
public class ChannelSkuCompareController {
private static final Logger LOGGER = LoggerFactory.getLogger(ChannelSkuCompareController.class);
@Autowired
private IChannelSkuCompareService channelSkuCompareService;
@RequestMapping(value = "/queryList")
public ApiResponse queryList(ChannelSkuCompareReq req) {
LOGGER.info("queryOrderList in. req is {}", req);
PageResponseBO<ChannelSkuCompareRspBo> result = channelSkuCompareService.queryList(req);
return new ApiResponse.ApiResponseBuilder().code(200).message("查询成功").data(result).build();
}
@RequestMapping(value = "/updateSuggestPrice")
public ApiResponse updateSuggestPrice(ChannelSkuCompareReq req) {
LOGGER.info("updateSuggestPrice in. req is {}", req);
int result = channelSkuCompareService.updateSuggestPrice(req);
if(result > 0) {
return new ApiResponse.ApiResponseBuilder().code(200).message("更新成功").data(result).build();
}else {
return new ApiResponse.ApiResponseBuilder().code(500).message("更新失败").data(result).build();
}
}
}
... ...
package com.yoho.ufo.service;
import com.yoho.ufo.model.ChannelSkuCompareReq;
import com.yoho.ufo.model.ChannelSkuCompareRspBo;
import com.yoho.ufo.service.model.PageResponseBO;
/**
* @author caoyan
*/
public interface IChannelSkuCompareService {
PageResponseBO<ChannelSkuCompareRspBo> queryList(ChannelSkuCompareReq req);
int updateSuggestPrice(ChannelSkuCompareReq req);
}
... ...
package com.yoho.ufo.service.impl;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.yoho.core.common.utils.DateUtil;
import com.yoho.error.exception.ServiceException;
import com.yoho.ufo.dal.ChannelSkuCompareMapper;
import com.yoho.ufo.dal.ProductMapper;
import com.yoho.ufo.dal.StorageMapper;
import com.yoho.ufo.dal.StoragePriceMapper;
import com.yoho.ufo.dal.UfoSizeMapper;
import com.yoho.ufo.dal.model.Product;
import com.yoho.ufo.dal.model.Storage;
import com.yoho.ufo.dal.model.StoragePrice;
import com.yoho.ufo.model.ChannelSkuCompare;
import com.yoho.ufo.model.ChannelSkuCompareReq;
import com.yoho.ufo.model.ChannelSkuCompareRspBo;
import com.yoho.ufo.model.commoditybasicrole.size.Size;
import com.yoho.ufo.service.IBusinessExportService;
import com.yoho.ufo.service.IChannelSkuCompareService;
import com.yoho.ufo.service.model.PageResponseBO;
/**
* @author caoyan
*/
@Service("channelSkuCompareServiceImpl")
public class ChannelSkuCompareServiceImpl implements IChannelSkuCompareService, IBusinessExportService {
private static final Logger LOGGER = LoggerFactory.getLogger(ChannelSkuCompareServiceImpl.class);
@Autowired
private ChannelSkuCompareMapper channelSkuCompareMapper;
@Autowired
private ProductMapper productMapper;
@Autowired
private UfoSizeMapper ufoSizeMapper;
@Autowired
private StorageMapper storageMapper;
@Autowired
private StoragePriceMapper storagePriceMapper;
@Override
public PageResponseBO<ChannelSkuCompareRspBo> queryList(ChannelSkuCompareReq req) {
LOGGER.info("queryList in. req is {}", req);
if(!checkAndBuildParam(req)) {
return null;
}
int total = channelSkuCompareMapper.selectTotalByCondition(req);
if(total == 0) {
return null;
}
List<ChannelSkuCompare> cscList = channelSkuCompareMapper.selectByCondition(req);
if(CollectionUtils.isEmpty(cscList)) {
return null;
}
//查询商品名称
List<Integer> productIdList = cscList.stream().map(ChannelSkuCompare::getProductId).collect(Collectors.toList());
List<Product> productList = productMapper.selectProductListByIds(productIdList);
if(CollectionUtils.isEmpty(productList)) {
return null;
}
Map<Integer, Product> productMap = productList.stream().collect(Collectors.toMap(Product::getId, p->p));
//查询尺码名称
List<Integer> sizeIdList = cscList.stream().map(ChannelSkuCompare::getSizeId).collect(Collectors.toList());
List<Size> sizeList = ufoSizeMapper.selectByIdList(sizeIdList);
Map<Integer, String> sizeIdNameMap = sizeList.stream().collect(Collectors.toMap(Size::getId, Size::getSizeName));
//查询平台建议价
List<Integer> skuList = cscList.stream().map(ChannelSkuCompare::getSku).collect(Collectors.toList());
List<Storage> storageList = storageMapper.selectByIds(skuList);
Map<Integer, Storage> storageMap = storageList.stream().collect(Collectors.toMap(Storage::getId, s->s));
//计算UFO当前价,可售sku最低价
List<StoragePrice> storagePriceList = storagePriceMapper.selectMinPriceByStorageIdList(skuList);
Map<Integer, BigDecimal> storageMinPriceMap = storagePriceList.stream().collect(Collectors.toMap(StoragePrice::getStorageId, StoragePrice::getPrice));
List<ChannelSkuCompareRspBo> respList = convertToResp(cscList, productMap, sizeIdNameMap, storageMap, storageMinPriceMap);
PageResponseBO<ChannelSkuCompareRspBo> result=new PageResponseBO<>();
result.setList(respList);
result.setPage(req.getPage());
result.setSize(req.getSize());
result.setTotal(total);
return result;
}
@Override
public int updateSuggestPrice(ChannelSkuCompareReq req) {
if(null == req.getStatus() || req.getStatus().intValue() != 0 || null == req.getId()) {
return 0;
}
ChannelSkuCompare csc = channelSkuCompareMapper.selectById(req.getId());
if(null == csc) {
LOGGER.error("id not exist! id is {}", req.getId());
return 0;
}
Storage storage = storageMapper.selectByPrimaryKey(csc.getSku());
if(null == storage) {
LOGGER.error("storageId not exist! storageId is {}", csc.getSku());
return 0;
}
//更新
BigDecimal suggestLowPrice = csc.getChannelPrice().multiply(BigDecimal.valueOf(1).subtract(csc.getLowRate()));
BigDecimal suggestHighPrice = csc.getChannelPrice().multiply(BigDecimal.valueOf(1).add(csc.getHighRate()));
//记录变更人信息
channelSkuCompareMapper.updateChangeStatusById(req.getId(), req.getStatus(), DateUtil.getCurrentTimeSecond(), new UserHelper().getUserId());
return storageMapper.updateSuggestPriceById(csc.getSku(), suggestLowPrice, suggestHighPrice);
}
@Override
public Class getDataClass() {
return ChannelSkuCompareRspBo.class;
}
@Override
public List<? extends Object> batchExport(String confStr) {
try {
ChannelSkuCompareReq request = new ChannelSkuCompareReq();
if(StringUtils.isNotEmpty(confStr)) {
request = JSONObject.parseObject(confStr, ChannelSkuCompareReq.class);
}
request.setSize(5000);
PageResponseBO<ChannelSkuCompareRspBo> pageResponseBO = queryList(request);
List<ChannelSkuCompareRspBo> boList = pageResponseBO.getList();
if (CollectionUtils.isEmpty(boList)) {
throw new ServiceException(400, "没有要导出的数据");
}
return boList;
} catch (JSONException e) {
LOGGER.warn("parse confStr error: confStr {}, e {}", confStr, e);
throw new ServiceException(400, "传入数据格式错误");
} catch (com.yoho.error.exception.ServiceException e) {
LOGGER.warn("make url error params is confStr {}, e {}", confStr, e);
throw new ServiceException(e.getCode(), e.getErrorMessage());
}
}
private List<ChannelSkuCompareRspBo> convertToResp(List<ChannelSkuCompare> cscList, Map<Integer, Product> productMap,
Map<Integer, String> sizeIdNameMap, Map<Integer, Storage> storageMap, Map<Integer, BigDecimal> storageMinPriceMap){
List<ChannelSkuCompareRspBo> boList = Lists.newArrayList();
for(ChannelSkuCompare csc : cscList) {
ChannelSkuCompareRspBo bo = new ChannelSkuCompareRspBo();
bo.setId(csc.getId());
bo.setProductId(csc.getProductId());
bo.setSku(csc.getSku());
bo.setProductName(productMap.get(csc.getProductId()).getProductName());
bo.setSizeName(sizeIdNameMap.get(csc.getSizeId()));
bo.setChannelPrice(getFormatPrice(csc.getChannelPrice()));
bo.setChannelUrl(csc.getChannelUrl());
bo.setLowRate(csc.getLowRate().multiply(BigDecimal.valueOf(100)) + "%");
bo.setHighRate(csc.getHighRate().multiply(BigDecimal.valueOf(100)) + "%");
BigDecimal channelLowPrice = csc.getChannelPrice().multiply(BigDecimal.valueOf(1).subtract(csc.getLowRate()));
BigDecimal channelHighPrice = csc.getChannelPrice().multiply(BigDecimal.valueOf(1).add(csc.getHighRate()));
bo.setChannelPriceRange(getFormatPrice(channelLowPrice) + "~" + getFormatPrice(channelHighPrice));
bo.setUfoCurrentPrice(getFormatPrice(storageMinPriceMap.get(csc.getSku())));
BigDecimal ufoMinPrice = productMap.get(csc.getProductId()).getMinPrice();
bo.setUfoMinPrice(getFormatPrice(ufoMinPrice));
//毒当前价低于UFO价格红线则为异常,那么显示上回的建议价
BigDecimal suggestLowPrice = storageMap.get(csc.getSku()).getSuggestLowPrice();
BigDecimal suggestHighPrice = storageMap.get(csc.getSku()).getSuggestHighPrice();
if(null != suggestLowPrice && null != suggestHighPrice) {
bo.setSuggestPriceRange(getFormatPrice(suggestLowPrice) + "~" + getFormatPrice(suggestHighPrice));
}
bo.setStatus(csc.getStatus());
bo.setStatusStr(bo.getStatus().intValue()==0 ? "正常" : "异常");
boList.add(bo);
}
return boList;
}
private static String getFormatPrice(BigDecimal price) {
return String.format("%.2f", price.doubleValue());
}
private boolean checkAndBuildParam(ChannelSkuCompareReq req) {
if(StringUtils.isNotEmpty(req.getProductName())) {
List<Product> productList = productMapper.selectByProductName(req.getProductName());
if(CollectionUtils.isEmpty(productList)) {
return false;
}else {
req.setProductIdList(productList.stream().map(Product::getId).collect(Collectors.toList()));
}
}
if(StringUtils.isNotEmpty(req.getSizeName())) {
List<Size> sizeList = ufoSizeMapper.selectBySizeName(req.getSizeName());
if(CollectionUtils.isEmpty(sizeList)) {
return false;
}else {
req.setSizeIdList(sizeList.stream().map(Size::getId).collect(Collectors.toList()));
}
}
return true;
}
}
... ...
... ... @@ -91,5 +91,7 @@
<util:map id="batchExportBusiness" key-type="java.lang.String"
value-type="com.yoho.ufo.service.IBusinessExportService">
<entry key="couponServiceImpl" value-ref="couponServiceImpl"></entry>
<entry key="channelSkuCompareServiceImpl" value-ref="channelSkuCompareServiceImpl"/>
</util:map>
</beans>
\ No newline at end of file
... ...