GoodsServiceImpl.java 21 KB

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