GoodsServiceImpl.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 (null != item.getGoodsId()) {
  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
  255. Date());
  256. return goodsDao.update(goods);
  257. }
  258. @Override
  259. public int delete(Integer id) {
  260. SysUserEntity user = ShiroUtils.getUserEntity();
  261. GoodsEntity goodsEntity = goodsDao.queryObject(id);
  262. goodsEntity.setIsDelete(1);
  263. goodsEntity.setIsOnSale(0);
  264. goodsEntity.setUpdateUserId(user.getUserId());
  265. goodsEntity.setUpdateTime(new Date());
  266. //更新团购
  267. Map params = new HashMap();
  268. params.put("goodsId", id);
  269. List<GoodsGroupEntity> groupVos = goodsGroupDao.queryList(params);
  270. if (null != groupVos && groupVos.size() > 0) {
  271. for (GoodsGroupEntity groupVo : groupVos) {
  272. groupVo.setOpenStatus(3);
  273. goodsGroupDao.update(groupVo);
  274. }
  275. }
  276. return goodsDao.update(goodsEntity);
  277. }
  278. @Override
  279. @Transactional
  280. public int deleteBatch(Integer[] ids) {
  281. int result = 0;
  282. for (Integer id : ids) {
  283. result += delete(id);
  284. }
  285. return result;
  286. }
  287. @Override
  288. @Transactional
  289. public int back(Integer[] ids) {
  290. SysUserEntity user = ShiroUtils.getUserEntity();
  291. int result = 0;
  292. for (Integer id : ids) {
  293. GoodsEntity goodsEntity = queryObject(id);
  294. goodsEntity.setIsDelete(0);
  295. goodsEntity.setIsOnSale(1);
  296. goodsEntity.setUpdateUserId(user.getUserId());
  297. goodsEntity.setUpdateTime(new Date());
  298. result += goodsDao.update(goodsEntity);
  299. }
  300. return result;
  301. }
  302. @Override
  303. public int enSale(Integer id) {
  304. SysUserEntity user = ShiroUtils.getUserEntity();
  305. GoodsEntity goodsEntity = queryObject(id);
  306. if (1 == goodsEntity.getIsOnSale()) {
  307. throw new RRException("此商品已处于上架状态!");
  308. }
  309. goodsEntity.setIsOnSale(1);
  310. goodsEntity.setUpdateUserId(user.getUserId());
  311. goodsEntity.setUpdateTime(new Date());
  312. return goodsDao.update(goodsEntity);
  313. }
  314. @Override
  315. public int unSale(Integer id) {
  316. SysUserEntity user = ShiroUtils.getUserEntity();
  317. GoodsEntity goodsEntity = queryObject(id);
  318. if (0 == goodsEntity.getIsOnSale()) {
  319. throw new RRException("此商品已处于下架状态!");
  320. }
  321. goodsEntity.setIsOnSale(0);
  322. goodsEntity.setUpdateUserId(user.getUserId());
  323. goodsEntity.setUpdateTime(new Date());
  324. return goodsDao.update(goodsEntity);
  325. }
  326. @Override
  327. public int enSaleBatch(Integer[] ids) {
  328. int result = 0;
  329. SysUserEntity user = ShiroUtils.getUserEntity();
  330. for (Integer id : ids) {
  331. GoodsEntity goodsEntity = queryObject(id);
  332. goodsEntity.setIsOnSale(1);
  333. goodsEntity.setUpdateUserId(user.getUserId());
  334. goodsEntity.setUpdateTime(new Date());
  335. result += goodsDao.update(goodsEntity);
  336. }
  337. return result;
  338. }
  339. @Override
  340. public int unSaleBatch(Integer[] ids) {
  341. int result = 0;
  342. SysUserEntity user = ShiroUtils.getUserEntity();
  343. for (Integer id : ids) {
  344. GoodsEntity goodsEntity = queryObject(id);
  345. goodsEntity.setIsOnSale(0);
  346. goodsEntity.setUpdateUserId(user.getUserId());
  347. goodsEntity.setUpdateTime(new Date());
  348. result += goodsDao.update(goodsEntity);
  349. }
  350. return result;
  351. }
  352. @Override
  353. @Transactional
  354. public int uploadExcel(MultipartFile file) {
  355. SysUserEntity user = ShiroUtils.getUserEntity();
  356. List<String[]> list = ExcelImport.getExcelData(file);
  357. // 取门店名称
  358. StoreEntity storeEntity = storeDao.queryObjectByName(list.get(0)[3]);
  359. if (null == storeEntity) {
  360. return 0;
  361. }
  362. //去除表头两行、底部合计
  363. if (list != null && list.size() > 3) {
  364. ProductStoreRelaEntity storeRelaEntity;
  365. ProductEntity productEntity;
  366. for (int i = 2; i < list.size() - 1; i++) {
  367. String[] item = list.get(i);
  368. String goodsSn = item[0];
  369. productEntity = productDao.queryObjectBySn(goodsSn);
  370. if (StringUtils.isNullOrEmpty(goodsSn)) {
  371. continue;
  372. }
  373. if (null == productEntity || null == productEntity.getId()) {
  374. continue;
  375. }
  376. storeRelaEntity = productStoreRelaDao.queryByStoreIdProductId(storeEntity.getId(), productEntity.getId());
  377. if (null != storeRelaEntity && null != storeRelaEntity.getId()) {
  378. storeRelaEntity.setRetailPrice(new BigDecimal(item[6]));
  379. storeRelaEntity.setStockNum(Integer.valueOf(item[3].replace(".00", "")));
  380. storeRelaEntity.setStockPrice(new BigDecimal(item[4]));
  381. productStoreRelaDao.update(storeRelaEntity);
  382. } else {
  383. storeRelaEntity = new ProductStoreRelaEntity();
  384. storeRelaEntity.setGoodsId(productEntity.getGoodsId());
  385. storeRelaEntity.setProductId(productEntity.getId());
  386. storeRelaEntity.setRetailPrice(new BigDecimal(item[6]));
  387. storeRelaEntity.setMarketPrice(new BigDecimal(item[6]));
  388. storeRelaEntity.setStockNum(Integer.valueOf(item[3]));
  389. storeRelaEntity.setStockPrice(new BigDecimal(item[4]));
  390. storeRelaEntity.setStoreId(storeEntity.getId());
  391. productStoreRelaDao.save(storeRelaEntity);
  392. }
  393. }
  394. }
  395. return 1;
  396. }
  397. public GoodsEntity queryObjectBySn(String goodsSn) {
  398. return goodsDao.queryObjectBySn(goodsSn);
  399. }
  400. }