123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484 |
- package com.kmall.admin.service.impl;
- import com.google.common.collect.ImmutableBiMap;
- import com.kmall.admin.dao.*;
- import com.kmall.admin.entity.*;
- import com.kmall.admin.service.GoodsService;
- import com.kmall.api.contants.Dict;
- import com.kmall.common.entity.SysUserEntity;
- import com.kmall.common.utils.*;
- import com.kmall.common.utils.excel.ExcelImport;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.web.multipart.MultipartFile;
- import java.math.BigDecimal;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- /**
- * Service实现类
- *
- * @author Scott
- * @email
- * @date 2017-08-21 21:19:49
- */
- @Service("goodsService")
- public class GoodsServiceImpl implements GoodsService {
- @Autowired
- private GoodsDao goodsDao;
- @Autowired
- private GoodsAttributeDao goodsAttributeDao;
- @Autowired
- private AttributeDao attributeDao;
- @Autowired
- private ProductDao productDao;
- @Autowired
- private GoodsGalleryDao goodsGalleryDao;
- @Autowired
- private GoodsSpecificationDao goodsSpecificationDao;
- @Autowired
- private ProductStoreRelaDao productStoreRelaDao;
- @Autowired
- private StoreDao storeDao;
- @Autowired
- private GoodsGroupDao goodsGroupDao;
- @Autowired
- private CategoryDao categoryDao;
- @Override
- public GoodsEntity queryObject(Integer id) {
- Map<String, Object> map = new HashMap<String, Object>();
- map.put("goodsId", id);
- List<GoodsAttributeEntity> attributeEntities = goodsAttributeDao.queryList(map);
- List<ProductEntity> productEntityList = productDao.queryList(map);
- GoodsEntity entity = goodsDao.queryObject(id);
- entity.setAttributeEntityList(attributeEntities);
- entity.setProductEntityList(productEntityList);
- return entity;
- }
- @Override
- public List<GoodsEntity> queryList(Map<String, Object> map) {
- return goodsDao.queryList(map);
- }
- @Override
- public List<GoodsEntity> querySame(Map<String, Object> map) {
- return goodsDao.querySame(map);
- }
- @Override
- public int queryTotal(Map<String, Object> map) {
- return goodsDao.queryTotal(map);
- }
- @Override
- @Transactional
- public int save(GoodsEntity goods) {
- Map<String, Object> valideDate = MapBeanUtil.fromObject(goods);
- ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
- builder.put("attributeCategory", "商品分类");
- builder.put("categoryId", "商品二级分类");
- builder.put("goodsSn", "商品编码");
- builder.put("goodsBizType", "货品业务类型");
- builder.put("name", "商品名称");
- builder.put("brandId", "品牌");
- builder.put("freightId", "运费模版");
- builder.put("goodsDesc", "商品描述");
- builder.put("isOnSale", "上架");
- builder.put("goodsUnit", "商品单位");
- builder.put("primaryPicUrl", "商品主图");
- builder.put("listPicUrl", "商品列表图");
- builder.put("isHot", "热销");
- R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
- if (Integer.valueOf(r.get("code").toString()) != 0) {
- throw new RRException(r.get("msg").toString());
- } else {
- if (!Dict.orderBizType.item_11.getItem().equals(goods.getGoodsBizType())) {
- // 海关信息,普通货物可不添加
- builder.put("sku", "SKU");
- builder.put("goodsRate", "商品税率");
- builder.put("retailPrice", "零售价");
- builder.put("prodBarcode", "产品编码");
- builder.put("brand", "产品品牌");
- builder.put("unitCode", "计量单位代码");
- builder.put("cusGoodsCode", "海关商品编码");
- builder.put("ciqProdModel", "国检规格型号");
- builder.put("oriCntCode", "原产国代码");
- builder.put("cusDeclEle", "海关申报要素");
- builder.put("cusRecCode", "海关备案编号");
- }
- r = ValidatorUtil.isEmpty(builder.build(), valideDate);
- if (Integer.valueOf(r.get("code").toString()) != 0) {
- throw new RRException(r.get("msg").toString());
- }
- }
- // 商品轮播图
- List<GoodsGalleryEntity> galleryEntityList = goods.getGoodsImgList();
- if (galleryEntityList == null || galleryEntityList.size() <= 0) {
- throw new RRException("至少添加一张商品轮播图!");
- }
- SysUserEntity user = ShiroUtils.getUserEntity();
- Map<String, Object> map = new HashMap<>();
- map.put("isSame", "true");
- map.put("sku", goods.getSku());
- map.put("goodsSn", goods.getGoodsSn());
- map.put("goodsBizType", goods.getGoodsBizType());
- List<GoodsEntity> list = querySame(map);
- if (list != null && list.size() != 0) {
- throw new RRException("已存在该商品编码或该货品业务类型下已存在此SKU!");
- }
- // 添加商品
- if (Dict.orderBizType.item_02.getItem().equals(goods.getGoodsBizType())
- || Dict.orderBizType.item_10.getItem().equals(goods.getGoodsBizType())) {
- goods.setIsHot(0);
- }
- goods.setAttributeCategory(categoryDao.queryObject(goods.getCategoryId()).getParentId());
- goods.setAddTime(new Date());
- goods.setIsDelete(0);
- goods.setIsNew(0);
- goods.setCreateUserId(user.getUserId());
- goods.setUpdateUserId(user.getUserId());
- goods.setUpdateTime(new Date());
- goods.setModTime(new Date());
- goods.setCreateTime(new Date());
- // 新增商品
- goodsDao.save(goods);
- Long id = goods.getId();
- // 添加商品轮播图
- for (GoodsGalleryEntity galleryEntity : galleryEntityList) {
- galleryEntity.setGoodsId(id);
- goodsGalleryDao.save(galleryEntity);
- }
- // 添加商品参数
- List<GoodsAttributeEntity> attributeEntityList = goods.getAttributeEntityList();
- if (attributeEntityList != null && attributeEntityList.size() > 0) {
- for (GoodsAttributeEntity item : attributeEntityList) {
- if (item.getIsDelete() == 0) {
- if (item.getId() == null && item.getAttributeId() != null && StringUtils.isNotEmpty(item.getValue())) {
- item.setGoodsId(id);
- goodsAttributeDao.save(item);
- } else if (item.getId() == null && item.getAttributeId() != null && StringUtils.isNullOrEmpty(item.getValue())) {
- throw new RRException("商品属性【" + attributeDao.queryObject(item.getAttributeId()).getName() + "】值不能为空!");
- } else if (item.getId() == null && item.getAttributeId() == null) {
- continue;
- }
- }
- }
- }
- // 添加产品
- ProductEntity product = new ProductEntity();
- product.setGoodsId(id);
- product.setGoodsSn(goods.getGoodsSn());
- // 保税商品,普通货物暂不添加商品规格
- if (!Dict.orderBizType.item_11.getItem().equals(goods.getGoodsBizType())) {
- // 添加商品规格
- GoodsSpecificationEntity goodsSpecification = new GoodsSpecificationEntity();
- goodsSpecification.setGoodsId(id);
- goodsSpecification.setValue(goods.getCiqProdModel());
- goodsSpecification.setSpecificationId(1);
- goodsSpecificationDao.save(goodsSpecification);
- product.setGoodsSpecificationIds(goodsSpecification.getId().toString());
- product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
- }
- return productDao.save(product);
- }
- @Override
- @Transactional
- public int update(GoodsEntity goods) {
- Map<String, Object> valideDate = MapBeanUtil.fromObject(goods);
- ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
- builder.put("attributeCategory", "商品分类");
- builder.put("categoryId", "商品二级分类");
- builder.put("goodsSn", "商品编码");
- builder.put("goodsBizType", "货品业务类型");
- builder.put("name", "商品名称");
- builder.put("brandId", "品牌");
- builder.put("freightId", "运费模版");
- builder.put("goodsDesc", "商品描述");
- builder.put("isOnSale", "上架");
- builder.put("goodsUnit", "商品单位");
- builder.put("primaryPicUrl", "商品主图");
- builder.put("listPicUrl", "商品列表图");
- builder.put("isHot", "热销");
- R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
- if (Integer.valueOf(r.get("code").toString()) != 0) {
- throw new RRException(r.get("msg").toString());
- } else {
- if (!Dict.orderBizType.item_11.getItem().equals(goods.getGoodsBizType())) {
- // 海关信息,普通货物可不添加
- builder.put("sku", "SKU");
- builder.put("goodsRate", "商品税率");
- builder.put("retailPrice", "零售价");
- builder.put("prodBarcode", "产品编码");
- builder.put("brand", "产品品牌");
- builder.put("unitCode", "计量单位代码");
- builder.put("cusGoodsCode", "海关商品编码");
- builder.put("ciqProdModel", "国检规格型号");
- builder.put("oriCntCode", "原产国代码");
- builder.put("cusDeclEle", "海关申报要素");
- builder.put("cusRecCode", "海关备案编号");
- }
- r = ValidatorUtil.isEmpty(builder.build(), valideDate);
- if (Integer.valueOf(r.get("code").toString()) != 0) {
- throw new RRException(r.get("msg").toString());
- }
- }
- // 商品轮播图
- List<GoodsGalleryEntity> galleryEntityList = goods.getGoodsImgList();
- if (galleryEntityList == null || galleryEntityList.size() <= 0) {
- throw new RRException("至少保留一张商品轮播图!");
- }
- SysUserEntity user = ShiroUtils.getUserEntity();
- Map<String, Object> map = new HashMap<>();
- map.put("isSame", "true");
- map.put("sku", goods.getSku());
- map.put("goodsSn", goods.getGoodsSn());
- map.put("goodsBizType", goods.getGoodsBizType());
- map.put("id", goods.getId());
- List<GoodsEntity> list = querySame(map);
- if (list != null && list.size() != 0) {
- throw new RRException("已存在该商品编码或该货品业务类型下已存在此SKU!");
- }
- // 修改商品
- if (Dict.orderBizType.item_02.getItem().equals(goods.getGoodsBizType())
- || Dict.orderBizType.item_10.getItem().equals(goods.getGoodsBizType())) {
- goods.setIsHot(0);
- }
- goods.setAttributeCategory(categoryDao.queryObject(goods.getCategoryId()).getParentId());
- goods.setIsDelete(0);
- goods.setIsNew(0);
- goods.setUpdateUserId(user.getUserId());
- goods.setUpdateTime(new Date());
- goods.setModTime(new Date());
- // 修改商品
- goodsDao.update(goods);
- // 保税商品修改各个门店商品价格
- if (!Dict.orderBizType.item_11.getItem().equals(goods.getGoodsBizType())) {
- List<ProductStoreRelaEntity> productStoreRelaEntityList = productStoreRelaDao.queryByGoodsId(goods.getId());
- if (productStoreRelaEntityList != null && productStoreRelaEntityList.size() > 0) {
- for (ProductStoreRelaEntity productStoreRela : productStoreRelaEntityList) {
- productStoreRela.setRetailPrice(goods.getRetailPrice());
- productStoreRela.setMarketPrice(goods.getMarketPrice());
- productStoreRelaDao.update(productStoreRela);
- }
- }
- }
- // 修改商品轮播图
- goodsGalleryDao.deleteByGoodsId(goods.getId());
- for (GoodsGalleryEntity galleryEntity : galleryEntityList) {
- galleryEntity.setGoodsId(goods.getId());
- goodsGalleryDao.save(galleryEntity);
- }
- // 修改商品参数
- List<GoodsAttributeEntity> attributeEntityList = goods.getAttributeEntityList();
- if (attributeEntityList != null && attributeEntityList.size() > 0) {
- for (GoodsAttributeEntity item : attributeEntityList) {
- if (item.getIsDelete() == 0) {
- if (item.getId() != null) {
- goodsAttributeDao.update(item);
- } else if (item.getId() == null && item.getAttributeId() != null && StringUtils.isNotEmpty(item.getValue())) {
- item.setGoodsId(goods.getId());
- goodsAttributeDao.save(item);
- } else if (item.getId() == null && item.getAttributeId() != null && StringUtils.isNullOrEmpty(item.getValue())) {
- throw new RRException("商品属性【" + attributeDao.queryObject(item.getAttributeId()).getName() + "】值不能为空!");
- } else if (item.getId() == null && item.getAttributeId() == null) {
- continue;
- }
- } else if (item.getIsDelete() == 1) {
- goodsAttributeDao.delete(item.getId());
- }
- }
- }
- // 修改产品
- ProductEntity product = productDao.queryObjectBySn(goods.getGoodsSn());
- // 保税商品,普通货物暂不添加商品规格
- if (!Dict.orderBizType.item_11.getItem().equals(goods.getGoodsBizType())) {
- // 添加商品规格
- GoodsSpecificationEntity goodsSpecification = goodsSpecificationDao.queryByGoodsId(goods.getId());
- goodsSpecification.setValue(goods.getCiqProdModel());
- goodsSpecificationDao.update(goodsSpecification);
- product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
- }
- return productDao.update(product);
- }
- @Override
- public int delete(Integer id) {
- SysUserEntity user = ShiroUtils.getUserEntity();
- GoodsEntity goodsEntity = goodsDao.queryObject(id);
- goodsEntity.setIsDelete(1);
- goodsEntity.setIsOnSale(0);
- goodsEntity.setUpdateUserId(user.getUserId());
- goodsEntity.setUpdateTime(new Date());
- //更新团购
- Map params = new HashMap();
- params.put("goodsId", id);
- List<GoodsGroupEntity> groupVos = goodsGroupDao.queryList(params);
- if (null != groupVos && groupVos.size() > 0) {
- for (GoodsGroupEntity groupVo : groupVos) {
- groupVo.setOpenStatus(3);
- goodsGroupDao.update(groupVo);
- }
- }
- return goodsDao.update(goodsEntity);
- }
- @Override
- @Transactional
- public int deleteBatch(Integer[] ids) {
- int result = 0;
- for (Integer id : ids) {
- result += delete(id);
- }
- return result;
- }
- @Override
- @Transactional
- public int back(Integer[] ids) {
- SysUserEntity user = ShiroUtils.getUserEntity();
- int result = 0;
- for (Integer id : ids) {
- GoodsEntity goodsEntity = queryObject(id);
- goodsEntity.setIsDelete(0);
- goodsEntity.setIsOnSale(1);
- goodsEntity.setUpdateUserId(user.getUserId());
- goodsEntity.setUpdateTime(new Date());
- result += goodsDao.update(goodsEntity);
- }
- return result;
- }
- @Override
- public int enSale(Integer id) {
- SysUserEntity user = ShiroUtils.getUserEntity();
- GoodsEntity goodsEntity = queryObject(id);
- if (1 == goodsEntity.getIsOnSale()) {
- throw new RRException("此商品已处于上架状态!");
- }
- goodsEntity.setIsOnSale(1);
- goodsEntity.setUpdateUserId(user.getUserId());
- goodsEntity.setUpdateTime(new Date());
- return goodsDao.update(goodsEntity);
- }
- @Override
- public int unSale(Integer id) {
- SysUserEntity user = ShiroUtils.getUserEntity();
- GoodsEntity goodsEntity = queryObject(id);
- if (0 == goodsEntity.getIsOnSale()) {
- throw new RRException("此商品已处于下架状态!");
- }
- goodsEntity.setIsOnSale(0);
- goodsEntity.setUpdateUserId(user.getUserId());
- goodsEntity.setUpdateTime(new Date());
- return goodsDao.update(goodsEntity);
- }
- @Override
- public int enSaleBatch(Integer[] ids) {
- int result = 0;
- SysUserEntity user = ShiroUtils.getUserEntity();
- for (Integer id : ids) {
- GoodsEntity goodsEntity = queryObject(id);
- goodsEntity.setIsOnSale(1);
- goodsEntity.setUpdateUserId(user.getUserId());
- goodsEntity.setUpdateTime(new Date());
- result += goodsDao.update(goodsEntity);
- }
- return result;
- }
- @Override
- public int unSaleBatch(Integer[] ids) {
- int result = 0;
- SysUserEntity user = ShiroUtils.getUserEntity();
- for (Integer id : ids) {
- GoodsEntity goodsEntity = queryObject(id);
- goodsEntity.setIsOnSale(0);
- goodsEntity.setUpdateUserId(user.getUserId());
- goodsEntity.setUpdateTime(new Date());
- result += goodsDao.update(goodsEntity);
- }
- return result;
- }
- @Override
- @Transactional
- public int uploadExcel(MultipartFile file) {
- SysUserEntity user = ShiroUtils.getUserEntity();
- List<String[]> list = ExcelImport.getExcelData(file);
- // 取门店名称
- StoreEntity storeEntity = storeDao.queryObjectByName(list.get(0)[3]);
- if (null == storeEntity) {
- return 0;
- }
- //去除表头两行、底部合计
- if (list != null && list.size() > 3) {
- ProductStoreRelaEntity storeRelaEntity;
- ProductEntity productEntity;
- for (int i = 2; i < list.size() - 1; i++) {
- String[] item = list.get(i);
- String goodsSn = item[0];
- productEntity = productDao.queryObjectBySn(goodsSn);
- if (StringUtils.isNullOrEmpty(goodsSn)) {
- continue;
- }
- if (null == productEntity || null == productEntity.getId()) {
- continue;
- }
- storeRelaEntity = productStoreRelaDao.queryByStoreIdProductId(storeEntity.getId(), productEntity.getId());
- if (null != storeRelaEntity && null != storeRelaEntity.getId()) {
- storeRelaEntity.setRetailPrice(new BigDecimal(item[6]));
- storeRelaEntity.setStockNum(Integer.valueOf(item[3].replace(".00", "")));
- storeRelaEntity.setStockPrice(new BigDecimal(item[4]));
- productStoreRelaDao.update(storeRelaEntity);
- } else {
- storeRelaEntity = new ProductStoreRelaEntity();
- storeRelaEntity.setGoodsId(productEntity.getGoodsId());
- storeRelaEntity.setProductId(productEntity.getId());
- storeRelaEntity.setRetailPrice(new BigDecimal(item[6]));
- storeRelaEntity.setMarketPrice(new BigDecimal(item[6]));
- storeRelaEntity.setStockNum(Integer.valueOf(item[3]));
- storeRelaEntity.setStockPrice(new BigDecimal(item[4]));
- storeRelaEntity.setStoreId(storeEntity.getId());
- productStoreRelaDao.save(storeRelaEntity);
- }
- }
- }
- return 1;
- }
- public GoodsEntity queryObjectBySn(String goodsSn) {
- return goodsDao.queryObjectBySn(goodsSn);
- }
- }
|