Authored by caoyan

自助上架

package com.yohoufo.product.controller;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.yohoufo.common.ApiResponse;
import com.yohoufo.dal.product.model.ProductSelfShelves;
import com.yohoufo.product.service.SelfShelvesService;
@RestController
public class SelfShelvesController {
private static final Logger LOGGER = LoggerFactory.getLogger(SelfShelvesController.class);
@Autowired
private SelfShelvesService selfShelvesService;
@RequestMapping(params = "method=ufo.selfShelves.save")
@ResponseBody
public ApiResponse save(@RequestParam(value = "uid", required = true) Integer uid,
@RequestParam(value = "brand", required = true) String brand,
@RequestParam(value = "product_name", required = true) String productName,
@RequestParam(value = "price", required = true) String price,
@RequestParam(value = "sale_time", required = true) String saleTime,
@RequestParam(value = "product_code", required = true) String productCode,
@RequestParam(value = "imageList", required = true) List<String> imageList) {
LOGGER.info("ufo.selfShelves.save in. uid is {}, brand is {}, productName is {}, price is {}, "
+ "saleTime is {}, productCode is {}, imageList is {}", uid, brand, productName, price, saleTime, productCode, imageList);
if(StringUtils.isEmpty(brand) || StringUtils.isEmpty(productName) || StringUtils.isEmpty(price)
|| StringUtils.isEmpty(saleTime) || StringUtils.isEmpty(productCode) || CollectionUtils.isEmpty(imageList)) {
return new ApiResponse.ApiResponseBuilder().code(500).message("有参数为空").build();
}
if(null == uid || 0 == uid.intValue()) {
return new ApiResponse.ApiResponseBuilder().code(401).message("uid为空,请登录").build();
}
ProductSelfShelves pss = new ProductSelfShelves();
pss.setUid(uid);
pss.setBrand(brand);
pss.setProductName(productName);
pss.setProductCode(productCode);
int result = selfShelvesService.save(pss, price, saleTime, imageList);
if(result>0) {
return new ApiResponse.ApiResponseBuilder().code(200).message("保存成功").build();
}else {
return new ApiResponse.ApiResponseBuilder().code(500).message("保存失败").build();
}
}
}
... ...
package com.yohoufo.product.service;
import java.util.List;
import com.yohoufo.dal.product.model.ProductSelfShelves;
public interface SelfShelvesService {
int save(ProductSelfShelves pss, String price, String saleTime, List<String> imageList);
}
... ...
package com.yohoufo.product.service.impl;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yohoufo.common.exception.UfoServiceException;
import com.yohoufo.common.utils.DateUtil;
import com.yohoufo.dal.product.ProductSelfShelvesMapper;
import com.yohoufo.dal.product.ProductSelfShelvesPicMapper;
import com.yohoufo.dal.product.model.ProductSelfShelves;
import com.yohoufo.product.service.SelfShelvesService;
/**
* @author caoyan
* @date 2019/3/19
*/
@Service
public class SelfShelvesServiceImpl implements SelfShelvesService {
private static final Logger LOGGER = LoggerFactory.getLogger(SelfShelvesServiceImpl.class);
@Autowired
private ProductSelfShelvesMapper productSelfShelvesMapper;
@Autowired
private ProductSelfShelvesPicMapper productSelfShelvesPicMapper;
@Override
public int save(ProductSelfShelves pss, String price, String saleTime, List<String> imageList) {
pss.setPrice(BigDecimal.valueOf(Long.parseLong(price)));
pss.setSaleTime((int)convertDateToLong(saleTime));
pss.setCreateTime(DateUtil.getCurrentTimeSecond());
pss.setUpdateTime(DateUtil.getCurrentTimeSecond());
LOGGER.info("insert data is {}", pss);
int num = productSelfShelvesMapper.insert(pss);
Integer selfShelvesId = pss.getId();
if(null == selfShelvesId) {
return 0;
}
//插入图片
productSelfShelvesPicMapper.insertBatch(selfShelvesId, imageList);
return num;
}
private long convertDateToLong(String date) {
if(date.equals("0")) {
return 0;
}
SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date dateDate = new Date();
try {
dateDate = formatter.parse(date + " 00:00:00");
} catch (ParseException e) {
throw new UfoServiceException(500, "时间格式有误");
}
return dateDate.getTime() / 1000;
}
}
... ...
... ... @@ -40,6 +40,8 @@ datasources:
- com.yohoufo.dal.product.TransferRecordsHistoryMapper
- com.yohoufo.dal.product.ProductSalesMapper
- com.yohoufo.dal.product.ProductLimitSaleMapper
- com.yohoufo.dal.product.ProductSelfShelvesMapper
- com.yohoufo.dal.product.ProductSelfShelvesPicMapper
ufo_order:
... ...
... ... @@ -40,6 +40,8 @@ datasources:
- com.yohoufo.dal.product.TransferRecordsHistoryMapper
- com.yohoufo.dal.product.ProductLimitSaleMapper
- com.yohoufo.dal.product.ProductSalesMapper
- com.yohoufo.dal.product.ProductSelfShelvesMapper
- com.yohoufo.dal.product.ProductSelfShelvesPicMapper
ufo_order:
servers:
... ...