package com.yeshi.fanli.service.impl.shop;
|
|
import java.util.Date;
|
import java.util.List;
|
|
import javax.annotation.Resource;
|
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import com.yeshi.fanli.dao.mybatis.shop.BanLiShopGoodsMapper;
|
import com.yeshi.fanli.entity.shop.BanLiShopGoods;
|
import com.yeshi.fanli.entity.shop.BanLiShopGoodsImg;
|
import com.yeshi.fanli.entity.shop.BanLiShopGoodsSets;
|
import com.yeshi.fanli.exception.shop.BanLiShopGoodsException;
|
import com.yeshi.fanli.exception.shop.BanLiShopGoodsSetException;
|
import com.yeshi.fanli.exception.shop.BanLiShopGoodsSetPayException;
|
import com.yeshi.fanli.service.inter.shop.BanLiShopGoodsImgService;
|
import com.yeshi.fanli.service.inter.shop.BanLiShopGoodsService;
|
import com.yeshi.fanli.service.inter.shop.BanLiShopGoodsSetService;
|
import com.yeshi.fanli.util.StringUtil;
|
|
@Service
|
public class BanLiShopGoodsServiceImpl implements BanLiShopGoodsService {
|
|
@Resource
|
private BanLiShopGoodsSetService banLiShopGoodsSetService;
|
|
@Resource
|
private BanLiShopGoodsImgService banLiShopGoodsImgService;
|
|
@Resource
|
private BanLiShopGoodsMapper banLiShopGoodsMapper;
|
|
@Override
|
public List<BanLiShopGoods> listGoods(String key, Integer state, int page, int pageSize) {
|
return banLiShopGoodsMapper.listGoods(key, state, (page - 1) * pageSize, pageSize);
|
}
|
|
@Override
|
public long countGoods(String key, Integer state) {
|
return banLiShopGoodsMapper.countGoods(key, state);
|
}
|
|
@Override
|
public BanLiShopGoods getGoodsDetail(Long goodsId) {
|
return banLiShopGoodsMapper.selectDetailByPrimaryKey(goodsId);
|
}
|
|
@Override
|
public BanLiShopGoods selectByPrimaryKey(Long id) {
|
return banLiShopGoodsMapper.selectByPrimaryKey(id);
|
}
|
|
@Override
|
public void addGoods(BanLiShopGoods goods)
|
throws BanLiShopGoodsException, BanLiShopGoodsSetException, BanLiShopGoodsSetPayException {
|
if (goods.getId() == null) {// 新增
|
if (goods.getGoodsClass() == null || goods.getGoodsClass().getId() == null) {
|
throw new BanLiShopGoodsSetException(1, "请指定商品分类");
|
}
|
|
if (StringUtil.isNullOrEmpty(goods.getTitle()))
|
throw new BanLiShopGoodsSetException(1, "缺少标题");
|
|
if (StringUtil.isNullOrEmpty(goods.getPicture()))
|
throw new BanLiShopGoodsSetException(1, "缺少封面图");
|
|
// 默认上线
|
if (goods.getState() == null)
|
goods.setState(BanLiShopGoods.STATE_ONLINE);
|
|
if (goods.getSalesCount() == null)
|
goods.setSalesCount(0L);
|
|
if (goods.getCreateTime() == null)
|
goods.setCreateTime(new Date());
|
|
banLiShopGoodsMapper.insertSelective(goods);
|
if (goods.getWeight() == null) {// 权重更新
|
BanLiShopGoods update = new BanLiShopGoods();
|
update.setId(goods.getId());
|
update.setWeight((int) goods.getId().longValue());
|
banLiShopGoodsMapper.updateByPrimaryKeySelective(update);
|
}
|
|
if (goods.getSetsList() != null && goods.getSetsList().size() > 0) {
|
for (BanLiShopGoodsSets set : goods.getSetsList())// 更新支付方式
|
{
|
set.setGoods(goods);
|
banLiShopGoodsSetService.addSet(set);
|
}
|
}
|
|
// TODO 更改图片
|
if (goods.getImgList() != null)
|
for (BanLiShopGoodsImg img : goods.getImgList()) {
|
banLiShopGoodsImgService.addImg(img.getUrl(), goods.getId());
|
}
|
|
} else {// 修改
|
updateSelectiveByPrimaryKey(goods);
|
// 更新支付方式
|
if (goods.getSetsList() != null && goods.getSetsList().size() > 0) {
|
// 删除原有套餐
|
List<BanLiShopGoodsSets> setList = banLiShopGoodsSetService.listByGoodsId(goods.getId());
|
for (BanLiShopGoodsSets set : setList)
|
banLiShopGoodsSetService.deleteByPrimaryKey(set.getId());
|
|
// 添加新的套餐
|
for (BanLiShopGoodsSets set : goods.getSetsList())// 更新支付方式
|
banLiShopGoodsSetService.addSet(set);
|
}
|
|
// 更新图片
|
if (goods.getImgList() != null && goods.getImgList().size() > 0) {
|
// 删除原有图片
|
List<BanLiShopGoodsImg> imgList = banLiShopGoodsImgService.listByGoodsId(goods.getId());
|
for (BanLiShopGoodsImg img : imgList)
|
banLiShopGoodsImgService.deleteByPrimaryKey(img.getId());
|
// 添加新的图片
|
for (BanLiShopGoodsImg img : goods.getImgList())// 更新支付方式
|
banLiShopGoodsImgService.addImg(img.getUrl(), goods.getId());
|
}
|
|
}
|
|
}
|
|
@Override
|
public void updateSelectiveByPrimaryKey(BanLiShopGoods goods) {
|
if (goods == null || goods.getId() == null)
|
return;
|
if (goods.getUpdateTime() == null)
|
goods.setUpdateTime(new Date());
|
|
banLiShopGoodsMapper.updateByPrimaryKeySelective(goods);
|
}
|
|
@Transactional
|
@Override
|
public void delete(List<Long> idsList) {
|
if (idsList != null)
|
for (Long id : idsList)
|
deleteByPrimaryKey(id);
|
|
}
|
|
@Transactional
|
@Override
|
public void deleteByPrimaryKey(Long id) {
|
banLiShopGoodsMapper.deleteByPrimaryKey(id);
|
List<BanLiShopGoodsSets> setList = banLiShopGoodsSetService.listByGoodsId(id);
|
if (setList != null)
|
for (BanLiShopGoodsSets set : setList)
|
banLiShopGoodsSetService.deleteByPrimaryKey(set.getId());
|
}
|
|
}
|