1
0

GoodsServiceImpl.java 20 KB

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