GoodsServiceImpl.java 19 KB

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