GoodsServiceImpl.java 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122
  1. package com.kmall.admin.service.impl;
  2. import cn.hutool.http.HttpUtil;
  3. import com.alibaba.fastjson.JSON;
  4. import com.google.common.collect.ImmutableBiMap;
  5. import com.google.common.collect.Lists;
  6. import com.google.common.collect.Maps;
  7. import com.kmall.admin.dao.*;
  8. import com.kmall.admin.dto.GoodsDetailsDto;
  9. import com.kmall.admin.dto.GoodsDto;
  10. import com.kmall.admin.dto.GoodsPanoramaDto;
  11. import com.kmall.admin.entity.*;
  12. import com.kmall.admin.entity.kmall2eccs.KtoEccsEntity;
  13. import com.kmall.admin.entity.mk.MkActivitiesEntity;
  14. import com.kmall.admin.entity.shop.ShopErrorPriceRecordEntity;
  15. import com.kmall.admin.haikong.client.HaiKongWarehouseTemplate;
  16. import com.kmall.admin.haikong.constant.Constants;
  17. import com.kmall.admin.haikong.dto.Criteria;
  18. import com.kmall.admin.haikong.dto.WareQueryStockParamDTO;
  19. import com.kmall.admin.haikong.dto.WareQueryStockResponseDTO;
  20. import com.kmall.admin.haikong.vo.QueryGoodsVO;
  21. import com.kmall.admin.service.*;
  22. import com.kmall.admin.service.kmall2eccs.KtoEccsService;
  23. import com.kmall.admin.service.mk.MkActivitiesService;
  24. import com.kmall.admin.service.mk.MkActivityFormService;
  25. import com.kmall.admin.service.GoodsService;
  26. import com.kmall.admin.service.shop.ShopErrorPriceRecordService;
  27. import com.kmall.admin.utils.CalculateTax;
  28. import com.kmall.admin.utils.GoodsUtils;
  29. import com.kmall.admin.utils.ShiroUtils;
  30. import com.kmall.admin.utils.jackson.JacksonUtil;
  31. import com.kmall.api.entity.exportpdf.PDFGoodsDto;
  32. import com.kmall.common.constant.Dict;
  33. import com.kmall.admin.fromcomm.entity.SysUserEntity;
  34. import com.kmall.common.utils.*;
  35. import com.kmall.manager.manager.express.sf.ServiceException;
  36. import com.kmall.manager.manager.redis.JedisCacheManager;
  37. import com.kmall.manager.manager.redis.JedisUtil;
  38. import org.slf4j.Logger;
  39. import org.slf4j.LoggerFactory;
  40. import org.springframework.beans.factory.annotation.Autowired;
  41. import org.springframework.core.env.Environment;
  42. import org.springframework.stereotype.Service;
  43. import org.springframework.transaction.annotation.Transactional;
  44. import java.math.BigDecimal;
  45. import java.math.RoundingMode;
  46. import java.text.SimpleDateFormat;
  47. import java.util.*;
  48. import java.util.stream.Collectors;
  49. /**
  50. * Service实现类
  51. *
  52. * @author Scott
  53. * @email
  54. * @date 2017-08-21 21:19:49
  55. */
  56. @Service("goodsService")
  57. public class GoodsServiceImpl implements GoodsService {
  58. private static final Logger log = LoggerFactory.getLogger(GoodsServiceImpl.class);
  59. @Autowired
  60. private MerchDao merchDao;
  61. @Autowired
  62. private GoodsDao goodsDao;
  63. @Autowired
  64. private ProductDao productDao;
  65. @Autowired
  66. private GoodsGalleryDao goodsGalleryDao;
  67. @Autowired
  68. private GoodsSpecificationDao goodsSpecificationDao;
  69. @Autowired
  70. private ProductStoreRelaDao productStoreRelaDao;
  71. @Autowired
  72. private StoreDao storeDao;
  73. @Autowired
  74. private GoodsGroupDao goodsGroupDao;
  75. @Autowired
  76. private CategoryDao categoryDao;
  77. @Autowired
  78. private SupplierDao supplierDao;
  79. @Autowired
  80. private SysCusNationCodeDao sysCusNationCodeDao;
  81. @Autowired
  82. private SysCusUnitCodeDao sysCusUnitCodeDao;
  83. @Autowired
  84. private ExportExceptionDataDao exportExceptionDataDao;
  85. @Autowired
  86. private CartDao cartDao;
  87. @Autowired
  88. private ThirdMerchantBizDao thirdMerchantBizDao;
  89. @Autowired
  90. private MngChangeDao mngChangeDao;
  91. @Autowired
  92. private MerchUserDao merchUserDao;
  93. @Autowired
  94. private StoreMngChangeDao storeMngChangeDao;
  95. @Autowired
  96. private GoodsUtils goodsUtils;
  97. @Autowired
  98. private TaxErrorRecordDao taxErrorRecordDao;
  99. @Autowired
  100. private MkActivityFormService mkActivityFormService; // 活动表
  101. @Autowired
  102. private MkActivitiesService mkActivitiesService; // 营销方式表
  103. @Autowired
  104. private MkDailyActivitiesService dailyActivitiesService; // 日常活动
  105. @Autowired
  106. private MkActivitiesCouponService couponService; // 优惠券
  107. @Autowired
  108. private MkActivitiesCombinationPriceService combinationPriceService; // 组合价
  109. @Autowired
  110. private MkActivitiesDiscountService discountService; // 折扣
  111. @Autowired
  112. private MkActivitiesFullGiftService fullGiftService; // 满赠
  113. @Autowired
  114. private MkActivitiesFullReductionService fullReductionService; // 满减
  115. @Autowired
  116. private MkActivitiesGetOneFreeGoodsService getOneFreeGoodsService; // 买一送一
  117. @Autowired
  118. private MkActivitiesPromotionService promotionService; // 临时促销
  119. @Autowired
  120. private MkActivitiesHalfPriceService halfPriceService; // 第二份半价
  121. @Autowired
  122. private BrandService brandService;
  123. @Autowired
  124. private ShopErrorPriceRecordService shopErrorPriceRecordService;
  125. @Autowired
  126. private KtoEccsService ktoEccsService;
  127. @Autowired
  128. private HaiKongWarehouseTemplate haiKongWarehouseTemplate;
  129. @Autowired
  130. private Environment environment;
  131. @Override
  132. public GoodsEntity queryObject(Integer id) {
  133. Map<String, Object> map = new HashMap<String, Object>();
  134. map.put("goodsId", id);
  135. // List<GoodsAttributeEntity> attributeEntities = goodsAttributeDao.queryList(map);
  136. List<ProductEntity> productEntityList = productDao.queryList(map);
  137. GoodsEntity entity = goodsDao.queryObject(id);
  138. // entity.setAttributeEntityList(attributeEntities);
  139. entity.setProductEntityList(productEntityList);
  140. return entity;
  141. }
  142. @Override
  143. public GoodsEntity queryObjectByProdBarcodeAndBizType(String prodBarcode, Integer storeId){
  144. Map<String, Object> map = new HashMap<String, Object>();
  145. map.put("prodBarcode", prodBarcode);
  146. GoodsEntity entity = goodsDao.queryObjectByProdBarcodeAndBizType(prodBarcode, storeId);
  147. return entity;
  148. }
  149. @Override
  150. public List<GoodsEntity> queryList(Map<String, Object> map) {
  151. return goodsDao.queryList(map);
  152. }
  153. @Override
  154. public List<GoodsEntity> querySame(Map<String, Object> map) {
  155. return goodsDao.querySame(map);
  156. }
  157. @Override
  158. public int queryTotal(Map<String, Object> map) {
  159. return goodsDao.queryTotal(map);
  160. }
  161. @Override
  162. @Transactional
  163. public int save(GoodsEntity goods) {
  164. Map<String, Object> valideDate = MapBeanUtil.fromObject(goods);
  165. ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
  166. builder.put("merchSn", "商户编号");
  167. builder.put("thirdPartyMerchCode", "第三方商户编号");
  168. // builder.put("attributeCategory", "商品分类");
  169. // builder.put("categoryId", "商品二级分类");
  170. builder.put("goodsSn", "商品编码");
  171. builder.put("name", "商品名称");
  172. builder.put("goodsUnit", "商品单位");
  173. builder.put("prodBarcode", "产品条码");
  174. builder.put("goodsBizType", "货品业务类型");
  175. // builder.put("brandId", "品牌");
  176. builder.put("supplierId", "供应商");
  177. // builder.put("freightId", "运费模版");
  178. builder.put("goodsNumber", "商品总库存");
  179. builder.put("primaryPicUrl", "商品主图");
  180. builder.put("listPicUrl", "商品列表图");
  181. builder.put("goodsDesc", "商品描述");
  182. builder.put("isOnSale", "上架");
  183. builder.put("isHot", "热销");
  184. //builder.put("englishName", "商品英文名称");
  185. // builder.put("plu", "PLU");
  186. R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
  187. if (Integer.valueOf(r.get("code").toString()) != 0) {
  188. throw new RRException(r.get("msg").toString());
  189. } else {
  190. if (!Dict.orderBizType.item_11.getItem().equals(goods.getGoodsBizType())) {
  191. // 海关信息,普通货物可不添加
  192. builder.put("sku", "SKU");
  193. builder.put("goodsRate", "商品税率");
  194. // builder.put("retailPrice", "零售价");
  195. // builder.put("brand", "产品品牌");
  196. builder.put("unitCode", "计量单位代码");
  197. //builder.put("cusGoodsCode", "海关商品编码");
  198. // builder.put("ciqProdModel", "国检规格型号");
  199. builder.put("oriCntCode", "原产国代码");
  200. // builder.put("cusDeclEle", "海关申报要素");
  201. //builder.put("cusRecCode", "海关备案编号");
  202. }
  203. r = ValidatorUtil.isEmpty(builder.build(), valideDate);
  204. if (Integer.valueOf(r.get("code").toString()) != 0) {
  205. throw new RRException(r.get("msg").toString());
  206. }
  207. }
  208. /*ThirdMerchantBizEntity thirdMerchantBizEntity = thirdMerchantBizDao.getThirdMerchangByCode(goods.getThirdPartyMerchCode());
  209. if(thirdMerchantBizEntity == null){
  210. throw new RRException("第三方商户信息不存在");
  211. }
  212. if(Dict.orderBizType.item_00.getItem().equalsIgnoreCase(goods.getGoodsBizType())){
  213. if(Dict.isStockShare.item_1.getItem().equalsIgnoreCase(thirdMerchantBizEntity.getIsStockShare())){
  214. builder.put("goodsNumber", "商品库存");
  215. }
  216. }*/
  217. r = ValidatorUtil.isEmpty(builder.build(), valideDate);
  218. if (Integer.valueOf(r.get("code").toString()) != 0) {
  219. throw new RRException(r.get("msg").toString());
  220. }
  221. // 商品轮播图
  222. // List<GoodsGalleryEntity> galleryEntityList = goods.getGoodsImgList();
  223. // if (galleryEntityList == null || galleryEntityList.size() <= 0) {
  224. // throw new RRException("至少添加一张商品轮播图!");
  225. // }
  226. // List<GoodsEntity> prodbarGoodsList = goodsDao.queryObjectByProdBarcode(goods.getProdBarcode(),goods.getMerchSn(),null);
  227. // if(prodbarGoodsList != null && prodbarGoodsList.size() > 0){
  228. // throw new RRException("不能有重复的产品条码信息!");
  229. // }
  230. SysUserEntity user = ShiroUtils.getUserEntity();
  231. Map<String, Object> map = new HashMap<>();
  232. map.put("isSame", "true");
  233. map.put("sku", goods.getSku());
  234. map.put("goodsSn", goods.getGoodsSn());
  235. map.put("goodsBizType", goods.getGoodsBizType());
  236. List<GoodsEntity> list = querySame(map);
  237. if (list != null && list.size() != 0) {
  238. throw new RRException("已存在该商品编码,或该货品业务类型下已存在此SKU!");
  239. }
  240. // 添加商品
  241. if (Dict.orderBizType.item_02.getItem().equals(goods.getGoodsBizType())
  242. || Dict.orderBizType.item_10.getItem().equals(goods.getGoodsBizType())) {
  243. goods.setIsHot(0);
  244. }
  245. // goods.setAttributeCategory(categoryDao.queryObject(goods.getCategoryId()).getParentId());
  246. goods.setAddTime(new Date());
  247. goods.setIsDelete(Integer.parseInt(Dict.isDelete.item_0.getItem()));
  248. goods.setIsNew(0);
  249. goods.setCreateUserId(user.getUserId());
  250. goods.setUpdateUserId(user.getUserId());
  251. goods.setUpdateTime(new Date());
  252. goods.setModTime(new Date());
  253. goods.setCreateTime(new Date());
  254. // 新增商品
  255. goodsDao.save(goods);
  256. Long id = goods.getId();
  257. // 添加商品轮播图
  258. // for (int i=0;i<galleryEntityList.size();i++) {
  259. // GoodsGalleryEntity galleryEntity =galleryEntityList.get(i);
  260. // galleryEntity.setMerchSn(goods.getMerchSn());
  261. // galleryEntity.setGoodsId(id);
  262. // galleryEntity.setSortOrder((i+1));
  263. // galleryEntity.setFileType("0");//图片
  264. // goodsGalleryDao.save(galleryEntity);
  265. // }
  266. if(org.apache.commons.lang.StringUtils.isNotEmpty(goods.getVideoUrl())){
  267. GoodsGalleryEntity galleryEntity = new GoodsGalleryEntity();
  268. galleryEntity.setMerchSn(goods.getMerchSn());
  269. galleryEntity.setGoodsId(id);
  270. galleryEntity.setSortOrder(0);
  271. galleryEntity.setFileType("1");//视频
  272. goodsGalleryDao.save(galleryEntity);
  273. }
  274. /*
  275. // 添加商品参数
  276. List<GoodsAttributeEntity> attributeEntityList = goods.getAttributeEntityList();
  277. if (attributeEntityList != null && attributeEntityList.size() > 0) {
  278. for (GoodsAttributeEntity item : attributeEntityList) {
  279. if (item.getIsDelete() == 0) {
  280. if (item.getId() == null && item.getAttributeId() != null && StringUtils.isNotEmpty(item.getValue())) {
  281. item.setGoodsId(id);
  282. item.setMerchSn(goods.getMerchSn());
  283. goodsAttributeDao.save(item);
  284. } else if (item.getId() == null && item.getAttributeId() != null && StringUtils.isNullOrEmpty(item.getValue())) {
  285. throw new RRException("商品属性【" + attributeDao.queryObject(item.getAttributeId()).getName() + "】值不能为空!");
  286. } else if (item.getId() == null && item.getAttributeId() == null) {
  287. continue;
  288. }
  289. }
  290. }
  291. }*/
  292. if (goods.getGoodsNumber() != null){
  293. MngChangeEntity mngChangeEntity = new MngChangeEntity();
  294. mngChangeEntity.setGoodsId(Integer.parseInt(String.valueOf(id)));
  295. mngChangeEntity.setThirdPartyMerchCode(goods.getThirdPartyMerchCode());
  296. mngChangeEntity.setChangeReason("新增商户商品总库存");
  297. mngChangeEntity.setChangeType(Dict.changeType.item_0.getItem());
  298. mngChangeEntity.setChangeNum(goods.getGoodsNumber());//变化数
  299. mngChangeEntity.setOriginalNum(0);//原库存数
  300. mngChangeEntity.setValidNum(goods.getGoodsNumber());//可用数
  301. mngChangeEntity.setCreateTime(new Date());
  302. mngChangeEntity.setModTime(new Date());
  303. mngChangeEntity.setCreaterSn(user.getUsername());
  304. mngChangeEntity.setModerSn(user.getUsername());
  305. mngChangeEntity.setIsValid(0);
  306. mngChangeEntity.setMerchSn(goods.getMerchSn());
  307. mngChangeDao.save(mngChangeEntity);
  308. }
  309. // 添加产品
  310. ProductEntity product = new ProductEntity();
  311. product.setGoodsId(id);
  312. product.setGoodsSn(goods.getGoodsSn());
  313. // 保税商品,普通货物暂不添加商品规格
  314. if (!Dict.orderBizType.item_11.getItem().equals(goods.getGoodsBizType())) {
  315. // 添加商品规格
  316. GoodsSpecificationEntity goodsSpecification = new GoodsSpecificationEntity();
  317. goodsSpecification.setGoodsId(id);
  318. goodsSpecification.setValue(goods.getCiqProdModel());
  319. goodsSpecification.setSpecificationId(1);
  320. goodsSpecificationDao.save(goodsSpecification);
  321. product.setGoodsSpecificationIds(goodsSpecification.getId().toString());
  322. product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
  323. }
  324. return productDao.save(product);
  325. }
  326. @Override
  327. @Transactional
  328. public int update(GoodsEntity goods) {
  329. Map<String, Object> valideDate = MapBeanUtil.fromObject(goods);
  330. ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
  331. builder.put("merchSn", "商户编号");
  332. builder.put("thirdPartyMerchCode", "第三方商户编号");
  333. // builder.put("attributeCategory", "商品分类");
  334. // builder.put("categoryId", "商品二级分类");
  335. builder.put("goodsSn", "商品编码");
  336. builder.put("name", "商品名称");
  337. builder.put("goodsUnit", "商品单位");
  338. builder.put("prodBarcode", "产品条码");
  339. builder.put("goodsBizType", "货品业务类型");
  340. // builder.put("brandId", "品牌");
  341. builder.put("supplierId", "供应商");
  342. builder.put("goodsNumber", "商品总库存");
  343. // builder.put("freightId", "运费模版");
  344. builder.put("primaryPicUrl", "商品主图");
  345. builder.put("listPicUrl", "商品列表图");
  346. // builder.put("goodsDesc", "商品描述");
  347. builder.put("isOnSale", "上架");
  348. builder.put("isHot", "热销");
  349. R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
  350. if (Integer.valueOf(r.get("code").toString()) != 0) {
  351. throw new RRException(r.get("msg").toString());
  352. } else {
  353. if (!Dict.orderBizType.item_11.getItem().equals(goods.getGoodsBizType())) {
  354. // 海关信息,普通货物可不添加
  355. builder.put("sku", "SKU");
  356. builder.put("goodsRate", "商品税率");
  357. // builder.put("retailPrice", "零售价");
  358. // builder.put("brand", "产品品牌");
  359. builder.put("unitCode", "计量单位代码");
  360. //builder.put("cusGoodsCode", "海关商品编码");
  361. //builder.put("ciqProdModel", "国检规格型号");
  362. builder.put("oriCntCode", "原产国代码");
  363. //builder.put("cusRecCode", "海关备案编号");
  364. //builder.put("cusDeclEle", "海关申报要素");
  365. }
  366. r = ValidatorUtil.isEmpty(builder.build(), valideDate);
  367. if (Integer.valueOf(r.get("code").toString()) != 0) {
  368. throw new RRException(r.get("msg").toString());
  369. }
  370. }
  371. /*ThirdMerchantBizEntity thirdMerchantBizEntity = thirdMerchantBizDao.getThirdMerchangByCode(goods.getThirdPartyMerchCode());
  372. if(thirdMerchantBizEntity == null){
  373. throw new RRException("所属第三方商户不存在");
  374. }*/
  375. GoodsEntity goodsEntity = goodsDao.queryObject(goods.getId());
  376. if(goodsEntity != null){
  377. /*if(Dict.orderBizType.item_00.getItem().equalsIgnoreCase(goods.getGoodsBizType())){
  378. if(Dict.isStockShare.item_1.getItem().equalsIgnoreCase(thirdMerchantBizEntity.getIsStockShare())){
  379. builder.put("goodsNumber", "商品库存");
  380. }
  381. }
  382. r = ValidatorUtil.isEmpty(builder.build(), valideDate);
  383. if (Integer.valueOf(r.get("code").toString()) != 0) {
  384. throw new RRException(r.get("msg").toString());
  385. }*/
  386. }else{
  387. throw new RRException("商品不存在");
  388. }
  389. // 商品轮播图
  390. List<GoodsGalleryEntity> galleryEntityList = goods.getGoodsImgList();
  391. if (galleryEntityList == null || galleryEntityList.size() <= 0) {
  392. // throw new RRException("至少保留一张商品轮播图!");
  393. }
  394. // List<GoodsEntity> prodbarGoodsList = goodsDao.queryObjectByProdBarcode(goods.getProdBarcode(),goods.getMerchSn(),goods.getId());
  395. // if(prodbarGoodsList != null && prodbarGoodsList.size() > 0){
  396. // throw new RRException("不能有重复的产品条码信息!");
  397. // }
  398. SysUserEntity user = ShiroUtils.getUserEntity();
  399. Map<String, Object> map = new HashMap<>();
  400. map.put("isSame", "true");
  401. map.put("sku", goods.getSku());
  402. map.put("goodsSn", goods.getGoodsSn());
  403. map.put("goodsBizType", goods.getGoodsBizType());
  404. map.put("id", goods.getId());
  405. List<GoodsEntity> list = querySame(map);
  406. if (list != null && list.size() != 0) {
  407. throw new RRException("已存在该商品编码,或该货品业务类型下已存在此SKU!");
  408. }
  409. // 修改商品
  410. if (Dict.orderBizType.item_02.getItem().equals(goods.getGoodsBizType())
  411. || Dict.orderBizType.item_10.getItem().equals(goods.getGoodsBizType())) {
  412. goods.setIsHot(0);
  413. }
  414. // goods.setAttributeCategory(categoryDao.queryObject(goods.getCategoryId()).getParentId());
  415. goods.setIsDelete(Integer.parseInt(Dict.isDelete.item_0.getItem()));
  416. goods.setIsNew(0);
  417. goods.setUpdateUserId(user.getUserId());
  418. goods.setUpdateTime(new Date());
  419. goods.setModTime(new Date());
  420. if(goods.getGoodsNumber()==null){
  421. goods.setGoodsNumber(0);
  422. }else{
  423. if(goodsEntity.getGoodsNumber() == null){
  424. goodsEntity.setGoodsNumber(0);
  425. }
  426. MngChangeEntity mngChangeEntity = new MngChangeEntity();
  427. mngChangeEntity.setGoodsId(Integer.parseInt(String.valueOf(goods.getId())));
  428. mngChangeEntity.setThirdPartyMerchCode(goods.getThirdPartyMerchCode());
  429. mngChangeEntity.setChangeReason("更新商户商品总库存");
  430. mngChangeEntity.setCreateTime(new Date());
  431. mngChangeEntity.setModTime(new Date());
  432. mngChangeEntity.setCreaterSn(user.getUsername());
  433. mngChangeEntity.setModerSn(user.getUsername());
  434. mngChangeEntity.setIsValid(0);
  435. mngChangeEntity.setMerchSn(goods.getMerchSn());
  436. if(goodsEntity.getGoodsNumber() != goods.getGoodsNumber()) {
  437. if (goodsEntity.getGoodsNumber() > goods.getGoodsNumber()) {
  438. mngChangeEntity.setChangeNum(goodsEntity.getGoodsNumber() - goods.getGoodsNumber());//变化数
  439. mngChangeEntity.setChangeType(Dict.changeType.item_4.getItem());
  440. } else {
  441. mngChangeEntity.setChangeNum(goods.getGoodsNumber() - goodsEntity.getGoodsNumber());//变化数
  442. mngChangeEntity.setChangeType(Dict.changeType.item_3.getItem());
  443. }
  444. mngChangeEntity.setOriginalNum(goodsEntity.getGoodsNumber());//原库存数
  445. mngChangeEntity.setValidNum(goods.getGoodsNumber());//可用数
  446. mngChangeDao.save(mngChangeEntity);
  447. }
  448. }
  449. // 修改商品
  450. goodsDao.update(goods);
  451. // 保税商品修改各个门店商品价格
  452. List<ProductStoreRelaEntity> productStoreRelaEntityList = productStoreRelaDao.queryByGoodsId(goodsEntity.getId());
  453. Long[] storeIds = new Long[productStoreRelaEntityList.size()];
  454. Integer goodsNumber = goods.getGoodsNumber();//商品总库存
  455. Integer storeTotalGoodsNumber = 0;//商品分配库存
  456. if (productStoreRelaEntityList != null && productStoreRelaEntityList.size() > 0) {
  457. for (int i = 0; i < productStoreRelaEntityList.size(); i++) {
  458. //修改该商品的所属商户信息,如在该商户门店中有该上架的商品信息,则提示该商品不能修改
  459. ProductStoreRelaEntity relaEntity = productStoreRelaEntityList.get(i);
  460. if(org.apache.commons.lang3.StringUtils.isNotEmpty(relaEntity.getMerchSn()) && relaEntity.getMerchSn().equalsIgnoreCase(goods.getMerchSn())
  461. && goods.getIsOnSale() == Integer.parseInt(Dict.isOnSale.item_1.getItem())) {
  462. }else {
  463. if (goods.getIsOnSale() != Integer.parseInt(Dict.isOnSale.item_0.getItem())) {
  464. throw new RRException("商品编码为【" + goods.getGoodsSn() + "】的商品已上架在商户编号为【" + relaEntity.getMerchSn() + "】的门店中,可先将该商品下架后再进行修改!");
  465. }
  466. }
  467. storeTotalGoodsNumber = storeTotalGoodsNumber + relaEntity.getStockNum();
  468. storeIds[i] = relaEntity.getStoreId();
  469. }
  470. }
  471. if(goodsEntity.getIsStockShare().equalsIgnoreCase(Dict.isStockShare.item_0.getItem())) {
  472. if (goodsNumber < storeTotalGoodsNumber) {
  473. throw new RRException("该商品已在各门店分配库存" + storeTotalGoodsNumber + ",当前商品总库存不能小于分配库存总额!请先修改门店库存!");
  474. }
  475. }else{
  476. //共享库存商品库存变化,修改该商品的所有库存信息
  477. ProductStoreRelaEntity productStoreRelaEntity = new ProductStoreRelaEntity();
  478. productStoreRelaEntity.setStockNum(goodsNumber);
  479. productStoreRelaEntity.setGoodsId(goods.getId());
  480. productStoreRelaDao.updateStockNumByGoodsId(productStoreRelaEntity);
  481. for (int i = 0; i < productStoreRelaEntityList.size(); i++) {
  482. ProductStoreRelaEntity relaEntity = productStoreRelaEntityList.get(i);
  483. //新增库存操作记录
  484. StoreMngChangeEntity storeMngChangeEntity = new StoreMngChangeEntity();
  485. storeMngChangeEntity.setChangeReason("共享库存变更,更新门店商品库存");
  486. storeMngChangeEntity.setGoodsId(Integer.parseInt(String.valueOf(relaEntity.getGoodsId())));
  487. storeMngChangeEntity.setStoreId(Integer.parseInt(String.valueOf(relaEntity.getStoreId())));
  488. storeMngChangeEntity.setMerchSn(goods.getMerchSn());
  489. storeMngChangeEntity.setCreateTime(new Date());
  490. storeMngChangeEntity.setModTime(new Date());
  491. storeMngChangeEntity.setCreaterSn(user.getUsername());
  492. storeMngChangeEntity.setModerSn(user.getUsername());
  493. storeMngChangeEntity.setIsValid(0);
  494. Integer orginalNum = relaEntity.getStockNum()==null?0:relaEntity.getStockNum();//原有库存
  495. if(goodsNumber != orginalNum) {
  496. if (orginalNum > goodsNumber) {
  497. storeMngChangeEntity.setChangeType(Dict.changeType.item_4.getItem());
  498. storeMngChangeEntity.setStoreChangeNum(orginalNum - goodsNumber);//变化数
  499. } else {
  500. storeMngChangeEntity.setChangeType(Dict.changeType.item_3.getItem());
  501. storeMngChangeEntity.setStoreChangeNum(goodsNumber - orginalNum);//变化数
  502. }
  503. storeMngChangeEntity.setStoreOriginalNum(orginalNum);//原库存数
  504. storeMngChangeEntity.setStoreValidNum(goodsNumber);//可用数
  505. storeMngChangeDao.save(storeMngChangeEntity);
  506. }
  507. }
  508. }
  509. Map cartMap = Maps.newHashMap();
  510. cartMap.put("goodsId",goods.getId());
  511. List<CartEntity> cartList = cartDao.queryList(cartMap);
  512. if (cartList != null && cartList.size() > 0) {
  513. for (CartEntity cartEntity : cartList) {
  514. // cartEntity.setRetailPrice(goods.getRetailPrice());
  515. // cartEntity.setMarketPrice(goods.getMarketPrice());
  516. cartEntity.setSku(goods.getSku());
  517. cartEntity.setGoodsName(goods.getName());
  518. cartEntity.setGoodsSn(goods.getGoodsSn());
  519. cartDao.update(cartEntity);
  520. }
  521. }
  522. // 修改商品轮播图
  523. goodsGalleryDao.deleteByGoodsId(goods.getId());
  524. for (int i=0;i<galleryEntityList.size();i++) {
  525. GoodsGalleryEntity galleryEntity =galleryEntityList.get(i);
  526. galleryEntity.setMerchSn(goods.getMerchSn());
  527. galleryEntity.setGoodsId(goods.getId());
  528. galleryEntity.setSortOrder((i+1));
  529. galleryEntity.setFileType("0");//图片
  530. goodsGalleryDao.save(galleryEntity);
  531. }
  532. if(org.apache.commons.lang.StringUtils.isNotEmpty(goods.getVideoUrl())){
  533. GoodsGalleryEntity galleryEntity = new GoodsGalleryEntity();
  534. galleryEntity.setMerchSn(goods.getMerchSn());
  535. galleryEntity.setGoodsId(goods.getId());
  536. galleryEntity.setSortOrder(0);
  537. galleryEntity.setFileType("1");//视频
  538. galleryEntity.setImgUrl(goods.getVideoUrl());
  539. goodsGalleryDao.save(galleryEntity);
  540. }
  541. /*// 修改商品参数
  542. List<GoodsAttributeEntity> attributeEntityList = goods.getAttributeEntityList();
  543. if (attributeEntityList != null && attributeEntityList.size() > 0) {
  544. for (GoodsAttributeEntity item : attributeEntityList) {
  545. if (item.getIsDelete() == 0) {
  546. if (item.getId() != null) {
  547. item.setMerchSn(goods.getMerchSn());
  548. goodsAttributeDao.update(item);
  549. } else if (item.getId() == null && item.getAttributeId() != null && StringUtils.isNotEmpty(item.getValue())) {
  550. item.setGoodsId(goods.getId());
  551. item.setMerchSn(goods.getMerchSn());
  552. goodsAttributeDao.save(item);
  553. } else if (item.getId() == null && item.getAttributeId() != null && StringUtils.isNullOrEmpty(item.getValue())) {
  554. throw new RRException("商品属性【" + attributeDao.queryObject(item.getAttributeId()).getName() + "】值不能为空!");
  555. } else if (item.getId() == null && item.getAttributeId() == null) {
  556. continue;
  557. }
  558. } else if (item.getIsDelete() == 1) {
  559. goodsAttributeDao.delete(item.getId());
  560. }
  561. }
  562. }*/
  563. // 修改产品
  564. ProductEntity product = productDao.queryObjectByGoodsIdAndStoreId(String.valueOf(goods.getId()),"");
  565. GoodsSpecificationEntity goodsSpecification = null;
  566. // 保税商品,普通货物暂不添加商品规格
  567. if (!Dict.orderBizType.item_11.getItem().equals(goods.getGoodsBizType())) {
  568. // 添加商品规格
  569. goodsSpecification = goodsSpecificationDao.queryByGoodsId(goods.getId());
  570. if(goodsSpecification != null) {
  571. goodsSpecification.setValue(goods.getCiqProdModel());
  572. goodsSpecificationDao.update(goodsSpecification);
  573. }else{
  574. goodsSpecification = new GoodsSpecificationEntity();
  575. goodsSpecification.setGoodsId(goods.getId());
  576. goodsSpecification.setValue(goods.getCiqProdModel());
  577. goodsSpecification.setSpecificationId(1);
  578. goodsSpecificationDao.save(goodsSpecification);
  579. }
  580. //更新门店商品是否有修改字段
  581. if(storeIds.length > 0){
  582. for(int i=0;i<storeIds.length;i++){
  583. updateLoadGoodsByStoreId(storeIds[i], user);
  584. }
  585. }
  586. if(product == null){
  587. product = new ProductEntity();
  588. product.setGoodsSn(goods.getGoodsSn());
  589. product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
  590. product.setGoodsSpecificationIds(goodsSpecification.getId()+"");
  591. product.setGoodsId(goods.getId());
  592. product.setGoodsNumber(goods.getGoodsNumber());
  593. product.setGoodsDefault(0);
  594. return productDao.save(product);
  595. }else{
  596. product.setGoodsSpecificationIds(goodsSpecification.getId().toString());
  597. product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
  598. return productDao.update(product);
  599. }
  600. }
  601. return 1;
  602. }
  603. @Transactional
  604. @Override
  605. public void updateForImgUrl(GoodsEntity goodsEntity){
  606. // 修改商品
  607. goodsDao.update(goodsEntity);
  608. }
  609. @Override
  610. public List<GoodsEntity> queryByName(String storeId, String goodsName) {
  611. return goodsDao.queryByName(storeId, goodsName);
  612. }
  613. /**
  614. * 更新门店商品是否有修改字段
  615. * @param storeId
  616. * @param user
  617. */
  618. private void updateLoadGoodsByStoreId(Long storeId, SysUserEntity user){
  619. List<MerchUserEntity> list = merchUserDao.queryMerchUserByLoadGoods(storeId);
  620. for(MerchUserEntity entity : list) {
  621. entity.setIsLoadGoods("1");
  622. entity.setModerSn(user.getUsername());
  623. entity.setStoreId(Integer.valueOf(String.valueOf(storeId)));
  624. merchUserDao.updateStoreLoadGoodsById(entity);
  625. }
  626. }
  627. @Override
  628. public int delete(Integer id) {
  629. SysUserEntity user = ShiroUtils.getUserEntity();
  630. GoodsEntity goodsEntity = goodsDao.queryObject(id);
  631. goodsEntity.setIsDelete(Integer.parseInt(Dict.isDelete.item_1.getItem()));
  632. goodsEntity.setIsOnSale(Integer.parseInt(Dict.isOnSale.item_0.getItem()));
  633. goodsEntity.setUpdateUserId(user.getUserId());
  634. goodsEntity.setUpdateTime(new Date());
  635. Map params = Maps.newHashMap();
  636. params.put("goodsId", id);
  637. List<GoodsGroupEntity> groupVos = goodsGroupDao.queryList(params);
  638. if (null != groupVos && groupVos.size() > 0) {
  639. for (GoodsGroupEntity groupVo : groupVos) {
  640. groupVo.setOpenStatus(3);
  641. goodsGroupDao.update(groupVo);
  642. }
  643. }
  644. return goodsDao.update(goodsEntity);
  645. }
  646. @Override
  647. @Transactional
  648. public int deleteBatch(Integer[] ids) {
  649. int result = 0;
  650. for (Integer id : ids) {
  651. result += delete(id);
  652. }
  653. return result;
  654. }
  655. @Override
  656. @Transactional
  657. public int back(Integer[] ids) {
  658. SysUserEntity user = ShiroUtils.getUserEntity();
  659. int result = 0;
  660. for (Integer id : ids) {
  661. GoodsEntity goodsEntity = queryObject(id);
  662. goodsEntity.setIsDelete(Integer.parseInt(Dict.isDelete.item_0.getItem()));
  663. goodsEntity.setIsOnSale(Integer.parseInt(Dict.isOnSale.item_1.getItem()));
  664. goodsEntity.setUpdateUserId(user.getUserId());
  665. goodsEntity.setUpdateTime(new Date());
  666. result += goodsDao.update(goodsEntity);
  667. }
  668. return result;
  669. }
  670. @Override
  671. public int enSale(Integer id) {
  672. SysUserEntity user = ShiroUtils.getUserEntity();
  673. GoodsEntity goodsEntity = queryObject(id);
  674. if (1 == goodsEntity.getIsOnSale()) {
  675. throw new RRException("此商品已处于上架状态!");
  676. }
  677. goodsEntity.setIsOnSale(Integer.parseInt(Dict.isOnSale.item_1.getItem()));
  678. goodsEntity.setIsDelete(Integer.parseInt(Dict.isDelete.item_0.getItem()));
  679. goodsEntity.setUpdateUserId(user.getUserId());
  680. goodsEntity.setUpdateTime(new Date());
  681. return goodsDao.update(goodsEntity);
  682. }
  683. @Override
  684. public int unSale(Integer id) {
  685. SysUserEntity user = ShiroUtils.getUserEntity();
  686. GoodsEntity goodsEntity = queryObject(id);
  687. if (0 == goodsEntity.getIsOnSale()) {
  688. throw new RRException("此商品已处于下架状态!");
  689. }
  690. goodsEntity.setIsOnSale(Integer.parseInt(Dict.isOnSale.item_0.getItem()));
  691. goodsEntity.setUpdateUserId(user.getUserId());
  692. goodsEntity.setUpdateTime(new Date());
  693. return goodsDao.update(goodsEntity);
  694. }
  695. @Override
  696. public int enSaleBatch(Integer[] ids) {
  697. int result = 0;
  698. SysUserEntity user = ShiroUtils.getUserEntity();
  699. for (Integer id : ids) {
  700. GoodsEntity goodsEntity = queryObject(id);
  701. goodsEntity.setIsOnSale(Integer.parseInt(Dict.isOnSale.item_1.getItem()));
  702. goodsEntity.setIsDelete(Integer.parseInt(Dict.isDelete.item_0.getItem()));
  703. goodsEntity.setUpdateUserId(user.getUserId());
  704. goodsEntity.setUpdateTime(new Date());
  705. result += goodsDao.update(goodsEntity);
  706. }
  707. return result;
  708. }
  709. @Override
  710. public int unSaleBatch(Integer[] ids) {
  711. int result = 0;
  712. SysUserEntity user = ShiroUtils.getUserEntity();
  713. for (Integer id : ids) {
  714. GoodsEntity goodsEntity = queryObject(id);
  715. goodsEntity.setIsOnSale(Integer.parseInt(Dict.isOnSale.item_0.getItem()));
  716. goodsEntity.setUpdateUserId(user.getUserId());
  717. goodsEntity.setUpdateTime(new Date());
  718. result += goodsDao.update(goodsEntity);
  719. }
  720. return result;
  721. }
  722. @Override
  723. @Transactional
  724. public int uploadExcel(List<GoodsDto> goodsEntityList,int exportDataType) {
  725. SysUserEntity user = ShiroUtils.getUserEntity();
  726. String merchSn = user.getMerchSn();
  727. boolean isFail = false;
  728. List<String> failSameSkuList = new ArrayList<>(),
  729. failUnitGoodsSnList = new ArrayList<>(),
  730. failNationGoodsSnList = new ArrayList<>(),
  731. failTypeGoodsSnList = new ArrayList<>(),
  732. failMerchGoodsSnList = new ArrayList<>(),
  733. failMerchUserGoodsSnList = new ArrayList<>();
  734. List<String> failGoodsSnList = new ArrayList<>();
  735. List<String> failGoodsTypeList = new ArrayList<>();
  736. // 定义需要批量插入的list
  737. List<MngChangeEntity> insertMngChangeList = new LinkedList<>();
  738. List<GoodsEntity> insertGoodsList = new LinkedList<>();
  739. // 定义需要批量修改的list
  740. List<ProductEntity> updateProductList = new LinkedList<>();
  741. List<GoodsSpecificationEntity> updateSpecificationList = new LinkedList<>();
  742. if (goodsEntityList != null && goodsEntityList.size() > 0) {
  743. for (int i = 0; i < goodsEntityList.size(); i++) {
  744. GoodsDto goodsDto = goodsEntityList.get(i);
  745. GoodsEntity goodsEntity = new GoodsEntity();
  746. Map<String, Object> valideDate = MapBeanUtil.fromObject(goodsDto);
  747. ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
  748. builder.put("goodsSn", "商品编码");
  749. builder.put("thirdPartyMerchCode", "第三方商户代码");
  750. builder.put("goodsBizType", "货品业务类型");
  751. builder.put("name", "商品名称");
  752. builder.put("prodBarcode", "产品条码");
  753. builder.put("sku", "sku");
  754. builder.put("isOnSaleStr", "上架");
  755. builder.put("goodsUnit", "商品单位");
  756. builder.put("isHotStr", "是否热销");
  757. builder.put("goodsRate", "商品税率");
  758. builder.put("brand", "产品品牌");
  759. builder.put("uniqueIdentifier", "品牌唯一简码");
  760. builder.put("unitName", "计量单位");
  761. builder.put("oriCntName", "原产国");
  762. builder.put("valueAddedTaxRate", "增值税率");
  763. builder.put("goodsNumber", "库存数");
  764. builder.put("firstLevel", "一级分类");
  765. builder.put("secondLevel", "二级分类");
  766. builder.put("warehouseSn", "仓库编码");
  767. builder.put("consignorSn", "货主编码");
  768. builder.put("warehousSysGoodId", "仓储系统商品ID");
  769. builder.put("inventoryType", "库存类型");
  770. builder.put("legalUnit1Qty", "第一法定单位数量");
  771. builder.put("legalUnit2Qty", "第二法定单位数量");
  772. R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
  773. if (Integer.valueOf(r.get("code").toString()) != 0) {
  774. throw new RRException(r.get("msg").toString());
  775. }
  776. //业务类型校验
  777. if(!Dict.orderBizType.item_11.getItem().equalsIgnoreCase(goodsDto.getGoodsBizType())){
  778. if(!(Dict.orderBizType.item_02.getItem().equalsIgnoreCase(goodsDto.getGoodsBizType())
  779. || Dict.orderBizType.item_10.getItem().equalsIgnoreCase(goodsDto.getGoodsBizType())
  780. || Dict.orderBizType.item_00.getItem().equalsIgnoreCase(goodsDto.getGoodsBizType()))){
  781. isFail = true;
  782. failTypeGoodsSnList.add(goodsDto.getSku());
  783. }
  784. }
  785. ThirdMerchantBizEntity thirdMerchantBizEntity = thirdMerchantBizDao.getThirdMerchangByCode(goodsDto.getThirdPartyMerchCode());
  786. if(thirdMerchantBizEntity != null){
  787. goodsEntity.setMerchSn(thirdMerchantBizEntity.getMerchSn());
  788. goodsEntity.setThirdPartyMerchCode(thirdMerchantBizEntity.getThirdPartyMerchCode());
  789. if(!user.getRoleType().equalsIgnoreCase(Dict.roleType.item_1.getItem())) {
  790. if (!merchSn.equalsIgnoreCase(thirdMerchantBizEntity.getMerchSn())) {
  791. isFail = true;
  792. failMerchUserGoodsSnList.add(goodsDto.getGoodsSn().trim());
  793. }
  794. }
  795. }else{//商户不存在
  796. isFail = true;
  797. failMerchGoodsSnList.add(goodsDto.getGoodsSn().trim());
  798. }
  799. //校验商品信息是否已存在
  800. Map<String, Object> map = new HashMap<>();
  801. map.put("isSame", "true");
  802. map.put("sku", goodsDto.getSku().trim());
  803. map.put("goodsSn", goodsDto.getGoodsSn().trim());
  804. map.put("goodsBizType", goodsDto.getGoodsBizType());
  805. List<GoodsEntity> list = querySame(map);
  806. if (list != null && list.size() != 0) {
  807. if(goodsDto.getSku()!=null) {
  808. failSameSkuList.add(goodsDto.getSku());
  809. }
  810. failGoodsSnList.add(goodsDto.getGoodsSn());
  811. failGoodsTypeList.add(goodsDto.getGoodsBizType());
  812. }
  813. goodsEntity.setProdBarcode(goodsDto.getProdBarcode());
  814. if(thirdMerchantBizEntity != null) {
  815. SupplierEntity supplierEntity = supplierDao.queryObjectByName(goodsDto.getSupplierName(), thirdMerchantBizEntity.getMerchSn(),thirdMerchantBizEntity.getThirdPartyMerchCode());
  816. if (supplierEntity == null) {
  817. //导入没有查到供货商,执行新增
  818. MerchEntity merchEntity = merchDao.findByMerchSn(thirdMerchantBizEntity.getMerchSn());
  819. supplierEntity = new SupplierEntity();
  820. supplierEntity.setLevelMerchSn(merchEntity.getMerchSn());
  821. supplierEntity.setLevelMerchFlag(merchEntity.getMerchShortName());
  822. supplierEntity.setThirdPartyMerchCode(thirdMerchantBizEntity.getThirdPartyMerchCode());
  823. supplierEntity.setChildSupplierName(goodsDto.getSupplierName());
  824. supplierEntity.setChildSupplierFlag(goodsDto.getSupplierFlag());
  825. supplierEntity.setIsShow("0");
  826. // TODO 这里改成批量插入 SupplierEntity
  827. //新增商品供货信息
  828. supplierDao.save(supplierEntity);
  829. goodsEntity.setSupplierId(supplierEntity.getId());
  830. } else {
  831. goodsEntity.setSupplierId(supplierEntity.getId());
  832. }
  833. }
  834. GoodsEntity goodsNew = goodsDao.queryObjectBySnNew(goodsDto.getGoodsSn());
  835. if(goodsNew!=null){
  836. throw new RRException("该商品数据已存在,请勿重复导入:"+goodsNew.getGoodsSn());
  837. }
  838. // 获取品牌唯一简码
  839. checkBrand(goodsDto, goodsEntity);
  840. // 获取二级分类id
  841. checkCategory(goodsDto, goodsEntity);
  842. if (!Dict.orderBizType.item_11.getItem().equals(goodsDto.getGoodsBizType())) {
  843. SysCusUnitCodeEntity sysCusUnitCodeEntity = sysCusUnitCodeDao.queryObjectByName(goodsDto.getUnitName());
  844. if (sysCusUnitCodeEntity == null) {
  845. isFail = true;
  846. failUnitGoodsSnList.add(goodsDto.getGoodsSn().trim());
  847. } else {
  848. goodsEntity.setUnitCode(sysCusUnitCodeEntity.getCode());
  849. }
  850. //原产国
  851. SysCusNationCodeEntity sysCusNationCodeEntity = sysCusNationCodeDao.queryObjectByName(goodsDto.getOriCntName());
  852. if (sysCusNationCodeEntity == null) {
  853. isFail = true;
  854. failNationGoodsSnList.add(goodsDto.getGoodsSn().trim());
  855. } else {
  856. goodsEntity.setOriCntCode(sysCusNationCodeEntity.getCode());
  857. }
  858. if(goodsDto.getGoodsRate().indexOf("%") != -1){
  859. goodsDto.setGoodsRate(goodsDto.getGoodsRate().substring(0, goodsDto.getGoodsRate().indexOf("%")));
  860. goodsEntity.setGoodsRate(BigDecimal.valueOf(Double.valueOf(goodsDto.getGoodsRate())).divide(new BigDecimal(100), 4, BigDecimal.ROUND_HALF_UP));
  861. }else{
  862. goodsEntity.setGoodsRate(BigDecimal.valueOf(Double.valueOf(goodsDto.getGoodsRate())));
  863. }
  864. }
  865. goodsEntity.setIsOnSale(Integer.parseInt(goodsDto.getIsOnSaleStr()));
  866. goodsEntity.setIsHot(Integer.parseInt(goodsDto.getIsHotStr()));
  867. goodsEntity.setGoodsSn(goodsDto.getGoodsSn().trim());
  868. goodsEntity.setSku(goodsDto.getSku().trim());
  869. goodsEntity.setName(goodsDto.getName());
  870. goodsEntity.setGoodsUnit(goodsDto.getGoodsUnit());
  871. goodsEntity.setGoodsBizType(goodsDto.getGoodsBizType());
  872. goodsEntity.setBrand(goodsDto.getBrand());
  873. goodsEntity.setCusDeclEle(goodsDto.getCusDeclEle());
  874. goodsEntity.setCusGoodsCode(goodsDto.getCusGoodsCode());
  875. goodsEntity.setCusRecCode(goodsDto.getCusRecCode());
  876. goodsEntity.setCiqProdModel(goodsDto.getCiqProdModel());
  877. goodsEntity.setIsDelete(Integer.parseInt(Dict.isDelete.item_0.getItem()));
  878. goodsEntity.setIsNew(0);
  879. goodsEntity.setUpdateUserId(user.getUserId());
  880. goodsEntity.setAddTime(new Date());
  881. goodsEntity.setCreateTime(new Date());
  882. goodsEntity.setUpdateTime(new Date());
  883. goodsEntity.setModTime(new Date());
  884. goodsEntity.setGoodsNumber(Integer.parseInt(goodsDto.getGoodsNumber()));
  885. //新加字段1208
  886. goodsEntity.setWarehouseSn(goodsDto.getWarehouseSn());
  887. goodsEntity.setConsignorSn(goodsDto.getConsignorSn());
  888. goodsEntity.setWarehousSysGoodId(goodsDto.getWarehousSysGoodId());
  889. goodsEntity.setConsignorSn(goodsDto.getConsignorSn());
  890. goodsEntity.setInventoryType(goodsDto.getInventoryType());
  891. goodsEntity.setDefectiveProductsGrade(goodsDto.getDefectiveProductsGrade());
  892. //goodsEntity.setNetWeight(new BigDecimal(goodsDto.getNetWeight()));
  893. goodsEntity.setLegalUnit1Qty(new BigDecimal(goodsDto.getLegalUnit1Qty()));
  894. goodsEntity.setLegalUnit2Qty(new BigDecimal(goodsDto.getLegalUnit2Qty()));
  895. if(null!=goodsDto.getImpConsumTaxRate()){
  896. goodsEntity.setImpConsumTaxRate(new BigDecimal(goodsDto.getImpConsumTaxRate()));
  897. }
  898. goodsEntity.setValueAddedTaxRate(new BigDecimal(goodsDto.getValueAddedTaxRate()));
  899. if(!isFail){
  900. GoodsEntity goods = goodsDao.queryObjectBySnNew(goodsDto.getGoodsSn());
  901. MngChangeEntity mngChangeEntity = new MngChangeEntity();
  902. mngChangeEntity.setThirdPartyMerchCode(goodsEntity.getThirdPartyMerchCode());
  903. mngChangeEntity.setChangeReason("更新商户商品总库存");
  904. mngChangeEntity.setCreateTime(new Date());
  905. mngChangeEntity.setModTime(new Date());
  906. mngChangeEntity.setCreaterSn(user.getUsername());
  907. mngChangeEntity.setModerSn(user.getUsername());
  908. mngChangeEntity.setIsValid(0);
  909. mngChangeEntity.setMerchSn(goodsEntity.getMerchSn());
  910. //新增、修改商品表
  911. if(goods!=null) {// 修改商品
  912. mngChangeEntity.setOriginalNum(goods.getGoodsNumber());//原库存数
  913. mngChangeEntity.setValidNum(Integer.parseInt(goodsDto.getGoodsNumber()));//可用数
  914. mngChangeEntity.setChangeNum(Integer.parseInt(goodsDto.getGoodsNumber()));//变化数
  915. mngChangeEntity.setChangeType(Dict.changeType.item_3.getItem());
  916. mngChangeEntity.setGoodsId(goods.getId().intValue());
  917. goodsEntity.setId(goods.getId());
  918. goodsEntity.setGoodsNumber(goods.getGoodsNumber()+Integer.parseInt(goodsDto.getGoodsNumber()));
  919. // TODO 这里改成批量修改 GoodsEntity
  920. goodsDao.update(goodsEntity);
  921. }else{
  922. mngChangeEntity.setOriginalNum(0);//原库存数
  923. mngChangeEntity.setValidNum(Integer.parseInt(goodsDto.getGoodsNumber()));//可用数
  924. mngChangeEntity.setChangeNum(Integer.parseInt(goodsDto.getGoodsNumber()));//变化数
  925. mngChangeEntity.setChangeType(Dict.changeType.item_2.getItem());
  926. goodsDao.save(goodsEntity);
  927. insertGoodsList.add(goodsEntity);
  928. mngChangeEntity.setGoodsId(goodsEntity.getId().intValue());
  929. }
  930. // TODO 这里改成批量插入 MngChangeEntity
  931. insertMngChangeList.add(mngChangeEntity);
  932. // 修改产品
  933. ProductEntity product = productDao.queryObjectByGoodsIdAndStoreId(String.valueOf(goodsEntity.getId()), "");
  934. GoodsSpecificationEntity goodsSpecification = new GoodsSpecificationEntity();
  935. // 普通货物暂不添加商品规格
  936. if (!Dict.orderBizType.item_11.getItem().equals(goodsDto.getGoodsBizType())) {
  937. // 添加商品规格
  938. GoodsSpecificationEntity specificationEntity = goodsSpecificationDao.queryByGoodsId(goodsEntity.getId());
  939. if(specificationEntity == null) {
  940. goodsSpecification.setGoodsId(goodsEntity.getId());
  941. goodsSpecification.setValue(goodsEntity.getCiqProdModel());
  942. goodsSpecification.setSpecificationId(1);
  943. // TODO 这里改成批量插入 GoodsSpecificationEntity
  944. goodsSpecificationDao.save(goodsSpecification);
  945. }else{
  946. // {
  947. // goodsSpecification.setValue(goodsDto.getCiqProdModel());
  948. goodsSpecification.setId(specificationEntity.getId());
  949. //
  950. // // TODO 这里改成批量更新 GoodsSpecificationEntity
  951. // updateSpecificationList.add(goodsSpecification);
  952. // }
  953. }
  954. if(product == null){
  955. product = new ProductEntity();
  956. product.setGoodsSn(goodsDto.getGoodsSn());
  957. product.setGoodsId(goodsEntity.getId());
  958. product.setGoodsDefault(0);
  959. product.setGoodsNumber(goodsEntity.getGoodsNumber());
  960. product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
  961. product.setGoodsSpecificationIds(goodsSpecification.getId().toString());
  962. // TODO 这里改成批量插入 ProductEntity
  963. productDao.save(product);
  964. }else{
  965. product.setGoodsSpecificationIds(goodsSpecification.getId().toString());
  966. product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
  967. // TODO 这里改成批量更新 ProductEntity
  968. updateProductList.add(product);
  969. }
  970. }
  971. }
  972. }
  973. if(insertMngChangeList != null && insertMngChangeList.size() > 0 ){
  974. if(insertMngChangeList.size() > 50){
  975. List<List<MngChangeEntity>> partition = Lists.partition(insertMngChangeList, 50);
  976. for (List<MngChangeEntity> mngChangeEntities : partition) {
  977. mngChangeDao.saveBatch(mngChangeEntities);
  978. }
  979. }else{
  980. mngChangeDao.saveBatch(insertMngChangeList);
  981. }
  982. }
  983. // 批量更新数据mall_product
  984. if(updateProductList != null && updateProductList.size() > 0 ){
  985. if(updateProductList.size() > 50){
  986. List<List<ProductEntity>> partition = Lists.partition(updateProductList, 50);
  987. for (List<ProductEntity> productEntities : partition) {
  988. productDao.updateBatch(productEntities);
  989. }
  990. }else{
  991. productDao.updateBatch(updateProductList);
  992. }
  993. }
  994. // 批量更新数据mall_goods_specification 商品对应规格表值表
  995. if(updateSpecificationList != null && updateSpecificationList.size() > 0 ){
  996. if(updateSpecificationList.size() > 50){
  997. List<List<GoodsSpecificationEntity>> partition = Lists.partition(updateSpecificationList, 50);
  998. for (List<GoodsSpecificationEntity> goodsSpecificationEntities : partition) {
  999. goodsSpecificationDao.updateBatch(goodsSpecificationEntities);
  1000. }
  1001. }
  1002. }
  1003. ExportExceptionDataEntity exportExceptionDataEntity = new ExportExceptionDataEntity();
  1004. exportExceptionDataEntity.setCreaterSn(user.getUserId().toString());
  1005. exportExceptionDataEntity.setUserId(user.getUserId().intValue());
  1006. exportExceptionDataEntity.setCreateTime(new Date());
  1007. exportExceptionDataEntity.setModTime(new Date());
  1008. exportExceptionDataEntity.setMerchSn(merchSn);
  1009. exportExceptionDataEntity.setStoreId(user.getStoreId());
  1010. if(exportDataType == 1) {
  1011. exportExceptionDataEntity.setExportDataType(Dict.exportDataType.item_1.getItem());
  1012. }else{
  1013. exportExceptionDataEntity.setExportDataType(Dict.exportDataType.item_2.getItem());
  1014. }
  1015. if(failMerchUserGoodsSnList != null && failMerchUserGoodsSnList.size() > 0){
  1016. exportExceptionDataEntity.setExportExceptionData("不能操作除了登录用户以外商户的商品,当前商户编号为【"+merchSn+"】,请检查商品编码【"+failMerchUserGoodsSnList+"】的商品信息");
  1017. goodsUtils.save(exportExceptionDataEntity);
  1018. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1019. }
  1020. if(failMerchGoodsSnList != null && failMerchGoodsSnList.size() > 0){
  1021. exportExceptionDataEntity.setExportExceptionData("第三方商户代码不存在,请在商城配置》第三方商户管理中维护,请检查商品编码【"+failMerchGoodsSnList+"】的商品信息,请先维护再继续操作!");
  1022. goodsUtils.save(exportExceptionDataEntity);
  1023. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1024. }
  1025. if(failTypeGoodsSnList != null && failTypeGoodsSnList.size() > 0){
  1026. exportExceptionDataEntity.setExportExceptionData("货品业务类型只能是【00保税备货、02保税补货、10保税展示】!请检查商品编码【"+failTypeGoodsSnList+"】的商品信息");
  1027. goodsUtils.save(exportExceptionDataEntity);
  1028. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1029. }
  1030. // if(failHotGoodsSnList != null && failHotGoodsSnList.size() > 0){
  1031. // exportExceptionDataEntity.setExportExceptionData("请检查业务类型为【保税补货或保税展示】的商品,商品编码【"+failHotGoodsSnList+"】的商品不能设置为热销!");
  1032. // goodsUtils.save(exportExceptionDataEntity);
  1033. // throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1034. // }
  1035. if(failUnitGoodsSnList != null && failUnitGoodsSnList.size() > 0){
  1036. exportExceptionDataEntity.setExportExceptionData("计算单位信息请在商城配置》计算单位中维护,不存在的商品编码【" + failUnitGoodsSnList + "】");
  1037. goodsUtils.save(exportExceptionDataEntity);
  1038. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1039. }
  1040. if(failNationGoodsSnList != null && failNationGoodsSnList.size() > 0){
  1041. exportExceptionDataEntity.setExportExceptionData("原产国信息请在商城配置》原产国中维护,不存在的商品编码【" + failNationGoodsSnList + "】");
  1042. goodsUtils.save(exportExceptionDataEntity);
  1043. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1044. }
  1045. }else{
  1046. throw new RRException("导入数据为出错,请检查数据是否按要求填写");
  1047. }
  1048. return 1;
  1049. }
  1050. private void checkCategory(GoodsDto goodsDto, GoodsEntity goodsEntity) {
  1051. // 二级分类名称
  1052. String secondLevel = goodsDto.getSecondLevel().trim();
  1053. String firstLevelName = goodsDto.getFirstLevel().trim();
  1054. CategoryEntity categoryNew = categoryDao.queryByName(secondLevel);
  1055. if(categoryNew != null){
  1056. String categoryId = categoryNew.getId().toString();
  1057. String level = categoryNew.getLevel();
  1058. if(categoryId == null){
  1059. throw new RuntimeException("该分类不是二级分类,请检查,对应sku是:"+ goodsDto.getSku()+",分类名称为:"+ goodsDto.getSecondLevel());
  1060. }else if(Dict.Level.item_L2.getItem().equals(level)){
  1061. CategoryEntity firstCategory = categoryDao.queryByName(firstLevelName);
  1062. if(firstCategory==null) {
  1063. throw new RuntimeException("该分类不是一级分类,请检查,对应sku是:" + goodsDto.getSku() + ",分类名称为:" + firstLevelName);
  1064. }
  1065. goodsEntity.setAttributeCategory(firstCategory.getId());
  1066. // 二级分类id在缓存中有,直接存入商品
  1067. goodsEntity.setCategoryId(Integer.parseInt(categoryId));
  1068. }
  1069. }else {
  1070. throw new RuntimeException("该分类不是二级分类,请检查,对应sku是:"+ goodsDto.getSku()+",分类名称为:"+ goodsDto.getSecondLevel());
  1071. }
  1072. }
  1073. private void checkBrand(GoodsDto goodsDto, GoodsEntity goodsEntity) {
  1074. String uniqueIdentifier = goodsDto.getUniqueIdentifier();
  1075. String brandName = goodsDto.getBrand();
  1076. Map<String, String> brandCache = JedisUtil.getMap(uniqueIdentifier);
  1077. if(brandCache != null) {
  1078. String brandId = brandCache.get("brandId");
  1079. if (brandId == null) {
  1080. Map<String, String> brandCacheData = new HashMap<>();
  1081. // 数据库里面查询
  1082. BrandEntity queryBrand = brandService.queryByUniqueIdentifier(uniqueIdentifier.trim());
  1083. if (queryBrand == null) {
  1084. throw new RuntimeException("该品牌不存在,请检查品牌唯一标识符是否正确:"+uniqueIdentifier);
  1085. } else {
  1086. brandCacheData.put("brandId", queryBrand.getId() + "");
  1087. brandCacheData.put("brandName", queryBrand.getName());
  1088. goodsEntity.setBrandId(queryBrand.getId());
  1089. }
  1090. JedisUtil.setMap(uniqueIdentifier, brandCacheData, 0);
  1091. } else {
  1092. goodsEntity.setBrandId(Integer.parseInt(brandId));
  1093. }
  1094. }else{
  1095. Map<String, String> brandCacheData = new HashMap<>();
  1096. // 数据库里面查询
  1097. BrandEntity queryBrand = brandService.queryByUniqueIdentifier(uniqueIdentifier.trim());
  1098. if (queryBrand == null) {
  1099. throw new RuntimeException("该品牌不存在,请检查品牌唯一标识符是否正确:"+uniqueIdentifier);
  1100. } else {
  1101. brandCacheData.put("brandId", queryBrand.getId() + "");
  1102. brandCacheData.put("brandName", queryBrand.getName());
  1103. goodsEntity.setBrandId(queryBrand.getId());
  1104. }
  1105. JedisUtil.setMap(uniqueIdentifier, brandCacheData, 0);
  1106. }
  1107. }
  1108. private CategoryEntity initCategory(String firstLevel, int parentId, Dict.Level item) {
  1109. CategoryEntity firstCategoryEntity = new CategoryEntity();
  1110. firstCategoryEntity.setName(firstLevel);
  1111. firstCategoryEntity.setParentId(parentId);
  1112. firstCategoryEntity.setIsShow(1);
  1113. firstCategoryEntity.setFrontDesc(firstLevel);
  1114. firstCategoryEntity.setType(0);
  1115. firstCategoryEntity.setLevel(item.getItem());
  1116. return firstCategoryEntity;
  1117. }
  1118. @Override
  1119. @Transactional
  1120. public int uploadExcelByCover(List<GoodsDto> goodsEntityList, int exportDataType) {
  1121. SysUserEntity user = ShiroUtils.getUserEntity();
  1122. String merchSn = user.getMerchSn();
  1123. boolean isFail = false;
  1124. List<String> failSameSkuList = new ArrayList<>(), failHotGoodsSnList = new ArrayList<>(),
  1125. failSuppGoodsSnList = new ArrayList<>(),
  1126. failUnitGoodsSnList = new ArrayList<>(), failNationGoodsSnList = new ArrayList<>(),
  1127. failTypeGoodsSnList = new ArrayList<>(), failMerchGoodsSnList = new ArrayList<>(),
  1128. failMerchUserGoodsSnList = new ArrayList<>();
  1129. // List<String> failGoodsSnList = new ArrayList<>();
  1130. List<String> failGoodsTypeList = new ArrayList<>();
  1131. if (goodsEntityList != null && goodsEntityList.size() > 0) {
  1132. for (int i = 0; i < goodsEntityList.size(); i++) {
  1133. GoodsDto goodsDto = goodsEntityList.get(i);
  1134. GoodsEntity goodsEntity = new GoodsEntity();
  1135. Map<String, Object> valideDate = MapBeanUtil.fromObject(goodsDto);
  1136. ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
  1137. builder.put("goodsSn", "商品编码");
  1138. builder.put("thirdPartyMerchCode", "第三方商户代码");
  1139. builder.put("goodsBizType", "货品业务类型");
  1140. builder.put("name", "商品名称");
  1141. builder.put("isOnSaleStr", "上架");
  1142. builder.put("goodsUnit", "商品单位");
  1143. builder.put("isHotStr", "热销");
  1144. builder.put("prodBarcode", "产品条码");
  1145. builder.put("supplierName", "供应商");
  1146. builder.put("goodsNumber", "商品总库存");
  1147. R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
  1148. if (Integer.valueOf(r.get("code").toString()) != 0) {
  1149. throw new RRException(r.get("msg").toString());
  1150. } else {
  1151. if (!Dict.orderBizType.item_11.getItem().equals(goodsDto.getGoodsBizType())) {
  1152. // 海关信息,普通货物可不添加
  1153. builder.put("goodsRate", "商品税率");
  1154. builder.put("sku", "SKU");
  1155. // builder.put("brand", "产品品牌");
  1156. builder.put("unitName", "计量单位");
  1157. builder.put("oriCntName", "原产国");
  1158. builder.put("cusGoodsCode", "海关商品编码");
  1159. //builder.put("ciqProdModel", "国检规格型号");
  1160. // builder.put("cusDeclEle", "海关申报要素");
  1161. //builder.put("cusRecCode", "海关备案编号");
  1162. }
  1163. r = ValidatorUtil.isEmpty(builder.build(), valideDate);
  1164. if (Integer.valueOf(r.get("code").toString()) != 0) {
  1165. throw new RRException(r.get("msg").toString());
  1166. }
  1167. }
  1168. //业务类型校验
  1169. if(!Dict.orderBizType.item_11.getItem().equalsIgnoreCase(goodsDto.getGoodsBizType())){
  1170. if(!(Dict.orderBizType.item_02.getItem().equalsIgnoreCase(goodsDto.getGoodsBizType())
  1171. || Dict.orderBizType.item_10.getItem().equalsIgnoreCase(goodsDto.getGoodsBizType())
  1172. || Dict.orderBizType.item_00.getItem().equalsIgnoreCase(goodsDto.getGoodsBizType()))){
  1173. isFail = true;
  1174. failTypeGoodsSnList.add(goodsDto.getSku());
  1175. }
  1176. }
  1177. ThirdMerchantBizEntity thirdMerchantBizEntity = thirdMerchantBizDao.getThirdMerchangByCode(goodsDto.getThirdPartyMerchCode());
  1178. if(thirdMerchantBizEntity != null){
  1179. goodsEntity.setMerchSn(thirdMerchantBizEntity.getMerchSn());
  1180. goodsEntity.setThirdPartyMerchCode(thirdMerchantBizEntity.getThirdPartyMerchCode());
  1181. if(!user.getRoleType().equalsIgnoreCase(Dict.roleType.item_1.getItem())) {
  1182. if (!merchSn.equalsIgnoreCase(thirdMerchantBizEntity.getMerchSn())) {
  1183. isFail = true;
  1184. failMerchUserGoodsSnList.add(goodsDto.getGoodsSn());
  1185. }
  1186. }
  1187. }else{//商户不存在
  1188. isFail = true;
  1189. failMerchGoodsSnList.add(goodsDto.getGoodsSn());
  1190. }
  1191. //校验商品信息是否已存在
  1192. Map<String, Object> map = new HashMap<>();
  1193. map.put("isSame", "true");
  1194. map.put("sku", goodsDto.getSku());
  1195. map.put("goodsSn", goodsDto.getGoodsSn());
  1196. map.put("goodsBizType", goodsDto.getGoodsBizType());
  1197. // List<GoodsEntity> list = querySame(map);
  1198. // if (list != null && list.size() != 0) {
  1199. // isFail = true;
  1200. // if(goodsDto.getSku()!=null) {
  1201. // failSameSkuList.add(goodsDto.getSku());
  1202. // }
  1203. // failGoodsSnList.add(goodsDto.getGoodsSn());
  1204. // failGoodsTypeList.add(goodsDto.getGoodsBizType());
  1205. // }
  1206. //校验产品条码是否存在
  1207. goodsEntity.setProdBarcode(goodsDto.getProdBarcode());
  1208. //热销商品校验
  1209. if (Dict.orderBizType.item_02.getItem().equals(goodsDto.getGoodsBizType())
  1210. || Dict.orderBizType.item_10.getItem().equals(goodsDto.getGoodsBizType())) {
  1211. if(goodsDto.getIsHotStr().equalsIgnoreCase("1")){
  1212. isFail = true;
  1213. failHotGoodsSnList.add(goodsDto.getGoodsSn());
  1214. }
  1215. }
  1216. if(thirdMerchantBizEntity != null) {
  1217. SupplierEntity supplierEntity = supplierDao.queryObjectByName(goodsDto.getSupplierName(), thirdMerchantBizEntity.getMerchSn(),thirdMerchantBizEntity.getThirdPartyMerchCode());
  1218. if (supplierEntity == null) {
  1219. //导入没有查到供货商,执行新增
  1220. MerchEntity merchEntity = merchDao.findByMerchSn(thirdMerchantBizEntity.getMerchSn());
  1221. if(null == merchEntity || !StringUtils.isNotEmpty(goodsDto.getSupplierFlag())){
  1222. isFail = true;
  1223. failSuppGoodsSnList.add(goodsDto.getGoodsSn());
  1224. }
  1225. supplierEntity = new SupplierEntity();
  1226. supplierEntity.setLevelMerchSn(merchEntity.getMerchSn());
  1227. supplierEntity.setLevelMerchFlag(merchEntity.getMerchShortName());
  1228. supplierEntity.setThirdPartyMerchCode(thirdMerchantBizEntity.getThirdPartyMerchCode());
  1229. supplierEntity.setChildSupplierName(goodsDto.getSupplierName());
  1230. supplierEntity.setChildSupplierFlag(goodsDto.getSupplierFlag());
  1231. supplierEntity.setIsShow("0");
  1232. supplierDao.save(supplierEntity);
  1233. goodsEntity.setSupplierId(supplierEntity.getId());
  1234. } else {
  1235. goodsEntity.setSupplierId(supplierEntity.getId());
  1236. }
  1237. }
  1238. if (!Dict.orderBizType.item_11.getItem().equals(goodsDto.getGoodsBizType())) {
  1239. SysCusUnitCodeEntity sysCusUnitCodeEntity = sysCusUnitCodeDao.queryObjectByName(goodsDto.getUnitName());
  1240. if (sysCusUnitCodeEntity == null) {
  1241. isFail = true;
  1242. failUnitGoodsSnList.add(goodsDto.getGoodsSn());
  1243. } else {
  1244. goodsEntity.setUnitCode(sysCusUnitCodeEntity.getCode());
  1245. }
  1246. //原产国
  1247. SysCusNationCodeEntity sysCusNationCodeEntity = sysCusNationCodeDao.queryObjectByName(goodsDto.getOriCntName());
  1248. if (sysCusNationCodeEntity == null) {
  1249. isFail = true;
  1250. failNationGoodsSnList.add(goodsDto.getGoodsSn());
  1251. } else {
  1252. goodsEntity.setOriCntCode(sysCusNationCodeEntity.getCode());
  1253. }
  1254. if(goodsDto.getGoodsRate().indexOf("%") != -1){
  1255. goodsDto.setGoodsRate(goodsDto.getGoodsRate().substring(0, goodsDto.getGoodsRate().indexOf("%")));
  1256. }
  1257. goodsEntity.setGoodsRate(BigDecimal.valueOf(Double.valueOf(goodsDto.getGoodsRate())).divide(new BigDecimal(100), 4, BigDecimal.ROUND_HALF_UP));
  1258. }
  1259. goodsEntity.setIsOnSale(Integer.parseInt(goodsDto.getIsOnSaleStr()));
  1260. goodsEntity.setIsHot(Integer.parseInt(goodsDto.getIsHotStr()));
  1261. goodsEntity.setGoodsSn(goodsDto.getGoodsSn());
  1262. goodsEntity.setSku(goodsDto.getSku());
  1263. goodsEntity.setName(goodsDto.getName());
  1264. goodsEntity.setGoodsUnit(goodsDto.getGoodsUnit());
  1265. goodsEntity.setGoodsBizType(goodsDto.getGoodsBizType());
  1266. goodsEntity.setBrand(goodsDto.getBrand());
  1267. goodsEntity.setCusDeclEle(goodsDto.getCusDeclEle());
  1268. goodsEntity.setCusGoodsCode(goodsDto.getCusGoodsCode());
  1269. goodsEntity.setCusRecCode(goodsDto.getCusRecCode());
  1270. goodsEntity.setCiqProdModel(goodsDto.getCiqProdModel());
  1271. goodsEntity.setIsDelete(Integer.parseInt(Dict.isDelete.item_0.getItem()));
  1272. goodsEntity.setIsNew(0);
  1273. goodsEntity.setUpdateUserId(user.getUserId());
  1274. goodsEntity.setAddTime(new Date());
  1275. goodsEntity.setCreateTime(new Date());
  1276. goodsEntity.setUpdateTime(new Date());
  1277. goodsEntity.setModTime(new Date());
  1278. goodsEntity.setGoodsNumber(Integer.parseInt(goodsDto.getGoodsNumber()));
  1279. if(!isFail){
  1280. GoodsEntity goods = goodsDao.queryObjectBySnNew(goodsDto.getGoodsSn());
  1281. MngChangeEntity mngChangeEntity = new MngChangeEntity();
  1282. mngChangeEntity.setThirdPartyMerchCode(goodsEntity.getThirdPartyMerchCode());
  1283. mngChangeEntity.setChangeReason("更新商户商品总库存");
  1284. mngChangeEntity.setCreateTime(new Date());
  1285. mngChangeEntity.setModTime(new Date());
  1286. mngChangeEntity.setCreaterSn(user.getUsername());
  1287. mngChangeEntity.setModerSn(user.getUsername());
  1288. mngChangeEntity.setIsValid(0);
  1289. mngChangeEntity.setMerchSn(goodsEntity.getMerchSn());
  1290. if(goods!=null) {// 修改商品
  1291. mngChangeEntity.setOriginalNum(goods.getGoodsNumber());//原库存数
  1292. mngChangeEntity.setValidNum(goods.getGoodsNumber() + Integer.parseInt(goodsDto.getGoodsNumber()));//可用数
  1293. mngChangeEntity.setChangeNum(Integer.parseInt(goodsDto.getGoodsNumber()));//变化数
  1294. mngChangeEntity.setChangeType(Dict.changeType.item_3.getItem());
  1295. mngChangeEntity.setGoodsId(Integer.parseInt(String.valueOf(goods.getId())));
  1296. goodsEntity.setId(goods.getId());
  1297. goodsEntity.setGoodsNumber(goods.getGoodsNumber()+Integer.parseInt(goodsDto.getGoodsNumber()));
  1298. goodsDao.update(goodsEntity);
  1299. }else{
  1300. mngChangeEntity.setOriginalNum(0);//原库存数
  1301. mngChangeEntity.setValidNum(Integer.parseInt(goodsDto.getGoodsNumber()));//可用数
  1302. mngChangeEntity.setChangeNum(Integer.parseInt(goodsDto.getGoodsNumber()));//变化数
  1303. mngChangeEntity.setChangeType(Dict.changeType.item_2.getItem());
  1304. goodsDao.save(goodsEntity);
  1305. mngChangeEntity.setGoodsId(goodsEntity.getId().intValue());
  1306. }
  1307. mngChangeDao.save(mngChangeEntity);
  1308. // 修改产品
  1309. ProductEntity product = productDao.queryObjectByGoodsIdAndStoreId(String.valueOf(goodsEntity.getId()), "");
  1310. GoodsSpecificationEntity goodsSpecification = new GoodsSpecificationEntity();
  1311. // 普通货物暂不添加商品规格
  1312. if (!Dict.orderBizType.item_11.getItem().equals(goodsDto.getGoodsBizType())) {
  1313. // 添加商品规格
  1314. GoodsSpecificationEntity specificationEntity = goodsSpecificationDao.queryByGoodsId(goodsEntity.getId());
  1315. if(specificationEntity == null) {
  1316. goodsSpecification.setGoodsId(goodsEntity.getId());
  1317. goodsSpecification.setValue(goodsEntity.getCiqProdModel());
  1318. goodsSpecification.setSpecificationId(1);
  1319. goodsSpecificationDao.save(goodsSpecification);
  1320. }else {
  1321. goodsSpecification.setValue(goodsDto.getCiqProdModel());
  1322. goodsSpecification.setId(specificationEntity.getId());
  1323. goodsSpecificationDao.update(goodsSpecification);
  1324. }
  1325. if(product == null){
  1326. product = new ProductEntity();
  1327. product.setGoodsSn(goodsDto.getGoodsSn());
  1328. product.setGoodsId(goodsEntity.getId());
  1329. product.setGoodsDefault(0);
  1330. product.setGoodsNumber(goodsEntity.getGoodsNumber());
  1331. product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
  1332. product.setGoodsSpecificationIds(goodsSpecification.getId().toString());
  1333. productDao.save(product);
  1334. }else{
  1335. product.setGoodsSpecificationIds(goodsSpecification.getId().toString());
  1336. product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
  1337. productDao.update(product);
  1338. }
  1339. }
  1340. }
  1341. }
  1342. ExportExceptionDataEntity exportExceptionDataEntity = new ExportExceptionDataEntity();
  1343. exportExceptionDataEntity.setCreaterSn(user.getUserId().toString());
  1344. exportExceptionDataEntity.setUserId(user.getUserId().intValue());
  1345. exportExceptionDataEntity.setCreateTime(new Date());
  1346. exportExceptionDataEntity.setModTime(new Date());
  1347. exportExceptionDataEntity.setMerchSn(merchSn);
  1348. exportExceptionDataEntity.setStoreId(user.getStoreId());
  1349. if(exportDataType == 1) {
  1350. exportExceptionDataEntity.setExportDataType(Dict.exportDataType.item_1.getItem());
  1351. }else{
  1352. exportExceptionDataEntity.setExportDataType(Dict.exportDataType.item_2.getItem());
  1353. }
  1354. if(failMerchUserGoodsSnList != null && failMerchUserGoodsSnList.size() > 0){
  1355. exportExceptionDataEntity.setExportExceptionData("不能操作除了登录用户以外商户的商品,当前商户编号为【"+merchSn+"】,请检查商品编码【"+failMerchUserGoodsSnList+"】的商品信息");
  1356. goodsUtils.save(exportExceptionDataEntity);
  1357. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1358. }
  1359. if(failMerchGoodsSnList != null && failMerchGoodsSnList.size() > 0){
  1360. exportExceptionDataEntity.setExportExceptionData("第三方商户代码不存在,请在商城配置》第三方商户管理中维护,请检查商品编码【"+failMerchGoodsSnList+"】的商品信息,请先维护再继续操作!");
  1361. goodsUtils.save(exportExceptionDataEntity);
  1362. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1363. }
  1364. // if(failGoodsSnList != null && failGoodsSnList.size() > 0){
  1365. // if(failSameSkuList.size()>0) {
  1366. // exportExceptionDataEntity.setExportExceptionData("不能有重复的商品编码、sku信息!请检查商品编码【" + failGoodsSnList + "】,业务类型【" +
  1367. // failGoodsTypeList + "】,SKU【" + failSameSkuList + "】的商品信息");
  1368. // goodsUtils.save(exportExceptionDataEntity);
  1369. // throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1370. // }else{
  1371. // exportExceptionDataEntity.setExportExceptionData("不能有重复的商品编码、sku信息!请检查商品编码【" + failGoodsSnList + "】,业务类型【" +
  1372. // failGoodsTypeList + "】的商品信息");
  1373. // goodsUtils.save(exportExceptionDataEntity);
  1374. // throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1375. // }
  1376. // }
  1377. if(failTypeGoodsSnList != null && failTypeGoodsSnList.size() > 0){
  1378. exportExceptionDataEntity.setExportExceptionData("货品业务类型只能是【00保税备货、02保税补货、10保税展示】!请检查商品编码【"+failTypeGoodsSnList+"】的商品信息");
  1379. goodsUtils.save(exportExceptionDataEntity);
  1380. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1381. }
  1382. if(failHotGoodsSnList != null && failHotGoodsSnList.size() > 0){
  1383. exportExceptionDataEntity.setExportExceptionData("请检查业务类型为【保税补货或保税展示】的商品,商品编码【"+failHotGoodsSnList+"】的商品不能设置为热销!");
  1384. goodsUtils.save(exportExceptionDataEntity);
  1385. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1386. }
  1387. if(failSuppGoodsSnList != null && failSuppGoodsSnList.size() > 0){
  1388. exportExceptionDataEntity.setExportExceptionData("供应商信息请在商城配置》商品供应商中维护,供应商与商户信息对应,请检查该商品商户信息下的供应商是否维护,不存在的商品编码【" + failSuppGoodsSnList + "】");
  1389. goodsUtils.save(exportExceptionDataEntity);
  1390. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1391. }
  1392. if(failUnitGoodsSnList != null && failUnitGoodsSnList.size() > 0){
  1393. exportExceptionDataEntity.setExportExceptionData("计算单位信息请在商城配置》计算单位中维护,不存在的商品编码【" + failUnitGoodsSnList + "】");
  1394. goodsUtils.save(exportExceptionDataEntity);
  1395. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1396. }
  1397. if(failNationGoodsSnList != null && failNationGoodsSnList.size() > 0){
  1398. exportExceptionDataEntity.setExportExceptionData("原产国信息请在商城配置》原产国中维护,不存在的商品编码【" + failNationGoodsSnList + "】");
  1399. goodsUtils.save(exportExceptionDataEntity);
  1400. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1401. }
  1402. }else{
  1403. throw new RRException("导入数据为空,或者检查商品编码数据是否为空");
  1404. }
  1405. return 1;
  1406. }
  1407. @Override
  1408. public GoodsDetailsDto queryGoodsDetailsByProdBarcode(String prodBarcode, String storeId,String sku) {
  1409. return goodsDao.queryGoodsDetailsByProdBarcode(prodBarcode,storeId,sku);
  1410. }
  1411. @Override
  1412. public GoodsPanoramaDto searchGoodsPanoramaDtoByKeyword(String keyword) {
  1413. return goodsDao.searchGoodsPanoramaDtoByKeyword(keyword);
  1414. }
  1415. @Override
  1416. public List<GoodsEntity> queryExportList(Map<String, Object> params) {
  1417. return goodsDao.queryExportList(params);
  1418. }
  1419. /**
  1420. * 查出pdf需要的需要
  1421. *
  1422. * @param sku 商品sku
  1423. * @param storeId
  1424. * @param prodBarcode
  1425. * @return
  1426. */
  1427. @Override
  1428. public PDFGoodsDto queryForPDFData(String sku, String storeId, String prodBarcode) {
  1429. return goodsDao.queryForPDFData(sku,storeId,prodBarcode);
  1430. }
  1431. /**
  1432. * 查询产品价格
  1433. * 改进后的逻辑:
  1434. * @param prodBarcode
  1435. * @param storeId
  1436. * @return
  1437. */
  1438. @Override
  1439. @Transactional
  1440. public synchronized Map<String,Object> calculateGoodsDetail(String prodBarcode, String storeId, String sku) {
  1441. /**
  1442. * 1.首先根据商品条码跟门店id查询是否有库存,没库存直接返回
  1443. */
  1444. GoodsDetailsDto goods = queryGoodsDetailsByProdBarcode(prodBarcode,storeId,sku);
  1445. if(goods == null) {
  1446. return null;
  1447. }
  1448. String env = environment.getProperty("haikong.env");
  1449. /*
  1450. * 2. 查询海仓仓库系统,判断库存是否足够
  1451. * */
  1452. sku = org.springframework.util.StringUtils.isEmpty(sku) ? goods.getSku() : sku;
  1453. String wareStockNumberKey = storeId + sku + prodBarcode;
  1454. String warehouseStockMapKey = Constants.WAREHOUSE_STOCK_MAP_KEY + "_" + storeId;
  1455. String redisCacheWareQuantity = JedisUtil.hget(warehouseStockMapKey, wareStockNumberKey);
  1456. Integer wareQuantity = 0;
  1457. Integer exitRegionNumber = goods.getExitRegionNumber();
  1458. String stockNum = goods.getStockNum();
  1459. Integer sellVolume = Objects.isNull(goods.getSellVolume()) ? 1 : goods.getSellVolume();
  1460. if (org.springframework.util.StringUtils.isEmpty(redisCacheWareQuantity)) {
  1461. WareQueryStockResponseDTO wareQueryStockResponseDTO = null;
  1462. String queryWarehouseStockResponse = null;
  1463. // 环境配置
  1464. if (!org.springframework.util.StringUtils.isEmpty(env) && Constants.PROD.equals(env)) {
  1465. WareQueryStockParamDTO wareQueryStockParamDTO = new WareQueryStockParamDTO();
  1466. List<Criteria> criteriaList = new ArrayList<>();
  1467. Criteria criteria = new Criteria();
  1468. criteria.setItemId(goods.getWarehouseSysGoodId());
  1469. criteria.setItemCode(goods.getSku());
  1470. criteria.setOwnerCode(goods.getConsignorSn());
  1471. criteria.setInventoryType(Constants.InventoryType.ZP.getType());
  1472. criteria.setWarehouseCode(goods.getWarehouseSn());
  1473. criteriaList.add(criteria);
  1474. wareQueryStockParamDTO.setCriteriaList(criteriaList);
  1475. try {
  1476. queryWarehouseStockResponse = haiKongWarehouseTemplate.queryWarehouseStock(wareQueryStockParamDTO);
  1477. } catch (Exception e) {
  1478. log.error("sku:【{}】,调用海控仓库系统查询库存出现异常!", sku, e);
  1479. throw new ServiceException(String.format("sku:【%s】,调用海控仓库系统查询库存出现异常!", sku));
  1480. }
  1481. log.info("调用海控仓库系统,请求参数,门店:{}、商品条码:{}、商品编码:{},响应数据:{}", storeId, prodBarcode, sku, queryWarehouseStockResponse);
  1482. if (org.springframework.util.StringUtils.isEmpty(queryWarehouseStockResponse)) {
  1483. log.error("调用库存系统接口出现错误!返回结果为空!");
  1484. throw new ServiceException("调用库存系统接口出现错误!");
  1485. }
  1486. wareQueryStockResponseDTO = JacksonUtil.fromStringJson(queryWarehouseStockResponse, WareQueryStockResponseDTO.class);
  1487. } else {
  1488. wareQueryStockResponseDTO = new WareQueryStockResponseDTO();
  1489. wareQueryStockResponseDTO.setCode("0");
  1490. wareQueryStockResponseDTO.setFlag("success");
  1491. wareQueryStockResponseDTO.setMessage("");
  1492. WareQueryStockResponseDTO.WareQueryStockResponseItemDTO wareQueryStockResponseItemDTO = new WareQueryStockResponseDTO.WareQueryStockResponseItemDTO();
  1493. wareQueryStockResponseItemDTO.setQuantity(3);
  1494. wareQueryStockResponseItemDTO.setItemCode(sku);
  1495. wareQueryStockResponseItemDTO.setProduceCode(prodBarcode);
  1496. List<WareQueryStockResponseDTO.WareQueryStockResponseItemDTO> wareQueryStockResponseItemDTOS = new ArrayList<>();
  1497. wareQueryStockResponseItemDTOS.add(wareQueryStockResponseItemDTO);
  1498. wareQueryStockResponseDTO.setItems(wareQueryStockResponseItemDTOS);
  1499. }
  1500. if (Objects.isNull(wareQueryStockResponseDTO)) {
  1501. log.error("解析一步达库存系统响应数据出现错误!请求响应结果:{}", queryWarehouseStockResponse);
  1502. throw new ServiceException("解析一步达库存系统响应数据出现错误!");
  1503. }
  1504. // 校验库存
  1505. WareQueryStockResponseDTO.WareQueryStockResponseItemDTO itemDTO = wareQueryStockResponseDTO.getItems().get(0);
  1506. // 仓库可用库存
  1507. wareQuantity = itemDTO.getQuantity();
  1508. JedisUtil.hset(warehouseStockMapKey, wareStockNumberKey, String.valueOf(wareQuantity));
  1509. } else {
  1510. wareQuantity = Integer.parseInt(redisCacheWareQuantity);
  1511. }
  1512. // 保税仓库存 + 展销店库存 - 出区数 >= 购买数
  1513. if (!((wareQuantity + Integer.parseInt(stockNum) - exitRegionNumber) > sellVolume)) {
  1514. // 库存不足
  1515. log.error("商品条码:【{}】,sku:【{}】,门店库存:【{}】,保税仓库存:【{}】,出区数:【{}】,该商品仓库库存不足!", prodBarcode, sku, stockNum, wareQuantity, exitRegionNumber);
  1516. throw new ServiceException(String.format("商品条码:【%s】,sku:【%s】,门店库存:【%s】,保税仓库存:【%s】,出区数:【%s】,该商品仓库库存不足!", prodBarcode, sku, stockNum, wareQuantity, exitRegionNumber));
  1517. } else {
  1518. JedisUtil.hset(warehouseStockMapKey, wareStockNumberKey, String.valueOf(wareQuantity - sellVolume));
  1519. }
  1520. goods.setDiscountedPrice(new BigDecimal(0));
  1521. BigDecimal retailPrice = goods.getRetailPrice();
  1522. goods.setActualPaymentAmount(retailPrice.setScale(2,RoundingMode.HALF_UP));
  1523. Map<String,Object> skuActivitiesMap = new HashMap<>();
  1524. // 计算税费
  1525. GoodsEntity goodsEntity = goodsDao.queryByBarcodeAndSku(prodBarcode, goods.getGoodsSn());
  1526. BigDecimal tax = CalculateTax.calculateFinalTax(goodsEntity, goods.getActualPaymentAmount(),this).setScale(3,RoundingMode.HALF_UP);
  1527. goods.setGoodstaxes(tax.toString());
  1528. goods.setSellVolume(1);
  1529. skuActivitiesMap.put("goods",goods);
  1530. return skuActivitiesMap;
  1531. }
  1532. @Override
  1533. public List<Map<String,Object>> selectSkuDetails(String prodBarcode, String storeId) {
  1534. /**
  1535. * 1.首先根据商品条码跟门店id查询是否有库存,没库存直接返回
  1536. */
  1537. List<GoodsDetailsDto> goodsList = goodsDao.queryGoodsSkuList(prodBarcode,storeId);
  1538. List<Map<String,Object>> mapList = new ArrayList<>();
  1539. if (goodsList == null) {
  1540. return null;
  1541. }
  1542. for(GoodsDetailsDto goods : goodsList){
  1543. mapList.add(selectGetSkuDetails(goods,prodBarcode,storeId));
  1544. }
  1545. return mapList;
  1546. }
  1547. public Map<String,Object> selectGetSkuDetails(GoodsDetailsDto goods,String prodBarcode, String storeId) {
  1548. /**
  1549. * 1.首先根据商品条码跟门店id查询是否有库存,没库存直接返回
  1550. */
  1551. goods.setDiscountedPrice(new BigDecimal(0));
  1552. BigDecimal retailPrice = goods.getRetailPrice();
  1553. goods.setActualPaymentAmount(retailPrice.setScale(2,RoundingMode.HALF_UP));
  1554. Map<String,Object> skuActivitiesMap = new HashMap<>();
  1555. // 计算税费
  1556. GoodsEntity goodsEntity = goodsDao.queryByBarcodeAndSku(prodBarcode, goods.getGoodsSn());
  1557. BigDecimal tax = CalculateTax.calculateFinalTax(goodsEntity,goods.getActualPaymentAmount(),this).setScale(3,RoundingMode.HALF_UP);
  1558. goods.setGoodstaxes(tax.toString());
  1559. skuActivitiesMap.put("goods",goods);
  1560. return skuActivitiesMap;
  1561. }
  1562. /**
  1563. * 根据条形码查询商品
  1564. *
  1565. * @param barCode
  1566. * @return
  1567. */
  1568. @Override
  1569. public GoodsEntity queryByBarcode(String barCode) {
  1570. return goodsDao.queryByBarcode(barCode);
  1571. }
  1572. /**
  1573. * 根据sku查询商品
  1574. *
  1575. * @param sku
  1576. * @return
  1577. */
  1578. @Override
  1579. public GoodsEntity queryBySku(String sku) {
  1580. return goodsDao.queryBySku(sku);
  1581. }
  1582. /* @Override
  1583. @Transactional
  1584. public int uploadExcel(MultipartFile file) {
  1585. SysUserEntity user = ShiroUtils.getUserEntity();
  1586. List<String[]> list = ExcelImport.getExcelData(file);
  1587. // 取门店名称
  1588. StoreEntity storeEntity = storeDao.queryObjectByName(list.get(0)[3]);
  1589. if (null == storeEntity) {
  1590. return 0;
  1591. }
  1592. //去除表头两行、底部合计
  1593. if (list != null && list.size() > 3) {
  1594. ProductStoreRelaEntity storeRelaEntity;
  1595. ProductEntity productEntity;
  1596. for (int i = 2; i < list.size() - 1; i++) {
  1597. String[] item = list.get(i);
  1598. String goodsSn = item[0];
  1599. productEntity = productDao.queryObjectBySn(goodsSn);
  1600. if (StringUtils.isNullOrEmpty(goodsSn)) {
  1601. continue;
  1602. }
  1603. if (null == productEntity || null == productEntity.getId()) {
  1604. continue;
  1605. }
  1606. storeRelaEntity = productStoreRelaDao.queryByStoreIdProductId(storeEntity.getId(), productEntity.getId());
  1607. if (null != storeRelaEntity && null != storeRelaEntity.getId()) {
  1608. storeRelaEntity.setRetailPrice(new BigDecimal(item[6]));
  1609. storeRelaEntity.setStockNum(Integer.valueOf(item[3].replace(".00", "")));
  1610. storeRelaEntity.setStockPrice(new BigDecimal(item[4]));
  1611. productStoreRelaDao.update(storeRelaEntity);
  1612. } else {
  1613. storeRelaEntity = new ProductStoreRelaEntity();
  1614. storeRelaEntity.setGoodsId(productEntity.getGoodsId());
  1615. storeRelaEntity.setProductId(productEntity.getId());
  1616. storeRelaEntity.setRetailPrice(new BigDecimal(item[6]));
  1617. storeRelaEntity.setMarketPrice(new BigDecimal(item[6]));
  1618. storeRelaEntity.setStockNum(Integer.valueOf(item[3]));
  1619. storeRelaEntity.setStockPrice(new BigDecimal(item[4]));
  1620. storeRelaEntity.setStoreId(storeEntity.getId());
  1621. productStoreRelaDao.save(storeRelaEntity);
  1622. }
  1623. }
  1624. }
  1625. return 1;
  1626. }*/
  1627. /**
  1628. * 全量拉取
  1629. */
  1630. @Override
  1631. public void syncOmsHsCodeTask() {
  1632. syncOmsHsCodeSegment(goodsDao.queryNonSyncOmsHsCodeSkuList());
  1633. }
  1634. /**
  1635. * 全量计算
  1636. */
  1637. @Override
  1638. public void syncGoodsRateTask() {
  1639. // 查询出所有商品
  1640. List<GoodsEntity> allGoodsList = goodsDao.queryAllList(null,null);
  1641. syncGoodsRate(allGoodsList);
  1642. }
  1643. /**
  1644. * 还原园区库存
  1645. *
  1646. * @param goodsEntities 数据
  1647. */
  1648. @Override
  1649. public int restoreBatch(List<GoodsEntity> goodsEntities) {
  1650. return goodsDao.restoreBatch(goodsEntities);
  1651. }
  1652. /**
  1653. * 查询库存及sku的信息,查询出来的库存是门店的库存
  1654. *
  1655. * @param prodBarcode 条码
  1656. * @param storeId 门店id
  1657. * @param sku sku
  1658. * @return 商品信息
  1659. */
  1660. @Override
  1661. public GoodsEntity queryGoodsStockByBarcodeAndStoreIdAndSku(String prodBarcode, Integer storeId, String sku) {
  1662. return goodsDao.queryGoodsStockByBarcodeAndStoreIdAndSku(prodBarcode, storeId, sku);
  1663. }
  1664. /**
  1665. * 查询库存及sku的信息,查询出来的库存是门店的库存
  1666. *
  1667. * @param queryGoodsVOList 查询条件,包含商品条码、sku、门店id
  1668. * @return 商品信息
  1669. */
  1670. @Override
  1671. public List<GoodsEntity> queryGoodsStockByQueryGoodsVoList(List<QueryGoodsVO> queryGoodsVOList) {
  1672. return goodsDao.queryGoodsStockByQueryGoodsVoList(queryGoodsVOList);
  1673. }
  1674. /**
  1675. * 选择拉取
  1676. * @param ids
  1677. */
  1678. @Override
  1679. public void syncOmsHsCodeGoode(List<Integer> ids) {
  1680. syncOmsHsCodeSegment(goodsDao.syncOmsHsCodeGoode(ids));
  1681. }
  1682. /**
  1683. * 选择计算
  1684. * @param ids
  1685. */
  1686. @Override
  1687. public void syncGoodsRateGoode(List<Integer> ids) {
  1688. syncGoodsRate(goodsDao.syncGoodsRateGoode(ids));
  1689. }
  1690. public void syncOmsHsCodeSegment(List<String> skuList) {
  1691. int segmentLimitSize = 300;
  1692. if (skuList.size() < segmentLimitSize) {
  1693. this.syncOmsHsCode(skuList);
  1694. } else {
  1695. int segmentSize = skuList.size() % segmentLimitSize == 0 ? skuList.size() / segmentLimitSize : (skuList.size() / segmentLimitSize) + 1;
  1696. List<List<String>> segment = new ArrayList<>();
  1697. for (int i = 0; i < segmentSize; i++) {
  1698. int end = (i == segmentSize - 1) ? skuList.size() : i * segmentLimitSize + segmentLimitSize;
  1699. segment.add(new ArrayList<>(skuList.subList(i * segmentLimitSize, end)));
  1700. }
  1701. for (List<String> tmp : segment) {
  1702. this.syncOmsHsCode(tmp);
  1703. }
  1704. }
  1705. }
  1706. @Transactional
  1707. public void syncOmsHsCode(List<String> skuList) {
  1708. if (Objects.nonNull(skuList) && skuList.size()>0){
  1709. String result = HttpUtil.get("https://oms.ds-bay.com/oms-controller-mgt/pdProductRecord/hsCodeMapBySkuList?skuList=" + skuList);
  1710. //String result = HttpUtil.get("http://183.3.221.143:8080/oms-controller-mgt/pdProductRecord/hsCodeMapBySkuList?skuList=" + skuList);
  1711. //String result = HttpUtil.get("http://127.0.0.1:8080/oms-controller-mgt/pdProductRecord/hsCodeMapBySkuList?skuList=" + skuList);
  1712. Map<String,Map> map = JSON.parseObject(result, Map.class);
  1713. for (String sku : skuList) {
  1714. Map skuInfoMap = map.get(sku);
  1715. if (Objects.nonNull(skuInfoMap)){
  1716. String hsCodeName = (String) skuInfoMap.get("cusGoodsName");
  1717. String hsCode = (String) skuInfoMap.get("cusGoodsCode");
  1718. String prodName = (String) skuInfoMap.get("prodName");
  1719. String prodRecordName = (String) skuInfoMap.get("prodRecordName");
  1720. BigDecimal impConsumTaxRate = (BigDecimal) skuInfoMap.get("impConsumTaxRate");
  1721. BigDecimal valueAddedTaxRate = (BigDecimal) skuInfoMap.get("valueAddedTaxRate");
  1722. BigDecimal legalUnit1Qty = (BigDecimal) skuInfoMap.get("legalUnit1Qty");
  1723. BigDecimal legalUnit2Qty = (BigDecimal) skuInfoMap.get("legalUnit2Qty");
  1724. BigDecimal cosmThresholdValue = (BigDecimal) skuInfoMap.get("cosmThresholdValue");
  1725. if (StringUtils.isNotEmpty(hsCodeName) && StringUtils.isNotEmpty(hsCode)){
  1726. List<GoodsEntity> goodsEntityList = goodsDao.queryListBySku(sku);
  1727. if (Objects.nonNull(goodsEntityList) && goodsEntityList.size()>0){
  1728. for (GoodsEntity goodsEntity : goodsEntityList) {
  1729. goodsEntity.setHsCode(hsCode);
  1730. goodsEntity.setHsCodeName(hsCodeName);
  1731. goodsEntity.setImpConsumTaxRate(impConsumTaxRate);
  1732. goodsEntity.setValueAddedTaxRate(valueAddedTaxRate);
  1733. goodsEntity.setIsSyncHsCode("1");
  1734. goodsEntity.setLegalUnit1Qty(legalUnit1Qty);
  1735. goodsEntity.setProdName(prodName);
  1736. goodsEntity.setProdRecordName(prodRecordName);
  1737. goodsEntity.setLegalUnit2Qty(legalUnit2Qty);
  1738. goodsEntity.setCosmThresholdValue(cosmThresholdValue);
  1739. goodsDao.update(goodsEntity);
  1740. }
  1741. }
  1742. }
  1743. }
  1744. }
  1745. }
  1746. }
  1747. @Transactional
  1748. public void syncGoodsRate(List<GoodsEntity> allGoodsList) {
  1749. String storeId = "163"; // 市场部说所有门店价格一致,并且活动一致,可直接取其中一个门店价格进行计算税率
  1750. for (GoodsEntity goodsEntity : allGoodsList) {
  1751. // 同步过海关商品编码才可以算税率
  1752. if ("1".equals(goodsEntity.getIsSyncHsCode())) {
  1753. // Map<String, Object> goodsDetailMap = calculateGoodsDetail(goodsEntity.getProdBarcode(), storeId);
  1754. // if (Objects.nonNull(goodsDetailMap)) {
  1755. // GoodsDetailsDto goods = (GoodsDetailsDto) goodsDetailMap.get("goods");
  1756. // 顾客给的钱 就是税后价
  1757. // BigDecimal actualPaymentAmount = goods.getActualPaymentAmount(); //随便拿一个门店的当前时间的价格,如果有活动价会使用活动价
  1758. // BigDecimal calculateTax = CalculateTax.calculateTax(goodsEntity, actualPaymentAmount);// 税费
  1759. // BigDecimal goodsRate = calculateTax.divide(actualPaymentAmount, 4, BigDecimal.ROUND_HALF_UP);
  1760. BigDecimal goodsRate =CalculateTax.calculateGoodsRate(goodsEntity);
  1761. goodsEntity.setGoodsRate(goodsRate);
  1762. goodsEntity.setIsSyncHsCode("0");
  1763. goodsDao.update(goodsEntity);
  1764. // }
  1765. }
  1766. }
  1767. }
  1768. @Override
  1769. public void updateTaxErrorRecord(TaxErrorRecordEntity taxErrorRecordEntity) {
  1770. taxErrorRecordDao.update(taxErrorRecordEntity);
  1771. }
  1772. @Override
  1773. public void insertTaxErrorRecord(TaxErrorRecordEntity taxErrorRecordEntity) {
  1774. taxErrorRecordDao.save(taxErrorRecordEntity);
  1775. }
  1776. @Override
  1777. public void updateByEntity(GoodsEntity updateGoods) {
  1778. goodsDao.update(updateGoods);
  1779. }
  1780. @Override
  1781. public void checkGoodsPrice(SysUserEntity user) {
  1782. Map<String, BigDecimal> hsCodeMap = CalculateTax.hsCodeMap;
  1783. Set<String> hdCodeSet = hsCodeMap.keySet();
  1784. List<Map<String,String>> errorPriceSkuList = new LinkedList<>();
  1785. // 保存各种异常价格对象集合
  1786. List<ShopErrorPriceRecordEntity> shopErrorPriceRecordEntities = new LinkedList<>();
  1787. // 查询所有特殊化妆品的商品
  1788. List<GoodsEntity> queryGoodsDetails = goodsDao.querySpecialCosmetics(hdCodeSet);
  1789. for (GoodsEntity goodsEntity : queryGoodsDetails) {
  1790. calculateShopPrice(user,goodsEntity,errorPriceSkuList,shopErrorPriceRecordEntities);
  1791. calculateDailyPrice(user,goodsEntity,errorPriceSkuList,shopErrorPriceRecordEntities);
  1792. calculeatePromotion(user,goodsEntity,errorPriceSkuList,shopErrorPriceRecordEntities);
  1793. }
  1794. if(shopErrorPriceRecordEntities.size() != 0){
  1795. shopErrorPriceRecordService.saveBatch(shopErrorPriceRecordEntities);
  1796. }
  1797. }
  1798. @Override
  1799. public List<GoodsEntity> queryAllList(Integer page,Integer pageSize) {
  1800. return goodsDao.queryAllList(page,pageSize);
  1801. }
  1802. private void calculeatePromotion(SysUserEntity user, GoodsEntity goods, List<Map<String, String>> errorPriceSkuList, List<ShopErrorPriceRecordEntity> shopErrorPriceRecordEntities) {
  1803. String prodBarcode = goods.getProdBarcode();
  1804. String storeId = goods.getStoreId()+"";
  1805. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  1806. /**
  1807. * 2.查询当前时间,该门店是否有活动,如果有活动,查询开启了哪些营销方式
  1808. * 参数: 当前时间 门店id
  1809. */
  1810. MkActivitiesPromotionEntity promotionEntity = promotionService.queryByDateAndBarcode(storeId,format.format(new Date()),"lscx",prodBarcode);
  1811. // 如果该商品存在临时促销,直接替换活动价格
  1812. if(promotionEntity == null){
  1813. return ;
  1814. }
  1815. goods.setActualPaymentAmount(promotionEntity.getActivityPrice());
  1816. goods.setRetailPrice(promotionEntity.getActivityPrice());
  1817. goods.setActivity("临时促销");
  1818. try {
  1819. CalculateTax.calculateFinalTax(goods,goods.getActualPaymentAmount(),this).setScale(3,RoundingMode.HALF_UP);
  1820. } catch (Exception e) {
  1821. ShopErrorPriceRecordEntity shopErrorPriceRecordEntity = new ShopErrorPriceRecordEntity();
  1822. shopErrorPriceRecordEntity.setMerchSn(goods.getMerchSn());
  1823. shopErrorPriceRecordEntity.setThirdMerchSn(goods.getThirdPartyMerchCode());
  1824. shopErrorPriceRecordEntity.setShopSn(goods.getStoreId()+"");
  1825. shopErrorPriceRecordEntity.setPriceType(3); // 3 是临时促销
  1826. shopErrorPriceRecordEntity.setSku(goods.getSku());
  1827. shopErrorPriceRecordEntity.setBarcode(goods.getProdBarcode());
  1828. shopErrorPriceRecordEntity.setVerifier(user.getUserId()+"");
  1829. shopErrorPriceRecordEntity.setCheckTime(new Date());
  1830. shopErrorPriceRecordEntity.setCreaterSn(user.getUserId()+"");
  1831. shopErrorPriceRecordEntity.setCreateTime(new Date());
  1832. shopErrorPriceRecordEntity.setCurrentPrice(goods.getActualPaymentAmount().toString());
  1833. if(e.getMessage().contains("-")){
  1834. String suggestPrice = e.getMessage().split("-")[0];
  1835. String highestPrice = e.getMessage().split("-")[1];
  1836. String lowestPrice = e.getMessage().split("-")[2];
  1837. shopErrorPriceRecordEntity.setSuggestedPrice(suggestPrice);
  1838. shopErrorPriceRecordEntity.setLowestPrice(lowestPrice);
  1839. shopErrorPriceRecordEntity.setHighestPrice(highestPrice);
  1840. }
  1841. shopErrorPriceRecordEntity.setMkaId(promotionEntity.getMkaId());
  1842. shopErrorPriceRecordEntities.add(shopErrorPriceRecordEntity);
  1843. }
  1844. }
  1845. private void calculateDailyPrice(SysUserEntity user, GoodsEntity goods, List<Map<String, String>> errorPriceSkuList, List<ShopErrorPriceRecordEntity> shopErrorPriceRecordEntities) {
  1846. String prodBarcode = goods.getProdBarcode();
  1847. String storeId = goods.getStoreId()+"";
  1848. /**
  1849. * 2.查询当前时间,该门店是否有活动,如果有活动,查询开启了哪些营销方式
  1850. * 参数: 当前时间 门店id
  1851. */
  1852. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  1853. /**
  1854. * 日常活动跟着条形码走,优先级比临时促销低,但是高于正常价格
  1855. */
  1856. MkDailyActivitiesEntity dailyActivitiesEntity = dailyActivitiesService.queryByDateAndBarcode(storeId,format.format(new Date()),"rchd",prodBarcode);
  1857. if(dailyActivitiesEntity == null){
  1858. return ;
  1859. }
  1860. goods.setActualPaymentAmount(dailyActivitiesEntity.getActivityPrice());
  1861. goods.setRetailPrice(dailyActivitiesEntity.getActivityPrice());
  1862. goods.setActivity("日常活动");
  1863. // 计算税费
  1864. try {
  1865. CalculateTax.calculateFinalTax(goods,goods.getActualPaymentAmount(),this).setScale(3,RoundingMode.HALF_UP);
  1866. } catch (Exception e) {
  1867. // 记录有异常的sku
  1868. ShopErrorPriceRecordEntity shopErrorPriceRecordEntity = new ShopErrorPriceRecordEntity();
  1869. shopErrorPriceRecordEntity.setMerchSn(goods.getMerchSn());
  1870. shopErrorPriceRecordEntity.setThirdMerchSn(goods.getThirdPartyMerchCode());
  1871. shopErrorPriceRecordEntity.setShopSn(goods.getStoreId()+"");
  1872. shopErrorPriceRecordEntity.setPriceType(2); // 2 是日常活动
  1873. shopErrorPriceRecordEntity.setSku(goods.getSku());
  1874. shopErrorPriceRecordEntity.setBarcode(goods.getProdBarcode());
  1875. shopErrorPriceRecordEntity.setVerifier(user.getUserId()+"");
  1876. shopErrorPriceRecordEntity.setCheckTime(new Date());
  1877. shopErrorPriceRecordEntity.setCreaterSn(user.getUserId()+"");
  1878. shopErrorPriceRecordEntity.setCreateTime(new Date());
  1879. shopErrorPriceRecordEntity.setCurrentPrice(goods.getActualPaymentAmount().toString());
  1880. if(e.getMessage().contains("-")){
  1881. String suggestPrice = e.getMessage().split("-")[0];
  1882. String highestPrice = e.getMessage().split("-")[1];
  1883. String lowestPrice = e.getMessage().split("-")[2];
  1884. shopErrorPriceRecordEntity.setSuggestedPrice(suggestPrice);
  1885. shopErrorPriceRecordEntity.setLowestPrice(lowestPrice);
  1886. shopErrorPriceRecordEntity.setHighestPrice(highestPrice);
  1887. }
  1888. shopErrorPriceRecordEntity.setMkaId(dailyActivitiesEntity.getMkaId());
  1889. shopErrorPriceRecordEntities.add(shopErrorPriceRecordEntity);
  1890. }
  1891. }
  1892. private void calculateShopPrice(SysUserEntity user, GoodsEntity goods, List<Map<String, String>> errorPriceSkuList, List<ShopErrorPriceRecordEntity> shopErrorPriceRecordEntities) {
  1893. goods.setDiscountedPrice(new BigDecimal(0));
  1894. BigDecimal retailPrice = goods.getRetailPrice();
  1895. goods.setActualPaymentAmount(retailPrice.setScale(2,RoundingMode.HALF_UP));
  1896. // 计算税费
  1897. try {
  1898. CalculateTax.calculateFinalTax(goods,goods.getActualPaymentAmount(),this).setScale(3,RoundingMode.HALF_UP);
  1899. } catch (Exception e) {
  1900. // 记录有异常的sku
  1901. ShopErrorPriceRecordEntity shopErrorPriceRecordEntity = new ShopErrorPriceRecordEntity();
  1902. shopErrorPriceRecordEntity.setMerchSn(goods.getMerchSn());
  1903. shopErrorPriceRecordEntity.setThirdMerchSn(goods.getThirdPartyMerchCode());
  1904. shopErrorPriceRecordEntity.setShopSn(goods.getStoreId()+"");
  1905. shopErrorPriceRecordEntity.setPriceType(1); // 1 门店商品价格
  1906. shopErrorPriceRecordEntity.setSku(goods.getSku());
  1907. shopErrorPriceRecordEntity.setBarcode(goods.getProdBarcode());
  1908. shopErrorPriceRecordEntity.setVerifier(user.getUserId()+"");
  1909. shopErrorPriceRecordEntity.setCheckTime(new Date());
  1910. shopErrorPriceRecordEntity.setCreaterSn(user.getUserId()+"");
  1911. shopErrorPriceRecordEntity.setCreateTime(new Date());
  1912. shopErrorPriceRecordEntity.setCurrentPrice(goods.getActualPaymentAmount().toString());
  1913. if(e.getMessage().contains("-")){
  1914. String suggestPrice = e.getMessage().split("-")[0];
  1915. String highestPrice = e.getMessage().split("-")[1];
  1916. String lowestPrice = e.getMessage().split("-")[2];
  1917. shopErrorPriceRecordEntity.setSuggestedPrice(suggestPrice);
  1918. shopErrorPriceRecordEntity.setLowestPrice(lowestPrice);
  1919. shopErrorPriceRecordEntity.setHighestPrice(highestPrice);
  1920. }
  1921. shopErrorPriceRecordEntities.add(shopErrorPriceRecordEntity);
  1922. }
  1923. }
  1924. /**
  1925. * 根据产品条码和sku查询保税展示补货的商品
  1926. *
  1927. * @param prodBarcode 商品条码
  1928. * @param sku sku
  1929. * @return 商品信息
  1930. */
  1931. @Override
  1932. public GoodsEntity queryGoodsInfoByProductBarcodeAndSku(String prodBarcode, String sku) {
  1933. return goodsDao.queryGoodsInfoByProductBarcodeAndSku(prodBarcode, sku);
  1934. }
  1935. @Override
  1936. public int updateStockNumberByProductCodeAndSku(GoodsEntity goodsEntity) {
  1937. return goodsDao.updateStockNumberByProductCodeAndSku(goodsEntity);
  1938. }
  1939. }