package com.yeshi.fanli.service.impl.goods;
|
|
import java.util.Date;
|
|
import javax.annotation.Resource;
|
|
import org.springframework.stereotype.Service;
|
|
import com.yeshi.fanli.dao.mybatis.goods.CommonGoodsMapper;
|
import com.yeshi.fanli.entity.goods.CommonGoods;
|
import com.yeshi.fanli.exception.goods.CommonGoodsException;
|
import com.yeshi.fanli.service.inter.goods.CommonGoodsService;
|
import com.yeshi.fanli.util.StringUtil;
|
|
@Service
|
public class CommonGoodsServiceImpl implements CommonGoodsService {
|
|
@Resource
|
private CommonGoodsMapper commonGoodsMapper;
|
|
/**
|
* 验证数据正确性
|
*
|
* @param commonGoods
|
* @throws CommonGoodsException
|
*/
|
private void filterCommonGoods(CommonGoods commonGoods) throws CommonGoodsException {
|
// 判断信息是否完成
|
if (commonGoods == null)
|
throw new CommonGoodsException(1, "商品信息为空");
|
if (commonGoods.getGoodsId() == null)
|
throw new CommonGoodsException(1, "商品信息不完整");
|
if (commonGoods.getGoodsType() == null)
|
throw new CommonGoodsException(1, "商品信息不完整");
|
if (StringUtil.isNullOrEmpty(commonGoods.getPicture()))
|
throw new CommonGoodsException(1, "商品信息不完整");
|
if (commonGoods.getPrice() == null)
|
throw new CommonGoodsException(1, "商品信息不完整");
|
if (commonGoods.getRate() == null)
|
throw new CommonGoodsException(1, "商品信息不完整");
|
if (commonGoods.getSales() == null)
|
throw new CommonGoodsException(1, "商品信息不完整");
|
if (commonGoods.getSellerId() == null)
|
throw new CommonGoodsException(1, "商品信息不完整");
|
if (commonGoods.getSellerName() == null)
|
throw new CommonGoodsException(1, "商品信息不完整");
|
if (commonGoods.getShopType() == null)
|
throw new CommonGoodsException(1, "商品信息不完整");
|
if (StringUtil.isNullOrEmpty(commonGoods.getTitle()))
|
throw new CommonGoodsException(1, "商品信息不完整");
|
}
|
|
@Override
|
public CommonGoods addOrUpdateCommonGoods(CommonGoods commonGoods) throws CommonGoodsException {
|
filterCommonGoods(commonGoods);
|
CommonGoods goods = commonGoodsMapper.selectByGoodsIdAndGoodsType(commonGoods.getGoodsId(),
|
commonGoods.getGoodsType());
|
if (goods != null) {
|
updateCommonGoods(commonGoods);
|
commonGoods.setId(goods.getId());
|
return commonGoods;
|
}
|
commonGoods.setCreateTime(new Date());
|
commonGoods.setUpdateTime(new Date());
|
commonGoodsMapper.insertSelective(commonGoods);
|
return commonGoods;
|
}
|
|
@Override
|
public void updateCommonGoods(CommonGoods commonGoods) throws CommonGoodsException {
|
// 判断信息是否完整
|
filterCommonGoods(commonGoods);
|
CommonGoods goods = commonGoodsMapper.selectByGoodsIdAndGoodsType(commonGoods.getGoodsId(),
|
commonGoods.getGoodsType());
|
if (goods == null) {
|
throw new CommonGoodsException(3, "商品不存在");
|
}
|
// 更新商品信息
|
commonGoods.setId(goods.getId());
|
commonGoods.setCreateTime(null);
|
commonGoods.setUpdateTime(new Date());
|
commonGoodsMapper.updateByPrimaryKeySelective(commonGoods);
|
}
|
|
@Override
|
public void offlineCommonGoods(Long goodsId, int goodsType) {
|
CommonGoods goods = commonGoodsMapper.selectByGoodsIdAndGoodsType(goodsId, goodsType);
|
if (goods == null)
|
return;
|
CommonGoods update = new CommonGoods();
|
update.setId(goods.getId());
|
update.setUpdateTime(new Date());
|
update.setState(CommonGoods.STATE_OFFLINE);
|
commonGoodsMapper.updateByPrimaryKeySelective(update);
|
}
|
|
@Override
|
public CommonGoods getCommonGoodsByGoodsIdAndGoodsType(Long goodsId, int goodsType) {
|
return commonGoodsMapper.selectByGoodsIdAndGoodsType(goodsId, goodsType);
|
}
|
|
}
|