GoodsServiceImpl.java 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  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.api.contants.Dict;
  8. import com.kmall.common.entity.SysUserEntity;
  9. import com.kmall.common.utils.*;
  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.*;
  17. /**
  18. * Service实现类
  19. *
  20. * @author Scott
  21. * @email
  22. * @date 2017-08-21 21:19:49
  23. */
  24. @Service("goodsService")
  25. public class GoodsServiceImpl implements GoodsService {
  26. @Autowired
  27. private GoodsDao goodsDao;
  28. @Autowired
  29. private GoodsAttributeDao goodsAttributeDao;
  30. @Autowired
  31. private AttributeDao attributeDao;
  32. @Autowired
  33. private ProductDao productDao;
  34. @Autowired
  35. private GoodsGalleryDao goodsGalleryDao;
  36. @Autowired
  37. private GoodsSpecificationDao goodsSpecificationDao;
  38. @Autowired
  39. private ProductStoreRelaDao productStoreRelaDao;
  40. @Autowired
  41. private StoreDao storeDao;
  42. @Autowired
  43. private GoodsGroupDao goodsGroupDao;
  44. @Autowired
  45. private CategoryDao categoryDao;
  46. @Autowired
  47. private SupplierDao supplierDao;
  48. @Autowired
  49. private BrandDao brandDao;
  50. @Autowired
  51. private FreightDao freightDao;
  52. @Autowired
  53. private SysCusNationCodeDao sysCusNationCodeDao;
  54. @Autowired
  55. private SysCusUnitCodeDao sysCusUnitCodeDao;
  56. @Autowired
  57. private MerchDao merchDao;
  58. @Autowired
  59. private ExportExceptionDataDao exportExceptionDataDao;
  60. @Override
  61. public GoodsEntity queryObject(Integer id) {
  62. Map<String, Object> map = new HashMap<String, Object>();
  63. map.put("goodsId", id);
  64. List<GoodsAttributeEntity> attributeEntities = goodsAttributeDao.queryList(map);
  65. List<ProductEntity> productEntityList = productDao.queryList(map);
  66. GoodsEntity entity = goodsDao.queryObject(id);
  67. entity.setAttributeEntityList(attributeEntities);
  68. entity.setProductEntityList(productEntityList);
  69. return entity;
  70. }
  71. @Override
  72. public GoodsEntity queryObjectByProdBarcodeAndBizType(String prodBarcode){
  73. Map<String, Object> map = new HashMap<String, Object>();
  74. map.put("prodBarcode", prodBarcode);
  75. GoodsEntity entity = goodsDao.queryObjectByProdBarcodeAndBizType(prodBarcode);
  76. return entity;
  77. }
  78. @Override
  79. public List<GoodsEntity> queryList(Map<String, Object> map) {
  80. return goodsDao.queryList(map);
  81. }
  82. @Override
  83. public List<GoodsEntity> querySame(Map<String, Object> map) {
  84. return goodsDao.querySame(map);
  85. }
  86. @Override
  87. public int queryTotal(Map<String, Object> map) {
  88. return goodsDao.queryTotal(map);
  89. }
  90. @Override
  91. @Transactional
  92. public int save(GoodsEntity goods) {
  93. Map<String, Object> valideDate = MapBeanUtil.fromObject(goods);
  94. ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
  95. builder.put("merchSn", "商户编号");
  96. builder.put("attributeCategory", "商品分类");
  97. builder.put("categoryId", "商品二级分类");
  98. builder.put("goodsSn", "商品编码");
  99. builder.put("name", "商品名称");
  100. builder.put("goodsUnit", "商品单位");
  101. builder.put("prodBarcode", "产品条码");
  102. builder.put("goodsBizType", "货品业务类型");
  103. builder.put("brandId", "品牌");
  104. builder.put("supplierId", "供应商");
  105. builder.put("freightId", "运费模版");
  106. builder.put("primaryPicUrl", "商品主图");
  107. builder.put("listPicUrl", "商品列表图");
  108. builder.put("goodsDesc", "商品描述");
  109. builder.put("isOnSale", "上架");
  110. builder.put("isHot", "热销");
  111. R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
  112. if (Integer.valueOf(r.get("code").toString()) != 0) {
  113. throw new RRException(r.get("msg").toString());
  114. } else {
  115. if (!Dict.orderBizType.item_11.getItem().equals(goods.getGoodsBizType())) {
  116. // 海关信息,普通货物可不添加
  117. builder.put("sku", "SKU");
  118. builder.put("goodsRate", "商品税率");
  119. builder.put("retailPrice", "零售价");
  120. builder.put("brand", "产品品牌");
  121. builder.put("unitCode", "计量单位代码");
  122. builder.put("cusGoodsCode", "海关商品编码");
  123. builder.put("ciqProdModel", "国检规格型号");
  124. builder.put("oriCntCode", "原产国代码");
  125. builder.put("cusDeclEle", "海关申报要素");
  126. builder.put("cusRecCode", "海关备案编号");
  127. }
  128. r = ValidatorUtil.isEmpty(builder.build(), valideDate);
  129. if (Integer.valueOf(r.get("code").toString()) != 0) {
  130. throw new RRException(r.get("msg").toString());
  131. }
  132. }
  133. // 商品轮播图
  134. List<GoodsGalleryEntity> galleryEntityList = goods.getGoodsImgList();
  135. if (galleryEntityList == null || galleryEntityList.size() <= 0) {
  136. throw new RRException("至少添加一张商品轮播图!");
  137. }
  138. GoodsEntity prodbarGoods = goodsDao.queryObjectByProdBarcode(goods.getProdBarcode(),"",null);
  139. if(prodbarGoods != null){
  140. throw new RRException("不能有重复的产品条码信息!");
  141. }
  142. SysUserEntity user = ShiroUtils.getUserEntity();
  143. Map<String, Object> map = new HashMap<>();
  144. map.put("isSame", "true");
  145. map.put("sku", goods.getSku());
  146. map.put("goodsSn", goods.getGoodsSn());
  147. map.put("goodsBizType", goods.getGoodsBizType());
  148. List<GoodsEntity> list = querySame(map);
  149. if (list != null && list.size() != 0) {
  150. throw new RRException("已存在该商品编码或该货品业务类型下已存在此SKU!");
  151. }
  152. // 添加商品
  153. if (Dict.orderBizType.item_02.getItem().equals(goods.getGoodsBizType())
  154. || Dict.orderBizType.item_10.getItem().equals(goods.getGoodsBizType())) {
  155. goods.setIsHot(0);
  156. }
  157. goods.setAttributeCategory(categoryDao.queryObject(goods.getCategoryId()).getParentId());
  158. goods.setAddTime(new Date());
  159. goods.setIsDelete(0);
  160. goods.setIsNew(0);
  161. goods.setCreateUserId(user.getUserId());
  162. goods.setUpdateUserId(user.getUserId());
  163. goods.setUpdateTime(new Date());
  164. goods.setModTime(new Date());
  165. goods.setCreateTime(new Date());
  166. // 新增商品
  167. goodsDao.save(goods);
  168. Long id = goods.getId();
  169. // 添加商品轮播图
  170. for (int i=0;i<galleryEntityList.size();i++) {
  171. GoodsGalleryEntity galleryEntity =galleryEntityList.get(i);
  172. galleryEntity.setMerchSn(goods.getMerchSn());
  173. galleryEntity.setGoodsId(id);
  174. galleryEntity.setSortOrder((i+1));
  175. galleryEntity.setFileType("0");//图片
  176. goodsGalleryDao.save(galleryEntity);
  177. }
  178. if(org.apache.commons.lang.StringUtils.isNotEmpty(goods.getVideoUrl())){
  179. GoodsGalleryEntity galleryEntity = new GoodsGalleryEntity();
  180. galleryEntity.setMerchSn(goods.getMerchSn());
  181. galleryEntity.setGoodsId(id);
  182. galleryEntity.setSortOrder(0);
  183. galleryEntity.setFileType("1");//视频
  184. goodsGalleryDao.save(galleryEntity);
  185. }
  186. // 添加商品参数
  187. List<GoodsAttributeEntity> attributeEntityList = goods.getAttributeEntityList();
  188. if (attributeEntityList != null && attributeEntityList.size() > 0) {
  189. for (GoodsAttributeEntity item : attributeEntityList) {
  190. if (item.getIsDelete() == 0) {
  191. if (item.getId() == null && item.getAttributeId() != null && StringUtils.isNotEmpty(item.getValue())) {
  192. item.setGoodsId(id);
  193. item.setMerchSn(goods.getMerchSn());
  194. goodsAttributeDao.save(item);
  195. } else if (item.getId() == null && item.getAttributeId() != null && StringUtils.isNullOrEmpty(item.getValue())) {
  196. throw new RRException("商品属性【" + attributeDao.queryObject(item.getAttributeId()).getName() + "】值不能为空!");
  197. } else if (item.getId() == null && item.getAttributeId() == null) {
  198. continue;
  199. }
  200. }
  201. }
  202. }
  203. // 添加产品
  204. ProductEntity product = new ProductEntity();
  205. product.setGoodsId(id);
  206. product.setGoodsSn(goods.getGoodsSn());
  207. // 保税商品,普通货物暂不添加商品规格
  208. if (!Dict.orderBizType.item_11.getItem().equals(goods.getGoodsBizType())) {
  209. // 添加商品规格
  210. GoodsSpecificationEntity goodsSpecification = new GoodsSpecificationEntity();
  211. goodsSpecification.setGoodsId(id);
  212. goodsSpecification.setValue(goods.getCiqProdModel());
  213. goodsSpecification.setSpecificationId(1);
  214. goodsSpecificationDao.save(goodsSpecification);
  215. product.setGoodsSpecificationIds(goodsSpecification.getId().toString());
  216. product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
  217. }
  218. return productDao.save(product);
  219. }
  220. @Override
  221. @Transactional
  222. public int update(GoodsEntity goods) {
  223. Map<String, Object> valideDate = MapBeanUtil.fromObject(goods);
  224. ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
  225. builder.put("merchSn", "商户编号");
  226. builder.put("attributeCategory", "商品分类");
  227. builder.put("categoryId", "商品二级分类");
  228. builder.put("goodsSn", "商品编码");
  229. builder.put("name", "商品名称");
  230. builder.put("goodsUnit", "商品单位");
  231. builder.put("prodBarcode", "产品条码");
  232. builder.put("goodsBizType", "货品业务类型");
  233. builder.put("brandId", "品牌");
  234. builder.put("supplierId", "供应商");
  235. builder.put("freightId", "运费模版");
  236. builder.put("primaryPicUrl", "商品主图");
  237. builder.put("listPicUrl", "商品列表图");
  238. builder.put("goodsDesc", "商品描述");
  239. builder.put("isOnSale", "上架");
  240. builder.put("isHot", "热销");
  241. R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
  242. if (Integer.valueOf(r.get("code").toString()) != 0) {
  243. throw new RRException(r.get("msg").toString());
  244. } else {
  245. if (!Dict.orderBizType.item_11.getItem().equals(goods.getGoodsBizType())) {
  246. // 海关信息,普通货物可不添加
  247. builder.put("sku", "SKU");
  248. builder.put("goodsRate", "商品税率");
  249. builder.put("retailPrice", "零售价");
  250. builder.put("brand", "产品品牌");
  251. builder.put("unitCode", "计量单位代码");
  252. builder.put("cusGoodsCode", "海关商品编码");
  253. builder.put("ciqProdModel", "国检规格型号");
  254. builder.put("oriCntCode", "原产国代码");
  255. builder.put("cusRecCode", "海关备案编号");
  256. builder.put("cusDeclEle", "海关申报要素");
  257. }
  258. r = ValidatorUtil.isEmpty(builder.build(), valideDate);
  259. if (Integer.valueOf(r.get("code").toString()) != 0) {
  260. throw new RRException(r.get("msg").toString());
  261. }
  262. }
  263. // 商品轮播图
  264. List<GoodsGalleryEntity> galleryEntityList = goods.getGoodsImgList();
  265. if (galleryEntityList == null || galleryEntityList.size() <= 0) {
  266. throw new RRException("至少保留一张商品轮播图!");
  267. }
  268. GoodsEntity prodbarGoods = goodsDao.queryObjectByProdBarcode(goods.getProdBarcode(),"",goods.getId());
  269. if(prodbarGoods != null){
  270. throw new RRException("不能有重复的产品条码信息!");
  271. }
  272. SysUserEntity user = ShiroUtils.getUserEntity();
  273. Map<String, Object> map = new HashMap<>();
  274. map.put("isSame", "true");
  275. map.put("sku", goods.getSku());
  276. map.put("goodsSn", goods.getGoodsSn());
  277. map.put("goodsBizType", goods.getGoodsBizType());
  278. map.put("id", goods.getId());
  279. List<GoodsEntity> list = querySame(map);
  280. if (list != null && list.size() != 0) {
  281. throw new RRException("已存在该商品编码或该货品业务类型下已存在此SKU!");
  282. }
  283. // 修改商品
  284. if (Dict.orderBizType.item_02.getItem().equals(goods.getGoodsBizType())
  285. || Dict.orderBizType.item_10.getItem().equals(goods.getGoodsBizType())) {
  286. goods.setIsHot(0);
  287. }
  288. goods.setAttributeCategory(categoryDao.queryObject(goods.getCategoryId()).getParentId());
  289. goods.setIsDelete(0);
  290. goods.setIsNew(0);
  291. goods.setUpdateUserId(user.getUserId());
  292. goods.setUpdateTime(new Date());
  293. goods.setModTime(new Date());
  294. // 修改商品
  295. goodsDao.update(goods);
  296. // 保税商品修改各个门店商品价格
  297. if (!Dict.orderBizType.item_11.getItem().equals(goods.getGoodsBizType())) {
  298. List<ProductStoreRelaEntity> productStoreRelaEntityList = productStoreRelaDao.queryByGoodsId(goods.getId());
  299. if (productStoreRelaEntityList != null && productStoreRelaEntityList.size() > 0) {
  300. for (ProductStoreRelaEntity productStoreRela : productStoreRelaEntityList) {
  301. productStoreRela.setRetailPrice(goods.getRetailPrice());
  302. productStoreRela.setMarketPrice(goods.getMarketPrice());
  303. productStoreRelaDao.update(productStoreRela);
  304. }
  305. }
  306. }
  307. // 修改商品轮播图
  308. goodsGalleryDao.deleteByGoodsId(goods.getId());
  309. for (int i=0;i<galleryEntityList.size();i++) {
  310. GoodsGalleryEntity galleryEntity =galleryEntityList.get(i);
  311. galleryEntity.setMerchSn(goods.getMerchSn());
  312. galleryEntity.setGoodsId(goods.getId());
  313. galleryEntity.setSortOrder((i+1));
  314. galleryEntity.setFileType("0");//图片
  315. goodsGalleryDao.save(galleryEntity);
  316. }
  317. if(org.apache.commons.lang.StringUtils.isNotEmpty(goods.getVideoUrl())){
  318. GoodsGalleryEntity galleryEntity = new GoodsGalleryEntity();
  319. galleryEntity.setMerchSn(goods.getMerchSn());
  320. galleryEntity.setGoodsId(goods.getId());
  321. galleryEntity.setSortOrder(0);
  322. galleryEntity.setFileType("1");//视频
  323. galleryEntity.setImgUrl(goods.getVideoUrl());
  324. goodsGalleryDao.save(galleryEntity);
  325. }
  326. // 修改商品参数
  327. List<GoodsAttributeEntity> attributeEntityList = goods.getAttributeEntityList();
  328. if (attributeEntityList != null && attributeEntityList.size() > 0) {
  329. for (GoodsAttributeEntity item : attributeEntityList) {
  330. if (item.getIsDelete() == 0) {
  331. if (item.getId() != null) {
  332. item.setMerchSn(goods.getMerchSn());
  333. goodsAttributeDao.update(item);
  334. } else if (item.getId() == null && item.getAttributeId() != null && StringUtils.isNotEmpty(item.getValue())) {
  335. item.setGoodsId(goods.getId());
  336. item.setMerchSn(goods.getMerchSn());
  337. goodsAttributeDao.save(item);
  338. } else if (item.getId() == null && item.getAttributeId() != null && StringUtils.isNullOrEmpty(item.getValue())) {
  339. throw new RRException("商品属性【" + attributeDao.queryObject(item.getAttributeId()).getName() + "】值不能为空!");
  340. } else if (item.getId() == null && item.getAttributeId() == null) {
  341. continue;
  342. }
  343. } else if (item.getIsDelete() == 1) {
  344. goodsAttributeDao.delete(item.getId());
  345. }
  346. }
  347. }
  348. // 修改产品
  349. ProductEntity product = productDao.queryObjectBySn(goods.getGoodsSn());
  350. GoodsSpecificationEntity goodsSpecification = new GoodsSpecificationEntity();
  351. // 保税商品,普通货物暂不添加商品规格
  352. if (!Dict.orderBizType.item_11.getItem().equals(goods.getGoodsBizType())) {
  353. // 添加商品规格
  354. goodsSpecification = goodsSpecificationDao.queryByGoodsId(goods.getId());
  355. goodsSpecification.setValue(goods.getCiqProdModel());
  356. goodsSpecificationDao.update(goodsSpecification);
  357. }
  358. if(product == null){
  359. product = new ProductEntity();
  360. product.setGoodsSn(goods.getGoodsSn());
  361. product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
  362. product.setGoodsSpecificationIds(goodsSpecification.getSpecificationId()+"");
  363. product.setGoodsId(goods.getId());
  364. product.setGoodsNumber(goods.getGoodsNumber());
  365. product.setGoodsDefault(0);
  366. return productDao.save(product);
  367. }else{
  368. product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
  369. return productDao.update(product);
  370. }
  371. }
  372. @Override
  373. public int delete(Integer id) {
  374. SysUserEntity user = ShiroUtils.getUserEntity();
  375. GoodsEntity goodsEntity = goodsDao.queryObject(id);
  376. goodsEntity.setIsDelete(1);
  377. goodsEntity.setIsOnSale(0);
  378. goodsEntity.setUpdateUserId(user.getUserId());
  379. goodsEntity.setUpdateTime(new Date());
  380. //更新团购
  381. Map params = new HashMap();
  382. params.put("goodsId", id);
  383. List<GoodsGroupEntity> groupVos = goodsGroupDao.queryList(params);
  384. if (null != groupVos && groupVos.size() > 0) {
  385. for (GoodsGroupEntity groupVo : groupVos) {
  386. groupVo.setOpenStatus(3);
  387. goodsGroupDao.update(groupVo);
  388. }
  389. }
  390. return goodsDao.update(goodsEntity);
  391. }
  392. @Override
  393. @Transactional
  394. public int deleteBatch(Integer[] ids) {
  395. int result = 0;
  396. for (Integer id : ids) {
  397. result += delete(id);
  398. }
  399. return result;
  400. }
  401. @Override
  402. @Transactional
  403. public int back(Integer[] ids) {
  404. SysUserEntity user = ShiroUtils.getUserEntity();
  405. int result = 0;
  406. for (Integer id : ids) {
  407. GoodsEntity goodsEntity = queryObject(id);
  408. goodsEntity.setIsDelete(0);
  409. goodsEntity.setIsOnSale(1);
  410. goodsEntity.setUpdateUserId(user.getUserId());
  411. goodsEntity.setUpdateTime(new Date());
  412. result += goodsDao.update(goodsEntity);
  413. }
  414. return result;
  415. }
  416. @Override
  417. public int enSale(Integer id) {
  418. SysUserEntity user = ShiroUtils.getUserEntity();
  419. GoodsEntity goodsEntity = queryObject(id);
  420. if (1 == goodsEntity.getIsOnSale()) {
  421. throw new RRException("此商品已处于上架状态!");
  422. }
  423. goodsEntity.setIsOnSale(1);
  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(0);
  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(1);
  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(0);
  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(0);
  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.getSpecificationId()+"");
  690. product.setGoodsId(goodsEntity.getId());
  691. product.setGoodsDefault(0);
  692. product.setGoodsNumber(goodsEntity.getGoodsNumber());
  693. productDao.save(product);
  694. }else{
  695. product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
  696. productDao.update(product);
  697. }
  698. }
  699. }
  700. ExportExceptionDataEntity exportExceptionDataEntity = new ExportExceptionDataEntity();
  701. exportExceptionDataEntity.setCreaterSn(user.getUserId().toString());
  702. exportExceptionDataEntity.setUserId(user.getUserId().intValue());
  703. exportExceptionDataEntity.setCreateTime(new Date());
  704. exportExceptionDataEntity.setModTime(new Date());
  705. exportExceptionDataEntity.setMerchSn(merchSn);
  706. exportExceptionDataEntity.setStoreId(user.getStoreId());
  707. if(exportDataType == 1) {
  708. exportExceptionDataEntity.setExportDataType("1");
  709. }else{
  710. exportExceptionDataEntity.setExportDataType("2");
  711. }
  712. if(failMerchUserGoodsSnList != null && failMerchUserGoodsSnList.size() > 0){
  713. exportExceptionDataEntity.setExportExceptionData("不能操作除了登录用户以外商户的商品,当前商户编号为【"+merchSn+"】,请检查商品编码【"+failMerchUserGoodsSnList+"】的商品信息");
  714. exportExceptionDataDao.save(exportExceptionDataEntity);
  715. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  716. }
  717. if(failMerchGoodsSnList != null && failMerchGoodsSnList.size() > 0){
  718. exportExceptionDataEntity.setExportExceptionData("商户编号不存在,请检查商品编码【"+failMerchGoodsSnList+"】的商品信息,请先维护用户商户编号信息再继续操作!");
  719. exportExceptionDataDao.save(exportExceptionDataEntity);
  720. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  721. }
  722. if(failGoodsSnList != null && failGoodsSnList.size() > 0){
  723. if(failSameSkuList.size()>0) {
  724. exportExceptionDataEntity.setExportExceptionData("不能有重复的商品编码、sku信息!请检查商品编码【" + failGoodsSnList + "】,业务类型【" +
  725. failGoodsTypeList + "】,SKU【" + failSameSkuList + "】的商品信息");
  726. exportExceptionDataDao.save(exportExceptionDataEntity);
  727. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  728. }else{
  729. exportExceptionDataEntity.setExportExceptionData("不能有重复的商品编码、sku信息!请检查商品编码【" + failGoodsSnList + "】,业务类型【" +
  730. failGoodsTypeList + "】的商品信息");
  731. exportExceptionDataDao.save(exportExceptionDataEntity);
  732. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  733. }
  734. }
  735. if(failTypeGoodsSnList != null && failTypeGoodsSnList.size() > 0){
  736. exportExceptionDataEntity.setExportExceptionData("货品业务类型只能是【00保税备货、02保税补货、10保税展示】!请检查商品编码【"+failTypeGoodsSnList+"】的商品信息");
  737. exportExceptionDataDao.save(exportExceptionDataEntity);
  738. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  739. }
  740. if(failProdbarGoodsSnList != null && failProdbarGoodsSnList.size() > 0){
  741. exportExceptionDataEntity.setExportExceptionData("不能有重复的产品条码信息!请检查商品编码【"+failProdbarGoodsSnList+"】的商品产品条码信息");
  742. exportExceptionDataDao.save(exportExceptionDataEntity);
  743. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  744. }
  745. if(failHotGoodsSnList != null && failHotGoodsSnList.size() > 0){
  746. exportExceptionDataEntity.setExportExceptionData("请检查业务类型为【保税补货或保税展示】的商品,商品编码【"+failHotGoodsSnList+"】的商品不能设置为热销!");
  747. exportExceptionDataDao.save(exportExceptionDataEntity);
  748. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  749. }
  750. if(failCateGoodsSnList != null && failCateGoodsSnList.size() > 0){
  751. exportExceptionDataEntity.setExportExceptionData("分类信息请在商城配置》商品分类中维护,商品分类与商户信息对应,请检查该商品商户信息下的分类是否维护,不存在的商品编码【"+failCateGoodsSnList+"】");
  752. exportExceptionDataDao.save(exportExceptionDataEntity);
  753. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  754. }
  755. if(failCateL2GoodsSnList != null && failCateL2GoodsSnList.size() > 0){
  756. exportExceptionDataEntity.setExportExceptionData("分类信息请在商城配置》商品分类中查看,商品分类必须为二级分类,不存在的商品编码【"+failCateL2GoodsSnList+"】");
  757. exportExceptionDataDao.save(exportExceptionDataEntity);
  758. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  759. }
  760. if(failBrandGoodsSnList != null && failBrandGoodsSnList.size() > 0){
  761. exportExceptionDataEntity.setExportExceptionData("品牌信息请在商城配置》品牌制造商中维护,品牌与商户信息对应,请检查该商品商户信息下的品牌是否维护,不存在的商品编码【" + failBrandGoodsSnList + "】");
  762. exportExceptionDataDao.save(exportExceptionDataEntity);
  763. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  764. }
  765. if(failFreightGoodsSnList != null && failFreightGoodsSnList.size() > 0){
  766. exportExceptionDataEntity.setExportExceptionData("运费信息请在商城配置》运费模板中维护,运费与商户信息对应,请检查该商品商户信息下的运费是否维护,不存在的商品编码【"+failFreightGoodsSnList+"】,运费【"+failFreightList+"】");
  767. exportExceptionDataDao.save(exportExceptionDataEntity);
  768. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  769. }
  770. if(failSuppGoodsSnList != null && failSuppGoodsSnList.size() > 0){
  771. exportExceptionDataEntity.setExportExceptionData("供应商信息请在商城配置》商品供应商中维护,供应商与商户信息对应,请检查该商品商户信息下的供应商是否维护,不存在的商品编码【" + failSuppGoodsSnList + "】");
  772. exportExceptionDataDao.save(exportExceptionDataEntity);
  773. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  774. }
  775. if(failUnitGoodsSnList != null && failUnitGoodsSnList.size() > 0){
  776. exportExceptionDataEntity.setExportExceptionData("计算单位信息请在商城配置》计算单位中维护,不存在的商品编码【" + failUnitGoodsSnList + "】");
  777. exportExceptionDataDao.save(exportExceptionDataEntity);
  778. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  779. }
  780. if(failNationGoodsSnList != null && failNationGoodsSnList.size() > 0){
  781. exportExceptionDataEntity.setExportExceptionData("原产国信息请在商城配置》原产国中维护,不存在的商品编码【" + failNationGoodsSnList + "】");
  782. exportExceptionDataDao.save(exportExceptionDataEntity);
  783. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  784. }
  785. }else{
  786. throw new RRException("导入数据为空,或者检查商品编码数据是否为空");
  787. }
  788. return 1;
  789. }
  790. /* @Override
  791. @Transactional
  792. public int uploadExcel(MultipartFile file) {
  793. SysUserEntity user = ShiroUtils.getUserEntity();
  794. List<String[]> list = ExcelImport.getExcelData(file);
  795. // 取门店名称
  796. StoreEntity storeEntity = storeDao.queryObjectByName(list.get(0)[3]);
  797. if (null == storeEntity) {
  798. return 0;
  799. }
  800. //去除表头两行、底部合计
  801. if (list != null && list.size() > 3) {
  802. ProductStoreRelaEntity storeRelaEntity;
  803. ProductEntity productEntity;
  804. for (int i = 2; i < list.size() - 1; i++) {
  805. String[] item = list.get(i);
  806. String goodsSn = item[0];
  807. productEntity = productDao.queryObjectBySn(goodsSn);
  808. if (StringUtils.isNullOrEmpty(goodsSn)) {
  809. continue;
  810. }
  811. if (null == productEntity || null == productEntity.getId()) {
  812. continue;
  813. }
  814. storeRelaEntity = productStoreRelaDao.queryByStoreIdProductId(storeEntity.getId(), productEntity.getId());
  815. if (null != storeRelaEntity && null != storeRelaEntity.getId()) {
  816. storeRelaEntity.setRetailPrice(new BigDecimal(item[6]));
  817. storeRelaEntity.setStockNum(Integer.valueOf(item[3].replace(".00", "")));
  818. storeRelaEntity.setStockPrice(new BigDecimal(item[4]));
  819. productStoreRelaDao.update(storeRelaEntity);
  820. } else {
  821. storeRelaEntity = new ProductStoreRelaEntity();
  822. storeRelaEntity.setGoodsId(productEntity.getGoodsId());
  823. storeRelaEntity.setProductId(productEntity.getId());
  824. storeRelaEntity.setRetailPrice(new BigDecimal(item[6]));
  825. storeRelaEntity.setMarketPrice(new BigDecimal(item[6]));
  826. storeRelaEntity.setStockNum(Integer.valueOf(item[3]));
  827. storeRelaEntity.setStockPrice(new BigDecimal(item[4]));
  828. storeRelaEntity.setStoreId(storeEntity.getId());
  829. productStoreRelaDao.save(storeRelaEntity);
  830. }
  831. }
  832. }
  833. return 1;
  834. }*/
  835. public GoodsEntity queryObjectBySn(String goodsSn) {
  836. return goodsDao.queryObjectBySn(goodsSn);
  837. }
  838. }