GoodsServiceImpl.java 125 KB

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