123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427 |
- package com.kmall.admin.service.impl;
- import com.kmall.admin.annotation.DataFilter;
- import com.kmall.admin.dao.*;
- import com.kmall.admin.entity.*;
- import com.kmall.admin.service.GoodsService;
- import com.kmall.common.entity.SysUserEntity;
- import com.kmall.common.utils.RRException;
- import com.kmall.common.utils.ShiroUtils;
- import com.kmall.common.utils.StringUtils;
- 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 ProductDao productDao;
- @Autowired
- private GoodsGalleryDao goodsGalleryDao;
- @Autowired
- private GoodsSpecificationDao goodsSpecificationDao;
- @Autowired
- private ProductStoreRelaDao productStoreRelaDao;
- @Autowired
- private StoreDao storeDao;
- @Autowired
- private GoodsGroupDao goodsGroupDao;
- @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 int queryTotal(Map<String, Object> map) {
- return goodsDao.queryTotal(map);
- }
- @Override
- @Transactional
- public int save(GoodsEntity goods) {
- SysUserEntity user = ShiroUtils.getUserEntity();
- Map<String, Object> map = new HashMap<>();
- map.put("name", goods.getName());
- List<GoodsEntity> list = queryList(map);
- if (null != list && list.size() != 0) {
- throw new RRException("商品名称已存在!");
- }
- Long id = goodsDao.queryMaxId() + 1;
- goods.setId(id);
- goods.setAddTime(new Date());
- //保存商品详情页面显示的属性
- List<GoodsAttributeEntity> attributeEntityList = goods.getAttributeEntityList();
- if (null != attributeEntityList && attributeEntityList.size() > 0) {
- for (GoodsAttributeEntity item : attributeEntityList) {
- if (item.getGoodsId() == null) {
- item.setGoodsId(id);
- goodsAttributeDao.save(item);
- }
- }
- }
- // 产品更新
- List<ProductEntity> productEntityList = goods.getProductEntityList();
- if (null != productEntityList && productEntityList.size() > 0) {
- for (ProductEntity productEntity : productEntityList) {
- if (1 == productEntity.getIsDelete()) {
- productDao.delete(productEntity);
- if (null != productEntity.getGoodsSpecificationIds()) {
- goodsSpecificationDao.delete(productEntity.getGoodsSpecificationIds());
- }
- }
- if (StringUtils.isNullOrEmpty(productEntity.getGoodsSn())) {
- throw new RRException("产品编码不能为空!");
- }
- // 判断是否goods_sn重复
- ProductEntity productDb = productDao.queryObjectBySn(productEntity.getGoodsSn());
- if (null != productDb && !productDb.getId().equals(productEntity.getId())) {
- throw new RRException("产品编码重复!"); // 重复不操作
- }
- goods.setPrimaryProductId(productEntity.getId());
- GoodsSpecificationEntity specificationEntity = new GoodsSpecificationEntity();
- // 规格
- if (StringUtils.isNullOrEmpty(productEntity.getGoodsSpecificationIds())
- && StringUtils.isNotEmpty(productEntity.getGoodsSpecificationNameValue())) {
- specificationEntity.setGoodsId(goods.getId());
- specificationEntity.setSpecificationId(1); // 规格写死
- specificationEntity.setValue(productEntity.getGoodsSpecificationNameValue());
- goodsSpecificationDao.save(specificationEntity);
- productEntity.setGoodsSpecificationNameValue(specificationEntity.getId() + "");
- } else if (StringUtils.isNotEmpty(productEntity.getGoodsSpecificationIds())) {
- specificationEntity = goodsSpecificationDao.queryObject(productEntity.getGoodsSpecificationIds());
- specificationEntity.setValue(productEntity.getGoodsSpecificationNameValue());
- goodsSpecificationDao.update(specificationEntity);
- }
- if (null != productEntity.getId()) {
- productDao.save(productEntity);
- } else {
- productDao.update(productEntity);
- }
- // default
- if (null == goods.getPrimaryProductId()) {
- goods.setPrimaryProductId(productEntity.getId());
- } else if (productEntity.getGoodsDefault() == 1) {
- goods.setPrimaryProductId(productEntity.getId());
- }
- }
- // default
- for (ProductEntity productEntity : productEntityList) {
- if (null == goods.getPrimaryProductId() && 1 != productEntity.getIsDelete()) {
- goods.setPrimaryProductId(productEntity.getId());
- break;
- }
- }
- }
- //商品轮播图
- List<GoodsGalleryEntity> galleryEntityList = goods.getGoodsImgList();
- if (null != galleryEntityList && galleryEntityList.size() > 0) {
- for (GoodsGalleryEntity galleryEntity : galleryEntityList) {
- galleryEntity.setGoodsId(id);
- goodsGalleryDao.save(galleryEntity);
- }
- }
- goods.setIsDelete(0);
- goods.setCreateUserId(user.getUserId());
- goods.setUpdateUserId(user.getUserId());
- goods.setUpdateTime(new Date());
- goods.setModTime(new Date());
- goods.setCreateTime(new Date());
- return goodsDao.save(goods);
- }
- @Override
- @Transactional
- public int update(GoodsEntity goods) {
- Map<String, Long> map = new HashMap<String, Long>();
- map.put("goodsId", goods.getId());
- SysUserEntity user = ShiroUtils.getUserEntity();
- List<GoodsAttributeEntity> attributeEntityList = goods.getAttributeEntityList();
- //商品参数
- if (null != attributeEntityList && attributeEntityList.size() > 0) {
- for (GoodsAttributeEntity goodsAttributeEntity : attributeEntityList) {
- if (null == goodsAttributeEntity.getId() && StringUtils.isNullOrEmpty(goodsAttributeEntity.getAttributeId())) {
- continue;
- }
- if (goodsAttributeEntity.getIsDelete() == 1) {
- goodsAttributeDao.delete(goodsAttributeEntity);
- } else if (null != goodsAttributeEntity.getId()) {
- goodsAttributeEntity.setGoodsId(goods.getId());
- goodsAttributeDao.update(goodsAttributeEntity);
- } else {
- goodsAttributeEntity.setGoodsId(goods.getId());
- goodsAttributeDao.save(goodsAttributeEntity);
- }
- }
- }
- // 产品更新
- List<ProductEntity> productEntityList = goods.getProductEntityList();
- if (null != productEntityList && productEntityList.size() > 0) {
- for (ProductEntity productEntity : productEntityList) {
- if (1 == productEntity.getIsDelete()) {
- if (null == productEntity.getId()) {
- continue;
- }
- productDao.delete(productEntity);
- if (null != productEntity.getGoodsSpecificationIds()) {
- goodsSpecificationDao.delete(productEntity.getGoodsSpecificationIds());
- }
- continue;
- }
- if (StringUtils.isNullOrEmpty(productEntity.getGoodsSn())) {
- throw new RRException("产品编码不能为空!");
- }
- // 判断是否goods_sn重复
- ProductEntity productDb = productDao.queryObjectBySn(productEntity.getGoodsSn());
- if (null != productDb && !productDb.getId().equals(productEntity.getId())) {
- throw new RRException("产品编码重复!"); // 重复不操作
- }
- GoodsSpecificationEntity specificationEntity = new GoodsSpecificationEntity();
- // 规格
- if (StringUtils.isNullOrEmpty(productEntity.getGoodsSpecificationIds())
- && StringUtils.isNotEmpty(productEntity.getGoodsSpecificationNameValue())) {
- specificationEntity.setGoodsId(goods.getId());
- specificationEntity.setSpecificationId(1); // 规格写死
- specificationEntity.setValue(productEntity.getGoodsSpecificationNameValue());
- goodsSpecificationDao.save(specificationEntity);
- productEntity.setGoodsSpecificationIds(specificationEntity.getId() + "");
- } else if (StringUtils.isNotEmpty(productEntity.getGoodsSpecificationIds())) {
- specificationEntity = goodsSpecificationDao.queryObject(productEntity.getGoodsSpecificationIds());
- specificationEntity.setValue(productEntity.getGoodsSpecificationNameValue());
- goodsSpecificationDao.update(specificationEntity);
- }
- if (null == productEntity.getId()) {
- productEntity.setGoodsId(goods.getId());
- productDao.save(productEntity);
- } else {
- productDao.update(productEntity);
- }
- // default
- if (null == goods.getPrimaryProductId()) {
- goods.setPrimaryProductId(productEntity.getId());
- } else if (productEntity.getGoodsDefault() == 1) {
- goods.setPrimaryProductId(productEntity.getId());
- }
- }
- // default
- for (ProductEntity productEntity : productEntityList) {
- if (null == goods.getPrimaryProductId() && 1 != productEntity.getIsDelete()) {
- goods.setPrimaryProductId(productEntity.getId());
- break;
- }
- }
- }
- //商品轮播图
- List<GoodsGalleryEntity> galleryEntityList = goods.getGoodsImgList();
- if (null != galleryEntityList && galleryEntityList.size() > 0)
- {
- for (GoodsGalleryEntity galleryEntity : galleryEntityList) {
- if (galleryEntity.getIsDelete() == 1) {
- goodsGalleryDao.delete(galleryEntity);
- } else if (null != galleryEntity.getId()) {
- galleryEntity.setGoodsId(goods.getId());
- goodsGalleryDao.update(galleryEntity);
- } else {
- galleryEntity.setGoodsId(goods.getId());
- goodsGalleryDao.save(galleryEntity);
- }
- }
- }
- goods.setUpdateUserId(user.getUserId());
- goods.setUpdateTime(new Date());
- return goodsDao.update(goods);
- }
- @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);
- }
- }
|