GoodsServiceImpl.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. GoodsSpecificationEntity goodsSpecification = new GoodsSpecificationEntity();
  292. // 保税商品,普通货物暂不添加商品规格
  293. if (!Dict.orderBizType.item_11.getItem().equals(goods.getGoodsBizType())) {
  294. // 添加商品规格
  295. goodsSpecification = goodsSpecificationDao.queryByGoodsId(goods.getId());
  296. goodsSpecification.setValue(goods.getCiqProdModel());
  297. goodsSpecificationDao.update(goodsSpecification);
  298. }
  299. if(product == null){
  300. product = new ProductEntity();
  301. product.setGoodsSn(goods.getGoodsSn());
  302. product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
  303. product.setGoodsSpecificationIds(goodsSpecification.getSpecificationId()+"");
  304. product.setGoodsId(goods.getId());
  305. product.setGoodsNumber(goods.getGoodsNumber());
  306. product.setGoodsDefault(0);
  307. return productDao.save(product);
  308. }else{
  309. product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
  310. return productDao.update(product);
  311. }
  312. }
  313. @Override
  314. public int delete(Integer id) {
  315. SysUserEntity user = ShiroUtils.getUserEntity();
  316. GoodsEntity goodsEntity = goodsDao.queryObject(id);
  317. goodsEntity.setIsDelete(1);
  318. goodsEntity.setIsOnSale(0);
  319. goodsEntity.setUpdateUserId(user.getUserId());
  320. goodsEntity.setUpdateTime(new Date());
  321. //更新团购
  322. Map params = new HashMap();
  323. params.put("goodsId", id);
  324. List<GoodsGroupEntity> groupVos = goodsGroupDao.queryList(params);
  325. if (null != groupVos && groupVos.size() > 0) {
  326. for (GoodsGroupEntity groupVo : groupVos) {
  327. groupVo.setOpenStatus(3);
  328. goodsGroupDao.update(groupVo);
  329. }
  330. }
  331. return goodsDao.update(goodsEntity);
  332. }
  333. @Override
  334. @Transactional
  335. public int deleteBatch(Integer[] ids) {
  336. int result = 0;
  337. for (Integer id : ids) {
  338. result += delete(id);
  339. }
  340. return result;
  341. }
  342. @Override
  343. @Transactional
  344. public int back(Integer[] ids) {
  345. SysUserEntity user = ShiroUtils.getUserEntity();
  346. int result = 0;
  347. for (Integer id : ids) {
  348. GoodsEntity goodsEntity = queryObject(id);
  349. goodsEntity.setIsDelete(0);
  350. goodsEntity.setIsOnSale(1);
  351. goodsEntity.setUpdateUserId(user.getUserId());
  352. goodsEntity.setUpdateTime(new Date());
  353. result += goodsDao.update(goodsEntity);
  354. }
  355. return result;
  356. }
  357. @Override
  358. public int enSale(Integer id) {
  359. SysUserEntity user = ShiroUtils.getUserEntity();
  360. GoodsEntity goodsEntity = queryObject(id);
  361. if (1 == goodsEntity.getIsOnSale()) {
  362. throw new RRException("此商品已处于上架状态!");
  363. }
  364. goodsEntity.setIsOnSale(1);
  365. goodsEntity.setUpdateUserId(user.getUserId());
  366. goodsEntity.setUpdateTime(new Date());
  367. return goodsDao.update(goodsEntity);
  368. }
  369. @Override
  370. public int unSale(Integer id) {
  371. SysUserEntity user = ShiroUtils.getUserEntity();
  372. GoodsEntity goodsEntity = queryObject(id);
  373. if (0 == goodsEntity.getIsOnSale()) {
  374. throw new RRException("此商品已处于下架状态!");
  375. }
  376. goodsEntity.setIsOnSale(0);
  377. goodsEntity.setUpdateUserId(user.getUserId());
  378. goodsEntity.setUpdateTime(new Date());
  379. return goodsDao.update(goodsEntity);
  380. }
  381. @Override
  382. public int enSaleBatch(Integer[] ids) {
  383. int result = 0;
  384. SysUserEntity user = ShiroUtils.getUserEntity();
  385. for (Integer id : ids) {
  386. GoodsEntity goodsEntity = queryObject(id);
  387. goodsEntity.setIsOnSale(1);
  388. goodsEntity.setUpdateUserId(user.getUserId());
  389. goodsEntity.setUpdateTime(new Date());
  390. result += goodsDao.update(goodsEntity);
  391. }
  392. return result;
  393. }
  394. @Override
  395. public int unSaleBatch(Integer[] ids) {
  396. int result = 0;
  397. SysUserEntity user = ShiroUtils.getUserEntity();
  398. for (Integer id : ids) {
  399. GoodsEntity goodsEntity = queryObject(id);
  400. goodsEntity.setIsOnSale(0);
  401. goodsEntity.setUpdateUserId(user.getUserId());
  402. goodsEntity.setUpdateTime(new Date());
  403. result += goodsDao.update(goodsEntity);
  404. }
  405. return result;
  406. }
  407. @Override
  408. @Transactional
  409. public int uploadExcel(MultipartFile file) {
  410. SysUserEntity user = ShiroUtils.getUserEntity();
  411. List<String[]> list = ExcelImport.getExcelData(file);
  412. // 取门店名称
  413. StoreEntity storeEntity = storeDao.queryObjectByName(list.get(0)[3]);
  414. if (null == storeEntity) {
  415. return 0;
  416. }
  417. //去除表头两行、底部合计
  418. if (list != null && list.size() > 3) {
  419. ProductStoreRelaEntity storeRelaEntity;
  420. ProductEntity productEntity;
  421. for (int i = 2; i < list.size() - 1; i++) {
  422. String[] item = list.get(i);
  423. String goodsSn = item[0];
  424. productEntity = productDao.queryObjectBySn(goodsSn);
  425. if (StringUtils.isNullOrEmpty(goodsSn)) {
  426. continue;
  427. }
  428. if (null == productEntity || null == productEntity.getId()) {
  429. continue;
  430. }
  431. storeRelaEntity = productStoreRelaDao.queryByStoreIdProductId(storeEntity.getId(), productEntity.getId());
  432. if (null != storeRelaEntity && null != storeRelaEntity.getId()) {
  433. storeRelaEntity.setRetailPrice(new BigDecimal(item[6]));
  434. storeRelaEntity.setStockNum(Integer.valueOf(item[3].replace(".00", "")));
  435. storeRelaEntity.setStockPrice(new BigDecimal(item[4]));
  436. productStoreRelaDao.update(storeRelaEntity);
  437. } else {
  438. storeRelaEntity = new ProductStoreRelaEntity();
  439. storeRelaEntity.setGoodsId(productEntity.getGoodsId());
  440. storeRelaEntity.setProductId(productEntity.getId());
  441. storeRelaEntity.setRetailPrice(new BigDecimal(item[6]));
  442. storeRelaEntity.setMarketPrice(new BigDecimal(item[6]));
  443. storeRelaEntity.setStockNum(Integer.valueOf(item[3]));
  444. storeRelaEntity.setStockPrice(new BigDecimal(item[4]));
  445. storeRelaEntity.setStoreId(storeEntity.getId());
  446. productStoreRelaDao.save(storeRelaEntity);
  447. }
  448. }
  449. }
  450. return 1;
  451. }
  452. public GoodsEntity queryObjectBySn(String goodsSn) {
  453. return goodsDao.queryObjectBySn(goodsSn);
  454. }
  455. }