GoodsServiceImpl.java 125 KB

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