GoodsServiceImpl.java 126 KB

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