GoodsServiceImpl.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. package com.kmall.admin.service.impl;
  2. import com.google.common.collect.ImmutableBiMap;
  3. import com.kmall.admin.dao.*;
  4. import com.kmall.admin.entity.*;
  5. import com.kmall.admin.service.GoodsService;
  6. import com.kmall.api.contants.Dict;
  7. import com.kmall.common.entity.SysUserEntity;
  8. import com.kmall.common.utils.*;
  9. import com.kmall.common.utils.excel.ExcelImport;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.stereotype.Service;
  12. import org.springframework.transaction.annotation.Transactional;
  13. import org.springframework.web.multipart.MultipartFile;
  14. import java.math.BigDecimal;
  15. import java.util.Date;
  16. import java.util.HashMap;
  17. import java.util.List;
  18. import java.util.Map;
  19. /**
  20. * Service实现类
  21. *
  22. * @author Scott
  23. * @email
  24. * @date 2017-08-21 21:19:49
  25. */
  26. @Service("goodsService")
  27. public class GoodsServiceImpl implements GoodsService {
  28. @Autowired
  29. private GoodsDao goodsDao;
  30. @Autowired
  31. private GoodsAttributeDao goodsAttributeDao;
  32. @Autowired
  33. private AttributeCategoryDao attributeCategoryDao;
  34. @Autowired
  35. private ProductDao productDao;
  36. @Autowired
  37. private GoodsGalleryDao goodsGalleryDao;
  38. @Autowired
  39. private GoodsSpecificationDao goodsSpecificationDao;
  40. @Autowired
  41. private ProductStoreRelaDao productStoreRelaDao;
  42. @Autowired
  43. private StoreDao storeDao;
  44. @Autowired
  45. private GoodsGroupDao goodsGroupDao;
  46. @Autowired
  47. private CategoryDao categoryDao;
  48. @Override
  49. public GoodsEntity queryObject(Integer id) {
  50. Map<String, Object> map = new HashMap<String, Object>();
  51. map.put("goodsId", id);
  52. List<GoodsAttributeEntity> attributeEntities = goodsAttributeDao.queryList(map);
  53. List<ProductEntity> productEntityList = productDao.queryList(map);
  54. GoodsEntity entity = goodsDao.queryObject(id);
  55. entity.setAttributeEntityList(attributeEntities);
  56. entity.setProductEntityList(productEntityList);
  57. return entity;
  58. }
  59. @Override
  60. public List<GoodsEntity> queryList(Map<String, Object> map) {
  61. return goodsDao.queryList(map);
  62. }
  63. @Override
  64. public List<GoodsEntity> querySame(Map<String, Object> map) {
  65. return goodsDao.querySame(map);
  66. }
  67. @Override
  68. public int queryTotal(Map<String, Object> map) {
  69. return goodsDao.queryTotal(map);
  70. }
  71. @Override
  72. @Transactional
  73. public int save(GoodsEntity goods) {
  74. Map<String, Object> valideDate = MapBeanUtil.fromObject(goods);
  75. ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
  76. builder.put("attributeCategory", "商品分类");
  77. builder.put("categoryId", "商品二级分类");
  78. builder.put("goodsSn", "商品编码");
  79. builder.put("goodsBizType", "货品业务类型");
  80. builder.put("name", "商品名称");
  81. builder.put("brandId", "品牌");
  82. builder.put("freightId", "运费模版");
  83. builder.put("goodsDesc", "商品描述");
  84. builder.put("isOnSale", "上架");
  85. builder.put("goodsUnit", "商品单位");
  86. builder.put("primaryPicUrl", "商品主图");
  87. builder.put("listPicUrl", "商品列表图");
  88. builder.put("goodsRate", "商品税率");
  89. builder.put("retailPrice", "零售价格");
  90. builder.put("isHot", "热销");
  91. if (!Dict.orderBizType.item_11.getItem().equals(goods.getGoodsBizType())) {
  92. // 海关信息,普通货物可不添加
  93. builder.put("sku", "SKU");
  94. builder.put("prodBarcode", "产品编码");
  95. builder.put("brand", "产品品牌");
  96. builder.put("unitCode", "计量单位代码");
  97. builder.put("cusGoodsCode", "海关商品编码");
  98. builder.put("ciqProdModel", "国检规格型号");
  99. builder.put("oriCntCode", "原产国代码");
  100. builder.put("cusDeclEle", "海关申报要素");
  101. builder.put("cusRecCode", "海关备案编号");
  102. }
  103. R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
  104. if (Integer.valueOf(r.get("code").toString()) != 0) {
  105. throw new RRException(r.get("msg").toString());
  106. }
  107. // 商品轮播图
  108. List<GoodsGalleryEntity> galleryEntityList = goods.getGoodsImgList();
  109. if (galleryEntityList == null || galleryEntityList.size() <= 0) {
  110. throw new RRException("至少添加一张商品轮播图!");
  111. }
  112. SysUserEntity user = ShiroUtils.getUserEntity();
  113. Map<String, Object> map = new HashMap<>();
  114. map.put("isSame", "true");
  115. map.put("sku", goods.getSku());
  116. map.put("goodsSn", goods.getGoodsSn());
  117. map.put("goodsBizType", goods.getGoodsBizType());
  118. List<GoodsEntity> list = querySame(map);
  119. if (list != null && list.size() != 0) {
  120. throw new RRException("已存在该商品编码或该货品业务类型下已存在此SKU!");
  121. }
  122. // 添加商品
  123. goods.setAttributeCategory(categoryDao.queryObject(goods.getCategoryId()).getParentId());
  124. goods.setAddTime(new Date());
  125. goods.setIsDelete(0);
  126. goods.setIsNew(0);
  127. goods.setCreateUserId(user.getUserId());
  128. goods.setUpdateUserId(user.getUserId());
  129. goods.setUpdateTime(new Date());
  130. goods.setModTime(new Date());
  131. goods.setCreateTime(new Date());
  132. // 新增商品
  133. goodsDao.save(goods);
  134. Long id = goods.getId();
  135. // 添加商品轮播图
  136. for (GoodsGalleryEntity galleryEntity : galleryEntityList) {
  137. galleryEntity.setGoodsId(id);
  138. goodsGalleryDao.save(galleryEntity);
  139. }
  140. // 添加商品参数
  141. List<GoodsAttributeEntity> attributeEntityList = goods.getAttributeEntityList();
  142. if (attributeEntityList != null && attributeEntityList.size() > 0) {
  143. for (GoodsAttributeEntity item : attributeEntityList) {
  144. if (item.getIsDelete() == 0) {
  145. if (item.getAttributeId() != null && StringUtils.isNotEmpty(item.getValue())) {
  146. item.setGoodsId(id);
  147. goodsAttributeDao.save(item);
  148. } else if (item.getAttributeId() != null && StringUtils.isNullOrEmpty(item.getValue())) {
  149. throw new RRException("商品属性【" + attributeCategoryDao.queryObject(item.getAttributeId()).getName() + "】值不能为空!");
  150. } else if (item.getAttributeId() == null) {
  151. continue;
  152. }
  153. }
  154. }
  155. }
  156. // 添加产品
  157. ProductEntity product = new ProductEntity();
  158. product.setGoodsId(id);
  159. product.setGoodsSn(goods.getGoodsSn());
  160. // 保税商品,普通货物暂不添加商品规格
  161. if (!Dict.orderBizType.item_11.getItem().equals(goods.getGoodsBizType())) {
  162. // 添加商品规格
  163. GoodsSpecificationEntity goodsSpecification = new GoodsSpecificationEntity();
  164. goodsSpecification.setGoodsId(id);
  165. goodsSpecification.setValue(goods.getCiqProdModel());
  166. goodsSpecification.setSpecificationId(1);
  167. goodsSpecificationDao.save(goodsSpecification);
  168. product.setGoodsSpecificationIds(goodsSpecification.getId().toString());
  169. product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
  170. }
  171. return productDao.save(product);
  172. }
  173. @Override
  174. @Transactional
  175. public int update(GoodsEntity goods) {
  176. Map<String, Object> valideDate = MapBeanUtil.fromObject(goods);
  177. ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
  178. builder.put("attributeCategory", "商品分类");
  179. builder.put("categoryId", "商品二级分类");
  180. builder.put("goodsSn", "商品编码");
  181. builder.put("goodsBizType", "货品业务类型");
  182. builder.put("name", "商品名称");
  183. builder.put("brandId", "品牌");
  184. builder.put("freightId", "运费模版");
  185. builder.put("goodsDesc", "商品描述");
  186. builder.put("isOnSale", "上架");
  187. builder.put("goodsUnit", "商品单位");
  188. builder.put("primaryPicUrl", "商品主图");
  189. builder.put("listPicUrl", "商品列表图");
  190. builder.put("goodsRate", "商品税率");
  191. builder.put("isHot", "热销");
  192. if (!Dict.orderBizType.item_11.getItem().equals(goods.getGoodsBizType())) {
  193. // 海关信息,普通货物可不添加
  194. builder.put("sku", "SKU");
  195. builder.put("retailPrice", "零售价");
  196. builder.put("prodBarcode", "产品编码");
  197. builder.put("brand", "产品品牌");
  198. builder.put("unitCode", "计量单位代码");
  199. builder.put("cusGoodsCode", "海关商品编码");
  200. builder.put("ciqProdModel", "国检规格型号");
  201. builder.put("oriCntCode", "原产国代码");
  202. builder.put("cusDeclEle", "海关申报要素");
  203. builder.put("cusRecCode", "海关备案编号");
  204. }
  205. R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
  206. if (Integer.valueOf(r.get("code").toString()) != 0) {
  207. throw new RRException(r.get("msg").toString());
  208. }
  209. // 商品轮播图
  210. List<GoodsGalleryEntity> galleryEntityList = goods.getGoodsImgList();
  211. if (galleryEntityList == null || galleryEntityList.size() <= 0) {
  212. throw new RRException("至少保留一张商品轮播图!");
  213. }
  214. SysUserEntity user = ShiroUtils.getUserEntity();
  215. Map<String, Object> map = new HashMap<>();
  216. map.put("isSame", "true");
  217. map.put("sku", goods.getSku());
  218. map.put("goodsSn", goods.getGoodsSn());
  219. map.put("goodsBizType", goods.getGoodsBizType());
  220. map.put("id", goods.getId());
  221. List<GoodsEntity> list = querySame(map);
  222. if (list != null && list.size() != 0) {
  223. throw new RRException("已存在该商品编码或该货品业务类型下已存在此SKU!");
  224. }
  225. // 修改商品
  226. goods.setAttributeCategory(categoryDao.queryObject(goods.getCategoryId()).getParentId());
  227. goods.setIsDelete(0);
  228. goods.setIsNew(0);
  229. goods.setUpdateUserId(user.getUserId());
  230. goods.setUpdateTime(new Date());
  231. goods.setModTime(new Date());
  232. // 修改商品
  233. goodsDao.update(goods);
  234. // 保税商品修改各个门店商品价格
  235. if (!Dict.orderBizType.item_11.getItem().equals(goods.getGoodsBizType())) {
  236. List<ProductStoreRelaEntity> productStoreRelaEntityList = productStoreRelaDao.queryByGoodsId(goods.getId());
  237. if (productStoreRelaEntityList != null && productStoreRelaEntityList.size() > 0) {
  238. for (ProductStoreRelaEntity productStoreRela : productStoreRelaEntityList) {
  239. productStoreRela.setRetailPrice(goods.getRetailPrice());
  240. productStoreRelaDao.update(productStoreRela);
  241. }
  242. }
  243. }
  244. // 修改商品轮播图
  245. goodsGalleryDao.deleteByGoodsId(goods.getId());
  246. for (GoodsGalleryEntity galleryEntity : galleryEntityList) {
  247. galleryEntity.setGoodsId(goods.getId());
  248. goodsGalleryDao.save(galleryEntity);
  249. }
  250. // 修改商品参数
  251. List<GoodsAttributeEntity> attributeEntityList = goods.getAttributeEntityList();
  252. if (attributeEntityList != null && attributeEntityList.size() > 0) {
  253. for (GoodsAttributeEntity item : attributeEntityList) {
  254. if (item.getIsDelete() == 0) {
  255. if (item.getAttributeId() != null && StringUtils.isNotEmpty(item.getValue())) {
  256. item.setGoodsId(goods.getId());
  257. goodsAttributeDao.save(item);
  258. } else if (item.getAttributeId() != null && StringUtils.isNullOrEmpty(item.getValue())) {
  259. throw new RRException("商品属性【" + attributeCategoryDao.queryObject(item.getAttributeId()).getName() + "】值不能为空!");
  260. } else if (item.getId() != null) {
  261. goodsAttributeDao.update(item);
  262. } else if (item.getAttributeId() == null) {
  263. continue;
  264. }
  265. } else if (item.getIsDelete() == 1) {
  266. goodsAttributeDao.delete(item.getId());
  267. }
  268. }
  269. }
  270. // 修改产品
  271. ProductEntity product = productDao.queryObjectBySn(goods.getGoodsSn());
  272. // 保税商品,普通货物暂不添加商品规格
  273. if (!Dict.orderBizType.item_11.getItem().equals(goods.getGoodsBizType())) {
  274. // 添加商品规格
  275. GoodsSpecificationEntity goodsSpecification = goodsSpecificationDao.queryByGoodsId(goods.getId());
  276. goodsSpecification.setValue(goods.getCiqProdModel());
  277. goodsSpecificationDao.update(goodsSpecification);
  278. product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
  279. }
  280. return productDao.update(product);
  281. }
  282. @Override
  283. public int delete(Integer id) {
  284. SysUserEntity user = ShiroUtils.getUserEntity();
  285. GoodsEntity goodsEntity = goodsDao.queryObject(id);
  286. goodsEntity.setIsDelete(1);
  287. goodsEntity.setIsOnSale(0);
  288. goodsEntity.setUpdateUserId(user.getUserId());
  289. goodsEntity.setUpdateTime(new Date());
  290. //更新团购
  291. Map params = new HashMap();
  292. params.put("goodsId", id);
  293. List<GoodsGroupEntity> groupVos = goodsGroupDao.queryList(params);
  294. if (null != groupVos && groupVos.size() > 0) {
  295. for (GoodsGroupEntity groupVo : groupVos) {
  296. groupVo.setOpenStatus(3);
  297. goodsGroupDao.update(groupVo);
  298. }
  299. }
  300. return goodsDao.update(goodsEntity);
  301. }
  302. @Override
  303. @Transactional
  304. public int deleteBatch(Integer[] ids) {
  305. int result = 0;
  306. for (Integer id : ids) {
  307. result += delete(id);
  308. }
  309. return result;
  310. }
  311. @Override
  312. @Transactional
  313. public int back(Integer[] ids) {
  314. SysUserEntity user = ShiroUtils.getUserEntity();
  315. int result = 0;
  316. for (Integer id : ids) {
  317. GoodsEntity goodsEntity = queryObject(id);
  318. goodsEntity.setIsDelete(0);
  319. goodsEntity.setIsOnSale(1);
  320. goodsEntity.setUpdateUserId(user.getUserId());
  321. goodsEntity.setUpdateTime(new Date());
  322. result += goodsDao.update(goodsEntity);
  323. }
  324. return result;
  325. }
  326. @Override
  327. public int enSale(Integer id) {
  328. SysUserEntity user = ShiroUtils.getUserEntity();
  329. GoodsEntity goodsEntity = queryObject(id);
  330. if (1 == goodsEntity.getIsOnSale()) {
  331. throw new RRException("此商品已处于上架状态!");
  332. }
  333. goodsEntity.setIsOnSale(1);
  334. goodsEntity.setUpdateUserId(user.getUserId());
  335. goodsEntity.setUpdateTime(new Date());
  336. return goodsDao.update(goodsEntity);
  337. }
  338. @Override
  339. public int unSale(Integer id) {
  340. SysUserEntity user = ShiroUtils.getUserEntity();
  341. GoodsEntity goodsEntity = queryObject(id);
  342. if (0 == goodsEntity.getIsOnSale()) {
  343. throw new RRException("此商品已处于下架状态!");
  344. }
  345. goodsEntity.setIsOnSale(0);
  346. goodsEntity.setUpdateUserId(user.getUserId());
  347. goodsEntity.setUpdateTime(new Date());
  348. return goodsDao.update(goodsEntity);
  349. }
  350. @Override
  351. public int enSaleBatch(Integer[] ids) {
  352. int result = 0;
  353. SysUserEntity user = ShiroUtils.getUserEntity();
  354. for (Integer id : ids) {
  355. GoodsEntity goodsEntity = queryObject(id);
  356. goodsEntity.setIsOnSale(1);
  357. goodsEntity.setUpdateUserId(user.getUserId());
  358. goodsEntity.setUpdateTime(new Date());
  359. result += goodsDao.update(goodsEntity);
  360. }
  361. return result;
  362. }
  363. @Override
  364. public int unSaleBatch(Integer[] ids) {
  365. int result = 0;
  366. SysUserEntity user = ShiroUtils.getUserEntity();
  367. for (Integer id : ids) {
  368. GoodsEntity goodsEntity = queryObject(id);
  369. goodsEntity.setIsOnSale(0);
  370. goodsEntity.setUpdateUserId(user.getUserId());
  371. goodsEntity.setUpdateTime(new Date());
  372. result += goodsDao.update(goodsEntity);
  373. }
  374. return result;
  375. }
  376. @Override
  377. @Transactional
  378. public int uploadExcel(MultipartFile file) {
  379. SysUserEntity user = ShiroUtils.getUserEntity();
  380. List<String[]> list = ExcelImport.getExcelData(file);
  381. // 取门店名称
  382. StoreEntity storeEntity = storeDao.queryObjectByName(list.get(0)[3]);
  383. if (null == storeEntity) {
  384. return 0;
  385. }
  386. //去除表头两行、底部合计
  387. if (list != null && list.size() > 3) {
  388. ProductStoreRelaEntity storeRelaEntity;
  389. ProductEntity productEntity;
  390. for (int i = 2; i < list.size() - 1; i++) {
  391. String[] item = list.get(i);
  392. String goodsSn = item[0];
  393. productEntity = productDao.queryObjectBySn(goodsSn);
  394. if (StringUtils.isNullOrEmpty(goodsSn)) {
  395. continue;
  396. }
  397. if (null == productEntity || null == productEntity.getId()) {
  398. continue;
  399. }
  400. storeRelaEntity = productStoreRelaDao.queryByStoreIdProductId(storeEntity.getId(), productEntity.getId());
  401. if (null != storeRelaEntity && null != storeRelaEntity.getId()) {
  402. storeRelaEntity.setRetailPrice(new BigDecimal(item[6]));
  403. storeRelaEntity.setStockNum(Integer.valueOf(item[3].replace(".00", "")));
  404. storeRelaEntity.setStockPrice(new BigDecimal(item[4]));
  405. productStoreRelaDao.update(storeRelaEntity);
  406. } else {
  407. storeRelaEntity = new ProductStoreRelaEntity();
  408. storeRelaEntity.setGoodsId(productEntity.getGoodsId());
  409. storeRelaEntity.setProductId(productEntity.getId());
  410. storeRelaEntity.setRetailPrice(new BigDecimal(item[6]));
  411. storeRelaEntity.setMarketPrice(new BigDecimal(item[6]));
  412. storeRelaEntity.setStockNum(Integer.valueOf(item[3]));
  413. storeRelaEntity.setStockPrice(new BigDecimal(item[4]));
  414. storeRelaEntity.setStoreId(storeEntity.getId());
  415. productStoreRelaDao.save(storeRelaEntity);
  416. }
  417. }
  418. }
  419. return 1;
  420. }
  421. public GoodsEntity queryObjectBySn(String goodsSn) {
  422. return goodsDao.queryObjectBySn(goodsSn);
  423. }
  424. }