GoodsServiceImpl.java 19 KB

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