package com.matrix.system.hive.service.imp; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.StrUtil; import com.matrix.core.constance.MatrixConstance; import com.matrix.core.exception.GlobleException; import com.matrix.core.pojo.PaginationVO; import com.matrix.core.tools.DateUtil; import com.matrix.core.tools.StringUtils; import com.matrix.core.tools.WebUtil; import com.matrix.system.app.dto.ShoppingGoodsListDto; import com.matrix.system.app.vo.ShoppingGoodsDetailVo; import com.matrix.system.app.vo.ShoppingGoodsListVo; 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.ShoppingGoods; import com.matrix.system.hive.bean.ShoppingGoodsAssemble; import com.matrix.system.hive.bean.ShoppingGoodsCategory; 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.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 javax.validation.constraints.NotNull; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; /** * @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()); //去重查询 ShoppingGoods queryGoods=new ShoppingGoods(); queryGoods.setCompanyId(shoppingGoods.getCompanyId()); queryGoods.setCode(shoppingGoods.getCode()); queryGoods.setIsDel(ShoppingGoods.NORMAL); List oldGoods=shoppingGoodsDao.selectByModel(queryGoods); if(CollUtil.isNotEmpty(oldGoods)){ throw new GlobleException("编号" + shoppingGoods.getCode() + "重复"); } if (shoppingGoods.getRealSealCount() == null) { shoppingGoods.setRealSealCount(0); } shoppingGoods.setCreateTime(new Date()); setPublicAttr(shoppingGoods); setShoppingGoodsInvalidTime(shoppingGoods); if (shoppingGoods.getReferencePice() == null) { //赠送金额 shoppingGoods.setReferencePice(0D); } // 验证套餐卡卡必须填写最大使用次数 if (Dictionary.FLAG_YES_Y.equals(shoppingGoods.getIsCourse()) && Dictionary.FLAG_NO_N.equals(shoppingGoods.getIsInfinite())) { 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 setShoppingGoodsInvalidTime(ShoppingGoods shoppingGoods) { // 若未设置购买有效期和消耗有效期,则默认永久有效 // if (shoppingGoods.getBuyDateNum() == null && shoppingGoods.getUseDateNum() == null) { // shoppingGoods.setInvalidTime(DateUtil.stringToDate("2099-12-31", DateUtil.DATE_FORMAT_DD)); // } else { // // 计算失效日期,判断购买有效期和消耗有效期哪个先失效,则为失效日期 // Date buyValidDate = DateUtil.calDate(shoppingGoods.getBuyDateNum(), shoppingGoods.getBuyDateUnit()); // Date useValidDate = DateUtil.calDate(shoppingGoods.getUseDateNum(), shoppingGoods.getUseDateUnit()); // if (buyValidDate.after(useValidDate)) { // shoppingGoods.setInvalidTime(useValidDate); // } else { // shoppingGoods.setInvalidTime(buyValidDate); // } // shoppingGoods.setBuyValid(shoppingGoods.getBuyDateNum() + shoppingGoods.getBuyDateUnit()); // shoppingGoods.setUseValid(shoppingGoods.getUseDateNum() + shoppingGoods.getUseDateUnit()); // } if (shoppingGoods.getBuyDateNum() != null && StringUtils.isNotBlank(shoppingGoods.getBuyDateUnit())) { shoppingGoods.setBuyValid(shoppingGoods.getBuyDateNum() + shoppingGoods.getBuyDateUnit()); } else { shoppingGoods.setBuyValid(null); } if (shoppingGoods.getUseDateNum() != null && StringUtils.isNotBlank(shoppingGoods.getUseDateUnit())) { shoppingGoods.setUseValid(shoppingGoods.getUseDateNum() + shoppingGoods.getUseDateUnit()); } else { shoppingGoods.setUseValid(null); } } private void setGoodsAssembles(ShoppingGoods shoppingGoods) { //先清空原有关系,在新增 shoppingGoodsAssembleDao.deleteByGoodsId(shoppingGoods.getId()); List 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 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); } } setShoppingGoodsInvalidTime(shoppingGoods); return shoppingGoodsDao.update(shoppingGoods); } @Override public int update(Long id) { return shoppingGoodsDao.update2(id); } @Override @Transactional(rollbackFor = Exception.class) public int remove(List list) { SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY); SysShopInfo zbShop = shopInfoDao.selectZbShop(user.getCompanyId()); //非总部员工只能删除自己门店的产品 if(user.getShopId()!=zbShop.getId()){ //校验是否可以删除 List dataList = shoppingGoodsDao.selectByIds(list); List collect = dataList.stream().filter(goods -> !Objects.equals(user.getShopId(), goods.getShopId())).collect(Collectors.toList()); if(CollUtil.isNotEmpty(collect)){ throw new GlobleException("非总部员工只能删除自己门店的产品"); } } return shoppingGoodsDao.deleteByIds(list); } @Override @Transactional(rollbackFor = Exception.class) public int removeById(Long id) { return shoppingGoodsDao.deleteById(id); } @Override public List findInPage(ShoppingGoods shoppingGoods, PaginationVO pageVo) { return shoppingGoodsDao.selectInPage(shoppingGoods, pageVo); } @Override public List findAll(ShoppingGoods shoppingGoods) { return shoppingGoodsDao.selectAll(shoppingGoods); } @Override public ShoppingGoods findByCode(String goodsCode) { return shoppingGoodsDao.findByCode(goodsCode); } @Override public List 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 list, Long id, List 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 findInPageNoTaocan(ShoppingGoods shoppingGoods, PaginationVO pageVo) { return shoppingGoodsDao.selectInPageNoTaocan(shoppingGoods); } /** * */ @Override public int findTotalNoTaocan(ShoppingGoods shoppingGoods) { return shoppingGoodsDao.selectTotalNoTaocan(shoppingGoods); } /** * 数据迁移专用查找,数据迁移专用查找返回简单对象 */ @Override public List findByModelData(ShoppingGoods shoppingGoods) { return shoppingGoodsDao.selectByModelData(shoppingGoods); } /** * 计算失效时间 * * @param shoppingGoods * @param type 1 - 购买时 2 - 消耗时 * @param buyDate 购买日期, 当计算消耗日期时,不能为空 * @return */ @Override public Date calInvalidTime(ShoppingGoods shoppingGoods, @NotNull Integer type, Date buyDate) { if (StringUtils.isBlank(shoppingGoods.getBuyValid()) && StringUtils.isBlank(shoppingGoods.getUseValid()) && shoppingGoods.getInvalidTime() == null) { return DateUtil.stringToDate("2099-12-31", DateUtil.DATE_FORMAT_DD); } if (type == 2 && shoppingGoods.getUseDateNum() == null && StringUtils.isNotBlank(shoppingGoods.getBuyDateUnit())) { return DateUtil.stringToDate("2099-12-31", DateUtil.DATE_FORMAT_DD); } if (type == 2 && buyDate == null) { throw new GlobleException("购买日期不能为空"); } Date target = null; Date buyValidDate = null; if (type == 1) { if (shoppingGoods.getBuyDateNum() != null && StrUtil.isNotBlank(shoppingGoods.getBuyDateUnit())) { buyValidDate = DateUtil.calDate(shoppingGoods.getBuyDateNum(), shoppingGoods.getBuyDateUnit()); } } else { buyValidDate = buyDate; } Date useValidDate = DateUtil.stringToDate("2099-12-31", DateUtil.DATE_FORMAT_DD); if (type == 2) { if (shoppingGoods.getUseDateNum() != null && StrUtil.isNotBlank(shoppingGoods.getUseDateUnit())) { useValidDate = DateUtil.calDate(shoppingGoods.getUseDateNum(), shoppingGoods.getUseDateUnit()); } } if (buyValidDate != null) { if (buyValidDate.after(useValidDate)) { target = useValidDate; } else { target = buyValidDate; } } if (shoppingGoods.getInvalidTime() != null) { if (target != null) { if (target.after(shoppingGoods.getInvalidTime())) { target = shoppingGoods.getInvalidTime(); } } else { target = shoppingGoods.getInvalidTime(); } } return target; } @Override public List findShoppingGoodsListForApi(ShoppingGoodsListDto shoppingGoodsListDto) { SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY); shoppingGoodsListDto.setShopId(user.getShopId()); PaginationVO pageVo = new PaginationVO(); int offset = (shoppingGoodsListDto.getPageNum() - 1) * shoppingGoodsListDto.getPageSize(); int limit = shoppingGoodsListDto.getPageSize(); pageVo.setOffset(offset); pageVo.setLimit(limit); return shoppingGoodsDao.selectShoppingGoodsApiInPage(shoppingGoodsListDto, pageVo); } @Override public int findShoppingGoodsListTotalForApi(ShoppingGoodsListDto shoppingGoodsListDto) { SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY); shoppingGoodsListDto.setShopId(user.getShopId()); return shoppingGoodsDao.selectShopppingGoodsAipTotal(shoppingGoodsListDto); } @Override public ShoppingGoodsDetailVo findApiShoppingGoodsDetailById(Long id) { ShoppingGoods shoppingGoods = shoppingGoodsDao.selectById(id); ShoppingGoodsDetailVo goodsDetailVo = goodsToGoodsDetailVo(shoppingGoods); if (ShoppingGoods.SHOPPING_GOODS_TYPE_TC.equals(shoppingGoods.getGoodType())) { List products = shoppingGoodsAssembleDao.selectGoodsRelationGoodsList(shoppingGoods.getId(), ShoppingGoods.SHOPPING_GOODS_TYPE_JJCP); List proj = shoppingGoodsAssembleDao.selectGoodsRelationGoodsList(shoppingGoods.getId(), ShoppingGoods.SHOPPING_GOODS_TYPE_XM); goodsDetailVo.setAssembleTaocanProduct(products); goodsDetailVo.setAssembleTaocanProj(proj); } else if (ShoppingGoods.SHOPPING_GOODS_TYPE_XM.equals(shoppingGoods.getGoodType())) { List products = shoppingGoodsAssembleDao.selectGoodsRelationGoodsList(shoppingGoods.getId(), null); goodsDetailVo.setAssembleProj(products); } else if (ShoppingGoods.SHOPPING_GOODS_TYPE_CZK.equals(shoppingGoods.getGoodType())) { List cardGoods = moneyCardAssembleDao.selectCardRelationGoods(shoppingGoods.getId()); List cardCategory = moneyCardAssembleDao.selectCardRelationCategory(shoppingGoods.getId()); goodsDetailVo.setAssembleProj(cardGoods); goodsDetailVo.setCardCategory(cardCategory); } return goodsDetailVo; } private ShoppingGoodsDetailVo goodsToGoodsDetailVo(ShoppingGoods shoppingGoods) { ShoppingGoodsDetailVo goodsDetailVo = new ShoppingGoodsDetailVo(); goodsDetailVo.setImg(shoppingGoods.getImg()); goodsDetailVo.setGoodsName(shoppingGoods.getName()); goodsDetailVo.setGoodsNo(shoppingGoods.getCode()); goodsDetailVo.setGoodsType(shoppingGoods.getGoodType()); goodsDetailVo.setDesc(shoppingGoods.getDescription()); goodsDetailVo.setMaxSaleCnt(shoppingGoods.getCarMaxSaleCount()); goodsDetailVo.setLimitBuyCnt(shoppingGoods.getIsOnce()); goodsDetailVo.setMaxUseCnt(shoppingGoods.getCarUseCount()); goodsDetailVo.setMeasure(shoppingGoods.getMeasure()); goodsDetailVo.setVolume(shoppingGoods.getVolume()); goodsDetailVo.setSalePrice(BigDecimal.valueOf(shoppingGoods.getSealPice())); goodsDetailVo.setSaleCnt(shoppingGoods.getRealSealCount()); goodsDetailVo.setGiftPrice(BigDecimal.valueOf(shoppingGoods.getReferencePice() == null ? 0 : shoppingGoods.getReferencePice())); return goodsDetailVo; } }