GoodsServiceImpl.java 20 KB

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