GoodsServiceImpl.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. package com.kmall.admin.service.impl;
  2. import com.kmall.admin.annotation.DataFilter;
  3. import com.kmall.admin.dao.*;
  4. import com.kmall.admin.entity.*;
  5. import com.kmall.admin.service.GoodsService;
  6. import com.kmall.common.entity.SysUserEntity;
  7. import com.kmall.common.utils.RRException;
  8. import com.kmall.common.utils.ShiroUtils;
  9. import com.kmall.common.utils.StringUtils;
  10. import com.kmall.common.utils.excel.ExcelImport;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.stereotype.Service;
  13. import org.springframework.transaction.annotation.Transactional;
  14. import org.springframework.web.multipart.MultipartFile;
  15. import java.math.BigDecimal;
  16. import java.util.Date;
  17. import java.util.HashMap;
  18. import java.util.List;
  19. import java.util.Map;
  20. /**
  21. * Service实现类
  22. *
  23. * @author Scott
  24. * @email
  25. * @date 2017-08-21 21:19:49
  26. */
  27. @Service("goodsService")
  28. public class GoodsServiceImpl implements GoodsService {
  29. @Autowired
  30. private GoodsDao goodsDao;
  31. @Autowired
  32. private GoodsAttributeDao goodsAttributeDao;
  33. @Autowired
  34. private ProductDao productDao;
  35. @Autowired
  36. private GoodsGalleryDao goodsGalleryDao;
  37. @Autowired
  38. private GoodsSpecificationDao goodsSpecificationDao;
  39. @Autowired
  40. private ProductStoreRelaDao productStoreRelaDao;
  41. @Autowired
  42. private StoreDao storeDao;
  43. @Autowired
  44. private GoodsGroupDao goodsGroupDao;
  45. @Override
  46. public GoodsEntity queryObject(Integer id) {
  47. Map<String, Object> map = new HashMap<String, Object>();
  48. map.put("goodsId", id);
  49. List<GoodsAttributeEntity> attributeEntities = goodsAttributeDao.queryList(map);
  50. List<ProductEntity> productEntityList = productDao.queryList(map);
  51. GoodsEntity entity = goodsDao.queryObject(id);
  52. entity.setAttributeEntityList(attributeEntities);
  53. entity.setProductEntityList(productEntityList);
  54. return entity;
  55. }
  56. @Override
  57. public List<GoodsEntity> queryList(Map<String, Object> map) {
  58. return goodsDao.queryList(map);
  59. }
  60. @Override
  61. public int queryTotal(Map<String, Object> map) {
  62. return goodsDao.queryTotal(map);
  63. }
  64. @Override
  65. @Transactional
  66. public int save(GoodsEntity goods) {
  67. SysUserEntity user = ShiroUtils.getUserEntity();
  68. Map<String, Object> map = new HashMap<>();
  69. map.put("name", goods.getName());
  70. List<GoodsEntity> list = queryList(map);
  71. if (null != list && list.size() != 0) {
  72. throw new RRException("商品名称已存在!");
  73. }
  74. Long id = goodsDao.queryMaxId() + 1;
  75. goods.setId(id);
  76. goods.setAddTime(new Date());
  77. //保存商品详情页面显示的属性
  78. List<GoodsAttributeEntity> attributeEntityList = goods.getAttributeEntityList();
  79. if (null != attributeEntityList && attributeEntityList.size() > 0) {
  80. for (GoodsAttributeEntity item : attributeEntityList) {
  81. if (item.getGoodsId() == null) {
  82. item.setGoodsId(id);
  83. goodsAttributeDao.save(item);
  84. }
  85. }
  86. }
  87. // 产品更新
  88. List<ProductEntity> productEntityList = goods.getProductEntityList();
  89. if (null != productEntityList && productEntityList.size() > 0) {
  90. for (ProductEntity productEntity : productEntityList) {
  91. if (1 == productEntity.getIsDelete()) {
  92. productDao.delete(productEntity);
  93. if (null != productEntity.getGoodsSpecificationIds()) {
  94. goodsSpecificationDao.delete(productEntity.getGoodsSpecificationIds());
  95. }
  96. }
  97. if (StringUtils.isNullOrEmpty(productEntity.getGoodsSn())) {
  98. throw new RRException("产品编码不能为空!");
  99. }
  100. // 判断是否goods_sn重复
  101. ProductEntity productDb = productDao.queryObjectBySn(productEntity.getGoodsSn());
  102. if (null != productDb && !productDb.getId().equals(productEntity.getId())) {
  103. throw new RRException("产品编码重复!"); // 重复不操作
  104. }
  105. goods.setPrimaryProductId(productEntity.getId());
  106. GoodsSpecificationEntity specificationEntity = new GoodsSpecificationEntity();
  107. // 规格
  108. if (StringUtils.isNullOrEmpty(productEntity.getGoodsSpecificationIds())
  109. && StringUtils.isNotEmpty(productEntity.getGoodsSpecificationNameValue())) {
  110. specificationEntity.setGoodsId(goods.getId());
  111. specificationEntity.setSpecificationId(1); // 规格写死
  112. specificationEntity.setValue(productEntity.getGoodsSpecificationNameValue());
  113. goodsSpecificationDao.save(specificationEntity);
  114. productEntity.setGoodsSpecificationNameValue(specificationEntity.getId() + "");
  115. } else if (StringUtils.isNotEmpty(productEntity.getGoodsSpecificationIds())) {
  116. specificationEntity = goodsSpecificationDao.queryObject(productEntity.getGoodsSpecificationIds());
  117. specificationEntity.setValue(productEntity.getGoodsSpecificationNameValue());
  118. goodsSpecificationDao.update(specificationEntity);
  119. }
  120. if (null != productEntity.getId()) {
  121. productDao.save(productEntity);
  122. } else {
  123. productDao.update(productEntity);
  124. }
  125. // default
  126. if (null == goods.getPrimaryProductId()) {
  127. goods.setPrimaryProductId(productEntity.getId());
  128. } else if (productEntity.getGoodsDefault() == 1) {
  129. goods.setPrimaryProductId(productEntity.getId());
  130. }
  131. }
  132. // default
  133. for (ProductEntity productEntity : productEntityList) {
  134. if (null == goods.getPrimaryProductId() && 1 != productEntity.getIsDelete()) {
  135. goods.setPrimaryProductId(productEntity.getId());
  136. break;
  137. }
  138. }
  139. }
  140. //商品轮播图
  141. List<GoodsGalleryEntity> galleryEntityList = goods.getGoodsImgList();
  142. if (null != galleryEntityList && galleryEntityList.size() > 0) {
  143. for (GoodsGalleryEntity galleryEntity : galleryEntityList) {
  144. galleryEntity.setGoodsId(id);
  145. goodsGalleryDao.save(galleryEntity);
  146. }
  147. }
  148. goods.setIsDelete(0);
  149. goods.setCreateUserId(user.getUserId());
  150. goods.setUpdateUserId(user.getUserId());
  151. goods.setUpdateTime(new Date());
  152. goods.setModTime(new Date());
  153. goods.setCreateTime(new Date());
  154. return goodsDao.save(goods);
  155. }
  156. @Override
  157. @Transactional
  158. public int update(GoodsEntity goods) {
  159. Map<String, Long> map = new HashMap<String, Long>();
  160. map.put("goodsId", goods.getId());
  161. SysUserEntity user = ShiroUtils.getUserEntity();
  162. List<GoodsAttributeEntity> attributeEntityList = goods.getAttributeEntityList();
  163. //商品参数
  164. if (null != attributeEntityList && attributeEntityList.size() > 0) {
  165. for (GoodsAttributeEntity goodsAttributeEntity : attributeEntityList) {
  166. if (null == goodsAttributeEntity.getId() && StringUtils.isNullOrEmpty(goodsAttributeEntity.getAttributeId())) {
  167. continue;
  168. }
  169. if (goodsAttributeEntity.getIsDelete() == 1) {
  170. goodsAttributeDao.delete(goodsAttributeEntity);
  171. } else if (null != goodsAttributeEntity.getId()) {
  172. goodsAttributeEntity.setGoodsId(goods.getId());
  173. goodsAttributeDao.update(goodsAttributeEntity);
  174. } else {
  175. goodsAttributeEntity.setGoodsId(goods.getId());
  176. goodsAttributeDao.save(goodsAttributeEntity);
  177. }
  178. }
  179. }
  180. // 产品更新
  181. List<ProductEntity> productEntityList = goods.getProductEntityList();
  182. if (null != productEntityList && productEntityList.size() > 0) {
  183. for (ProductEntity productEntity : productEntityList) {
  184. if (1 == productEntity.getIsDelete()) {
  185. if (null == productEntity.getId()) {
  186. continue;
  187. }
  188. productDao.delete(productEntity);
  189. if (null != productEntity.getGoodsSpecificationIds()) {
  190. goodsSpecificationDao.delete(productEntity.getGoodsSpecificationIds());
  191. }
  192. continue;
  193. }
  194. if (StringUtils.isNullOrEmpty(productEntity.getGoodsSn())) {
  195. throw new RRException("产品编码不能为空!");
  196. }
  197. // 判断是否goods_sn重复
  198. ProductEntity productDb = productDao.queryObjectBySn(productEntity.getGoodsSn());
  199. if (null != productDb && !productDb.getId().equals(productEntity.getId())) {
  200. throw new RRException("产品编码重复!"); // 重复不操作
  201. }
  202. GoodsSpecificationEntity specificationEntity = new GoodsSpecificationEntity();
  203. // 规格
  204. if (StringUtils.isNullOrEmpty(productEntity.getGoodsSpecificationIds())
  205. && StringUtils.isNotEmpty(productEntity.getGoodsSpecificationNameValue())) {
  206. specificationEntity.setGoodsId(goods.getId());
  207. specificationEntity.setSpecificationId(1); // 规格写死
  208. specificationEntity.setValue(productEntity.getGoodsSpecificationNameValue());
  209. goodsSpecificationDao.save(specificationEntity);
  210. productEntity.setGoodsSpecificationIds(specificationEntity.getId() + "");
  211. } else if (StringUtils.isNotEmpty(productEntity.getGoodsSpecificationIds())) {
  212. specificationEntity = goodsSpecificationDao.queryObject(productEntity.getGoodsSpecificationIds());
  213. specificationEntity.setValue(productEntity.getGoodsSpecificationNameValue());
  214. goodsSpecificationDao.update(specificationEntity);
  215. }
  216. if (null == productEntity.getId()) {
  217. productEntity.setGoodsId(goods.getId());
  218. productDao.save(productEntity);
  219. } else {
  220. productDao.update(productEntity);
  221. }
  222. // default
  223. if (null == goods.getPrimaryProductId()) {
  224. goods.setPrimaryProductId(productEntity.getId());
  225. } else if (productEntity.getGoodsDefault() == 1) {
  226. goods.setPrimaryProductId(productEntity.getId());
  227. }
  228. }
  229. // default
  230. for (ProductEntity productEntity : productEntityList) {
  231. if (null == goods.getPrimaryProductId() && 1 != productEntity.getIsDelete()) {
  232. goods.setPrimaryProductId(productEntity.getId());
  233. break;
  234. }
  235. }
  236. }
  237. //商品轮播图
  238. List<GoodsGalleryEntity> galleryEntityList = goods.getGoodsImgList();
  239. if (null != galleryEntityList && galleryEntityList.size() > 0)
  240. {
  241. for (GoodsGalleryEntity galleryEntity : galleryEntityList) {
  242. if (galleryEntity.getIsDelete() == 1) {
  243. goodsGalleryDao.delete(galleryEntity);
  244. } else if (null != galleryEntity.getId()) {
  245. galleryEntity.setGoodsId(goods.getId());
  246. goodsGalleryDao.update(galleryEntity);
  247. } else {
  248. galleryEntity.setGoodsId(goods.getId());
  249. goodsGalleryDao.save(galleryEntity);
  250. }
  251. }
  252. }
  253. goods.setUpdateUserId(user.getUserId());
  254. goods.setUpdateTime(new Date());
  255. return goodsDao.update(goods);
  256. }
  257. @Override
  258. public int delete(Integer id) {
  259. SysUserEntity user = ShiroUtils.getUserEntity();
  260. GoodsEntity goodsEntity = goodsDao.queryObject(id);
  261. goodsEntity.setIsDelete(1);
  262. goodsEntity.setIsOnSale(0);
  263. goodsEntity.setUpdateUserId(user.getUserId());
  264. goodsEntity.setUpdateTime(new Date());
  265. //更新团购
  266. Map params = new HashMap();
  267. params.put("goodsId", id);
  268. List<GoodsGroupEntity> groupVos = goodsGroupDao.queryList(params);
  269. if (null != groupVos && groupVos.size() > 0) {
  270. for (GoodsGroupEntity groupVo : groupVos) {
  271. groupVo.setOpenStatus(3);
  272. goodsGroupDao.update(groupVo);
  273. }
  274. }
  275. return goodsDao.update(goodsEntity);
  276. }
  277. @Override
  278. @Transactional
  279. public int deleteBatch(Integer[] ids) {
  280. int result = 0;
  281. for (Integer id : ids) {
  282. result += delete(id);
  283. }
  284. return result;
  285. }
  286. @Override
  287. @Transactional
  288. public int back(Integer[] ids) {
  289. SysUserEntity user = ShiroUtils.getUserEntity();
  290. int result = 0;
  291. for (Integer id : ids) {
  292. GoodsEntity goodsEntity = queryObject(id);
  293. goodsEntity.setIsDelete(0);
  294. goodsEntity.setIsOnSale(1);
  295. goodsEntity.setUpdateUserId(user.getUserId());
  296. goodsEntity.setUpdateTime(new Date());
  297. result += goodsDao.update(goodsEntity);
  298. }
  299. return result;
  300. }
  301. @Override
  302. public int enSale(Integer id) {
  303. SysUserEntity user = ShiroUtils.getUserEntity();
  304. GoodsEntity goodsEntity = queryObject(id);
  305. if (1 == goodsEntity.getIsOnSale()) {
  306. throw new RRException("此商品已处于上架状态!");
  307. }
  308. goodsEntity.setIsOnSale(1);
  309. goodsEntity.setUpdateUserId(user.getUserId());
  310. goodsEntity.setUpdateTime(new Date());
  311. return goodsDao.update(goodsEntity);
  312. }
  313. @Override
  314. public int unSale(Integer id) {
  315. SysUserEntity user = ShiroUtils.getUserEntity();
  316. GoodsEntity goodsEntity = queryObject(id);
  317. if (0 == goodsEntity.getIsOnSale()) {
  318. throw new RRException("此商品已处于下架状态!");
  319. }
  320. goodsEntity.setIsOnSale(0);
  321. goodsEntity.setUpdateUserId(user.getUserId());
  322. goodsEntity.setUpdateTime(new Date());
  323. return goodsDao.update(goodsEntity);
  324. }
  325. @Override
  326. public int enSaleBatch(Integer[] ids) {
  327. int result = 0;
  328. SysUserEntity user = ShiroUtils.getUserEntity();
  329. for (Integer id : ids) {
  330. GoodsEntity goodsEntity = queryObject(id);
  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 unSaleBatch(Integer[] ids) {
  340. int result = 0;
  341. SysUserEntity user = ShiroUtils.getUserEntity();
  342. for (Integer id : ids) {
  343. GoodsEntity goodsEntity = queryObject(id);
  344. goodsEntity.setIsOnSale(0);
  345. goodsEntity.setUpdateUserId(user.getUserId());
  346. goodsEntity.setUpdateTime(new Date());
  347. result += goodsDao.update(goodsEntity);
  348. }
  349. return result;
  350. }
  351. @Override
  352. @Transactional
  353. public int uploadExcel(MultipartFile file) {
  354. SysUserEntity user = ShiroUtils.getUserEntity();
  355. List<String[]> list = ExcelImport.getExcelData(file);
  356. // 取门店名称
  357. StoreEntity storeEntity = storeDao.queryObjectByName(list.get(0)[3]);
  358. if (null == storeEntity) {
  359. return 0;
  360. }
  361. //去除表头两行、底部合计
  362. if (list != null && list.size() > 3) {
  363. ProductStoreRelaEntity storeRelaEntity;
  364. ProductEntity productEntity;
  365. for (int i = 2; i < list.size() - 1; i++) {
  366. String[] item = list.get(i);
  367. String goodsSn = item[0];
  368. productEntity = productDao.queryObjectBySn(goodsSn);
  369. if (StringUtils.isNullOrEmpty(goodsSn)) {
  370. continue;
  371. }
  372. if (null == productEntity || null == productEntity.getId()) {
  373. continue;
  374. }
  375. storeRelaEntity = productStoreRelaDao.queryByStoreIdProductId(storeEntity.getId(), productEntity.getId());
  376. if (null != storeRelaEntity && null != storeRelaEntity.getId()) {
  377. storeRelaEntity.setRetailPrice(new BigDecimal(item[6]));
  378. storeRelaEntity.setStockNum(Integer.valueOf(item[3].replace(".00", "")));
  379. storeRelaEntity.setStockPrice(new BigDecimal(item[4]));
  380. productStoreRelaDao.update(storeRelaEntity);
  381. } else {
  382. storeRelaEntity = new ProductStoreRelaEntity();
  383. storeRelaEntity.setGoodsId(productEntity.getGoodsId());
  384. storeRelaEntity.setProductId(productEntity.getId());
  385. storeRelaEntity.setRetailPrice(new BigDecimal(item[6]));
  386. storeRelaEntity.setMarketPrice(new BigDecimal(item[6]));
  387. storeRelaEntity.setStockNum(Integer.valueOf(item[3]));
  388. storeRelaEntity.setStockPrice(new BigDecimal(item[4]));
  389. storeRelaEntity.setStoreId(storeEntity.getId());
  390. productStoreRelaDao.save(storeRelaEntity);
  391. }
  392. }
  393. }
  394. return 1;
  395. }
  396. public GoodsEntity queryObjectBySn(String goodsSn) {
  397. return goodsDao.queryObjectBySn(goodsSn);
  398. }
  399. }