GoodsServiceImpl.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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 AttributeDao attributeDao;
  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 GoodsEntity queryObjectByProdBarcodeAndBizType(String prodBarcode){
  61. Map<String, Object> map = new HashMap<String, Object>();
  62. map.put("prodBarcode", prodBarcode);
  63. GoodsEntity entity = goodsDao.queryObjectByProdBarcodeAndBizType(prodBarcode);
  64. return entity;
  65. }
  66. @Override
  67. public List<GoodsEntity> queryList(Map<String, Object> map) {
  68. return goodsDao.queryList(map);
  69. }
  70. @Override
  71. public List<GoodsEntity> querySame(Map<String, Object> map) {
  72. return goodsDao.querySame(map);
  73. }
  74. @Override
  75. public int queryTotal(Map<String, Object> map) {
  76. return goodsDao.queryTotal(map);
  77. }
  78. @Override
  79. @Transactional
  80. public int save(GoodsEntity goods) {
  81. Map<String, Object> valideDate = MapBeanUtil.fromObject(goods);
  82. ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
  83. builder.put("attributeCategory", "商品分类");
  84. builder.put("categoryId", "商品二级分类");
  85. builder.put("goodsSn", "商品编码");
  86. builder.put("goodsBizType", "货品业务类型");
  87. builder.put("name", "商品名称");
  88. builder.put("brandId", "品牌");
  89. builder.put("supplierId", "供应商");
  90. builder.put("freightId", "运费模版");
  91. builder.put("goodsDesc", "商品描述");
  92. builder.put("isOnSale", "上架");
  93. builder.put("goodsUnit", "商品单位");
  94. builder.put("primaryPicUrl", "商品主图");
  95. builder.put("listPicUrl", "商品列表图");
  96. builder.put("isHot", "热销");
  97. R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
  98. if (Integer.valueOf(r.get("code").toString()) != 0) {
  99. throw new RRException(r.get("msg").toString());
  100. } else {
  101. if (!Dict.orderBizType.item_11.getItem().equals(goods.getGoodsBizType())) {
  102. // 海关信息,普通货物可不添加
  103. builder.put("sku", "SKU");
  104. builder.put("goodsRate", "商品税率");
  105. builder.put("retailPrice", "零售价");
  106. builder.put("prodBarcode", "产品编码");
  107. builder.put("brand", "产品品牌");
  108. builder.put("unitCode", "计量单位代码");
  109. builder.put("cusGoodsCode", "海关商品编码");
  110. builder.put("ciqProdModel", "国检规格型号");
  111. builder.put("oriCntCode", "原产国代码");
  112. builder.put("cusDeclEle", "海关申报要素");
  113. builder.put("cusRecCode", "海关备案编号");
  114. }
  115. r = ValidatorUtil.isEmpty(builder.build(), valideDate);
  116. if (Integer.valueOf(r.get("code").toString()) != 0) {
  117. throw new RRException(r.get("msg").toString());
  118. }
  119. }
  120. // 商品轮播图
  121. List<GoodsGalleryEntity> galleryEntityList = goods.getGoodsImgList();
  122. if (galleryEntityList == null || galleryEntityList.size() <= 0) {
  123. throw new RRException("至少添加一张商品轮播图!");
  124. }
  125. SysUserEntity user = ShiroUtils.getUserEntity();
  126. Map<String, Object> map = new HashMap<>();
  127. map.put("isSame", "true");
  128. map.put("sku", goods.getSku());
  129. map.put("goodsSn", goods.getGoodsSn());
  130. map.put("goodsBizType", goods.getGoodsBizType());
  131. List<GoodsEntity> list = querySame(map);
  132. if (list != null && list.size() != 0) {
  133. throw new RRException("已存在该商品编码或该货品业务类型下已存在此SKU!");
  134. }
  135. // 添加商品
  136. if (Dict.orderBizType.item_02.getItem().equals(goods.getGoodsBizType())
  137. || Dict.orderBizType.item_10.getItem().equals(goods.getGoodsBizType())) {
  138. goods.setIsHot(0);
  139. }
  140. goods.setAttributeCategory(categoryDao.queryObject(goods.getCategoryId()).getParentId());
  141. goods.setAddTime(new Date());
  142. goods.setIsDelete(0);
  143. goods.setIsNew(0);
  144. goods.setCreateUserId(user.getUserId());
  145. goods.setUpdateUserId(user.getUserId());
  146. goods.setUpdateTime(new Date());
  147. goods.setModTime(new Date());
  148. goods.setCreateTime(new Date());
  149. // 新增商品
  150. goodsDao.save(goods);
  151. Long id = goods.getId();
  152. // 添加商品轮播图
  153. for (int i=0;i<galleryEntityList.size();i++) {
  154. GoodsGalleryEntity galleryEntity =galleryEntityList.get(i);
  155. galleryEntity.setGoodsId(id);
  156. galleryEntity.setSortOrder((i+1));
  157. galleryEntity.setFileType("0");//图片
  158. goodsGalleryDao.save(galleryEntity);
  159. }
  160. if(org.apache.commons.lang.StringUtils.isNotEmpty(goods.getVideoUrl())){
  161. GoodsGalleryEntity galleryEntity = new GoodsGalleryEntity();
  162. galleryEntity.setGoodsId(id);
  163. galleryEntity.setSortOrder(0);
  164. galleryEntity.setFileType("1");//视频
  165. goodsGalleryDao.save(galleryEntity);
  166. }
  167. // 添加商品参数
  168. List<GoodsAttributeEntity> attributeEntityList = goods.getAttributeEntityList();
  169. if (attributeEntityList != null && attributeEntityList.size() > 0) {
  170. for (GoodsAttributeEntity item : attributeEntityList) {
  171. if (item.getIsDelete() == 0) {
  172. if (item.getId() == null && item.getAttributeId() != null && StringUtils.isNotEmpty(item.getValue())) {
  173. item.setGoodsId(id);
  174. goodsAttributeDao.save(item);
  175. } else if (item.getId() == null && item.getAttributeId() != null && StringUtils.isNullOrEmpty(item.getValue())) {
  176. throw new RRException("商品属性【" + attributeDao.queryObject(item.getAttributeId()).getName() + "】值不能为空!");
  177. } else if (item.getId() == null && item.getAttributeId() == null) {
  178. continue;
  179. }
  180. }
  181. }
  182. }
  183. // 添加产品
  184. ProductEntity product = new ProductEntity();
  185. product.setGoodsId(id);
  186. product.setGoodsSn(goods.getGoodsSn());
  187. // 保税商品,普通货物暂不添加商品规格
  188. if (!Dict.orderBizType.item_11.getItem().equals(goods.getGoodsBizType())) {
  189. // 添加商品规格
  190. GoodsSpecificationEntity goodsSpecification = new GoodsSpecificationEntity();
  191. goodsSpecification.setGoodsId(id);
  192. goodsSpecification.setValue(goods.getCiqProdModel());
  193. goodsSpecification.setSpecificationId(1);
  194. goodsSpecificationDao.save(goodsSpecification);
  195. product.setGoodsSpecificationIds(goodsSpecification.getId().toString());
  196. product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
  197. }
  198. return productDao.save(product);
  199. }
  200. @Override
  201. @Transactional
  202. public int update(GoodsEntity goods) {
  203. Map<String, Object> valideDate = MapBeanUtil.fromObject(goods);
  204. ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
  205. builder.put("attributeCategory", "商品分类");
  206. builder.put("categoryId", "商品二级分类");
  207. builder.put("goodsSn", "商品编码");
  208. builder.put("goodsBizType", "货品业务类型");
  209. builder.put("name", "商品名称");
  210. builder.put("brandId", "品牌");
  211. builder.put("supplierId", "供应商");
  212. builder.put("freightId", "运费模版");
  213. builder.put("goodsDesc", "商品描述");
  214. builder.put("isOnSale", "上架");
  215. builder.put("goodsUnit", "商品单位");
  216. builder.put("primaryPicUrl", "商品主图");
  217. builder.put("listPicUrl", "商品列表图");
  218. builder.put("isHot", "热销");
  219. R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
  220. if (Integer.valueOf(r.get("code").toString()) != 0) {
  221. throw new RRException(r.get("msg").toString());
  222. } else {
  223. if (!Dict.orderBizType.item_11.getItem().equals(goods.getGoodsBizType())) {
  224. // 海关信息,普通货物可不添加
  225. builder.put("sku", "SKU");
  226. builder.put("goodsRate", "商品税率");
  227. builder.put("retailPrice", "零售价");
  228. builder.put("prodBarcode", "产品编码");
  229. builder.put("brand", "产品品牌");
  230. builder.put("unitCode", "计量单位代码");
  231. builder.put("cusGoodsCode", "海关商品编码");
  232. builder.put("ciqProdModel", "国检规格型号");
  233. builder.put("oriCntCode", "原产国代码");
  234. builder.put("cusDeclEle", "海关申报要素");
  235. builder.put("cusRecCode", "海关备案编号");
  236. }
  237. r = ValidatorUtil.isEmpty(builder.build(), valideDate);
  238. if (Integer.valueOf(r.get("code").toString()) != 0) {
  239. throw new RRException(r.get("msg").toString());
  240. }
  241. }
  242. // 商品轮播图
  243. List<GoodsGalleryEntity> galleryEntityList = goods.getGoodsImgList();
  244. if (galleryEntityList == null || galleryEntityList.size() <= 0) {
  245. throw new RRException("至少保留一张商品轮播图!");
  246. }
  247. SysUserEntity user = ShiroUtils.getUserEntity();
  248. Map<String, Object> map = new HashMap<>();
  249. map.put("isSame", "true");
  250. map.put("sku", goods.getSku());
  251. map.put("goodsSn", goods.getGoodsSn());
  252. map.put("goodsBizType", goods.getGoodsBizType());
  253. map.put("id", goods.getId());
  254. List<GoodsEntity> list = querySame(map);
  255. if (list != null && list.size() != 0) {
  256. throw new RRException("已存在该商品编码或该货品业务类型下已存在此SKU!");
  257. }
  258. // 修改商品
  259. if (Dict.orderBizType.item_02.getItem().equals(goods.getGoodsBizType())
  260. || Dict.orderBizType.item_10.getItem().equals(goods.getGoodsBizType())) {
  261. goods.setIsHot(0);
  262. }
  263. goods.setAttributeCategory(categoryDao.queryObject(goods.getCategoryId()).getParentId());
  264. goods.setIsDelete(0);
  265. goods.setIsNew(0);
  266. goods.setUpdateUserId(user.getUserId());
  267. goods.setUpdateTime(new Date());
  268. goods.setModTime(new Date());
  269. // 修改商品
  270. goodsDao.update(goods);
  271. // 保税商品修改各个门店商品价格
  272. if (!Dict.orderBizType.item_11.getItem().equals(goods.getGoodsBizType())) {
  273. List<ProductStoreRelaEntity> productStoreRelaEntityList = productStoreRelaDao.queryByGoodsId(goods.getId());
  274. if (productStoreRelaEntityList != null && productStoreRelaEntityList.size() > 0) {
  275. for (ProductStoreRelaEntity productStoreRela : productStoreRelaEntityList) {
  276. productStoreRela.setRetailPrice(goods.getRetailPrice());
  277. productStoreRela.setMarketPrice(goods.getMarketPrice());
  278. productStoreRelaDao.update(productStoreRela);
  279. }
  280. }
  281. }
  282. // 修改商品轮播图
  283. goodsGalleryDao.deleteByGoodsId(goods.getId());
  284. for (int i=0;i<galleryEntityList.size();i++) {
  285. GoodsGalleryEntity galleryEntity =galleryEntityList.get(i);
  286. galleryEntity.setGoodsId(goods.getId());
  287. galleryEntity.setSortOrder((i+1));
  288. galleryEntity.setFileType("0");//图片
  289. goodsGalleryDao.save(galleryEntity);
  290. }
  291. if(org.apache.commons.lang.StringUtils.isNotEmpty(goods.getVideoUrl())){
  292. GoodsGalleryEntity galleryEntity = new GoodsGalleryEntity();
  293. galleryEntity.setGoodsId(goods.getId());
  294. galleryEntity.setSortOrder(0);
  295. galleryEntity.setFileType("1");//视频
  296. galleryEntity.setImgUrl(goods.getVideoUrl());
  297. goodsGalleryDao.save(galleryEntity);
  298. }
  299. // 修改商品参数
  300. List<GoodsAttributeEntity> attributeEntityList = goods.getAttributeEntityList();
  301. if (attributeEntityList != null && attributeEntityList.size() > 0) {
  302. for (GoodsAttributeEntity item : attributeEntityList) {
  303. if (item.getIsDelete() == 0) {
  304. if (item.getId() != null) {
  305. goodsAttributeDao.update(item);
  306. } else if (item.getId() == null && item.getAttributeId() != null && StringUtils.isNotEmpty(item.getValue())) {
  307. item.setGoodsId(goods.getId());
  308. goodsAttributeDao.save(item);
  309. } else if (item.getId() == null && item.getAttributeId() != null && StringUtils.isNullOrEmpty(item.getValue())) {
  310. throw new RRException("商品属性【" + attributeDao.queryObject(item.getAttributeId()).getName() + "】值不能为空!");
  311. } else if (item.getId() == null && item.getAttributeId() == null) {
  312. continue;
  313. }
  314. } else if (item.getIsDelete() == 1) {
  315. goodsAttributeDao.delete(item.getId());
  316. }
  317. }
  318. }
  319. // 修改产品
  320. ProductEntity product = productDao.queryObjectBySn(goods.getGoodsSn());
  321. GoodsSpecificationEntity goodsSpecification = new GoodsSpecificationEntity();
  322. // 保税商品,普通货物暂不添加商品规格
  323. if (!Dict.orderBizType.item_11.getItem().equals(goods.getGoodsBizType())) {
  324. // 添加商品规格
  325. goodsSpecification = goodsSpecificationDao.queryByGoodsId(goods.getId());
  326. goodsSpecification.setValue(goods.getCiqProdModel());
  327. goodsSpecificationDao.update(goodsSpecification);
  328. }
  329. if(product == null){
  330. product = new ProductEntity();
  331. product.setGoodsSn(goods.getGoodsSn());
  332. product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
  333. product.setGoodsSpecificationIds(goodsSpecification.getSpecificationId()+"");
  334. product.setGoodsId(goods.getId());
  335. product.setGoodsNumber(goods.getGoodsNumber());
  336. product.setGoodsDefault(0);
  337. return productDao.save(product);
  338. }else{
  339. product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
  340. return productDao.update(product);
  341. }
  342. }
  343. @Override
  344. public int delete(Integer id) {
  345. SysUserEntity user = ShiroUtils.getUserEntity();
  346. GoodsEntity goodsEntity = goodsDao.queryObject(id);
  347. goodsEntity.setIsDelete(1);
  348. goodsEntity.setIsOnSale(0);
  349. goodsEntity.setUpdateUserId(user.getUserId());
  350. goodsEntity.setUpdateTime(new Date());
  351. //更新团购
  352. Map params = new HashMap();
  353. params.put("goodsId", id);
  354. List<GoodsGroupEntity> groupVos = goodsGroupDao.queryList(params);
  355. if (null != groupVos && groupVos.size() > 0) {
  356. for (GoodsGroupEntity groupVo : groupVos) {
  357. groupVo.setOpenStatus(3);
  358. goodsGroupDao.update(groupVo);
  359. }
  360. }
  361. return goodsDao.update(goodsEntity);
  362. }
  363. @Override
  364. @Transactional
  365. public int deleteBatch(Integer[] ids) {
  366. int result = 0;
  367. for (Integer id : ids) {
  368. result += delete(id);
  369. }
  370. return result;
  371. }
  372. @Override
  373. @Transactional
  374. public int back(Integer[] ids) {
  375. SysUserEntity user = ShiroUtils.getUserEntity();
  376. int result = 0;
  377. for (Integer id : ids) {
  378. GoodsEntity goodsEntity = queryObject(id);
  379. goodsEntity.setIsDelete(0);
  380. goodsEntity.setIsOnSale(1);
  381. goodsEntity.setUpdateUserId(user.getUserId());
  382. goodsEntity.setUpdateTime(new Date());
  383. result += goodsDao.update(goodsEntity);
  384. }
  385. return result;
  386. }
  387. @Override
  388. public int enSale(Integer id) {
  389. SysUserEntity user = ShiroUtils.getUserEntity();
  390. GoodsEntity goodsEntity = queryObject(id);
  391. if (1 == goodsEntity.getIsOnSale()) {
  392. throw new RRException("此商品已处于上架状态!");
  393. }
  394. goodsEntity.setIsOnSale(1);
  395. goodsEntity.setUpdateUserId(user.getUserId());
  396. goodsEntity.setUpdateTime(new Date());
  397. return goodsDao.update(goodsEntity);
  398. }
  399. @Override
  400. public int unSale(Integer id) {
  401. SysUserEntity user = ShiroUtils.getUserEntity();
  402. GoodsEntity goodsEntity = queryObject(id);
  403. if (0 == goodsEntity.getIsOnSale()) {
  404. throw new RRException("此商品已处于下架状态!");
  405. }
  406. goodsEntity.setIsOnSale(0);
  407. goodsEntity.setUpdateUserId(user.getUserId());
  408. goodsEntity.setUpdateTime(new Date());
  409. return goodsDao.update(goodsEntity);
  410. }
  411. @Override
  412. public int enSaleBatch(Integer[] ids) {
  413. int result = 0;
  414. SysUserEntity user = ShiroUtils.getUserEntity();
  415. for (Integer id : ids) {
  416. GoodsEntity goodsEntity = queryObject(id);
  417. goodsEntity.setIsOnSale(1);
  418. goodsEntity.setUpdateUserId(user.getUserId());
  419. goodsEntity.setUpdateTime(new Date());
  420. result += goodsDao.update(goodsEntity);
  421. }
  422. return result;
  423. }
  424. @Override
  425. public int unSaleBatch(Integer[] ids) {
  426. int result = 0;
  427. SysUserEntity user = ShiroUtils.getUserEntity();
  428. for (Integer id : ids) {
  429. GoodsEntity goodsEntity = queryObject(id);
  430. goodsEntity.setIsOnSale(0);
  431. goodsEntity.setUpdateUserId(user.getUserId());
  432. goodsEntity.setUpdateTime(new Date());
  433. result += goodsDao.update(goodsEntity);
  434. }
  435. return result;
  436. }
  437. @Override
  438. @Transactional
  439. public int uploadExcel(MultipartFile file) {
  440. SysUserEntity user = ShiroUtils.getUserEntity();
  441. List<String[]> list = ExcelImport.getExcelData(file);
  442. // 取门店名称
  443. StoreEntity storeEntity = storeDao.queryObjectByName(list.get(0)[3]);
  444. if (null == storeEntity) {
  445. return 0;
  446. }
  447. //去除表头两行、底部合计
  448. if (list != null && list.size() > 3) {
  449. ProductStoreRelaEntity storeRelaEntity;
  450. ProductEntity productEntity;
  451. for (int i = 2; i < list.size() - 1; i++) {
  452. String[] item = list.get(i);
  453. String goodsSn = item[0];
  454. productEntity = productDao.queryObjectBySn(goodsSn);
  455. if (StringUtils.isNullOrEmpty(goodsSn)) {
  456. continue;
  457. }
  458. if (null == productEntity || null == productEntity.getId()) {
  459. continue;
  460. }
  461. storeRelaEntity = productStoreRelaDao.queryByStoreIdProductId(storeEntity.getId(), productEntity.getId());
  462. if (null != storeRelaEntity && null != storeRelaEntity.getId()) {
  463. storeRelaEntity.setRetailPrice(new BigDecimal(item[6]));
  464. storeRelaEntity.setStockNum(Integer.valueOf(item[3].replace(".00", "")));
  465. storeRelaEntity.setStockPrice(new BigDecimal(item[4]));
  466. productStoreRelaDao.update(storeRelaEntity);
  467. } else {
  468. storeRelaEntity = new ProductStoreRelaEntity();
  469. storeRelaEntity.setGoodsId(productEntity.getGoodsId());
  470. storeRelaEntity.setProductId(productEntity.getId());
  471. storeRelaEntity.setRetailPrice(new BigDecimal(item[6]));
  472. storeRelaEntity.setMarketPrice(new BigDecimal(item[6]));
  473. storeRelaEntity.setStockNum(Integer.valueOf(item[3]));
  474. storeRelaEntity.setStockPrice(new BigDecimal(item[4]));
  475. storeRelaEntity.setStoreId(storeEntity.getId());
  476. productStoreRelaDao.save(storeRelaEntity);
  477. }
  478. }
  479. }
  480. return 1;
  481. }
  482. public GoodsEntity queryObjectBySn(String goodsSn) {
  483. return goodsDao.queryObjectBySn(goodsSn);
  484. }
  485. }