GoodsServiceImpl.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. productStoreRela.setMarketPrice(goods.getMarketPrice());
  241. productStoreRelaDao.update(productStoreRela);
  242. }
  243. }
  244. }
  245. // 修改商品轮播图
  246. goodsGalleryDao.deleteByGoodsId(goods.getId());
  247. for (GoodsGalleryEntity galleryEntity : galleryEntityList) {
  248. galleryEntity.setGoodsId(goods.getId());
  249. goodsGalleryDao.save(galleryEntity);
  250. }
  251. // 修改商品参数
  252. List<GoodsAttributeEntity> attributeEntityList = goods.getAttributeEntityList();
  253. if (attributeEntityList != null && attributeEntityList.size() > 0) {
  254. for (GoodsAttributeEntity item : attributeEntityList) {
  255. if (item.getIsDelete() == 0) {
  256. if (item.getAttributeId() != null && StringUtils.isNotEmpty(item.getValue())) {
  257. item.setGoodsId(goods.getId());
  258. goodsAttributeDao.save(item);
  259. } else if (item.getAttributeId() != null && StringUtils.isNullOrEmpty(item.getValue())) {
  260. throw new RRException("商品属性【" + attributeCategoryDao.queryObject(item.getAttributeId()).getName() + "】值不能为空!");
  261. } else if (item.getId() != null) {
  262. goodsAttributeDao.update(item);
  263. } else if (item.getAttributeId() == null) {
  264. continue;
  265. }
  266. } else if (item.getIsDelete() == 1) {
  267. goodsAttributeDao.delete(item.getId());
  268. }
  269. }
  270. }
  271. // 修改产品
  272. ProductEntity product = productDao.queryObjectBySn(goods.getGoodsSn());
  273. // 保税商品,普通货物暂不添加商品规格
  274. if (!Dict.orderBizType.item_11.getItem().equals(goods.getGoodsBizType())) {
  275. // 添加商品规格
  276. GoodsSpecificationEntity goodsSpecification = goodsSpecificationDao.queryByGoodsId(goods.getId());
  277. goodsSpecification.setValue(goods.getCiqProdModel());
  278. goodsSpecificationDao.update(goodsSpecification);
  279. product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
  280. }
  281. return productDao.update(product);
  282. }
  283. @Override
  284. public int delete(Integer id) {
  285. SysUserEntity user = ShiroUtils.getUserEntity();
  286. GoodsEntity goodsEntity = goodsDao.queryObject(id);
  287. goodsEntity.setIsDelete(1);
  288. goodsEntity.setIsOnSale(0);
  289. goodsEntity.setUpdateUserId(user.getUserId());
  290. goodsEntity.setUpdateTime(new Date());
  291. //更新团购
  292. Map params = new HashMap();
  293. params.put("goodsId", id);
  294. List<GoodsGroupEntity> groupVos = goodsGroupDao.queryList(params);
  295. if (null != groupVos && groupVos.size() > 0) {
  296. for (GoodsGroupEntity groupVo : groupVos) {
  297. groupVo.setOpenStatus(3);
  298. goodsGroupDao.update(groupVo);
  299. }
  300. }
  301. return goodsDao.update(goodsEntity);
  302. }
  303. @Override
  304. @Transactional
  305. public int deleteBatch(Integer[] ids) {
  306. int result = 0;
  307. for (Integer id : ids) {
  308. result += delete(id);
  309. }
  310. return result;
  311. }
  312. @Override
  313. @Transactional
  314. public int back(Integer[] ids) {
  315. SysUserEntity user = ShiroUtils.getUserEntity();
  316. int result = 0;
  317. for (Integer id : ids) {
  318. GoodsEntity goodsEntity = queryObject(id);
  319. goodsEntity.setIsDelete(0);
  320. goodsEntity.setIsOnSale(1);
  321. goodsEntity.setUpdateUserId(user.getUserId());
  322. goodsEntity.setUpdateTime(new Date());
  323. result += goodsDao.update(goodsEntity);
  324. }
  325. return result;
  326. }
  327. @Override
  328. public int enSale(Integer id) {
  329. SysUserEntity user = ShiroUtils.getUserEntity();
  330. GoodsEntity goodsEntity = queryObject(id);
  331. if (1 == goodsEntity.getIsOnSale()) {
  332. throw new RRException("此商品已处于上架状态!");
  333. }
  334. goodsEntity.setIsOnSale(1);
  335. goodsEntity.setUpdateUserId(user.getUserId());
  336. goodsEntity.setUpdateTime(new Date());
  337. return goodsDao.update(goodsEntity);
  338. }
  339. @Override
  340. public int unSale(Integer id) {
  341. SysUserEntity user = ShiroUtils.getUserEntity();
  342. GoodsEntity goodsEntity = queryObject(id);
  343. if (0 == goodsEntity.getIsOnSale()) {
  344. throw new RRException("此商品已处于下架状态!");
  345. }
  346. goodsEntity.setIsOnSale(0);
  347. goodsEntity.setUpdateUserId(user.getUserId());
  348. goodsEntity.setUpdateTime(new Date());
  349. return goodsDao.update(goodsEntity);
  350. }
  351. @Override
  352. public int enSaleBatch(Integer[] ids) {
  353. int result = 0;
  354. SysUserEntity user = ShiroUtils.getUserEntity();
  355. for (Integer id : ids) {
  356. GoodsEntity goodsEntity = queryObject(id);
  357. goodsEntity.setIsOnSale(1);
  358. goodsEntity.setUpdateUserId(user.getUserId());
  359. goodsEntity.setUpdateTime(new Date());
  360. result += goodsDao.update(goodsEntity);
  361. }
  362. return result;
  363. }
  364. @Override
  365. public int unSaleBatch(Integer[] ids) {
  366. int result = 0;
  367. SysUserEntity user = ShiroUtils.getUserEntity();
  368. for (Integer id : ids) {
  369. GoodsEntity goodsEntity = queryObject(id);
  370. goodsEntity.setIsOnSale(0);
  371. goodsEntity.setUpdateUserId(user.getUserId());
  372. goodsEntity.setUpdateTime(new Date());
  373. result += goodsDao.update(goodsEntity);
  374. }
  375. return result;
  376. }
  377. @Override
  378. @Transactional
  379. public int uploadExcel(MultipartFile file) {
  380. SysUserEntity user = ShiroUtils.getUserEntity();
  381. List<String[]> list = ExcelImport.getExcelData(file);
  382. // 取门店名称
  383. StoreEntity storeEntity = storeDao.queryObjectByName(list.get(0)[3]);
  384. if (null == storeEntity) {
  385. return 0;
  386. }
  387. //去除表头两行、底部合计
  388. if (list != null && list.size() > 3) {
  389. ProductStoreRelaEntity storeRelaEntity;
  390. ProductEntity productEntity;
  391. for (int i = 2; i < list.size() - 1; i++) {
  392. String[] item = list.get(i);
  393. String goodsSn = item[0];
  394. productEntity = productDao.queryObjectBySn(goodsSn);
  395. if (StringUtils.isNullOrEmpty(goodsSn)) {
  396. continue;
  397. }
  398. if (null == productEntity || null == productEntity.getId()) {
  399. continue;
  400. }
  401. storeRelaEntity = productStoreRelaDao.queryByStoreIdProductId(storeEntity.getId(), productEntity.getId());
  402. if (null != storeRelaEntity && null != storeRelaEntity.getId()) {
  403. storeRelaEntity.setRetailPrice(new BigDecimal(item[6]));
  404. storeRelaEntity.setStockNum(Integer.valueOf(item[3].replace(".00", "")));
  405. storeRelaEntity.setStockPrice(new BigDecimal(item[4]));
  406. productStoreRelaDao.update(storeRelaEntity);
  407. } else {
  408. storeRelaEntity = new ProductStoreRelaEntity();
  409. storeRelaEntity.setGoodsId(productEntity.getGoodsId());
  410. storeRelaEntity.setProductId(productEntity.getId());
  411. storeRelaEntity.setRetailPrice(new BigDecimal(item[6]));
  412. storeRelaEntity.setMarketPrice(new BigDecimal(item[6]));
  413. storeRelaEntity.setStockNum(Integer.valueOf(item[3]));
  414. storeRelaEntity.setStockPrice(new BigDecimal(item[4]));
  415. storeRelaEntity.setStoreId(storeEntity.getId());
  416. productStoreRelaDao.save(storeRelaEntity);
  417. }
  418. }
  419. }
  420. return 1;
  421. }
  422. public GoodsEntity queryObjectBySn(String goodsSn) {
  423. return goodsDao.queryObjectBySn(goodsSn);
  424. }
  425. }