package com.matrix.system.hive.service.imp;
|
|
import com.matrix.core.constance.MatrixConstance;
|
import com.matrix.core.exception.GlobleException;
|
import com.matrix.core.pojo.PaginationVO;
|
import com.matrix.core.tools.StringUtils;
|
import com.matrix.core.tools.WebUtil;
|
import com.matrix.system.common.bean.SysUsers;
|
import com.matrix.system.common.dao.UtilDao;
|
import com.matrix.system.common.tools.ServiceUtil;
|
import com.matrix.system.constance.Dictionary;
|
import com.matrix.system.constance.TableMapping;
|
import com.matrix.system.hive.bean.MoneyCardAssemble;
|
import com.matrix.system.hive.bean.ShoppingGoods;
|
import com.matrix.system.hive.bean.ShoppingGoodsAssemble;
|
import com.matrix.system.hive.bean.SysShopInfo;
|
import com.matrix.system.hive.dao.MoneyCardAssembleDao;
|
import com.matrix.system.hive.dao.ShoppingGoodsAssembleDao;
|
import com.matrix.system.hive.dao.ShoppingGoodsDao;
|
import com.matrix.system.hive.dao.SysShopInfoDao;
|
import com.matrix.system.hive.plugin.message.StringUtil;
|
import com.matrix.system.hive.service.ShoppingGoodsService;
|
import org.apache.commons.collections.CollectionUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import java.awt.event.WindowStateListener;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
|
|
/**
|
* @date 2016-07-30 09:54
|
*/
|
@Service("shoppingGoodsService")
|
public class ShoppingGoodsServiceImpl implements ShoppingGoodsService {
|
|
@Autowired
|
private ShoppingGoodsDao shoppingGoodsDao;
|
@Autowired
|
private MoneyCardAssembleDao moneyCardAssembleDao;
|
|
@Autowired
|
private ShoppingGoodsAssembleDao shoppingGoodsAssembleDao;
|
@Autowired
|
private UtilDao utilDao;
|
|
@Autowired
|
private ServiceUtil serviceUtil;
|
|
@Autowired
|
private SysShopInfoDao shopInfoDao;
|
|
|
|
|
|
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public int add(ShoppingGoods shoppingGoods) {
|
SysUsers sysUsers = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
|
SysShopInfo shopInfo = shopInfoDao.selectById(sysUsers.getShopId());
|
|
|
|
if(shopInfo.getShopType()==SysShopInfo.SHOP_TYPE_ZONGBU){
|
shoppingGoods.setHeadquarters(1);
|
}else{
|
shoppingGoods.setHeadquarters(2);
|
}
|
|
shoppingGoods.setShopId(sysUsers.getShopId());
|
// 校验去重
|
if (serviceUtil.addCheckRepeatTowColumn("shopping_goods",
|
"code", shoppingGoods.getCode(),
|
"company_id", shoppingGoods.getCompanyId())) {
|
throw new GlobleException("编号" + shoppingGoods.getCode() + "重复");
|
}
|
|
|
if (shoppingGoods.getRealSealCount() == null) {
|
shoppingGoods.setRealSealCount(0);
|
}
|
|
shoppingGoods.setCreateTime(new Date());
|
setPublicAttr(shoppingGoods);
|
if (shoppingGoods.getReferencePice() == null) {
|
//赠送金额
|
shoppingGoods.setReferencePice(0D);
|
}
|
// 验证套餐卡卡必须填写最大使用次数
|
if (Dictionary.FLAG_YES_Y.equals(shoppingGoods.getIsCourse())) {
|
if (shoppingGoods.getCarUseCount() == null) {
|
shoppingGoods.setCarUseCount(0);
|
}
|
}
|
|
if (shoppingGoods.getReferencePice() == null) {
|
shoppingGoods.setReferencePice(0d);
|
}
|
shoppingGoods.setZjm(StringUtils.toHanyuPinyin(shoppingGoods.getName())+","+StringUtils.toHeadWordHanyuPinyin(shoppingGoods.getName()));
|
shoppingGoods.setIsDel(ShoppingGoods.NORMAL);
|
//新增销售产品
|
int i = shoppingGoodsDao.insert(shoppingGoods);
|
|
if (ShoppingGoods.SHOPPING_GOODS_TYPE_CZK.equals(shoppingGoods.getGoodType())) {
|
setCardAssemble(shoppingGoods);
|
} else {
|
setGoodsAssembles(shoppingGoods);
|
}
|
|
|
|
return i;
|
}
|
|
private void setGoodsAssembles(ShoppingGoods shoppingGoods) {
|
//先清空原有关系,在新增
|
shoppingGoodsAssembleDao.deleteByGoodsId(shoppingGoods.getId());
|
List<ShoppingGoodsAssemble> assembles = new ArrayList<>();
|
if (CollectionUtils.isNotEmpty(shoppingGoods.getAssembleGoods())) {
|
assembles.addAll(shoppingGoods.getAssembleGoods());
|
}
|
if (CollectionUtils.isNotEmpty(shoppingGoods.getAssembleProj())) {
|
assembles.addAll(shoppingGoods.getAssembleProj());
|
}
|
if (CollectionUtils.isNotEmpty(assembles)) {
|
assembles.forEach(item -> item.setShoppingGoodsId(shoppingGoods.getId()));
|
shoppingGoodsAssembleDao.batchInsert(assembles);
|
}
|
}
|
|
/**
|
* 设置充值卡,绑定的消费产品
|
*
|
* @param cardGoods
|
* @author jiangyouyao
|
*/
|
private void setCardAssemble(ShoppingGoods cardGoods) {
|
moneyCardAssembleDao.deleteByCardId(cardGoods.getId());
|
if (CollectionUtils.isNotEmpty(cardGoods.getCardAssembles())) {
|
cardGoods.getCardAssembles().forEach(item -> item.setCardId(cardGoods.getId()));
|
moneyCardAssembleDao.batchInsert(cardGoods.getCardAssembles());
|
}
|
}
|
|
@Transactional(rollbackFor = Exception.class)
|
@Override
|
public int modify(ShoppingGoods shoppingGoods) {
|
ShoppingGoods checkShoppingGoods = shoppingGoodsDao.selectById(shoppingGoods.getId());
|
|
// 校验去重
|
if (serviceUtil.updateCheckRepeatTowColumn("shopping_goods",
|
"code", shoppingGoods.getCode(),
|
"company_id", checkShoppingGoods.getCompanyId(), "id", shoppingGoods.getId())) {
|
throw new GlobleException("编号" + shoppingGoods.getCode() + "重复");
|
}
|
|
|
// 验证套餐卡卡必须填写最大使用次数
|
if (Dictionary.FLAG_YES_Y.equals(shoppingGoods.getIsCourse())) {
|
if (shoppingGoods.getCarUseCount() == null) {
|
throw new GlobleException("套餐卡必须填写最大使用次数!");
|
}
|
}
|
|
if (ShoppingGoods.SHOPPING_GOODS_TYPE_CZK.equals(shoppingGoods.getGoodType())) {
|
setCardAssemble(shoppingGoods);
|
}else{
|
|
//清除原有绑定关系
|
shoppingGoodsAssembleDao.deleteByGoodsId(shoppingGoods.getId());
|
//合并绑定的产品和项目
|
List<ShoppingGoodsAssemble> assembles = new ArrayList<>();
|
if(CollectionUtils.isNotEmpty(shoppingGoods.getAssembleGoods())){
|
assembles.addAll(shoppingGoods.getAssembleGoods());
|
}
|
//如果是套餐叠加套餐绑定的项目部分
|
if (Dictionary.SHOPPING_GOODS_TYPE_TC.equals(shoppingGoods.getGoodType())){
|
assembles.addAll(shoppingGoods.getAssembleProj());
|
}
|
|
if (CollectionUtils.isNotEmpty(assembles)) {
|
assembles.forEach(item -> item.setShoppingGoodsId(shoppingGoods.getId()));
|
shoppingGoodsAssembleDao.batchInsert(assembles);
|
}
|
|
}
|
|
|
return shoppingGoodsDao.update(shoppingGoods);
|
|
}
|
|
@Override
|
public int update(Long id) {
|
return shoppingGoodsDao.update2(id);
|
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public int remove(List<Long> list) {
|
return shoppingGoodsDao.deleteByIds(list);
|
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public int removeById(Long id) {
|
return shoppingGoodsDao.deleteById(id);
|
|
}
|
|
@Override
|
public List<ShoppingGoods> findInPage(ShoppingGoods shoppingGoods, PaginationVO pageVo) {
|
|
return shoppingGoodsDao.selectInPage(shoppingGoods, pageVo);
|
|
}
|
|
@Override
|
public List<ShoppingGoods> findAll(ShoppingGoods shoppingGoods) {
|
|
return shoppingGoodsDao.selectAll(shoppingGoods);
|
|
}
|
|
@Override
|
public ShoppingGoods findByCode(String goodsCode) {
|
return shoppingGoodsDao.findByCode(goodsCode);
|
}
|
|
@Override
|
public List<ShoppingGoods> findByModel(ShoppingGoods shoppingGoods) {
|
|
return shoppingGoodsDao.selectByModel(shoppingGoods);
|
|
}
|
|
@Override
|
public int findTotal(ShoppingGoods shoppingGoods) {
|
|
return shoppingGoodsDao.selectTotalRecord(shoppingGoods);
|
|
}
|
|
@Override
|
public ShoppingGoods findById(Long id) {
|
|
return shoppingGoodsDao.selectById(id);
|
|
}
|
|
|
public void updateAassembel(List<ShoppingGoodsAssemble> list, Long id, List<ShoppingGoodsAssemble> checkList,
|
boolean isUpdate) {
|
String ids = ",";
|
if (list != null) {
|
for (ShoppingGoodsAssemble asseble : list) {
|
if (asseble.getTotal() == null && asseble.getPrice() == null) {
|
continue;
|
}
|
if (asseble.getId() == null) {
|
// if (!isUpdate) {
|
// throw new GlobleException("该商品已产生订单,不能再增删绑定信息!");
|
// }
|
// 设置商品id
|
asseble.setShoppingGoodsId(id);
|
shoppingGoodsAssembleDao.insert(asseble);
|
} else {
|
ids = ids + asseble.getId() + ",";
|
shoppingGoodsAssembleDao.update(asseble);
|
}
|
}
|
}
|
|
for (ShoppingGoodsAssemble asseble : checkList) {
|
if (ids.length() > 1) {
|
if (!ids.contains("," + asseble.getId() + ",")) {
|
if (!isUpdate) {
|
throw new GlobleException("该商品已产生订单,不能再删除绑定信息!");
|
}
|
shoppingGoodsAssembleDao.deleteById(asseble.getId());
|
}
|
} else {
|
//如果原产品已经被删除了则清除绑定关系
|
shoppingGoodsAssembleDao.deleteById(asseble.getId());
|
}
|
}
|
}
|
|
/**
|
* 设置公共的属性,设置isAssemble属性,如果goodType='项目'则绑定价=售价,数量为1,为非组合商品,没有图片给一张默认的图片地址
|
*
|
* @param shoppingGoods
|
*/
|
public void setPublicAttr(ShoppingGoods shoppingGoods) {
|
// 设置默认图片
|
setDefaultImg(shoppingGoods);
|
// 不同类型设置不同的默认的属性
|
String type = shoppingGoods.getGoodType();
|
switch (type) {
|
case Dictionary.SHOPPING_GOODS_TYPE_JJCP:
|
shoppingGoods.setIsAssemble(Dictionary.FLAG_NO);
|
break;
|
case Dictionary.SHOPPING_GOODS_TYPE_XM:
|
// 如果是项目的话绑定价和售价一样,且数量默认为1
|
shoppingGoods.setIsAssemble(Dictionary.FLAG_NO);
|
break;
|
case Dictionary.SHOPPING_GOODS_TYPE_TC:
|
shoppingGoods.setIsAssemble(Dictionary.FLAG_YES);
|
break;
|
case Dictionary.SHOPPING_GOODS_TYPE_ZHK:
|
shoppingGoods.setIsAssemble(Dictionary.FLAG_YES);
|
break;
|
}
|
}
|
|
/**
|
* 商城商品这是一张默认图片
|
*
|
* @param shoppingGoods
|
*/
|
public void setDefaultImg(ShoppingGoods shoppingGoods) {
|
// 如果没有图片则给一张默认图片
|
String img = shoppingGoods.getImg();
|
if (img == null || img.equals("")) {
|
shoppingGoods.setImg(Dictionary.DEFAULT_IMG);
|
}
|
if (shoppingGoods.getWeiImg() == null || shoppingGoods.getWeiImg().equals("")) {
|
shoppingGoods.setWeiImg(Dictionary.DEFAULT_IMG);
|
}
|
}
|
|
|
|
/**
|
* 检测是否能更新 @Title: checkIsUpdate @author:jyy @return boolean
|
* 返回类型 @date 2016年9月18日 上午10:06:23 @throws
|
*/
|
@Override
|
public boolean checkIsUpdate(Long id) {
|
ShoppingGoods goods = shoppingGoodsDao.selectById(id);
|
|
/*
|
* if (goods.getGoodType().equals(Dictionary.GOODS_ATTR_HOME)) { for
|
* (ShoppingGoodsAssemble assemble : goods.getAssembleGoods()) { if
|
* (utilDao.selectCount(TableMapping.SHOPPING_ORDER_ITEM, "goods_id",
|
* assemble.getId() + "") > 0) { return false; } } } else
|
*/
|
if (utilDao.selectCount(TableMapping.SHOPPING_ORDER_ITEM, "goods_id", goods.getId() + "") > 0) {
|
return false;
|
}
|
if (utilDao.selectCount(TableMapping.SYS_ORDER_ITEM, "goods_id", goods.getId() + "") > 0) {
|
return false;
|
}
|
return true;
|
}
|
|
/**
|
*
|
*/
|
@Override
|
public List<ShoppingGoods> findInPageNoTaocan(ShoppingGoods shoppingGoods, PaginationVO pageVo) {
|
return shoppingGoodsDao.selectInPageNoTaocan(shoppingGoods);
|
}
|
|
/**
|
*
|
*/
|
@Override
|
public int findTotalNoTaocan(ShoppingGoods shoppingGoods) {
|
return shoppingGoodsDao.selectTotalNoTaocan(shoppingGoods);
|
}
|
|
/**
|
* 数据迁移专用查找,数据迁移专用查找返回简单对象
|
*/
|
@Override
|
public List<ShoppingGoods> findByModelData(ShoppingGoods shoppingGoods) {
|
|
return shoppingGoodsDao.selectByModelData(shoppingGoods);
|
}
|
}
|