1
0

GoodsServiceImpl.java 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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.dto.GoodsDto;
  5. import com.kmall.admin.entity.*;
  6. import com.kmall.admin.service.GoodsService;
  7. import com.kmall.common.constant.Dict;
  8. import com.kmall.common.entity.SysUserEntity;
  9. import com.kmall.common.utils.*;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.stereotype.Service;
  12. import org.springframework.transaction.annotation.Transactional;
  13. import java.math.BigDecimal;
  14. import java.util.*;
  15. /**
  16. * Service实现类
  17. *
  18. * @author Scott
  19. * @email
  20. * @date 2017-08-21 21:19:49
  21. */
  22. @Service("goodsService")
  23. public class GoodsServiceImpl implements GoodsService {
  24. @Autowired
  25. private GoodsDao goodsDao;
  26. @Autowired
  27. private GoodsAttributeDao goodsAttributeDao;
  28. @Autowired
  29. private AttributeDao attributeDao;
  30. @Autowired
  31. private ProductDao productDao;
  32. @Autowired
  33. private GoodsGalleryDao goodsGalleryDao;
  34. @Autowired
  35. private GoodsSpecificationDao goodsSpecificationDao;
  36. @Autowired
  37. private ProductStoreRelaDao productStoreRelaDao;
  38. @Autowired
  39. private StoreDao storeDao;
  40. @Autowired
  41. private GoodsGroupDao goodsGroupDao;
  42. @Autowired
  43. private CategoryDao categoryDao;
  44. @Autowired
  45. private SupplierDao supplierDao;
  46. @Autowired
  47. private BrandDao brandDao;
  48. @Autowired
  49. private FreightDao freightDao;
  50. @Autowired
  51. private SysCusNationCodeDao sysCusNationCodeDao;
  52. @Autowired
  53. private SysCusUnitCodeDao sysCusUnitCodeDao;
  54. @Autowired
  55. private MerchDao merchDao;
  56. @Autowired
  57. private ExportExceptionDataDao exportExceptionDataDao;
  58. @Override
  59. public GoodsEntity queryObject(Integer id) {
  60. Map<String, Object> map = new HashMap<String, Object>();
  61. map.put("goodsId", id);
  62. List<GoodsAttributeEntity> attributeEntities = goodsAttributeDao.queryList(map);
  63. List<ProductEntity> productEntityList = productDao.queryList(map);
  64. GoodsEntity entity = goodsDao.queryObject(id);
  65. entity.setAttributeEntityList(attributeEntities);
  66. entity.setProductEntityList(productEntityList);
  67. return entity;
  68. }
  69. @Override
  70. public GoodsEntity queryObjectByProdBarcodeAndBizType(String prodBarcode){
  71. Map<String, Object> map = new HashMap<String, Object>();
  72. map.put("prodBarcode", prodBarcode);
  73. GoodsEntity entity = goodsDao.queryObjectByProdBarcodeAndBizType(prodBarcode);
  74. return entity;
  75. }
  76. @Override
  77. public List<GoodsEntity> queryList(Map<String, Object> map) {
  78. return goodsDao.queryList(map);
  79. }
  80. @Override
  81. public List<GoodsEntity> querySame(Map<String, Object> map) {
  82. return goodsDao.querySame(map);
  83. }
  84. @Override
  85. public int queryTotal(Map<String, Object> map) {
  86. return goodsDao.queryTotal(map);
  87. }
  88. @Override
  89. @Transactional
  90. public int save(GoodsEntity goods) {
  91. Map<String, Object> valideDate = MapBeanUtil.fromObject(goods);
  92. ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
  93. builder.put("merchSn", "商户编号");
  94. builder.put("attributeCategory", "商品分类");
  95. builder.put("categoryId", "商品二级分类");
  96. builder.put("goodsSn", "商品编码");
  97. builder.put("name", "商品名称");
  98. builder.put("goodsUnit", "商品单位");
  99. builder.put("prodBarcode", "产品条码");
  100. builder.put("goodsBizType", "货品业务类型");
  101. builder.put("brandId", "品牌");
  102. builder.put("supplierId", "供应商");
  103. builder.put("freightId", "运费模版");
  104. builder.put("primaryPicUrl", "商品主图");
  105. builder.put("listPicUrl", "商品列表图");
  106. builder.put("goodsDesc", "商品描述");
  107. builder.put("isOnSale", "上架");
  108. builder.put("isHot", "热销");
  109. R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
  110. if (Integer.valueOf(r.get("code").toString()) != 0) {
  111. throw new RRException(r.get("msg").toString());
  112. } else {
  113. if (!Dict.orderBizType.item_11.getItem().equals(goods.getGoodsBizType())) {
  114. // 海关信息,普通货物可不添加
  115. builder.put("sku", "SKU");
  116. builder.put("goodsRate", "商品税率");
  117. builder.put("retailPrice", "零售价");
  118. builder.put("brand", "产品品牌");
  119. builder.put("unitCode", "计量单位代码");
  120. builder.put("cusGoodsCode", "海关商品编码");
  121. builder.put("ciqProdModel", "国检规格型号");
  122. builder.put("oriCntCode", "原产国代码");
  123. builder.put("cusDeclEle", "海关申报要素");
  124. builder.put("cusRecCode", "海关备案编号");
  125. }
  126. r = ValidatorUtil.isEmpty(builder.build(), valideDate);
  127. if (Integer.valueOf(r.get("code").toString()) != 0) {
  128. throw new RRException(r.get("msg").toString());
  129. }
  130. }
  131. // 商品轮播图
  132. List<GoodsGalleryEntity> galleryEntityList = goods.getGoodsImgList();
  133. if (galleryEntityList == null || galleryEntityList.size() <= 0) {
  134. throw new RRException("至少添加一张商品轮播图!");
  135. }
  136. GoodsEntity prodbarGoods = goodsDao.queryObjectByProdBarcode(goods.getProdBarcode(),"",null);
  137. if(prodbarGoods != null){
  138. throw new RRException("不能有重复的产品条码信息!");
  139. }
  140. SysUserEntity user = ShiroUtils.getUserEntity();
  141. Map<String, Object> map = new HashMap<>();
  142. map.put("isSame", "true");
  143. map.put("sku", goods.getSku());
  144. map.put("goodsSn", goods.getGoodsSn());
  145. map.put("goodsBizType", goods.getGoodsBizType());
  146. List<GoodsEntity> list = querySame(map);
  147. if (list != null && list.size() != 0) {
  148. throw new RRException("已存在该商品编码或该货品业务类型下已存在此SKU!");
  149. }
  150. // 添加商品
  151. if (Dict.orderBizType.item_02.getItem().equals(goods.getGoodsBizType())
  152. || Dict.orderBizType.item_10.getItem().equals(goods.getGoodsBizType())) {
  153. goods.setIsHot(0);
  154. }
  155. goods.setAttributeCategory(categoryDao.queryObject(goods.getCategoryId()).getParentId());
  156. goods.setAddTime(new Date());
  157. goods.setIsDelete(Integer.parseInt(Dict.isDelete.item_0.getItem()));
  158. goods.setIsNew(0);
  159. goods.setCreateUserId(user.getUserId());
  160. goods.setUpdateUserId(user.getUserId());
  161. goods.setUpdateTime(new Date());
  162. goods.setModTime(new Date());
  163. goods.setCreateTime(new Date());
  164. // 新增商品
  165. goodsDao.save(goods);
  166. Long id = goods.getId();
  167. // 添加商品轮播图
  168. for (int i=0;i<galleryEntityList.size();i++) {
  169. GoodsGalleryEntity galleryEntity =galleryEntityList.get(i);
  170. galleryEntity.setMerchSn(goods.getMerchSn());
  171. galleryEntity.setGoodsId(id);
  172. galleryEntity.setSortOrder((i+1));
  173. galleryEntity.setFileType("0");//图片
  174. goodsGalleryDao.save(galleryEntity);
  175. }
  176. if(org.apache.commons.lang.StringUtils.isNotEmpty(goods.getVideoUrl())){
  177. GoodsGalleryEntity galleryEntity = new GoodsGalleryEntity();
  178. galleryEntity.setMerchSn(goods.getMerchSn());
  179. galleryEntity.setGoodsId(id);
  180. galleryEntity.setSortOrder(0);
  181. galleryEntity.setFileType("1");//视频
  182. goodsGalleryDao.save(galleryEntity);
  183. }
  184. // 添加商品参数
  185. List<GoodsAttributeEntity> attributeEntityList = goods.getAttributeEntityList();
  186. if (attributeEntityList != null && attributeEntityList.size() > 0) {
  187. for (GoodsAttributeEntity item : attributeEntityList) {
  188. if (item.getIsDelete() == 0) {
  189. if (item.getId() == null && item.getAttributeId() != null && StringUtils.isNotEmpty(item.getValue())) {
  190. item.setGoodsId(id);
  191. item.setMerchSn(goods.getMerchSn());
  192. goodsAttributeDao.save(item);
  193. } else if (item.getId() == null && item.getAttributeId() != null && StringUtils.isNullOrEmpty(item.getValue())) {
  194. throw new RRException("商品属性【" + attributeDao.queryObject(item.getAttributeId()).getName() + "】值不能为空!");
  195. } else if (item.getId() == null && item.getAttributeId() == null) {
  196. continue;
  197. }
  198. }
  199. }
  200. }
  201. // 添加产品
  202. ProductEntity product = new ProductEntity();
  203. product.setGoodsId(id);
  204. product.setGoodsSn(goods.getGoodsSn());
  205. // 保税商品,普通货物暂不添加商品规格
  206. if (!Dict.orderBizType.item_11.getItem().equals(goods.getGoodsBizType())) {
  207. // 添加商品规格
  208. GoodsSpecificationEntity goodsSpecification = new GoodsSpecificationEntity();
  209. goodsSpecification.setGoodsId(id);
  210. goodsSpecification.setValue(goods.getCiqProdModel());
  211. goodsSpecification.setSpecificationId(1);
  212. goodsSpecificationDao.save(goodsSpecification);
  213. product.setGoodsSpecificationIds(goodsSpecification.getId().toString());
  214. product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
  215. }
  216. return productDao.save(product);
  217. }
  218. @Override
  219. @Transactional
  220. public int update(GoodsEntity goods) {
  221. Map<String, Object> valideDate = MapBeanUtil.fromObject(goods);
  222. ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
  223. builder.put("merchSn", "商户编号");
  224. builder.put("attributeCategory", "商品分类");
  225. builder.put("categoryId", "商品二级分类");
  226. builder.put("goodsSn", "商品编码");
  227. builder.put("name", "商品名称");
  228. builder.put("goodsUnit", "商品单位");
  229. builder.put("prodBarcode", "产品条码");
  230. builder.put("goodsBizType", "货品业务类型");
  231. builder.put("brandId", "品牌");
  232. builder.put("supplierId", "供应商");
  233. builder.put("freightId", "运费模版");
  234. builder.put("primaryPicUrl", "商品主图");
  235. builder.put("listPicUrl", "商品列表图");
  236. builder.put("goodsDesc", "商品描述");
  237. builder.put("isOnSale", "上架");
  238. builder.put("isHot", "热销");
  239. R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
  240. if (Integer.valueOf(r.get("code").toString()) != 0) {
  241. throw new RRException(r.get("msg").toString());
  242. } else {
  243. if (!Dict.orderBizType.item_11.getItem().equals(goods.getGoodsBizType())) {
  244. // 海关信息,普通货物可不添加
  245. builder.put("sku", "SKU");
  246. builder.put("goodsRate", "商品税率");
  247. builder.put("retailPrice", "零售价");
  248. builder.put("brand", "产品品牌");
  249. builder.put("unitCode", "计量单位代码");
  250. builder.put("cusGoodsCode", "海关商品编码");
  251. builder.put("ciqProdModel", "国检规格型号");
  252. builder.put("oriCntCode", "原产国代码");
  253. builder.put("cusRecCode", "海关备案编号");
  254. builder.put("cusDeclEle", "海关申报要素");
  255. }
  256. r = ValidatorUtil.isEmpty(builder.build(), valideDate);
  257. if (Integer.valueOf(r.get("code").toString()) != 0) {
  258. throw new RRException(r.get("msg").toString());
  259. }
  260. }
  261. // 商品轮播图
  262. List<GoodsGalleryEntity> galleryEntityList = goods.getGoodsImgList();
  263. if (galleryEntityList == null || galleryEntityList.size() <= 0) {
  264. throw new RRException("至少保留一张商品轮播图!");
  265. }
  266. GoodsEntity prodbarGoods = goodsDao.queryObjectByProdBarcode(goods.getProdBarcode(),"",goods.getId());
  267. if(prodbarGoods != null){
  268. throw new RRException("不能有重复的产品条码信息!");
  269. }
  270. SysUserEntity user = ShiroUtils.getUserEntity();
  271. Map<String, Object> map = new HashMap<>();
  272. map.put("isSame", "true");
  273. map.put("sku", goods.getSku());
  274. map.put("goodsSn", goods.getGoodsSn());
  275. map.put("goodsBizType", goods.getGoodsBizType());
  276. map.put("id", goods.getId());
  277. List<GoodsEntity> list = querySame(map);
  278. if (list != null && list.size() != 0) {
  279. throw new RRException("已存在该商品编码或该货品业务类型下已存在此SKU!");
  280. }
  281. // 修改商品
  282. if (Dict.orderBizType.item_02.getItem().equals(goods.getGoodsBizType())
  283. || Dict.orderBizType.item_10.getItem().equals(goods.getGoodsBizType())) {
  284. goods.setIsHot(0);
  285. }
  286. goods.setAttributeCategory(categoryDao.queryObject(goods.getCategoryId()).getParentId());
  287. goods.setIsDelete(Integer.parseInt(Dict.isDelete.item_0.getItem()));
  288. goods.setIsNew(0);
  289. goods.setUpdateUserId(user.getUserId());
  290. goods.setUpdateTime(new Date());
  291. goods.setModTime(new Date());
  292. // 修改商品
  293. goodsDao.update(goods);
  294. // 保税商品修改各个门店商品价格
  295. if (!Dict.orderBizType.item_11.getItem().equals(goods.getGoodsBizType())) {
  296. List<ProductStoreRelaEntity> productStoreRelaEntityList = productStoreRelaDao.queryByGoodsId(goods.getId());
  297. if (productStoreRelaEntityList != null && productStoreRelaEntityList.size() > 0) {
  298. for (ProductStoreRelaEntity productStoreRela : productStoreRelaEntityList) {
  299. productStoreRela.setRetailPrice(goods.getRetailPrice());
  300. productStoreRela.setMarketPrice(goods.getMarketPrice());
  301. productStoreRelaDao.update(productStoreRela);
  302. }
  303. }
  304. }
  305. // 修改商品轮播图
  306. goodsGalleryDao.deleteByGoodsId(goods.getId());
  307. for (int i=0;i<galleryEntityList.size();i++) {
  308. GoodsGalleryEntity galleryEntity =galleryEntityList.get(i);
  309. galleryEntity.setMerchSn(goods.getMerchSn());
  310. galleryEntity.setGoodsId(goods.getId());
  311. galleryEntity.setSortOrder((i+1));
  312. galleryEntity.setFileType("0");//图片
  313. goodsGalleryDao.save(galleryEntity);
  314. }
  315. if(org.apache.commons.lang.StringUtils.isNotEmpty(goods.getVideoUrl())){
  316. GoodsGalleryEntity galleryEntity = new GoodsGalleryEntity();
  317. galleryEntity.setMerchSn(goods.getMerchSn());
  318. galleryEntity.setGoodsId(goods.getId());
  319. galleryEntity.setSortOrder(0);
  320. galleryEntity.setFileType("1");//视频
  321. galleryEntity.setImgUrl(goods.getVideoUrl());
  322. goodsGalleryDao.save(galleryEntity);
  323. }
  324. // 修改商品参数
  325. List<GoodsAttributeEntity> attributeEntityList = goods.getAttributeEntityList();
  326. if (attributeEntityList != null && attributeEntityList.size() > 0) {
  327. for (GoodsAttributeEntity item : attributeEntityList) {
  328. if (item.getIsDelete() == 0) {
  329. if (item.getId() != null) {
  330. item.setMerchSn(goods.getMerchSn());
  331. goodsAttributeDao.update(item);
  332. } else if (item.getId() == null && item.getAttributeId() != null && StringUtils.isNotEmpty(item.getValue())) {
  333. item.setGoodsId(goods.getId());
  334. item.setMerchSn(goods.getMerchSn());
  335. goodsAttributeDao.save(item);
  336. } else if (item.getId() == null && item.getAttributeId() != null && StringUtils.isNullOrEmpty(item.getValue())) {
  337. throw new RRException("商品属性【" + attributeDao.queryObject(item.getAttributeId()).getName() + "】值不能为空!");
  338. } else if (item.getId() == null && item.getAttributeId() == null) {
  339. continue;
  340. }
  341. } else if (item.getIsDelete() == 1) {
  342. goodsAttributeDao.delete(item.getId());
  343. }
  344. }
  345. }
  346. // 修改产品
  347. ProductEntity product = productDao.queryObjectBySn(goods.getGoodsSn());
  348. GoodsSpecificationEntity goodsSpecification = new GoodsSpecificationEntity();
  349. // 保税商品,普通货物暂不添加商品规格
  350. if (!Dict.orderBizType.item_11.getItem().equals(goods.getGoodsBizType())) {
  351. // 添加商品规格
  352. goodsSpecification = goodsSpecificationDao.queryByGoodsId(goods.getId());
  353. goodsSpecification.setValue(goods.getCiqProdModel());
  354. goodsSpecificationDao.update(goodsSpecification);
  355. if(product == null){
  356. product = new ProductEntity();
  357. product.setGoodsSn(goods.getGoodsSn());
  358. product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
  359. product.setGoodsSpecificationIds(goodsSpecification.getId()+"");
  360. product.setGoodsId(goods.getId());
  361. product.setGoodsNumber(goods.getGoodsNumber());
  362. product.setGoodsDefault(0);
  363. return productDao.save(product);
  364. }else{
  365. product.setGoodsSpecificationIds(goodsSpecification.getId().toString());
  366. product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
  367. return productDao.update(product);
  368. }
  369. }
  370. return 1;
  371. }
  372. @Override
  373. public int delete(Integer id) {
  374. SysUserEntity user = ShiroUtils.getUserEntity();
  375. GoodsEntity goodsEntity = goodsDao.queryObject(id);
  376. goodsEntity.setIsDelete(Integer.parseInt(Dict.isDelete.item_1.getItem()));
  377. goodsEntity.setIsOnSale(Integer.parseInt(Dict.isOnSale.item_0.getItem()));
  378. goodsEntity.setUpdateUserId(user.getUserId());
  379. goodsEntity.setUpdateTime(new Date());
  380. Map params = new HashMap();
  381. params.put("goodsId", id);
  382. List<GoodsGroupEntity> groupVos = goodsGroupDao.queryList(params);
  383. if (null != groupVos && groupVos.size() > 0) {
  384. for (GoodsGroupEntity groupVo : groupVos) {
  385. groupVo.setOpenStatus(3);
  386. goodsGroupDao.update(groupVo);
  387. }
  388. }
  389. return goodsDao.update(goodsEntity);
  390. }
  391. @Override
  392. @Transactional
  393. public int deleteBatch(Integer[] ids) {
  394. int result = 0;
  395. for (Integer id : ids) {
  396. result += delete(id);
  397. }
  398. return result;
  399. }
  400. @Override
  401. @Transactional
  402. public int back(Integer[] ids) {
  403. SysUserEntity user = ShiroUtils.getUserEntity();
  404. int result = 0;
  405. for (Integer id : ids) {
  406. GoodsEntity goodsEntity = queryObject(id);
  407. goodsEntity.setIsDelete(Integer.parseInt(Dict.isDelete.item_0.getItem()));
  408. goodsEntity.setIsOnSale(Integer.parseInt(Dict.isOnSale.item_1.getItem()));
  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 enSale(Integer id) {
  417. SysUserEntity user = ShiroUtils.getUserEntity();
  418. GoodsEntity goodsEntity = queryObject(id);
  419. if (1 == goodsEntity.getIsOnSale()) {
  420. throw new RRException("此商品已处于上架状态!");
  421. }
  422. goodsEntity.setIsOnSale(Integer.parseInt(Dict.isOnSale.item_1.getItem()));
  423. goodsEntity.setIsDelete(Integer.parseInt(Dict.isDelete.item_0.getItem()));
  424. goodsEntity.setUpdateUserId(user.getUserId());
  425. goodsEntity.setUpdateTime(new Date());
  426. return goodsDao.update(goodsEntity);
  427. }
  428. @Override
  429. public int unSale(Integer id) {
  430. SysUserEntity user = ShiroUtils.getUserEntity();
  431. GoodsEntity goodsEntity = queryObject(id);
  432. if (0 == goodsEntity.getIsOnSale()) {
  433. throw new RRException("此商品已处于下架状态!");
  434. }
  435. goodsEntity.setIsOnSale(Integer.parseInt(Dict.isOnSale.item_0.getItem()));
  436. goodsEntity.setUpdateUserId(user.getUserId());
  437. goodsEntity.setUpdateTime(new Date());
  438. return goodsDao.update(goodsEntity);
  439. }
  440. @Override
  441. public int enSaleBatch(Integer[] ids) {
  442. int result = 0;
  443. SysUserEntity user = ShiroUtils.getUserEntity();
  444. for (Integer id : ids) {
  445. GoodsEntity goodsEntity = queryObject(id);
  446. goodsEntity.setIsOnSale(Integer.parseInt(Dict.isOnSale.item_1.getItem()));
  447. goodsEntity.setUpdateUserId(user.getUserId());
  448. goodsEntity.setUpdateTime(new Date());
  449. result += goodsDao.update(goodsEntity);
  450. }
  451. return result;
  452. }
  453. @Override
  454. public int unSaleBatch(Integer[] ids) {
  455. int result = 0;
  456. SysUserEntity user = ShiroUtils.getUserEntity();
  457. for (Integer id : ids) {
  458. GoodsEntity goodsEntity = queryObject(id);
  459. goodsEntity.setIsOnSale(Integer.parseInt(Dict.isOnSale.item_0.getItem()));
  460. goodsEntity.setUpdateUserId(user.getUserId());
  461. goodsEntity.setUpdateTime(new Date());
  462. result += goodsDao.update(goodsEntity);
  463. }
  464. return result;
  465. }
  466. @Override
  467. public int uploadExcel(List<GoodsDto> goodsEntityList,int exportDataType) {
  468. SysUserEntity user = ShiroUtils.getUserEntity();
  469. String merchSn = user.getMerchSn();
  470. boolean isSuccess = false;
  471. List<String> failSameSkuList = new ArrayList<>(), failHotGoodsSnList = new ArrayList<>(), failCateGoodsSnList = new ArrayList<>(),
  472. failBrandGoodsSnList = new ArrayList<>(), failFreightGoodsSnList = new ArrayList<>(), failSuppGoodsSnList = new ArrayList<>(),
  473. failUnitGoodsSnList = new ArrayList<>(), failNationGoodsSnList = new ArrayList<>(),failProdbarGoodsSnList = new ArrayList<>(),
  474. failCateL2GoodsSnList = new ArrayList<>(), failTypeGoodsSnList = new ArrayList<>(), failMerchGoodsSnList = new ArrayList<>(),
  475. failMerchUserGoodsSnList = new ArrayList<>();
  476. List<String> failGoodsSnList = new ArrayList<>();
  477. List<String> failGoodsTypeList = new ArrayList<>();
  478. List<String> failFreightList = new ArrayList<>();
  479. if (goodsEntityList != null && goodsEntityList.size() > 0) {
  480. for (int i = 0; i < goodsEntityList.size(); i++) {
  481. GoodsDto goodsDto = goodsEntityList.get(i);
  482. GoodsEntity goodsEntity = new GoodsEntity();
  483. Map<String, Object> valideDate = MapBeanUtil.fromObject(goodsDto);
  484. ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
  485. builder.put("merchSn", "商户编号");
  486. builder.put("goodsSn", "商品编码");
  487. builder.put("categoryName", "商品分类");
  488. builder.put("goodsBizType", "货品业务类型");
  489. builder.put("name", "商品名称");
  490. builder.put("brandName", "商品品牌名称");
  491. builder.put("defaultFreight", "运费");
  492. builder.put("isOnSaleStr", "上架");
  493. builder.put("goodsUnit", "商品单位");
  494. builder.put("isHotStr", "热销");
  495. builder.put("prodBarcode", "产品条码");
  496. builder.put("marketPrice", "市场价");
  497. builder.put("retailPrice", "零售价");
  498. builder.put("supplierName", "供应商");
  499. R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
  500. if (Integer.valueOf(r.get("code").toString()) != 0) {
  501. throw new RRException(r.get("msg").toString());
  502. } else {
  503. if (!Dict.orderBizType.item_11.getItem().equals(goodsDto.getGoodsBizType())) {
  504. // 海关信息,普通货物可不添加
  505. builder.put("goodsRate", "商品税率");
  506. builder.put("sku", "SKU");
  507. builder.put("brand", "产品品牌");
  508. builder.put("unitName", "计量单位");
  509. builder.put("oriCntName", "原产国");
  510. builder.put("cusGoodsCode", "海关商品编码");
  511. builder.put("ciqProdModel", "国检规格型号");
  512. builder.put("cusDeclEle", "海关申报要素");
  513. builder.put("cusRecCode", "海关备案编号");
  514. }
  515. r = ValidatorUtil.isEmpty(builder.build(), valideDate);
  516. if (Integer.valueOf(r.get("code").toString()) != 0) {
  517. throw new RRException(r.get("msg").toString());
  518. }
  519. }
  520. //业务类型校验
  521. if(!Dict.orderBizType.item_11.getItem().equalsIgnoreCase(goodsDto.getGoodsBizType())){
  522. if(!(Dict.orderBizType.item_02.getItem().equalsIgnoreCase(goodsDto.getGoodsBizType())
  523. || Dict.orderBizType.item_10.getItem().equalsIgnoreCase(goodsDto.getGoodsBizType())
  524. || Dict.orderBizType.item_00.getItem().equalsIgnoreCase(goodsDto.getGoodsBizType()))){
  525. isSuccess = true;
  526. failTypeGoodsSnList.add(goodsDto.getSku());
  527. }
  528. }
  529. if(!user.getRoleType().equalsIgnoreCase(Dict.roleType.item_1.getItem())) {
  530. if (!merchSn.equalsIgnoreCase(goodsDto.getMerchSn())) {
  531. isSuccess = true;
  532. failMerchUserGoodsSnList.add(goodsDto.getGoodsSn());
  533. }
  534. }
  535. MerchEntity merchEntity = merchDao.findByMerchSn(goodsDto.getMerchSn());
  536. if(merchEntity != null){
  537. goodsEntity.setMerchSn(merchEntity.getMerchSn());
  538. }else{//商户不存在
  539. isSuccess = true;
  540. failMerchGoodsSnList.add(goodsDto.getGoodsSn());
  541. }
  542. //校验商品信息是否已存在
  543. Map<String, Object> map = new HashMap<>();
  544. map.put("isSame", "true");
  545. map.put("sku", goodsDto.getSku());
  546. map.put("goodsSn", goodsDto.getGoodsSn());
  547. map.put("goodsBizType", goodsDto.getGoodsBizType());
  548. List<GoodsEntity> list = querySame(map);
  549. if (list != null && list.size() != 0) {
  550. isSuccess = true;
  551. if(goodsDto.getSku()!=null) {
  552. failSameSkuList.add(goodsDto.getSku());
  553. }
  554. failGoodsSnList.add(goodsDto.getGoodsSn());
  555. failGoodsTypeList.add(goodsDto.getGoodsBizType());
  556. }
  557. //校验产品条码是否存在
  558. GoodsEntity prodbarGoods = goodsDao.queryObjectByProdBarcode(goodsDto.getProdBarcode(),merchSn,null);
  559. if(prodbarGoods != null){
  560. isSuccess = true;
  561. failProdbarGoodsSnList.add(goodsDto.getGoodsSn());
  562. }else{
  563. goodsEntity.setProdBarcode(goodsDto.getProdBarcode());
  564. }
  565. //热销商品校验
  566. if (Dict.orderBizType.item_02.getItem().equals(goodsDto.getGoodsBizType())
  567. || Dict.orderBizType.item_10.getItem().equals(goodsDto.getGoodsBizType())) {
  568. if(goodsDto.getIsHotStr().equalsIgnoreCase("1")){
  569. isSuccess = true;
  570. failHotGoodsSnList.add(goodsDto.getGoodsSn());
  571. }
  572. }
  573. //商品配置校验
  574. CategoryEntity categoryEntity = categoryDao.queryObjectByName(goodsDto.getCategoryName(),goodsDto.getMerchSn());
  575. if(categoryEntity==null){
  576. isSuccess = true;
  577. failCateGoodsSnList.add(goodsDto.getGoodsSn());
  578. }else{
  579. if(categoryEntity.getLevel().equalsIgnoreCase("L2")) {
  580. goodsEntity.setCategoryId(categoryEntity.getId());
  581. goodsEntity.setAttributeCategory(categoryEntity.getParentId());
  582. }else{
  583. isSuccess = true;
  584. failCateL2GoodsSnList.add(goodsDto.getGoodsSn());
  585. }
  586. }
  587. BrandEntity brandEntity = brandDao.queryObjectByName(goodsDto.getBrandName(),goodsDto.getMerchSn());
  588. if (brandEntity == null) {
  589. isSuccess = true;
  590. failBrandGoodsSnList.add(goodsDto.getGoodsSn());
  591. } else {
  592. goodsEntity.setBrandId(brandEntity.getId());
  593. }
  594. //运费
  595. FreightEntity freightEntity = freightDao.queryObjectByName(goodsDto.getDefaultFreight(),goodsDto.getMerchSn());
  596. if(freightEntity==null){
  597. isSuccess = true;
  598. failFreightGoodsSnList.add(goodsDto.getGoodsSn());
  599. failFreightList.add(goodsDto.getDefaultFreight());
  600. }else {
  601. goodsEntity.setFreightId(freightEntity.getId());
  602. }
  603. if (!Dict.orderBizType.item_11.getItem().equals(goodsDto.getGoodsBizType())) {
  604. SupplierEntity supplierEntity = supplierDao.queryObjectByName(goodsDto.getSupplierName(),goodsDto.getMerchSn());
  605. if (supplierEntity == null) {
  606. isSuccess = true;
  607. failSuppGoodsSnList.add(goodsDto.getGoodsSn());
  608. } else {
  609. goodsEntity.setSupplierId(supplierEntity.getId());
  610. }
  611. SysCusUnitCodeEntity sysCusUnitCodeEntity = sysCusUnitCodeDao.queryObjectByName(goodsDto.getUnitName());
  612. if (sysCusUnitCodeEntity == null) {
  613. isSuccess = true;
  614. failUnitGoodsSnList.add(goodsDto.getGoodsSn());
  615. } else {
  616. goodsEntity.setUnitCode(sysCusUnitCodeEntity.getCode());
  617. }
  618. //原产国
  619. SysCusNationCodeEntity sysCusNationCodeEntity = sysCusNationCodeDao.queryObjectByName(goodsDto.getOriCntName());
  620. if (sysCusNationCodeEntity == null) {
  621. isSuccess = true;
  622. failNationGoodsSnList.add(goodsDto.getGoodsSn());
  623. } else {
  624. goodsEntity.setOriCntCode(sysCusNationCodeEntity.getCode());
  625. }
  626. goodsEntity.setGoodsRate(BigDecimal.valueOf(Integer.valueOf(goodsDto.getGoodsRate())));
  627. }
  628. goodsEntity.setIsOnSale(Integer.parseInt(goodsDto.getIsOnSaleStr()));
  629. goodsEntity.setIsHot(Integer.parseInt(goodsDto.getIsHotStr()));
  630. goodsEntity.setRetailPrice(BigDecimal.valueOf(Integer.valueOf(goodsDto.getRetailPrice())));
  631. goodsEntity.setMarketPrice(BigDecimal.valueOf(Integer.valueOf(goodsDto.getMarketPrice())));
  632. goodsEntity.setGoodsSn(goodsDto.getGoodsSn());
  633. goodsEntity.setSku(goodsDto.getSku());
  634. goodsEntity.setName(goodsDto.getName());
  635. goodsEntity.setGoodsUnit(goodsDto.getGoodsUnit());
  636. goodsEntity.setGoodsBizType(goodsDto.getGoodsBizType());
  637. goodsEntity.setBrand(goodsDto.getBrand());
  638. goodsEntity.setCusDeclEle(goodsDto.getCusDeclEle());
  639. goodsEntity.setCusGoodsCode(goodsDto.getCusGoodsCode());
  640. goodsEntity.setCusRecCode(goodsDto.getCusRecCode());
  641. goodsEntity.setCiqProdModel(goodsDto.getCiqProdModel());
  642. goodsEntity.setIsDelete(Integer.parseInt(Dict.isDelete.item_0.getItem()));
  643. goodsEntity.setIsNew(0);
  644. goodsEntity.setUpdateUserId(user.getUserId());
  645. goodsEntity.setAddTime(new Date());
  646. goodsEntity.setCreateTime(new Date());
  647. goodsEntity.setUpdateTime(new Date());
  648. goodsEntity.setModTime(new Date());
  649. if(!isSuccess){
  650. GoodsEntity goods = goodsDao.queryObjectBySn(goodsDto.getGoodsSn());
  651. if(goods!=null) {// 修改商品
  652. goodsEntity.setId(goods.getId());
  653. goodsDao.update(goodsEntity);
  654. }else{
  655. goodsDao.save(goodsEntity);
  656. }
  657. // 保税商品修改各个门店商品价格
  658. if (!Dict.orderBizType.item_11.getItem().equals(goodsDto.getGoodsBizType())) {
  659. List<ProductStoreRelaEntity> productStoreRelaEntityList = productStoreRelaDao.queryByGoodsId(goodsDto.getId());
  660. if (productStoreRelaEntityList != null && productStoreRelaEntityList.size() > 0) {
  661. for (ProductStoreRelaEntity productStoreRela : productStoreRelaEntityList) {
  662. productStoreRela.setMarketPrice(goodsEntity.getMarketPrice());
  663. productStoreRela.setRetailPrice(goodsEntity.getRetailPrice());
  664. productStoreRelaDao.update(productStoreRela);
  665. }
  666. }
  667. }
  668. // 修改产品
  669. ProductEntity product = productDao.queryObjectBySn(goodsDto.getGoodsSn());
  670. GoodsSpecificationEntity goodsSpecification = new GoodsSpecificationEntity();
  671. // 保税商品,普通货物暂不添加商品规格
  672. if (!Dict.orderBizType.item_11.getItem().equals(goodsDto.getGoodsBizType())) {
  673. // 添加商品规格
  674. GoodsSpecificationEntity specificationEntity = goodsSpecificationDao.queryByGoodsId(goodsEntity.getId());
  675. if(specificationEntity ==null) {
  676. goodsSpecification.setGoodsId(goodsEntity.getId());
  677. goodsSpecification.setValue(goodsEntity.getCiqProdModel());
  678. goodsSpecification.setSpecificationId(1);
  679. goodsSpecificationDao.save(goodsSpecification);
  680. }else {
  681. goodsSpecification.setValue(goodsDto.getCiqProdModel());
  682. goodsSpecificationDao.update(goodsSpecification);
  683. }
  684. }
  685. if(product == null){
  686. product = new ProductEntity();
  687. product.setGoodsSn(goodsDto.getGoodsSn());
  688. product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
  689. product.setGoodsSpecificationIds(goodsSpecification.getId().toString());
  690. product.setGoodsId(goodsEntity.getId());
  691. product.setGoodsDefault(0);
  692. product.setGoodsNumber(goodsEntity.getGoodsNumber());
  693. productDao.save(product);
  694. }else{
  695. product.setGoodsSpecificationIds(goodsSpecification.getId().toString());
  696. product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
  697. productDao.update(product);
  698. }
  699. }
  700. }
  701. ExportExceptionDataEntity exportExceptionDataEntity = new ExportExceptionDataEntity();
  702. exportExceptionDataEntity.setCreaterSn(user.getUserId().toString());
  703. exportExceptionDataEntity.setUserId(user.getUserId().intValue());
  704. exportExceptionDataEntity.setCreateTime(new Date());
  705. exportExceptionDataEntity.setModTime(new Date());
  706. exportExceptionDataEntity.setMerchSn(merchSn);
  707. exportExceptionDataEntity.setStoreId(user.getStoreId());
  708. if(exportDataType == 1) {
  709. exportExceptionDataEntity.setExportDataType("1");
  710. }else{
  711. exportExceptionDataEntity.setExportDataType("2");
  712. }
  713. if(failMerchUserGoodsSnList != null && failMerchUserGoodsSnList.size() > 0){
  714. exportExceptionDataEntity.setExportExceptionData("不能操作除了登录用户以外商户的商品,当前商户编号为【"+merchSn+"】,请检查商品编码【"+failMerchUserGoodsSnList+"】的商品信息");
  715. exportExceptionDataDao.save(exportExceptionDataEntity);
  716. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  717. }
  718. if(failMerchGoodsSnList != null && failMerchGoodsSnList.size() > 0){
  719. exportExceptionDataEntity.setExportExceptionData("商户编号不存在,请检查商品编码【"+failMerchGoodsSnList+"】的商品信息,请先维护用户商户编号信息再继续操作!");
  720. exportExceptionDataDao.save(exportExceptionDataEntity);
  721. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  722. }
  723. if(failGoodsSnList != null && failGoodsSnList.size() > 0){
  724. if(failSameSkuList.size()>0) {
  725. exportExceptionDataEntity.setExportExceptionData("不能有重复的商品编码、sku信息!请检查商品编码【" + failGoodsSnList + "】,业务类型【" +
  726. failGoodsTypeList + "】,SKU【" + failSameSkuList + "】的商品信息");
  727. exportExceptionDataDao.save(exportExceptionDataEntity);
  728. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  729. }else{
  730. exportExceptionDataEntity.setExportExceptionData("不能有重复的商品编码、sku信息!请检查商品编码【" + failGoodsSnList + "】,业务类型【" +
  731. failGoodsTypeList + "】的商品信息");
  732. exportExceptionDataDao.save(exportExceptionDataEntity);
  733. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  734. }
  735. }
  736. if(failTypeGoodsSnList != null && failTypeGoodsSnList.size() > 0){
  737. exportExceptionDataEntity.setExportExceptionData("货品业务类型只能是【00保税备货、02保税补货、10保税展示】!请检查商品编码【"+failTypeGoodsSnList+"】的商品信息");
  738. exportExceptionDataDao.save(exportExceptionDataEntity);
  739. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  740. }
  741. if(failProdbarGoodsSnList != null && failProdbarGoodsSnList.size() > 0){
  742. exportExceptionDataEntity.setExportExceptionData("不能有重复的产品条码信息!请检查商品编码【"+failProdbarGoodsSnList+"】的商品产品条码信息");
  743. exportExceptionDataDao.save(exportExceptionDataEntity);
  744. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  745. }
  746. if(failHotGoodsSnList != null && failHotGoodsSnList.size() > 0){
  747. exportExceptionDataEntity.setExportExceptionData("请检查业务类型为【保税补货或保税展示】的商品,商品编码【"+failHotGoodsSnList+"】的商品不能设置为热销!");
  748. exportExceptionDataDao.save(exportExceptionDataEntity);
  749. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  750. }
  751. if(failCateGoodsSnList != null && failCateGoodsSnList.size() > 0){
  752. exportExceptionDataEntity.setExportExceptionData("分类信息请在商城配置》商品分类中维护,商品分类与商户信息对应,请检查该商品商户信息下的分类是否维护,不存在的商品编码【"+failCateGoodsSnList+"】");
  753. exportExceptionDataDao.save(exportExceptionDataEntity);
  754. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  755. }
  756. if(failCateL2GoodsSnList != null && failCateL2GoodsSnList.size() > 0){
  757. exportExceptionDataEntity.setExportExceptionData("分类信息请在商城配置》商品分类中查看,商品分类必须为二级分类,不存在的商品编码【"+failCateL2GoodsSnList+"】");
  758. exportExceptionDataDao.save(exportExceptionDataEntity);
  759. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  760. }
  761. if(failBrandGoodsSnList != null && failBrandGoodsSnList.size() > 0){
  762. exportExceptionDataEntity.setExportExceptionData("品牌信息请在商城配置》品牌制造商中维护,品牌与商户信息对应,请检查该商品商户信息下的品牌是否维护,不存在的商品编码【" + failBrandGoodsSnList + "】");
  763. exportExceptionDataDao.save(exportExceptionDataEntity);
  764. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  765. }
  766. if(failFreightGoodsSnList != null && failFreightGoodsSnList.size() > 0){
  767. exportExceptionDataEntity.setExportExceptionData("运费信息请在商城配置》运费模板中维护,运费与商户信息对应,请检查该商品商户信息下的运费是否维护,不存在的商品编码【"+failFreightGoodsSnList+"】,运费【"+failFreightList+"】");
  768. exportExceptionDataDao.save(exportExceptionDataEntity);
  769. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  770. }
  771. if(failSuppGoodsSnList != null && failSuppGoodsSnList.size() > 0){
  772. exportExceptionDataEntity.setExportExceptionData("供应商信息请在商城配置》商品供应商中维护,供应商与商户信息对应,请检查该商品商户信息下的供应商是否维护,不存在的商品编码【" + failSuppGoodsSnList + "】");
  773. exportExceptionDataDao.save(exportExceptionDataEntity);
  774. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  775. }
  776. if(failUnitGoodsSnList != null && failUnitGoodsSnList.size() > 0){
  777. exportExceptionDataEntity.setExportExceptionData("计算单位信息请在商城配置》计算单位中维护,不存在的商品编码【" + failUnitGoodsSnList + "】");
  778. exportExceptionDataDao.save(exportExceptionDataEntity);
  779. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  780. }
  781. if(failNationGoodsSnList != null && failNationGoodsSnList.size() > 0){
  782. exportExceptionDataEntity.setExportExceptionData("原产国信息请在商城配置》原产国中维护,不存在的商品编码【" + failNationGoodsSnList + "】");
  783. exportExceptionDataDao.save(exportExceptionDataEntity);
  784. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  785. }
  786. }else{
  787. throw new RRException("导入数据为空,或者检查商品编码数据是否为空");
  788. }
  789. return 1;
  790. }
  791. /* @Override
  792. @Transactional
  793. public int uploadExcel(MultipartFile file) {
  794. SysUserEntity user = ShiroUtils.getUserEntity();
  795. List<String[]> list = ExcelImport.getExcelData(file);
  796. // 取门店名称
  797. StoreEntity storeEntity = storeDao.queryObjectByName(list.get(0)[3]);
  798. if (null == storeEntity) {
  799. return 0;
  800. }
  801. //去除表头两行、底部合计
  802. if (list != null && list.size() > 3) {
  803. ProductStoreRelaEntity storeRelaEntity;
  804. ProductEntity productEntity;
  805. for (int i = 2; i < list.size() - 1; i++) {
  806. String[] item = list.get(i);
  807. String goodsSn = item[0];
  808. productEntity = productDao.queryObjectBySn(goodsSn);
  809. if (StringUtils.isNullOrEmpty(goodsSn)) {
  810. continue;
  811. }
  812. if (null == productEntity || null == productEntity.getId()) {
  813. continue;
  814. }
  815. storeRelaEntity = productStoreRelaDao.queryByStoreIdProductId(storeEntity.getId(), productEntity.getId());
  816. if (null != storeRelaEntity && null != storeRelaEntity.getId()) {
  817. storeRelaEntity.setRetailPrice(new BigDecimal(item[6]));
  818. storeRelaEntity.setStockNum(Integer.valueOf(item[3].replace(".00", "")));
  819. storeRelaEntity.setStockPrice(new BigDecimal(item[4]));
  820. productStoreRelaDao.update(storeRelaEntity);
  821. } else {
  822. storeRelaEntity = new ProductStoreRelaEntity();
  823. storeRelaEntity.setGoodsId(productEntity.getGoodsId());
  824. storeRelaEntity.setProductId(productEntity.getId());
  825. storeRelaEntity.setRetailPrice(new BigDecimal(item[6]));
  826. storeRelaEntity.setMarketPrice(new BigDecimal(item[6]));
  827. storeRelaEntity.setStockNum(Integer.valueOf(item[3]));
  828. storeRelaEntity.setStockPrice(new BigDecimal(item[4]));
  829. storeRelaEntity.setStoreId(storeEntity.getId());
  830. productStoreRelaDao.save(storeRelaEntity);
  831. }
  832. }
  833. }
  834. return 1;
  835. }*/
  836. public GoodsEntity queryObjectBySn(String goodsSn) {
  837. return goodsDao.queryObjectBySn(goodsSn);
  838. }
  839. }