GoodsServiceImpl.java 129 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570
  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<>(), failHotGoodsSnList = new ArrayList<>(),
  726. // failSuppGoodsSnList = new ArrayList<>(),
  727. failUnitGoodsSnList = new ArrayList<>(), failNationGoodsSnList = new ArrayList<>(),
  728. // failProdbarGoodsSnList = new ArrayList<>(),
  729. failTypeGoodsSnList = new ArrayList<>(), failMerchGoodsSnList = new ArrayList<>(),
  730. // failCateL2GoodsSnList = new ArrayList<>(),
  731. // failCateGoodsSnList = new ArrayList<>(),
  732. // failBrandGoodsSnList = new ArrayList<>(),
  733. // failFreightGoodsSnList = new ArrayList<>(),
  734. failMerchUserGoodsSnList = new ArrayList<>();
  735. List<String> failGoodsSnList = new ArrayList<>();
  736. List<String> failGoodsTypeList = new ArrayList<>();
  737. // List<String> failFreightList = new ArrayList<>();
  738. // 定义需要批量插入的list
  739. List<ProductEntity> insertProductList = new LinkedList<>();
  740. List<GoodsSpecificationEntity> insertSpecificationList = new LinkedList<>();
  741. List<MngChangeEntity> insertMngChangeList = new LinkedList<>();
  742. List<GoodsEntity> insertGoodsList = new LinkedList<>();
  743. List<SupplierEntity> insertSupplierList = new LinkedList<>();
  744. // 定义需要批量修改的list
  745. List<ProductEntity> updateProductList = new LinkedList<>();
  746. List<GoodsSpecificationEntity> updateSpecificationList = new LinkedList<>();
  747. List<GoodsEntity> updateGoodsList = new LinkedList<>();
  748. if (goodsEntityList != null && goodsEntityList.size() > 0) {
  749. for (int i = 0; i < goodsEntityList.size(); i++) {
  750. GoodsDto goodsDto = goodsEntityList.get(i);
  751. GoodsEntity goodsEntity = new GoodsEntity();
  752. Map<String, Object> valideDate = MapBeanUtil.fromObject(goodsDto);
  753. ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
  754. builder.put("goodsSn", "商品编码");
  755. builder.put("thirdPartyMerchCode", "第三方商户代码");
  756. // builder.put("categoryName", "商品分类");
  757. builder.put("goodsBizType", "货品业务类型");
  758. builder.put("name", "商品名称");
  759. // builder.put("brandName", "商品品牌名称");
  760. // builder.put("defaultFreight", "运费");
  761. builder.put("isOnSaleStr", "上架");
  762. builder.put("goodsUnit", "商品单位");
  763. builder.put("isHotStr", "热销");
  764. builder.put("prodBarcode", "产品条码");
  765. // builder.put("marketPrice", "市场价");
  766. // builder.put("retailPrice", "零售价");
  767. builder.put("supplierName", "供应商");
  768. builder.put("goodsNumber", "商品总库存");
  769. R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
  770. if (Integer.valueOf(r.get("code").toString()) != 0) {
  771. throw new RRException(r.get("msg").toString());
  772. } else {
  773. if (!Dict.orderBizType.item_11.getItem().equals(goodsDto.getGoodsBizType())) {
  774. // 海关信息,普通货物可不添加
  775. builder.put("goodsRate", "商品税率");
  776. builder.put("sku", "SKU");
  777. builder.put("brand", "产品品牌");
  778. builder.put("unitName", "计量单位");
  779. builder.put("oriCntName", "原产国");
  780. builder.put("cusGoodsCode", "海关商品编码");
  781. builder.put("ciqProdModel", "国检规格型号");
  782. builder.put("cusDeclEle", "海关申报要素");
  783. builder.put("cusRecCode", "海关备案编号");
  784. }
  785. r = ValidatorUtil.isEmpty(builder.build(), valideDate);
  786. if (Integer.valueOf(r.get("code").toString()) != 0) {
  787. throw new RRException(r.get("msg").toString());
  788. }
  789. }
  790. //业务类型校验
  791. if(!Dict.orderBizType.item_11.getItem().equalsIgnoreCase(goodsDto.getGoodsBizType())){
  792. if(!(Dict.orderBizType.item_02.getItem().equalsIgnoreCase(goodsDto.getGoodsBizType())
  793. || Dict.orderBizType.item_10.getItem().equalsIgnoreCase(goodsDto.getGoodsBizType())
  794. || Dict.orderBizType.item_00.getItem().equalsIgnoreCase(goodsDto.getGoodsBizType()))){
  795. isFail = true;
  796. failTypeGoodsSnList.add(goodsDto.getSku());
  797. }
  798. }
  799. ThirdMerchantBizEntity thirdMerchantBizEntity = thirdMerchantBizDao.getThirdMerchangByCode(goodsDto.getThirdPartyMerchCode());
  800. if(thirdMerchantBizEntity != null){
  801. goodsEntity.setMerchSn(thirdMerchantBizEntity.getMerchSn());
  802. goodsEntity.setThirdPartyMerchCode(thirdMerchantBizEntity.getThirdPartyMerchCode());
  803. if(!user.getRoleType().equalsIgnoreCase(Dict.roleType.item_1.getItem())) {
  804. if (!merchSn.equalsIgnoreCase(thirdMerchantBizEntity.getMerchSn())) {
  805. isFail = true;
  806. failMerchUserGoodsSnList.add(goodsDto.getGoodsSn());
  807. }
  808. }
  809. }else{//商户不存在
  810. isFail = true;
  811. failMerchGoodsSnList.add(goodsDto.getGoodsSn());
  812. }
  813. //校验商品信息是否已存在
  814. Map<String, Object> map = new HashMap<>();
  815. map.put("isSame", "true");
  816. map.put("sku", goodsDto.getSku());
  817. map.put("goodsSn", goodsDto.getGoodsSn());
  818. map.put("goodsBizType", goodsDto.getGoodsBizType());
  819. List<GoodsEntity> list = querySame(map);
  820. if (list != null && list.size() != 0) {
  821. // isFail = true;
  822. if(goodsDto.getSku()!=null) {
  823. failSameSkuList.add(goodsDto.getSku());
  824. }
  825. failGoodsSnList.add(goodsDto.getGoodsSn());
  826. failGoodsTypeList.add(goodsDto.getGoodsBizType());
  827. }
  828. goodsEntity.setProdBarcode(goodsDto.getProdBarcode());
  829. //热销商品校验
  830. if (Dict.orderBizType.item_02.getItem().equals(goodsDto.getGoodsBizType())
  831. || Dict.orderBizType.item_10.getItem().equals(goodsDto.getGoodsBizType())) {
  832. if(goodsDto.getIsHotStr().equalsIgnoreCase("1")){
  833. isFail = true;
  834. failHotGoodsSnList.add(goodsDto.getGoodsSn());
  835. }
  836. }
  837. if(thirdMerchantBizEntity != null) {
  838. SupplierEntity supplierEntity = supplierDao.queryObjectByName(goodsDto.getSupplierName(), thirdMerchantBizEntity.getMerchSn(),thirdMerchantBizEntity.getThirdPartyMerchCode());
  839. if (supplierEntity == null) {
  840. //导入没有查到供货商,执行新增
  841. MerchEntity merchEntity = merchDao.findByMerchSn(thirdMerchantBizEntity.getMerchSn());
  842. // if(null == merchEntity || !StringUtils.isNotEmpty(goodsDto.getSupplierFlag())){
  843. // isFail = true;
  844. // failSuppGoodsSnList.add(goodsDto.getGoodsSn());
  845. // }
  846. supplierEntity = new SupplierEntity();
  847. supplierEntity.setLevelMerchSn(merchEntity.getMerchSn());
  848. supplierEntity.setLevelMerchFlag(merchEntity.getMerchShortName());
  849. supplierEntity.setThirdPartyMerchCode(thirdMerchantBizEntity.getThirdPartyMerchCode());
  850. supplierEntity.setChildSupplierName(goodsDto.getSupplierName());
  851. supplierEntity.setChildSupplierFlag(goodsDto.getSupplierFlag());
  852. supplierEntity.setIsShow("0");
  853. // TODO 这里改成批量插入 SupplierEntity
  854. // insertSupplierList.add(supplierEntity);
  855. //新增商品供货信息
  856. supplierDao.save(supplierEntity);
  857. // supplierEntity = supplierDao.queryObjectByName(goodsDto.getSupplierName(), thirdMerchantBizEntity.getMerchSn(),thirdMerchantBizEntity.getThirdPartyMerchCode());
  858. goodsEntity.setSupplierId(supplierEntity.getId());
  859. } else {
  860. goodsEntity.setSupplierId(supplierEntity.getId());
  861. }
  862. }
  863. //商品配置校验
  864. /*
  865. 先在redis在查询该品牌是否存在,如果存在获取品牌id,如果不存在,先新增该品牌,在将品牌存入到redis中,在获取品牌id
  866. */
  867. // 获取品牌唯一简码
  868. checkBrand(goodsDto, goodsEntity);
  869. // 获取二级分类id
  870. checkCategory(goodsDto, goodsEntity);
  871. if (!Dict.orderBizType.item_11.getItem().equals(goodsDto.getGoodsBizType())) {
  872. SysCusUnitCodeEntity sysCusUnitCodeEntity = sysCusUnitCodeDao.queryObjectByName(goodsDto.getUnitName());
  873. if (sysCusUnitCodeEntity == null) {
  874. isFail = true;
  875. failUnitGoodsSnList.add(goodsDto.getGoodsSn());
  876. } else {
  877. goodsEntity.setUnitCode(sysCusUnitCodeEntity.getCode());
  878. }
  879. //原产国
  880. SysCusNationCodeEntity sysCusNationCodeEntity = sysCusNationCodeDao.queryObjectByName(goodsDto.getOriCntName());
  881. if (sysCusNationCodeEntity == null) {
  882. isFail = true;
  883. failNationGoodsSnList.add(goodsDto.getGoodsSn());
  884. } else {
  885. goodsEntity.setOriCntCode(sysCusNationCodeEntity.getCode());
  886. }
  887. if(goodsDto.getGoodsRate().indexOf("%") != -1){
  888. goodsDto.setGoodsRate(goodsDto.getGoodsRate().substring(0, goodsDto.getGoodsRate().indexOf("%")));
  889. goodsEntity.setGoodsRate(BigDecimal.valueOf(Double.valueOf(goodsDto.getGoodsRate())).divide(new BigDecimal(100), 4, BigDecimal.ROUND_HALF_UP));
  890. }else{
  891. goodsEntity.setGoodsRate(BigDecimal.valueOf(Double.valueOf(goodsDto.getGoodsRate())));
  892. }
  893. }
  894. goodsEntity.setIsOnSale(Integer.parseInt(goodsDto.getIsOnSaleStr()));
  895. goodsEntity.setIsHot(Integer.parseInt(goodsDto.getIsHotStr()));
  896. // goodsEntity.setRetailPrice(BigDecimal.valueOf(Integer.valueOf(goodsDto.getRetailPrice())));
  897. // goodsEntity.setMarketPrice(BigDecimal.valueOf(Integer.valueOf(goodsDto.getMarketPrice())));
  898. goodsEntity.setGoodsSn(goodsDto.getGoodsSn());
  899. goodsEntity.setSku(goodsDto.getSku());
  900. goodsEntity.setName(goodsDto.getName());
  901. goodsEntity.setGoodsUnit(goodsDto.getGoodsUnit());
  902. goodsEntity.setGoodsBizType(goodsDto.getGoodsBizType());
  903. goodsEntity.setBrand(goodsDto.getBrand());
  904. goodsEntity.setCusDeclEle(goodsDto.getCusDeclEle());
  905. goodsEntity.setCusGoodsCode(goodsDto.getCusGoodsCode());
  906. goodsEntity.setCusRecCode(goodsDto.getCusRecCode());
  907. goodsEntity.setCiqProdModel(goodsDto.getCiqProdModel());
  908. goodsEntity.setIsDelete(Integer.parseInt(Dict.isDelete.item_0.getItem()));
  909. goodsEntity.setIsNew(0);
  910. goodsEntity.setUpdateUserId(user.getUserId());
  911. goodsEntity.setAddTime(new Date());
  912. goodsEntity.setCreateTime(new Date());
  913. goodsEntity.setUpdateTime(new Date());
  914. goodsEntity.setModTime(new Date());
  915. goodsEntity.setGoodsNumber(Integer.parseInt(goodsDto.getGoodsNumber()));
  916. goodsEntity.setPlu(goodsDto.getPlu());
  917. goodsEntity.setMychemId(goodsDto.getMychemId());
  918. if(!isFail){
  919. GoodsEntity goods = goodsDao.queryObjectBySn(goodsDto.getGoodsSn());
  920. MngChangeEntity mngChangeEntity = new MngChangeEntity();
  921. mngChangeEntity.setThirdPartyMerchCode(goodsEntity.getThirdPartyMerchCode());
  922. mngChangeEntity.setChangeReason("更新商户商品总库存");
  923. mngChangeEntity.setCreateTime(new Date());
  924. mngChangeEntity.setModTime(new Date());
  925. mngChangeEntity.setCreaterSn(user.getUsername());
  926. mngChangeEntity.setModerSn(user.getUsername());
  927. mngChangeEntity.setIsValid(0);
  928. mngChangeEntity.setMerchSn(goodsEntity.getMerchSn());
  929. if(goods!=null) {// 修改商品
  930. mngChangeEntity.setOriginalNum(goods.getGoodsNumber());//原库存数
  931. mngChangeEntity.setValidNum(goods.getGoodsNumber() + Integer.parseInt(goodsDto.getGoodsNumber()));//可用数
  932. mngChangeEntity.setChangeNum(Integer.parseInt(goodsDto.getGoodsNumber()));//变化数
  933. mngChangeEntity.setChangeType(Dict.changeType.item_3.getItem());
  934. mngChangeEntity.setGoodsId(goods.getId().intValue());
  935. goodsEntity.setId(goods.getId());
  936. goodsEntity.setGoodsNumber(goods.getGoodsNumber()+Integer.parseInt(goodsDto.getGoodsNumber()));
  937. // TODO 这里改成批量修改 GoodsEntity
  938. // updateGoodsList.add(goodsEntity);
  939. goodsDao.update(goodsEntity);
  940. }else{
  941. mngChangeEntity.setOriginalNum(0);//原库存数
  942. mngChangeEntity.setValidNum(Integer.parseInt(goodsDto.getGoodsNumber()));//可用数
  943. mngChangeEntity.setChangeNum(Integer.parseInt(goodsDto.getGoodsNumber()));//变化数
  944. mngChangeEntity.setChangeType(Dict.changeType.item_2.getItem());
  945. goodsDao.save(goodsEntity);
  946. insertGoodsList.add(goodsEntity);
  947. mngChangeEntity.setGoodsId(goodsEntity.getId().intValue());
  948. }
  949. // TODO 这里改成批量插入 MngChangeEntity
  950. // mngChangeDao.save(mngChangeEntity);
  951. insertMngChangeList.add(mngChangeEntity);
  952. // // 保税商品修改各个门店商品价格
  953. // if (!Dict.orderBizType.item_11.getItem().equals(goodsDto.getGoodsBizType())) {
  954. // List<ProductStoreRelaEntity> productStoreRelaEntityList = productStoreRelaDao.queryByGoodsId(goodsDto.getId());
  955. // if (productStoreRelaEntityList != null && productStoreRelaEntityList.size() > 0) {
  956. // for (ProductStoreRelaEntity productStoreRela : productStoreRelaEntityList) {
  957. // productStoreRela.setMarketPrice(goodsEntity.getMarketPrice());
  958. // productStoreRela.setRetailPrice(goodsEntity.getRetailPrice());
  959. // productStoreRelaDao.update(productStoreRela);
  960. // }
  961. // }
  962. // }
  963. // 修改产品
  964. ProductEntity product = productDao.queryObjectByGoodsIdAndStoreId(String.valueOf(goodsEntity.getId()), "");
  965. GoodsSpecificationEntity goodsSpecification = new GoodsSpecificationEntity();
  966. // 普通货物暂不添加商品规格
  967. if (!Dict.orderBizType.item_11.getItem().equals(goodsDto.getGoodsBizType())) {
  968. // 添加商品规格
  969. GoodsSpecificationEntity specificationEntity = goodsSpecificationDao.queryByGoodsId(goodsEntity.getId());
  970. if(specificationEntity == null) {
  971. goodsSpecification.setGoodsId(goodsEntity.getId());
  972. goodsSpecification.setValue(goodsEntity.getCiqProdModel());
  973. goodsSpecification.setSpecificationId(1);
  974. // TODO 这里改成批量插入 GoodsSpecificationEntity
  975. goodsSpecificationDao.save(goodsSpecification);
  976. // insertSpecificationList.add(goodsSpecification);
  977. }else {
  978. goodsSpecification.setValue(goodsDto.getCiqProdModel());
  979. goodsSpecification.setId(specificationEntity.getId());
  980. // TODO 这里改成批量更新 GoodsSpecificationEntity
  981. // goodsSpecificationDao.update(goodsSpecification);
  982. updateSpecificationList.add(goodsSpecification);
  983. }
  984. if(product == null){
  985. product = new ProductEntity();
  986. product.setGoodsSn(goodsDto.getGoodsSn());
  987. product.setGoodsId(goodsEntity.getId());
  988. product.setGoodsDefault(0);
  989. product.setGoodsNumber(goodsEntity.getGoodsNumber());
  990. product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
  991. product.setGoodsSpecificationIds(goodsSpecification.getId().toString());
  992. // TODO 这里改成批量插入 ProductEntity
  993. // insertProductList.add(product);
  994. productDao.save(product);
  995. }else{
  996. product.setGoodsSpecificationIds(goodsSpecification.getId().toString());
  997. product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
  998. // TODO 这里改成批量更新 ProductEntity
  999. updateProductList.add(product);
  1000. // productDao.update(product);
  1001. }
  1002. }
  1003. }
  1004. }
  1005. // 批量插入数据
  1006. // if(insertProductList != null && insertProductList.size() > 0) {
  1007. // // 长度限制,如果大于50,就分多次插入
  1008. // if(insertProductList.size() > 50){
  1009. // List<List<ProductEntity>> partition = Lists.partition(insertProductList, 50);
  1010. // for (List<ProductEntity> productEntities : partition) {
  1011. // productDao.saveBatch(productEntities);
  1012. // }
  1013. // }else{
  1014. // productDao.saveBatch(insertProductList);;
  1015. //
  1016. // }
  1017. // }
  1018. // if(insertSpecificationList != null && insertSpecificationList.size() > 0) {
  1019. // goodsSpecificationDao.saveBatch(insertSpecificationList);
  1020. // }
  1021. if(insertMngChangeList != null && insertMngChangeList.size() > 0 ){
  1022. if(insertMngChangeList.size() > 50){
  1023. List<List<MngChangeEntity>> partition = Lists.partition(insertMngChangeList, 50);
  1024. for (List<MngChangeEntity> mngChangeEntities : partition) {
  1025. mngChangeDao.saveBatch(mngChangeEntities);
  1026. }
  1027. }else{
  1028. mngChangeDao.saveBatch(insertMngChangeList);
  1029. }
  1030. }
  1031. // if(insertGoodsList != null && insertGoodsList.size() > 0 ){
  1032. // goodsDao.saveBatch(insertGoodsList);
  1033. // }
  1034. // if(insertSupplierList != null && insertSupplierList.size() > 0 ){
  1035. // if(insertSupplierList.size() > 50){
  1036. // List<List<SupplierEntity>> partition = Lists.partition(insertSupplierList, 50);
  1037. // for (List<SupplierEntity> supplierEntities : partition) {
  1038. // supplierDao.saveBatch(supplierEntities);
  1039. // }
  1040. // }else{
  1041. // supplierDao.saveBatch(insertSupplierList);
  1042. //
  1043. // }
  1044. // }
  1045. // 批量更新数据
  1046. if(updateProductList != null && updateProductList.size() > 0 ){
  1047. if(updateProductList.size() > 50){
  1048. List<List<ProductEntity>> partition = Lists.partition(updateProductList, 50);
  1049. for (List<ProductEntity> productEntities : partition) {
  1050. productDao.updateBatch(productEntities);
  1051. }
  1052. }else{
  1053. productDao.updateBatch(updateProductList);
  1054. }
  1055. }
  1056. if(updateSpecificationList != null && updateSpecificationList.size() > 0 ){
  1057. if(updateSpecificationList.size() > 50){
  1058. List<List<GoodsSpecificationEntity>> partition = Lists.partition(updateSpecificationList, 50);
  1059. for (List<GoodsSpecificationEntity> goodsSpecificationEntities : partition) {
  1060. goodsSpecificationDao.updateBatch(goodsSpecificationEntities);
  1061. }
  1062. }else{
  1063. goodsSpecificationDao.updateBatch(updateSpecificationList);
  1064. }
  1065. }
  1066. // if(updateGoodsList != null && updateGoodsList.size() > 0 ){
  1067. // if(updateGoodsList.size() > 50){
  1068. // List<List<GoodsEntity>> partition = Lists.partition(updateGoodsList, 50);
  1069. // for (List<GoodsEntity> goodsEntities : partition) {
  1070. // goodsDao.updateBatch(goodsEntities);
  1071. // }
  1072. // }else{
  1073. // goodsDao.updateBatch(updateGoodsList);
  1074. // }
  1075. // }
  1076. ExportExceptionDataEntity exportExceptionDataEntity = new ExportExceptionDataEntity();
  1077. exportExceptionDataEntity.setCreaterSn(user.getUserId().toString());
  1078. exportExceptionDataEntity.setUserId(user.getUserId().intValue());
  1079. exportExceptionDataEntity.setCreateTime(new Date());
  1080. exportExceptionDataEntity.setModTime(new Date());
  1081. exportExceptionDataEntity.setMerchSn(merchSn);
  1082. exportExceptionDataEntity.setStoreId(user.getStoreId());
  1083. if(exportDataType == 1) {
  1084. exportExceptionDataEntity.setExportDataType(Dict.exportDataType.item_1.getItem());
  1085. }else{
  1086. exportExceptionDataEntity.setExportDataType(Dict.exportDataType.item_2.getItem());
  1087. }
  1088. if(failMerchUserGoodsSnList != null && failMerchUserGoodsSnList.size() > 0){
  1089. exportExceptionDataEntity.setExportExceptionData("不能操作除了登录用户以外商户的商品,当前商户编号为【"+merchSn+"】,请检查商品编码【"+failMerchUserGoodsSnList+"】的商品信息");
  1090. goodsUtils.save(exportExceptionDataEntity);
  1091. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1092. }
  1093. if(failMerchGoodsSnList != null && failMerchGoodsSnList.size() > 0){
  1094. exportExceptionDataEntity.setExportExceptionData("第三方商户代码不存在,请在商城配置》第三方商户管理中维护,请检查商品编码【"+failMerchGoodsSnList+"】的商品信息,请先维护再继续操作!");
  1095. goodsUtils.save(exportExceptionDataEntity);
  1096. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1097. }
  1098. if(failTypeGoodsSnList != null && failTypeGoodsSnList.size() > 0){
  1099. exportExceptionDataEntity.setExportExceptionData("货品业务类型只能是【00保税备货、02保税补货、10保税展示】!请检查商品编码【"+failTypeGoodsSnList+"】的商品信息");
  1100. goodsUtils.save(exportExceptionDataEntity);
  1101. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1102. }
  1103. if(failHotGoodsSnList != null && failHotGoodsSnList.size() > 0){
  1104. exportExceptionDataEntity.setExportExceptionData("请检查业务类型为【保税补货或保税展示】的商品,商品编码【"+failHotGoodsSnList+"】的商品不能设置为热销!");
  1105. goodsUtils.save(exportExceptionDataEntity);
  1106. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1107. }
  1108. if(failUnitGoodsSnList != null && failUnitGoodsSnList.size() > 0){
  1109. exportExceptionDataEntity.setExportExceptionData("计算单位信息请在商城配置》计算单位中维护,不存在的商品编码【" + failUnitGoodsSnList + "】");
  1110. goodsUtils.save(exportExceptionDataEntity);
  1111. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1112. }
  1113. if(failNationGoodsSnList != null && failNationGoodsSnList.size() > 0){
  1114. exportExceptionDataEntity.setExportExceptionData("原产国信息请在商城配置》原产国中维护,不存在的商品编码【" + failNationGoodsSnList + "】");
  1115. goodsUtils.save(exportExceptionDataEntity);
  1116. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1117. }
  1118. // 将插入的商品和更新的商品放到一个list中
  1119. List<GoodsEntity> goodsEntities = new LinkedList<>();
  1120. goodsEntities.addAll(insertGoodsList);
  1121. goodsEntities.addAll(updateGoodsList);
  1122. List<KtoEccsEntity> ktoEccsEntities = new LinkedList<>();
  1123. if(goodsEntities.size() > 0){
  1124. for (GoodsEntity goodsEntity : goodsEntities) {
  1125. KtoEccsEntity ktoEccsEntity = new KtoEccsEntity();
  1126. ktoEccsEntity.setRequestData(JSON.toJSONString(goodsEntity));
  1127. ktoEccsEntity.setRequestType("01");
  1128. ktoEccsEntity.setSendStatus("00");
  1129. ktoEccsEntity.setCreaterSn(user.getUserId()+"");
  1130. ktoEccsEntity.setCreateTime(new Date());
  1131. ktoEccsEntities.add(ktoEccsEntity);
  1132. }
  1133. }
  1134. if(ktoEccsEntities.size() > 0){
  1135. if(ktoEccsEntities.size() > 50){
  1136. List<List<KtoEccsEntity>> partition = Lists.partition(ktoEccsEntities, 50);
  1137. for (List<KtoEccsEntity> ktoEccs : partition) {
  1138. ktoEccsService.saveBatch(ktoEccs);
  1139. }
  1140. }else{
  1141. ktoEccsService.saveBatch(ktoEccsEntities);
  1142. }
  1143. }
  1144. }else{
  1145. throw new RRException("导入数据为空,或者检查商品编码数据是否为空");
  1146. }
  1147. return 1;
  1148. }
  1149. private void checkCategory(GoodsDto goodsDto, GoodsEntity goodsEntity) {
  1150. String secondLevel = goodsDto.getSecondLevel(); // 二级分类名称
  1151. Map<String, String> categoryCache = JedisUtil.getMap(secondLevel);
  1152. if(categoryCache != null){
  1153. String categoryId = categoryCache.get("categoryId");
  1154. String level = categoryCache.get("level");
  1155. if(categoryId == null){
  1156. CategoryEntity category = categoryDao.queryByName(secondLevel);
  1157. if (category == null) {
  1158. // 将该id存入带缓存中
  1159. Map<String,String> categoryCacheData = new HashMap<>();
  1160. categoryCacheData.put("level",Dict.Level.item_L2.getItem());
  1161. // 查询数据库中是否有一级分类
  1162. String firstLevel = goodsDto.getFirstLevel();
  1163. CategoryEntity firstCategory = categoryDao.queryByName(firstLevel);
  1164. if(firstCategory == null){
  1165. // 一级分类不存在
  1166. // 新增一级分类
  1167. CategoryEntity firstCategoryEntity = initCategory(firstLevel, 0, Dict.Level.item_L1);
  1168. categoryDao.save(firstCategoryEntity);
  1169. // 新增二级分类
  1170. CategoryEntity secondCategoryEntity = initCategory(secondLevel, firstCategoryEntity.getId(), Dict.Level.item_L2);
  1171. categoryDao.save(secondCategoryEntity);
  1172. categoryId = secondCategoryEntity.getId()+"";
  1173. }else{
  1174. // 新增二级分类
  1175. CategoryEntity saveCategoryEntity = initCategory(secondLevel, firstCategory.getId(), Dict.Level.item_L2);
  1176. categoryDao.save(saveCategoryEntity);
  1177. categoryId = saveCategoryEntity.getId()+"";
  1178. }
  1179. categoryCacheData.put("categoryId",categoryId);
  1180. JedisUtil.setMap(secondLevel,categoryCacheData,0);
  1181. goodsEntity.setCategoryId(Integer.parseInt(categoryId));
  1182. }else if(category != null && Dict.Level.item_L2.getItem().equals(category.getLevel())){
  1183. // 缓存中没有,数据库中有,直接存入数据库中查询到的数据
  1184. goodsEntity.setCategoryId(category.getId());
  1185. // 将该id存入带缓存中
  1186. Map<String,String> categoryCacheData = new HashMap<>();
  1187. categoryCacheData.put("level",Dict.Level.item_L2.getItem());
  1188. categoryCacheData.put("categoryId",category.getId()+"");
  1189. JedisUtil.setMap(secondLevel,categoryCacheData,0);
  1190. }else{
  1191. throw new RuntimeException("该分类不是二级分类,请检查,对应sku是:"+ goodsDto.getSku()+",分类名称为:"+ goodsDto.getSecondLevel());
  1192. }
  1193. }else if(Dict.Level.item_L2.getItem().equals(level)){
  1194. // 二级分类id在缓存中有,直接存入商品
  1195. goodsEntity.setCategoryId(Integer.parseInt(categoryId));
  1196. }else {
  1197. throw new RuntimeException("该分类不是二级分类,请检查,对应sku是:"+ goodsDto.getSku()+",分类名称为:"+ goodsDto.getSecondLevel());
  1198. }
  1199. }else {
  1200. String categoryId = "";
  1201. CategoryEntity category = categoryDao.queryByName(secondLevel);
  1202. if (category == null) {
  1203. // 将该id存入带缓存中
  1204. Map<String,String> categoryCacheData = new HashMap<>();
  1205. categoryCacheData.put("level",Dict.Level.item_L2.getItem());
  1206. // 查询数据库中是否有一级分类
  1207. String firstLevel = goodsDto.getFirstLevel();
  1208. CategoryEntity firstCategory = categoryDao.queryByName(firstLevel);
  1209. if(firstCategory == null){
  1210. // 一级分类不存在
  1211. // 新增一级分类
  1212. CategoryEntity firstCategoryEntity = initCategory(firstLevel, 0, Dict.Level.item_L1);
  1213. categoryDao.save(firstCategoryEntity);
  1214. // 新增二级分类
  1215. CategoryEntity secondCategoryEntity = initCategory(secondLevel, firstCategoryEntity.getId(), Dict.Level.item_L2);
  1216. categoryDao.save(secondCategoryEntity);
  1217. categoryId = secondCategoryEntity.getId()+"";
  1218. }else{
  1219. // 新增二级分类
  1220. CategoryEntity saveCategoryEntity = initCategory(secondLevel, firstCategory.getId(), Dict.Level.item_L2);
  1221. categoryDao.save(saveCategoryEntity);
  1222. categoryId = saveCategoryEntity.getId()+"";
  1223. }
  1224. categoryCacheData.put("categoryId",categoryId);
  1225. JedisUtil.setMap(secondLevel,categoryCacheData,0);
  1226. goodsEntity.setCategoryId(Integer.parseInt(categoryId));
  1227. }else if(category != null && Dict.Level.item_L2.getItem().equals(category.getLevel())){
  1228. // 缓存中没有,数据库中有,直接存入数据库中查询到的数据
  1229. goodsEntity.setCategoryId(category.getId());
  1230. // 将该id存入带缓存中
  1231. Map<String,String> categoryCacheData = new HashMap<>();
  1232. categoryCacheData.put("level",Dict.Level.item_L2.getItem());
  1233. categoryCacheData.put("categoryId",category.getId()+"");
  1234. JedisUtil.setMap(secondLevel,categoryCacheData,0);
  1235. }else{
  1236. throw new RuntimeException("该分类不是二级分类,请检查,对应sku是:"+ goodsDto.getSku()+",分类名称为:"+ goodsDto.getSecondLevel());
  1237. }
  1238. }
  1239. }
  1240. private void checkBrand(GoodsDto goodsDto, GoodsEntity goodsEntity) {
  1241. String uniqueIdentifier = goodsDto.getUniqueIdentifier();
  1242. String brandName = goodsDto.getBrand();
  1243. Map<String, String> brandCache = JedisUtil.getMap(uniqueIdentifier);
  1244. if(brandCache != null) {
  1245. String brandId = brandCache.get("brandId");
  1246. if (brandId == null) {
  1247. Map<String, String> brandCacheData = new HashMap<>();
  1248. // 数据库里面查询
  1249. BrandEntity queryBrand = brandService.queryByUniqueIdentifier(uniqueIdentifier);
  1250. if (queryBrand == null) {
  1251. BrandEntity brandEntity = new BrandEntity();
  1252. brandEntity.setName(brandName);
  1253. brandEntity.setUniqueIdentifier(uniqueIdentifier);
  1254. brandEntity.setSimpleDesc(brandName);
  1255. brandEntity.setIsShow(1);
  1256. brandEntity.setIsNew(0);
  1257. brandService.save(brandEntity);
  1258. brandCacheData.put("brandId", brandEntity.getId() + "");
  1259. brandCacheData.put("brandName", brandName);
  1260. goodsEntity.setBrandId(brandEntity.getId());
  1261. } else {
  1262. brandCacheData.put("brandId", queryBrand.getId() + "");
  1263. brandCacheData.put("brandName", queryBrand.getName());
  1264. goodsEntity.setBrandId(queryBrand.getId());
  1265. }
  1266. JedisUtil.setMap(uniqueIdentifier, brandCacheData, 0);
  1267. } else {
  1268. goodsEntity.setBrandId(Integer.parseInt(brandId));
  1269. }
  1270. }else{
  1271. Map<String, String> brandCacheData = new HashMap<>();
  1272. // 数据库里面查询
  1273. BrandEntity queryBrand = brandService.queryByUniqueIdentifier(uniqueIdentifier);
  1274. if (queryBrand == null) {
  1275. BrandEntity brandEntity = new BrandEntity();
  1276. brandEntity.setName(brandName);
  1277. brandEntity.setUniqueIdentifier(uniqueIdentifier);
  1278. brandEntity.setSimpleDesc(brandName);
  1279. brandEntity.setIsShow(1);
  1280. brandEntity.setIsNew(0);
  1281. brandService.save(brandEntity);
  1282. brandCacheData.put("brandId", brandEntity.getId() + "");
  1283. brandCacheData.put("brandName", brandName);
  1284. goodsEntity.setBrandId(brandEntity.getId());
  1285. } else {
  1286. brandCacheData.put("brandId", queryBrand.getId() + "");
  1287. brandCacheData.put("brandName", queryBrand.getName());
  1288. goodsEntity.setBrandId(queryBrand.getId());
  1289. }
  1290. JedisUtil.setMap(uniqueIdentifier, brandCacheData, 0);
  1291. }
  1292. }
  1293. private CategoryEntity initCategory(String firstLevel, int parentId, Dict.Level item) {
  1294. CategoryEntity firstCategoryEntity = new CategoryEntity();
  1295. firstCategoryEntity.setName(firstLevel);
  1296. firstCategoryEntity.setParentId(parentId);
  1297. firstCategoryEntity.setIsShow(1);
  1298. firstCategoryEntity.setFrontDesc(firstLevel);
  1299. firstCategoryEntity.setType(0);
  1300. firstCategoryEntity.setLevel(item.getItem());
  1301. return firstCategoryEntity;
  1302. }
  1303. @Override
  1304. @Transactional
  1305. public int uploadExcelByCover(List<GoodsDto> goodsEntityList, int exportDataType) {
  1306. SysUserEntity user = ShiroUtils.getUserEntity();
  1307. String merchSn = user.getMerchSn();
  1308. boolean isFail = false;
  1309. List<String> failSameSkuList = new ArrayList<>(), failHotGoodsSnList = new ArrayList<>(),
  1310. failSuppGoodsSnList = new ArrayList<>(),
  1311. failUnitGoodsSnList = new ArrayList<>(), failNationGoodsSnList = new ArrayList<>(),
  1312. failTypeGoodsSnList = new ArrayList<>(), failMerchGoodsSnList = new ArrayList<>(),
  1313. failMerchUserGoodsSnList = new ArrayList<>();
  1314. // List<String> failGoodsSnList = new ArrayList<>();
  1315. List<String> failGoodsTypeList = new ArrayList<>();
  1316. if (goodsEntityList != null && goodsEntityList.size() > 0) {
  1317. for (int i = 0; i < goodsEntityList.size(); i++) {
  1318. GoodsDto goodsDto = goodsEntityList.get(i);
  1319. GoodsEntity goodsEntity = new GoodsEntity();
  1320. Map<String, Object> valideDate = MapBeanUtil.fromObject(goodsDto);
  1321. ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
  1322. builder.put("goodsSn", "商品编码");
  1323. builder.put("thirdPartyMerchCode", "第三方商户代码");
  1324. builder.put("goodsBizType", "货品业务类型");
  1325. builder.put("name", "商品名称");
  1326. builder.put("isOnSaleStr", "上架");
  1327. builder.put("goodsUnit", "商品单位");
  1328. builder.put("isHotStr", "热销");
  1329. builder.put("prodBarcode", "产品条码");
  1330. builder.put("supplierName", "供应商");
  1331. builder.put("goodsNumber", "商品总库存");
  1332. R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
  1333. if (Integer.valueOf(r.get("code").toString()) != 0) {
  1334. throw new RRException(r.get("msg").toString());
  1335. } else {
  1336. if (!Dict.orderBizType.item_11.getItem().equals(goodsDto.getGoodsBizType())) {
  1337. // 海关信息,普通货物可不添加
  1338. builder.put("goodsRate", "商品税率");
  1339. builder.put("sku", "SKU");
  1340. builder.put("brand", "产品品牌");
  1341. builder.put("unitName", "计量单位");
  1342. builder.put("oriCntName", "原产国");
  1343. builder.put("cusGoodsCode", "海关商品编码");
  1344. builder.put("ciqProdModel", "国检规格型号");
  1345. builder.put("cusDeclEle", "海关申报要素");
  1346. builder.put("cusRecCode", "海关备案编号");
  1347. }
  1348. r = ValidatorUtil.isEmpty(builder.build(), valideDate);
  1349. if (Integer.valueOf(r.get("code").toString()) != 0) {
  1350. throw new RRException(r.get("msg").toString());
  1351. }
  1352. }
  1353. //业务类型校验
  1354. if(!Dict.orderBizType.item_11.getItem().equalsIgnoreCase(goodsDto.getGoodsBizType())){
  1355. if(!(Dict.orderBizType.item_02.getItem().equalsIgnoreCase(goodsDto.getGoodsBizType())
  1356. || Dict.orderBizType.item_10.getItem().equalsIgnoreCase(goodsDto.getGoodsBizType())
  1357. || Dict.orderBizType.item_00.getItem().equalsIgnoreCase(goodsDto.getGoodsBizType()))){
  1358. isFail = true;
  1359. failTypeGoodsSnList.add(goodsDto.getSku());
  1360. }
  1361. }
  1362. ThirdMerchantBizEntity thirdMerchantBizEntity = thirdMerchantBizDao.getThirdMerchangByCode(goodsDto.getThirdPartyMerchCode());
  1363. if(thirdMerchantBizEntity != null){
  1364. goodsEntity.setMerchSn(thirdMerchantBizEntity.getMerchSn());
  1365. goodsEntity.setThirdPartyMerchCode(thirdMerchantBizEntity.getThirdPartyMerchCode());
  1366. if(!user.getRoleType().equalsIgnoreCase(Dict.roleType.item_1.getItem())) {
  1367. if (!merchSn.equalsIgnoreCase(thirdMerchantBizEntity.getMerchSn())) {
  1368. isFail = true;
  1369. failMerchUserGoodsSnList.add(goodsDto.getGoodsSn());
  1370. }
  1371. }
  1372. }else{//商户不存在
  1373. isFail = true;
  1374. failMerchGoodsSnList.add(goodsDto.getGoodsSn());
  1375. }
  1376. //校验商品信息是否已存在
  1377. Map<String, Object> map = new HashMap<>();
  1378. map.put("isSame", "true");
  1379. map.put("sku", goodsDto.getSku());
  1380. map.put("goodsSn", goodsDto.getGoodsSn());
  1381. map.put("goodsBizType", goodsDto.getGoodsBizType());
  1382. // List<GoodsEntity> list = querySame(map);
  1383. // if (list != null && list.size() != 0) {
  1384. // isFail = true;
  1385. // if(goodsDto.getSku()!=null) {
  1386. // failSameSkuList.add(goodsDto.getSku());
  1387. // }
  1388. // failGoodsSnList.add(goodsDto.getGoodsSn());
  1389. // failGoodsTypeList.add(goodsDto.getGoodsBizType());
  1390. // }
  1391. //校验产品条码是否存在
  1392. goodsEntity.setProdBarcode(goodsDto.getProdBarcode());
  1393. //热销商品校验
  1394. if (Dict.orderBizType.item_02.getItem().equals(goodsDto.getGoodsBizType())
  1395. || Dict.orderBizType.item_10.getItem().equals(goodsDto.getGoodsBizType())) {
  1396. if(goodsDto.getIsHotStr().equalsIgnoreCase("1")){
  1397. isFail = true;
  1398. failHotGoodsSnList.add(goodsDto.getGoodsSn());
  1399. }
  1400. }
  1401. if(thirdMerchantBizEntity != null) {
  1402. SupplierEntity supplierEntity = supplierDao.queryObjectByName(goodsDto.getSupplierName(), thirdMerchantBizEntity.getMerchSn(),thirdMerchantBizEntity.getThirdPartyMerchCode());
  1403. if (supplierEntity == null) {
  1404. //导入没有查到供货商,执行新增
  1405. MerchEntity merchEntity = merchDao.findByMerchSn(thirdMerchantBizEntity.getMerchSn());
  1406. if(null == merchEntity || !StringUtils.isNotEmpty(goodsDto.getSupplierFlag())){
  1407. isFail = true;
  1408. failSuppGoodsSnList.add(goodsDto.getGoodsSn());
  1409. }
  1410. supplierEntity = new SupplierEntity();
  1411. supplierEntity.setLevelMerchSn(merchEntity.getMerchSn());
  1412. supplierEntity.setLevelMerchFlag(merchEntity.getMerchShortName());
  1413. supplierEntity.setThirdPartyMerchCode(thirdMerchantBizEntity.getThirdPartyMerchCode());
  1414. supplierEntity.setChildSupplierName(goodsDto.getSupplierName());
  1415. supplierEntity.setChildSupplierFlag(goodsDto.getSupplierFlag());
  1416. supplierEntity.setIsShow("0");
  1417. supplierDao.save(supplierEntity);
  1418. goodsEntity.setSupplierId(supplierEntity.getId());
  1419. } else {
  1420. goodsEntity.setSupplierId(supplierEntity.getId());
  1421. }
  1422. }
  1423. if (!Dict.orderBizType.item_11.getItem().equals(goodsDto.getGoodsBizType())) {
  1424. SysCusUnitCodeEntity sysCusUnitCodeEntity = sysCusUnitCodeDao.queryObjectByName(goodsDto.getUnitName());
  1425. if (sysCusUnitCodeEntity == null) {
  1426. isFail = true;
  1427. failUnitGoodsSnList.add(goodsDto.getGoodsSn());
  1428. } else {
  1429. goodsEntity.setUnitCode(sysCusUnitCodeEntity.getCode());
  1430. }
  1431. //原产国
  1432. SysCusNationCodeEntity sysCusNationCodeEntity = sysCusNationCodeDao.queryObjectByName(goodsDto.getOriCntName());
  1433. if (sysCusNationCodeEntity == null) {
  1434. isFail = true;
  1435. failNationGoodsSnList.add(goodsDto.getGoodsSn());
  1436. } else {
  1437. goodsEntity.setOriCntCode(sysCusNationCodeEntity.getCode());
  1438. }
  1439. if(goodsDto.getGoodsRate().indexOf("%") != -1){
  1440. goodsDto.setGoodsRate(goodsDto.getGoodsRate().substring(0, goodsDto.getGoodsRate().indexOf("%")));
  1441. }
  1442. goodsEntity.setGoodsRate(BigDecimal.valueOf(Double.valueOf(goodsDto.getGoodsRate())).divide(new BigDecimal(100), 4, BigDecimal.ROUND_HALF_UP));
  1443. }
  1444. goodsEntity.setIsOnSale(Integer.parseInt(goodsDto.getIsOnSaleStr()));
  1445. goodsEntity.setIsHot(Integer.parseInt(goodsDto.getIsHotStr()));
  1446. goodsEntity.setGoodsSn(goodsDto.getGoodsSn());
  1447. goodsEntity.setSku(goodsDto.getSku());
  1448. goodsEntity.setName(goodsDto.getName());
  1449. goodsEntity.setGoodsUnit(goodsDto.getGoodsUnit());
  1450. goodsEntity.setGoodsBizType(goodsDto.getGoodsBizType());
  1451. goodsEntity.setBrand(goodsDto.getBrand());
  1452. goodsEntity.setCusDeclEle(goodsDto.getCusDeclEle());
  1453. goodsEntity.setCusGoodsCode(goodsDto.getCusGoodsCode());
  1454. goodsEntity.setCusRecCode(goodsDto.getCusRecCode());
  1455. goodsEntity.setCiqProdModel(goodsDto.getCiqProdModel());
  1456. goodsEntity.setIsDelete(Integer.parseInt(Dict.isDelete.item_0.getItem()));
  1457. goodsEntity.setIsNew(0);
  1458. goodsEntity.setUpdateUserId(user.getUserId());
  1459. goodsEntity.setAddTime(new Date());
  1460. goodsEntity.setCreateTime(new Date());
  1461. goodsEntity.setUpdateTime(new Date());
  1462. goodsEntity.setModTime(new Date());
  1463. goodsEntity.setGoodsNumber(Integer.parseInt(goodsDto.getGoodsNumber()));
  1464. if(!isFail){
  1465. GoodsEntity goods = goodsDao.queryObjectBySn(goodsDto.getGoodsSn());
  1466. MngChangeEntity mngChangeEntity = new MngChangeEntity();
  1467. mngChangeEntity.setThirdPartyMerchCode(goodsEntity.getThirdPartyMerchCode());
  1468. mngChangeEntity.setChangeReason("更新商户商品总库存");
  1469. mngChangeEntity.setCreateTime(new Date());
  1470. mngChangeEntity.setModTime(new Date());
  1471. mngChangeEntity.setCreaterSn(user.getUsername());
  1472. mngChangeEntity.setModerSn(user.getUsername());
  1473. mngChangeEntity.setIsValid(0);
  1474. mngChangeEntity.setMerchSn(goodsEntity.getMerchSn());
  1475. if(goods!=null) {// 修改商品
  1476. mngChangeEntity.setOriginalNum(goods.getGoodsNumber());//原库存数
  1477. mngChangeEntity.setValidNum(goods.getGoodsNumber() + Integer.parseInt(goodsDto.getGoodsNumber()));//可用数
  1478. mngChangeEntity.setChangeNum(Integer.parseInt(goodsDto.getGoodsNumber()));//变化数
  1479. mngChangeEntity.setChangeType(Dict.changeType.item_3.getItem());
  1480. mngChangeEntity.setGoodsId(Integer.parseInt(String.valueOf(goods.getId())));
  1481. goodsEntity.setId(goods.getId());
  1482. goodsEntity.setGoodsNumber(goods.getGoodsNumber()+Integer.parseInt(goodsDto.getGoodsNumber()));
  1483. goodsDao.update(goodsEntity);
  1484. }else{
  1485. mngChangeEntity.setOriginalNum(0);//原库存数
  1486. mngChangeEntity.setValidNum(Integer.parseInt(goodsDto.getGoodsNumber()));//可用数
  1487. mngChangeEntity.setChangeNum(Integer.parseInt(goodsDto.getGoodsNumber()));//变化数
  1488. mngChangeEntity.setChangeType(Dict.changeType.item_2.getItem());
  1489. goodsDao.save(goodsEntity);
  1490. mngChangeEntity.setGoodsId(goodsEntity.getId().intValue());
  1491. }
  1492. mngChangeDao.save(mngChangeEntity);
  1493. // 修改产品
  1494. ProductEntity product = productDao.queryObjectByGoodsIdAndStoreId(String.valueOf(goodsEntity.getId()), "");
  1495. GoodsSpecificationEntity goodsSpecification = new GoodsSpecificationEntity();
  1496. // 普通货物暂不添加商品规格
  1497. if (!Dict.orderBizType.item_11.getItem().equals(goodsDto.getGoodsBizType())) {
  1498. // 添加商品规格
  1499. GoodsSpecificationEntity specificationEntity = goodsSpecificationDao.queryByGoodsId(goodsEntity.getId());
  1500. if(specificationEntity == null) {
  1501. goodsSpecification.setGoodsId(goodsEntity.getId());
  1502. goodsSpecification.setValue(goodsEntity.getCiqProdModel());
  1503. goodsSpecification.setSpecificationId(1);
  1504. goodsSpecificationDao.save(goodsSpecification);
  1505. }else {
  1506. goodsSpecification.setValue(goodsDto.getCiqProdModel());
  1507. goodsSpecification.setId(specificationEntity.getId());
  1508. goodsSpecificationDao.update(goodsSpecification);
  1509. }
  1510. if(product == null){
  1511. product = new ProductEntity();
  1512. product.setGoodsSn(goodsDto.getGoodsSn());
  1513. product.setGoodsId(goodsEntity.getId());
  1514. product.setGoodsDefault(0);
  1515. product.setGoodsNumber(goodsEntity.getGoodsNumber());
  1516. product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
  1517. product.setGoodsSpecificationIds(goodsSpecification.getId().toString());
  1518. productDao.save(product);
  1519. }else{
  1520. product.setGoodsSpecificationIds(goodsSpecification.getId().toString());
  1521. product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
  1522. productDao.update(product);
  1523. }
  1524. }
  1525. }
  1526. }
  1527. ExportExceptionDataEntity exportExceptionDataEntity = new ExportExceptionDataEntity();
  1528. exportExceptionDataEntity.setCreaterSn(user.getUserId().toString());
  1529. exportExceptionDataEntity.setUserId(user.getUserId().intValue());
  1530. exportExceptionDataEntity.setCreateTime(new Date());
  1531. exportExceptionDataEntity.setModTime(new Date());
  1532. exportExceptionDataEntity.setMerchSn(merchSn);
  1533. exportExceptionDataEntity.setStoreId(user.getStoreId());
  1534. if(exportDataType == 1) {
  1535. exportExceptionDataEntity.setExportDataType(Dict.exportDataType.item_1.getItem());
  1536. }else{
  1537. exportExceptionDataEntity.setExportDataType(Dict.exportDataType.item_2.getItem());
  1538. }
  1539. if(failMerchUserGoodsSnList != null && failMerchUserGoodsSnList.size() > 0){
  1540. exportExceptionDataEntity.setExportExceptionData("不能操作除了登录用户以外商户的商品,当前商户编号为【"+merchSn+"】,请检查商品编码【"+failMerchUserGoodsSnList+"】的商品信息");
  1541. goodsUtils.save(exportExceptionDataEntity);
  1542. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1543. }
  1544. if(failMerchGoodsSnList != null && failMerchGoodsSnList.size() > 0){
  1545. exportExceptionDataEntity.setExportExceptionData("第三方商户代码不存在,请在商城配置》第三方商户管理中维护,请检查商品编码【"+failMerchGoodsSnList+"】的商品信息,请先维护再继续操作!");
  1546. goodsUtils.save(exportExceptionDataEntity);
  1547. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1548. }
  1549. // if(failGoodsSnList != null && failGoodsSnList.size() > 0){
  1550. // if(failSameSkuList.size()>0) {
  1551. // exportExceptionDataEntity.setExportExceptionData("不能有重复的商品编码、sku信息!请检查商品编码【" + failGoodsSnList + "】,业务类型【" +
  1552. // failGoodsTypeList + "】,SKU【" + failSameSkuList + "】的商品信息");
  1553. // goodsUtils.save(exportExceptionDataEntity);
  1554. // throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1555. // }else{
  1556. // exportExceptionDataEntity.setExportExceptionData("不能有重复的商品编码、sku信息!请检查商品编码【" + failGoodsSnList + "】,业务类型【" +
  1557. // failGoodsTypeList + "】的商品信息");
  1558. // goodsUtils.save(exportExceptionDataEntity);
  1559. // throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1560. // }
  1561. // }
  1562. if(failTypeGoodsSnList != null && failTypeGoodsSnList.size() > 0){
  1563. exportExceptionDataEntity.setExportExceptionData("货品业务类型只能是【00保税备货、02保税补货、10保税展示】!请检查商品编码【"+failTypeGoodsSnList+"】的商品信息");
  1564. goodsUtils.save(exportExceptionDataEntity);
  1565. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1566. }
  1567. if(failHotGoodsSnList != null && failHotGoodsSnList.size() > 0){
  1568. exportExceptionDataEntity.setExportExceptionData("请检查业务类型为【保税补货或保税展示】的商品,商品编码【"+failHotGoodsSnList+"】的商品不能设置为热销!");
  1569. goodsUtils.save(exportExceptionDataEntity);
  1570. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1571. }
  1572. if(failSuppGoodsSnList != null && failSuppGoodsSnList.size() > 0){
  1573. exportExceptionDataEntity.setExportExceptionData("供应商信息请在商城配置》商品供应商中维护,供应商与商户信息对应,请检查该商品商户信息下的供应商是否维护,不存在的商品编码【" + failSuppGoodsSnList + "】");
  1574. goodsUtils.save(exportExceptionDataEntity);
  1575. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1576. }
  1577. if(failUnitGoodsSnList != null && failUnitGoodsSnList.size() > 0){
  1578. exportExceptionDataEntity.setExportExceptionData("计算单位信息请在商城配置》计算单位中维护,不存在的商品编码【" + failUnitGoodsSnList + "】");
  1579. goodsUtils.save(exportExceptionDataEntity);
  1580. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1581. }
  1582. if(failNationGoodsSnList != null && failNationGoodsSnList.size() > 0){
  1583. exportExceptionDataEntity.setExportExceptionData("原产国信息请在商城配置》原产国中维护,不存在的商品编码【" + failNationGoodsSnList + "】");
  1584. goodsUtils.save(exportExceptionDataEntity);
  1585. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1586. }
  1587. }else{
  1588. throw new RRException("导入数据为空,或者检查商品编码数据是否为空");
  1589. }
  1590. return 1;
  1591. }
  1592. @Override
  1593. public GoodsDetailsDto queryGoodsDetailsByProdBarcode(String prodBarcode, String storeId,String sku) {
  1594. return goodsDao.queryGoodsDetailsByProdBarcode(prodBarcode,storeId,sku);
  1595. }
  1596. @Override
  1597. public GoodsPanoramaDto searchGoodsPanoramaDtoByKeyword(String keyword) {
  1598. return goodsDao.searchGoodsPanoramaDtoByKeyword(keyword);
  1599. }
  1600. @Override
  1601. public List<GoodsEntity> queryExportList(Map<String, Object> params) {
  1602. return goodsDao.queryExportList(params);
  1603. }
  1604. /**
  1605. * 查出pdf需要的需要
  1606. *
  1607. * @param sku 商品sku
  1608. * @param storeId
  1609. * @param prodBarcode
  1610. * @return
  1611. */
  1612. @Override
  1613. public PDFGoodsDto queryForPDFData(String sku, String storeId, String prodBarcode) {
  1614. return goodsDao.queryForPDFData(sku,storeId,prodBarcode);
  1615. }
  1616. /**
  1617. * 查询产品价格
  1618. * 改进后的逻辑:
  1619. * @param prodBarcode
  1620. * @param storeId
  1621. * @return
  1622. */
  1623. @Override
  1624. @Transactional
  1625. public synchronized Map<String,Object> calculateGoodsDetail(String prodBarcode, String storeId, String sku) {
  1626. /**
  1627. * 1.首先根据商品条码跟门店id查询是否有库存,没库存直接返回
  1628. */
  1629. GoodsDetailsDto goods = queryGoodsDetailsByProdBarcode(prodBarcode,storeId,sku);
  1630. if(goods == null) {
  1631. return null;
  1632. }
  1633. /*
  1634. * 2. 查询海仓仓库系统,判断库存是否足够
  1635. * TODO 测试的时候注释,海控仓库系统没有测试环境,上生产时放开注释
  1636. * */
  1637. sku = org.springframework.util.StringUtils.isEmpty(sku) ? goods.getSku() : sku;
  1638. String wareStockNumberKey = storeId + sku + prodBarcode;
  1639. String warehouseStockMapKey = Constants.WAREHOUSE_STOCK_MAP_KEY + "_" + storeId;
  1640. String redisCacheWareQuantity = JedisUtil.hget(warehouseStockMapKey, wareStockNumberKey);
  1641. Integer wareQuantity = 0;
  1642. Integer exitRegionNumber = goods.getExitRegionNumber();
  1643. String stockNum = goods.getStockNum();
  1644. Integer sellVolume = Objects.isNull(goods.getSellVolume()) ? 1 : goods.getSellVolume();
  1645. if (org.springframework.util.StringUtils.isEmpty(redisCacheWareQuantity)) {
  1646. // WareQueryStockParamDTO wareQueryStockParamDTO = new WareQueryStockParamDTO();
  1647. // List<Criteria> criteriaList = new ArrayList<>();
  1648. // Criteria criteria = new Criteria();
  1649. // criteria.setItemId(goods.getProdBarcode());
  1650. // criteria.setItemCode(goods.getSku());
  1651. // criteriaList.add(criteria);
  1652. // wareQueryStockParamDTO.setCriteriaList(criteriaList);
  1653. // String queryWarehouseStockResponse = haiKongWarehouseTemplate.queryWarehouseStock(wareQueryStockParamDTO);
  1654. // log.info("调用海控仓库系统,请求参数,门店:{}、商品条码:{}、商品编码:{},响应数据:{}", storeId, prodBarcode, sku, queryWarehouseStockResponse);
  1655. // if (org.springframework.util.StringUtils.isEmpty(queryWarehouseStockResponse)) {
  1656. // log.error("调用库存系统接口出现错误!返回结果为空!");
  1657. // throw new ServiceException("调用库存系统接口出现错误!");
  1658. // }
  1659. // WareQueryStockResponseDTO wareQueryStockResponseDTO = JacksonUtil.fromStringJson(queryWarehouseStockResponse, WareQueryStockResponseDTO.class);
  1660. /* -----------------↓--------------测试数据,生产请注释-------------------↓------------ */
  1661. WareQueryStockResponseDTO wareQueryStockResponseDTO = new WareQueryStockResponseDTO();
  1662. wareQueryStockResponseDTO.setCode("0");
  1663. wareQueryStockResponseDTO.setFlag("success");
  1664. wareQueryStockResponseDTO.setMessage("");
  1665. WareQueryStockResponseDTO.WareQueryStockResponseItemDTO wareQueryStockResponseItemDTO = new WareQueryStockResponseDTO.WareQueryStockResponseItemDTO();
  1666. wareQueryStockResponseItemDTO.setQuantity(3);
  1667. wareQueryStockResponseItemDTO.setItemCode(sku);
  1668. wareQueryStockResponseItemDTO.setProduceCode(prodBarcode);
  1669. List<WareQueryStockResponseDTO.WareQueryStockResponseItemDTO> wareQueryStockResponseItemDTOS = new ArrayList<>();
  1670. wareQueryStockResponseItemDTOS.add(wareQueryStockResponseItemDTO);
  1671. wareQueryStockResponseDTO.setItems(wareQueryStockResponseItemDTOS);
  1672. /* -----------------↑--------------测试数据,生产请注释------------------↑------------- */
  1673. if (Objects.isNull(wareQueryStockResponseDTO)) {
  1674. // log.error("解析一步达库存系统响应数据出现错误!请求响应结果:{}", queryWarehouseStockResponse);
  1675. throw new ServiceException("解析一步达库存系统响应数据出现错误!");
  1676. }
  1677. // 校验库存
  1678. WareQueryStockResponseDTO.WareQueryStockResponseItemDTO itemDTO = wareQueryStockResponseDTO.getItems().get(0);
  1679. // 仓库可用库存
  1680. wareQuantity = itemDTO.getQuantity();
  1681. JedisUtil.hset(warehouseStockMapKey, wareStockNumberKey, String.valueOf(wareQuantity));
  1682. } else {
  1683. wareQuantity = Integer.parseInt(redisCacheWareQuantity);
  1684. }
  1685. // 保税仓库存 + 展销店库存 - 出区数 >= 购买数
  1686. if (!((wareQuantity + Integer.parseInt(stockNum) - exitRegionNumber) > sellVolume)) {
  1687. // 库存不足
  1688. log.error("商品条码:【{}】,sku:【{}】,门店库存:【{}】,保税仓库存:【{}】,出区数:【{}】,该商品仓库库存不足!", prodBarcode, sku, stockNum, wareQuantity, exitRegionNumber);
  1689. throw new ServiceException(String.format("商品条码:【%s】,sku:【%s】,门店库存:【%s】,保税仓库存:【%s】,出区数:【%s】,该商品仓库库存不足!", prodBarcode, sku, stockNum, wareQuantity, exitRegionNumber));
  1690. } else {
  1691. JedisUtil.hset(warehouseStockMapKey, wareStockNumberKey, String.valueOf(wareQuantity - sellVolume));
  1692. }
  1693. goods.setDiscountedPrice(new BigDecimal(0));
  1694. BigDecimal retailPrice = goods.getRetailPrice();
  1695. goods.setActualPaymentAmount(retailPrice.setScale(2,RoundingMode.HALF_UP));
  1696. Map<String,Object> skuActivitiesMap = new HashMap<>();
  1697. // 计算税费
  1698. GoodsEntity goodsEntity = goodsDao.queryByBarcodeAndSku(prodBarcode, goods.getGoodsSn());
  1699. BigDecimal tax = CalculateTax.calculateFinalTax(goodsEntity, goods.getActualPaymentAmount(),this).setScale(3,RoundingMode.HALF_UP);
  1700. goods.setGoodstaxes(tax.toString());
  1701. goods.setSellVolume(1);
  1702. skuActivitiesMap.put("goods",goods);
  1703. return skuActivitiesMap;
  1704. }
  1705. @Override
  1706. public List<Map<String,Object>> selectSkuDetails(String prodBarcode, String storeId) {
  1707. /**
  1708. * 1.首先根据商品条码跟门店id查询是否有库存,没库存直接返回
  1709. */
  1710. List<GoodsDetailsDto> goodsList = goodsDao.queryGoodsSkuList(prodBarcode,storeId);
  1711. List<Map<String,Object>> mapList = new ArrayList<>();
  1712. if (goodsList == null) {
  1713. return null;
  1714. }
  1715. for(GoodsDetailsDto goods : goodsList){
  1716. mapList.add(selectGetSkuDetails(goods,prodBarcode,storeId));
  1717. }
  1718. return mapList;
  1719. }
  1720. public Map<String,Object> selectGetSkuDetails(GoodsDetailsDto goods,String prodBarcode, String storeId) {
  1721. /**
  1722. * 1.首先根据商品条码跟门店id查询是否有库存,没库存直接返回
  1723. */
  1724. goods.setDiscountedPrice(new BigDecimal(0));
  1725. BigDecimal retailPrice = goods.getRetailPrice();
  1726. goods.setActualPaymentAmount(retailPrice.setScale(2,RoundingMode.HALF_UP));
  1727. Map<String,Object> skuActivitiesMap = new HashMap<>();
  1728. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  1729. /**
  1730. * 2.查询当前时间,该门店是否有活动,如果有活动,查询开启了哪些营销方式
  1731. * 参数: 当前时间 门店id
  1732. */
  1733. List<MkActivitiesEntity> mkActivitiesEntityList = mkActivitiesService.queryByNow(storeId,format.format(new Date()));
  1734. if(mkActivitiesEntityList == null || mkActivitiesEntityList.size() == 0){
  1735. // 计算税费
  1736. GoodsEntity goodsEntity = goodsDao.queryByBarcodeAndSku(prodBarcode, goods.getGoodsSn());
  1737. BigDecimal tax = CalculateTax.calculateFinalTax(goodsEntity,goods.getActualPaymentAmount(),this).setScale(3,RoundingMode.HALF_UP);
  1738. goods.setGoodstaxes(tax.toString());
  1739. skuActivitiesMap.put("goods",goods);
  1740. return skuActivitiesMap;
  1741. }
  1742. // 遍历活动集合,查询有哪些活动是开启的
  1743. boolean daily = false,coupon = false,combinationPrice = false
  1744. ,discount = false,fullGift = false,fullReduction = false
  1745. ,getOneFree = false,promotion = false,halfPrice = false;
  1746. List<String> topicList = new ArrayList<>(); // 记录有哪些营销活动的topic
  1747. Map<String,String> mkaIdMap = new HashMap<>(); // 记录topic跟mkaId的关系
  1748. // 将所有的营销活动新增到list中
  1749. for(MkActivitiesEntity mkActivitiesEntity : mkActivitiesEntityList){
  1750. topicList.add(mkActivitiesEntity.getMkaTopic());
  1751. String mkaId = mkaIdMap.putIfAbsent(mkActivitiesEntity.getMkaTopic(), mkActivitiesEntity.getMkaId()+"");
  1752. if(StringUtils.isNotEmpty(mkaId)){
  1753. // mkaId = "'"+mkaId + "','" + mkActivitiesEntity.getMkaId()+"'";
  1754. mkaId += String.format(",%s",mkActivitiesEntity.getMkaId());
  1755. mkaIdMap.put(mkActivitiesEntity.getMkaTopic(),mkaId);
  1756. }
  1757. }
  1758. // 判断有哪些营销活动
  1759. if(topicList.contains("zhjsp")) // 组合价
  1760. combinationPrice = true;
  1761. if(topicList.contains("dz")) // 打折
  1762. discount = true;
  1763. if(topicList.contains("mz")) //满赠
  1764. fullGift = true;
  1765. if(topicList.contains("mj")) // 满减
  1766. fullReduction = true;
  1767. if(topicList.contains("mysy")) // 买一送一
  1768. getOneFree = true;
  1769. if(topicList.contains("rchd")) // 日常活动
  1770. daily = true;
  1771. if(topicList.contains("yhq")) // 优惠券
  1772. coupon = true;
  1773. if(topicList.contains("lscx")) // 临时促销
  1774. promotion = true;
  1775. if(topicList.contains("drjbj"))
  1776. halfPrice = true;
  1777. // 获取未优惠前的商品价格
  1778. retailPrice = goods.getRetailPrice();
  1779. // 根据条码查询商品品牌名称 mall_brand mall_product_store_rela mall_goods
  1780. String brandName = goods.getBrand();
  1781. /**
  1782. * 优先级:临时促销 》买一送一=满赠 》 组合价=日常活动 》 打折=满减 》 优惠券
  1783. */
  1784. /**
  1785. * 组合价的做法就是将参与组合的条码带到收银端
  1786. * 现根据营销活动id跟条形码,查询有哪些参与该条码组合的商品
  1787. *
  1788. *
  1789. * TODO
  1790. */
  1791. if(combinationPrice){
  1792. String mkaId = mkaIdMap.get("zhjsp");
  1793. Map<String,Object> param = new HashMap<>();
  1794. param.put("mkaId",mkaId);
  1795. param.put("prodBarcode",prodBarcode);
  1796. List<MkActivitiesCombinationPriceEntity> combinationPriceList = combinationPriceService.queryList(param);
  1797. if(combinationPriceList != null && combinationPriceList.size() > 0) {
  1798. Map<String, List<MkActivitiesCombinationPriceEntity>> collect =
  1799. combinationPriceList.stream().collect(Collectors.groupingBy(MkActivitiesCombinationPriceEntity::getCombinationType));
  1800. skuActivitiesMap.put("zhjsp",collect);
  1801. }
  1802. }
  1803. format = new SimpleDateFormat("yyyy-MM-dd");
  1804. String nowTime = format.format(new Date());
  1805. /**
  1806. * 满减可能是跟着条码,也可能跟着品牌
  1807. * 根据商品品牌跟商品条码去查询是否有优惠金额
  1808. *
  1809. * 满足金额 购买商品条码 赠品条码
  1810. *
  1811. */
  1812. Map<String,Object> fullReductionMap = new HashMap<>();
  1813. if(fullReduction){
  1814. String mkaId = mkaIdMap.get("mj");
  1815. MkActivitiesFullReductionEntity fullReductionEntity = fullReductionService.queryByCodeOrBrand(mkaId,prodBarcode,brandName,nowTime);
  1816. if(fullReductionEntity != null) {
  1817. if(!StringUtils.isNullOrEmpty(fullReductionEntity.getProductBrand())){
  1818. // 跟着品牌走
  1819. fullReductionMap.put(brandName,fullReductionEntity);
  1820. }else{
  1821. // 跟着条码走
  1822. fullReductionMap.put(fullReductionEntity.getBarcode(), fullReductionEntity);
  1823. }
  1824. skuActivitiesMap.put("mj",fullReductionMap);
  1825. }
  1826. }
  1827. /**
  1828. * 满赠可能是跟着条码,也可能跟着品牌
  1829. * 根据商品品牌跟商品条码去查询是否有满赠
  1830. * 1.先扫买的商品,然后查询出赠送的商品条码
  1831. * 2.先扫赠的商品,然后查询出符合条件的商品条码或者品牌
  1832. * 满足的金额 购买的商品条码或者品牌 赠送的商品条码
  1833. * TODO
  1834. */
  1835. Map<String,Object> fullGiftMap = new HashMap<>();
  1836. if(fullGift){
  1837. String mkaId = mkaIdMap.get("mz");
  1838. MkActivitiesFullGiftEntity giftEntity = fullGiftService.queryByCodeOrBrand(mkaId,prodBarcode,brandName,nowTime);
  1839. if(giftEntity != null) {
  1840. if(giftEntity.getProductBrand() != null){
  1841. // 跟着品牌走
  1842. fullGiftMap.put(brandName,giftEntity);
  1843. }else{
  1844. // 跟着条码走
  1845. fullGiftMap.put(giftEntity.getBarcode(), giftEntity);
  1846. }
  1847. skuActivitiesMap.put("mz",fullGiftMap);
  1848. }
  1849. }
  1850. /**
  1851. * 买一送一可能是跟着条码,也可能跟着品牌
  1852. * 根据商品品牌跟商品条码去查询是否有送的商品
  1853. * 有两个场景
  1854. * 1.先扫买的商品,然后查询出赠送的商品条码
  1855. * 2.先扫赠的商品,然后查询出符合条件的商品条码或者品牌
  1856. * 所以就需要一个map
  1857. * key为购买的商品条码或者商品品牌 value为赠送的商品条码
  1858. * 但是品牌是根据条码查询的,所以最终map里面的结构是
  1859. * key 商品条码 value 赠品条码
  1860. */
  1861. if(getOneFree){
  1862. String mkaId = mkaIdMap.get("mysy");
  1863. MkActivitiesGetOneFreeGoodsEntity getOneFreeGoodsEntity = getOneFreeGoodsService.queryByCodeOrBrand(mkaId,prodBarcode,brandName);
  1864. if(getOneFreeGoodsEntity != null){
  1865. if("无".equals(getOneFreeGoodsEntity.getProductBrand())){
  1866. getOneFreeGoodsEntity.setBrand(false);
  1867. skuActivitiesMap.put("mysy",getOneFreeGoodsEntity);
  1868. }else{
  1869. getOneFreeGoodsEntity.setBrand(true);
  1870. skuActivitiesMap.put("mysy",getOneFreeGoodsEntity);
  1871. }
  1872. }
  1873. }
  1874. // 第二份半价
  1875. if(halfPrice){
  1876. String mkaId = mkaIdMap.get("drjbj");
  1877. MkActivitiesHalfPriceEntity activitiesHalfPriceEntity = halfPriceService.queryByCodeOrBrand(mkaId,prodBarcode);
  1878. if(activitiesHalfPriceEntity != null){
  1879. skuActivitiesMap.put("drjbj",activitiesHalfPriceEntity);
  1880. }
  1881. }
  1882. // --------------------------------------------------------------------------------------
  1883. /**
  1884. * 优惠券跟着条形码走,一般是设置一个标识,然后最后输入优惠券码后,减扣对应的标识,所以返回一个map数组
  1885. */
  1886. if(coupon){
  1887. String mkaId = mkaIdMap.get("yhq");
  1888. MkActivitiesCouponEntity couponEntity = couponService.queryByBarCode(mkaId,prodBarcode,nowTime);
  1889. if(couponEntity != null){ // 优惠券码,优惠金额
  1890. Map<String,Object> returnMap = new HashMap<>();
  1891. returnMap.put(couponEntity.getCouponSn() , couponEntity.getCouponPrice());
  1892. skuActivitiesMap.put("yhq",returnMap);
  1893. }
  1894. }
  1895. /**
  1896. * 打折的价格是与条形码对应的,所以需要根据条形码和营销方式id去查询活动价格
  1897. */
  1898. if(discount){
  1899. String mkaId = mkaIdMap.get("dz");
  1900. MkActivitiesDiscountEntity discountEntity = discountService.queryByBarCode(mkaId,prodBarcode);
  1901. // TODO 可能会直接替代产品价格
  1902. if (discountEntity != null) {
  1903. goods.setActualPaymentAmount(discountEntity.getActivityPrice());
  1904. goods.setRetailPrice(discountEntity.getActivityPrice());
  1905. goods.setActivity("打折");
  1906. }
  1907. }
  1908. /**
  1909. * 日常活动跟着条形码走,优先级比临时促销低,但是高于正常价格
  1910. */
  1911. if(daily){
  1912. String mkaId = mkaIdMap.get("rchd");
  1913. MkDailyActivitiesEntity dailyActivitiesEntity = dailyActivitiesService.queryByBarCode(mkaId,prodBarcode);
  1914. if(dailyActivitiesEntity != null){
  1915. goods.setActualPaymentAmount(dailyActivitiesEntity.getActivityPrice());
  1916. goods.setRetailPrice(dailyActivitiesEntity.getActivityPrice());
  1917. goods.setActivity("日常活动");
  1918. }
  1919. }
  1920. /**
  1921. * 临时促销跟着条形码走,优先级应该最高,所以排到了最下面
  1922. */
  1923. if(promotion){
  1924. String mkaId = mkaIdMap.get("lscx");
  1925. MkActivitiesPromotionEntity promotionEntity = promotionService.queryByBarCode(mkaId,prodBarcode);
  1926. // 如果该商品存在临时促销,直接替换活动价格
  1927. if(promotionEntity != null){
  1928. goods.setActualPaymentAmount(promotionEntity.getActivityPrice());
  1929. goods.setRetailPrice(promotionEntity.getActivityPrice());
  1930. goods.setActivity("临时促销");
  1931. }
  1932. }
  1933. // 计算税费
  1934. GoodsEntity goodsEntity = goodsDao.queryByBarcodeAndSku(prodBarcode, goods.getGoodsSn());
  1935. BigDecimal tax = CalculateTax.calculateFinalTax(goodsEntity,goods.getActualPaymentAmount(),this).setScale(3,RoundingMode.HALF_UP);
  1936. goods.setGoodstaxes(tax.toString());
  1937. skuActivitiesMap.put("goods",goods);
  1938. return skuActivitiesMap;
  1939. }
  1940. /**
  1941. * 根据条形码查询商品
  1942. *
  1943. * @param barCode
  1944. * @return
  1945. */
  1946. @Override
  1947. public GoodsEntity queryByBarcode(String barCode) {
  1948. return goodsDao.queryByBarcode(barCode);
  1949. }
  1950. /**
  1951. * 根据sku查询商品
  1952. *
  1953. * @param sku
  1954. * @return
  1955. */
  1956. @Override
  1957. public GoodsEntity queryBySku(String sku) {
  1958. return goodsDao.queryBySku(sku);
  1959. }
  1960. /* @Override
  1961. @Transactional
  1962. public int uploadExcel(MultipartFile file) {
  1963. SysUserEntity user = ShiroUtils.getUserEntity();
  1964. List<String[]> list = ExcelImport.getExcelData(file);
  1965. // 取门店名称
  1966. StoreEntity storeEntity = storeDao.queryObjectByName(list.get(0)[3]);
  1967. if (null == storeEntity) {
  1968. return 0;
  1969. }
  1970. //去除表头两行、底部合计
  1971. if (list != null && list.size() > 3) {
  1972. ProductStoreRelaEntity storeRelaEntity;
  1973. ProductEntity productEntity;
  1974. for (int i = 2; i < list.size() - 1; i++) {
  1975. String[] item = list.get(i);
  1976. String goodsSn = item[0];
  1977. productEntity = productDao.queryObjectBySn(goodsSn);
  1978. if (StringUtils.isNullOrEmpty(goodsSn)) {
  1979. continue;
  1980. }
  1981. if (null == productEntity || null == productEntity.getId()) {
  1982. continue;
  1983. }
  1984. storeRelaEntity = productStoreRelaDao.queryByStoreIdProductId(storeEntity.getId(), productEntity.getId());
  1985. if (null != storeRelaEntity && null != storeRelaEntity.getId()) {
  1986. storeRelaEntity.setRetailPrice(new BigDecimal(item[6]));
  1987. storeRelaEntity.setStockNum(Integer.valueOf(item[3].replace(".00", "")));
  1988. storeRelaEntity.setStockPrice(new BigDecimal(item[4]));
  1989. productStoreRelaDao.update(storeRelaEntity);
  1990. } else {
  1991. storeRelaEntity = new ProductStoreRelaEntity();
  1992. storeRelaEntity.setGoodsId(productEntity.getGoodsId());
  1993. storeRelaEntity.setProductId(productEntity.getId());
  1994. storeRelaEntity.setRetailPrice(new BigDecimal(item[6]));
  1995. storeRelaEntity.setMarketPrice(new BigDecimal(item[6]));
  1996. storeRelaEntity.setStockNum(Integer.valueOf(item[3]));
  1997. storeRelaEntity.setStockPrice(new BigDecimal(item[4]));
  1998. storeRelaEntity.setStoreId(storeEntity.getId());
  1999. productStoreRelaDao.save(storeRelaEntity);
  2000. }
  2001. }
  2002. }
  2003. return 1;
  2004. }*/
  2005. public GoodsEntity queryObjectBySn(String goodsSn) {
  2006. return goodsDao.queryObjectBySn(goodsSn);
  2007. }
  2008. /**
  2009. * 全量拉取
  2010. */
  2011. @Override
  2012. public void syncOmsHsCodeTask() {
  2013. syncOmsHsCodeSegment(goodsDao.queryNonSyncOmsHsCodeSkuList());
  2014. }
  2015. /**
  2016. * 全量计算
  2017. */
  2018. @Override
  2019. public void syncGoodsRateTask() {
  2020. // 查询出所有商品
  2021. List<GoodsEntity> allGoodsList = goodsDao.queryAllList(null,null);
  2022. syncGoodsRate(allGoodsList);
  2023. }
  2024. /**
  2025. * 还原园区库存
  2026. *
  2027. * @param goodsEntities 数据
  2028. */
  2029. @Override
  2030. public int restoreBatch(List<GoodsEntity> goodsEntities) {
  2031. return goodsDao.restoreBatch(goodsEntities);
  2032. }
  2033. /**
  2034. * 查询库存及sku的信息,查询出来的库存是门店的库存
  2035. *
  2036. * @param prodBarcode 条码
  2037. * @param storeId 门店id
  2038. * @param sku sku
  2039. * @return 商品信息
  2040. */
  2041. @Override
  2042. public GoodsEntity queryGoodsStockByBarcodeAndStoreIdAndSku(String prodBarcode, Integer storeId, String sku) {
  2043. return goodsDao.queryGoodsStockByBarcodeAndStoreIdAndSku(prodBarcode, storeId, sku);
  2044. }
  2045. /**
  2046. * 查询库存及sku的信息,查询出来的库存是门店的库存
  2047. *
  2048. * @param queryGoodsVOList 查询条件,包含商品条码、sku、门店id
  2049. * @return 商品信息
  2050. */
  2051. @Override
  2052. public List<GoodsEntity> queryGoodsStockByQueryGoodsVoList(List<QueryGoodsVO> queryGoodsVOList) {
  2053. return goodsDao.queryGoodsStockByQueryGoodsVoList(queryGoodsVOList);
  2054. }
  2055. /**
  2056. * 选择拉取
  2057. * @param ids
  2058. */
  2059. @Override
  2060. public void syncOmsHsCodeGoode(List<Integer> ids) {
  2061. syncOmsHsCodeSegment(goodsDao.syncOmsHsCodeGoode(ids));
  2062. }
  2063. /**
  2064. * 选择计算
  2065. * @param ids
  2066. */
  2067. @Override
  2068. public void syncGoodsRateGoode(List<Integer> ids) {
  2069. syncGoodsRate(goodsDao.syncGoodsRateGoode(ids));
  2070. }
  2071. public void syncOmsHsCodeSegment(List<String> skuList) {
  2072. int segmentLimitSize = 300;
  2073. if (skuList.size() < segmentLimitSize) {
  2074. this.syncOmsHsCode(skuList);
  2075. } else {
  2076. int segmentSize = skuList.size() % segmentLimitSize == 0 ? skuList.size() / segmentLimitSize : (skuList.size() / segmentLimitSize) + 1;
  2077. List<List<String>> segment = new ArrayList<>();
  2078. for (int i = 0; i < segmentSize; i++) {
  2079. int end = (i == segmentSize - 1) ? skuList.size() : i * segmentLimitSize + segmentLimitSize;
  2080. segment.add(new ArrayList<>(skuList.subList(i * segmentLimitSize, end)));
  2081. }
  2082. for (List<String> tmp : segment) {
  2083. this.syncOmsHsCode(tmp);
  2084. }
  2085. }
  2086. }
  2087. @Transactional
  2088. public void syncOmsHsCode(List<String> skuList) {
  2089. if (Objects.nonNull(skuList) && skuList.size()>0){
  2090. String result = HttpUtil.get("https://oms.ds-bay.com/oms-controller-mgt/pdProductRecord/hsCodeMapBySkuList?skuList=" + skuList);
  2091. //String result = HttpUtil.get("http://183.3.221.143:8080/oms-controller-mgt/pdProductRecord/hsCodeMapBySkuList?skuList=" + skuList);
  2092. //String result = HttpUtil.get("http://127.0.0.1:8080/oms-controller-mgt/pdProductRecord/hsCodeMapBySkuList?skuList=" + skuList);
  2093. Map<String,Map> map = JSON.parseObject(result, Map.class);
  2094. for (String sku : skuList) {
  2095. Map skuInfoMap = map.get(sku);
  2096. if (Objects.nonNull(skuInfoMap)){
  2097. String hsCodeName = (String) skuInfoMap.get("cusGoodsName");
  2098. String hsCode = (String) skuInfoMap.get("cusGoodsCode");
  2099. String prodName = (String) skuInfoMap.get("prodName");
  2100. String prodRecordName = (String) skuInfoMap.get("prodRecordName");
  2101. BigDecimal impConsumTaxRate = (BigDecimal) skuInfoMap.get("impConsumTaxRate");
  2102. BigDecimal valueAddedTaxRate = (BigDecimal) skuInfoMap.get("valueAddedTaxRate");
  2103. BigDecimal legalUnit1Qty = (BigDecimal) skuInfoMap.get("legalUnit1Qty");
  2104. BigDecimal legalUnit2Qty = (BigDecimal) skuInfoMap.get("legalUnit2Qty");
  2105. BigDecimal cosmThresholdValue = (BigDecimal) skuInfoMap.get("cosmThresholdValue");
  2106. if (StringUtils.isNotEmpty(hsCodeName) && StringUtils.isNotEmpty(hsCode)){
  2107. List<GoodsEntity> goodsEntityList = goodsDao.queryListBySku(sku);
  2108. if (Objects.nonNull(goodsEntityList) && goodsEntityList.size()>0){
  2109. for (GoodsEntity goodsEntity : goodsEntityList) {
  2110. goodsEntity.setHsCode(hsCode);
  2111. goodsEntity.setHsCodeName(hsCodeName);
  2112. goodsEntity.setImpConsumTaxRate(impConsumTaxRate);
  2113. goodsEntity.setValueAddedTaxRate(valueAddedTaxRate);
  2114. goodsEntity.setIsSyncHsCode("1");
  2115. goodsEntity.setLegalUnit1Qty(legalUnit1Qty);
  2116. goodsEntity.setProdName(prodName);
  2117. goodsEntity.setProdRecordName(prodRecordName);
  2118. goodsEntity.setLegalUnit2Qty(legalUnit2Qty);
  2119. goodsEntity.setCosmThresholdValue(cosmThresholdValue);
  2120. goodsDao.update(goodsEntity);
  2121. }
  2122. }
  2123. }
  2124. }
  2125. }
  2126. }
  2127. }
  2128. @Transactional
  2129. public void syncGoodsRate(List<GoodsEntity> allGoodsList) {
  2130. String storeId = "163"; // 市场部说所有门店价格一致,并且活动一致,可直接取其中一个门店价格进行计算税率
  2131. for (GoodsEntity goodsEntity : allGoodsList) {
  2132. // 同步过海关商品编码才可以算税率
  2133. if ("1".equals(goodsEntity.getIsSyncHsCode())) {
  2134. // Map<String, Object> goodsDetailMap = calculateGoodsDetail(goodsEntity.getProdBarcode(), storeId);
  2135. // if (Objects.nonNull(goodsDetailMap)) {
  2136. // GoodsDetailsDto goods = (GoodsDetailsDto) goodsDetailMap.get("goods");
  2137. // 顾客给的钱 就是税后价
  2138. // BigDecimal actualPaymentAmount = goods.getActualPaymentAmount(); //随便拿一个门店的当前时间的价格,如果有活动价会使用活动价
  2139. // BigDecimal calculateTax = CalculateTax.calculateTax(goodsEntity, actualPaymentAmount);// 税费
  2140. // BigDecimal goodsRate = calculateTax.divide(actualPaymentAmount, 4, BigDecimal.ROUND_HALF_UP);
  2141. BigDecimal goodsRate =CalculateTax.calculateGoodsRate(goodsEntity);
  2142. goodsEntity.setGoodsRate(goodsRate);
  2143. goodsEntity.setIsSyncHsCode("0");
  2144. goodsDao.update(goodsEntity);
  2145. // }
  2146. }
  2147. }
  2148. }
  2149. @Override
  2150. public void updateTaxErrorRecord(TaxErrorRecordEntity taxErrorRecordEntity) {
  2151. taxErrorRecordDao.update(taxErrorRecordEntity);
  2152. }
  2153. @Override
  2154. public void insertTaxErrorRecord(TaxErrorRecordEntity taxErrorRecordEntity) {
  2155. taxErrorRecordDao.save(taxErrorRecordEntity);
  2156. }
  2157. @Override
  2158. public void updateByEntity(GoodsEntity updateGoods) {
  2159. goodsDao.update(updateGoods);
  2160. }
  2161. @Override
  2162. public void checkGoodsPrice(SysUserEntity user) {
  2163. Map<String, BigDecimal> hsCodeMap = CalculateTax.hsCodeMap;
  2164. Set<String> hdCodeSet = hsCodeMap.keySet();
  2165. List<Map<String,String>> errorPriceSkuList = new LinkedList<>();
  2166. // 保存各种异常价格对象集合
  2167. List<ShopErrorPriceRecordEntity> shopErrorPriceRecordEntities = new LinkedList<>();
  2168. // 查询所有特殊化妆品的商品
  2169. List<GoodsEntity> queryGoodsDetails = goodsDao.querySpecialCosmetics(hdCodeSet);
  2170. for (GoodsEntity goodsEntity : queryGoodsDetails) {
  2171. calculateShopPrice(user,goodsEntity,errorPriceSkuList,shopErrorPriceRecordEntities);
  2172. calculateDailyPrice(user,goodsEntity,errorPriceSkuList,shopErrorPriceRecordEntities);
  2173. calculeatePromotion(user,goodsEntity,errorPriceSkuList,shopErrorPriceRecordEntities);
  2174. }
  2175. if(shopErrorPriceRecordEntities.size() != 0){
  2176. shopErrorPriceRecordService.saveBatch(shopErrorPriceRecordEntities);
  2177. }
  2178. }
  2179. @Override
  2180. public List<GoodsEntity> queryAllList(Integer page,Integer pageSize) {
  2181. return goodsDao.queryAllList(page,pageSize);
  2182. }
  2183. private void calculeatePromotion(SysUserEntity user, GoodsEntity goods, List<Map<String, String>> errorPriceSkuList, List<ShopErrorPriceRecordEntity> shopErrorPriceRecordEntities) {
  2184. String prodBarcode = goods.getProdBarcode();
  2185. String storeId = goods.getStoreId()+"";
  2186. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  2187. /**
  2188. * 2.查询当前时间,该门店是否有活动,如果有活动,查询开启了哪些营销方式
  2189. * 参数: 当前时间 门店id
  2190. */
  2191. MkActivitiesPromotionEntity promotionEntity = promotionService.queryByDateAndBarcode(storeId,format.format(new Date()),"lscx",prodBarcode);
  2192. // 如果该商品存在临时促销,直接替换活动价格
  2193. if(promotionEntity == null){
  2194. return ;
  2195. }
  2196. goods.setActualPaymentAmount(promotionEntity.getActivityPrice());
  2197. goods.setRetailPrice(promotionEntity.getActivityPrice());
  2198. goods.setActivity("临时促销");
  2199. try {
  2200. CalculateTax.calculateFinalTax(goods,goods.getActualPaymentAmount(),this).setScale(3,RoundingMode.HALF_UP);
  2201. } catch (Exception e) {
  2202. ShopErrorPriceRecordEntity shopErrorPriceRecordEntity = new ShopErrorPriceRecordEntity();
  2203. shopErrorPriceRecordEntity.setMerchSn(goods.getMerchSn());
  2204. shopErrorPriceRecordEntity.setThirdMerchSn(goods.getThirdPartyMerchCode());
  2205. shopErrorPriceRecordEntity.setShopSn(goods.getStoreId()+"");
  2206. shopErrorPriceRecordEntity.setPriceType(3); // 3 是临时促销
  2207. shopErrorPriceRecordEntity.setSku(goods.getSku());
  2208. shopErrorPriceRecordEntity.setBarcode(goods.getProdBarcode());
  2209. shopErrorPriceRecordEntity.setVerifier(user.getUserId()+"");
  2210. shopErrorPriceRecordEntity.setCheckTime(new Date());
  2211. shopErrorPriceRecordEntity.setCreaterSn(user.getUserId()+"");
  2212. shopErrorPriceRecordEntity.setCreateTime(new Date());
  2213. shopErrorPriceRecordEntity.setCurrentPrice(goods.getActualPaymentAmount().toString());
  2214. if(e.getMessage().contains("-")){
  2215. String suggestPrice = e.getMessage().split("-")[0];
  2216. String highestPrice = e.getMessage().split("-")[1];
  2217. String lowestPrice = e.getMessage().split("-")[2];
  2218. shopErrorPriceRecordEntity.setSuggestedPrice(suggestPrice);
  2219. shopErrorPriceRecordEntity.setLowestPrice(lowestPrice);
  2220. shopErrorPriceRecordEntity.setHighestPrice(highestPrice);
  2221. }
  2222. shopErrorPriceRecordEntity.setMkaId(promotionEntity.getMkaId());
  2223. shopErrorPriceRecordEntities.add(shopErrorPriceRecordEntity);
  2224. }
  2225. }
  2226. private void calculateDailyPrice(SysUserEntity user, GoodsEntity goods, List<Map<String, String>> errorPriceSkuList, List<ShopErrorPriceRecordEntity> shopErrorPriceRecordEntities) {
  2227. String prodBarcode = goods.getProdBarcode();
  2228. String storeId = goods.getStoreId()+"";
  2229. /**
  2230. * 2.查询当前时间,该门店是否有活动,如果有活动,查询开启了哪些营销方式
  2231. * 参数: 当前时间 门店id
  2232. */
  2233. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  2234. /**
  2235. * 日常活动跟着条形码走,优先级比临时促销低,但是高于正常价格
  2236. */
  2237. MkDailyActivitiesEntity dailyActivitiesEntity = dailyActivitiesService.queryByDateAndBarcode(storeId,format.format(new Date()),"rchd",prodBarcode);
  2238. if(dailyActivitiesEntity == null){
  2239. return ;
  2240. }
  2241. goods.setActualPaymentAmount(dailyActivitiesEntity.getActivityPrice());
  2242. goods.setRetailPrice(dailyActivitiesEntity.getActivityPrice());
  2243. goods.setActivity("日常活动");
  2244. // 计算税费
  2245. try {
  2246. CalculateTax.calculateFinalTax(goods,goods.getActualPaymentAmount(),this).setScale(3,RoundingMode.HALF_UP);
  2247. } catch (Exception e) {
  2248. // 记录有异常的sku
  2249. ShopErrorPriceRecordEntity shopErrorPriceRecordEntity = new ShopErrorPriceRecordEntity();
  2250. shopErrorPriceRecordEntity.setMerchSn(goods.getMerchSn());
  2251. shopErrorPriceRecordEntity.setThirdMerchSn(goods.getThirdPartyMerchCode());
  2252. shopErrorPriceRecordEntity.setShopSn(goods.getStoreId()+"");
  2253. shopErrorPriceRecordEntity.setPriceType(2); // 2 是日常活动
  2254. shopErrorPriceRecordEntity.setSku(goods.getSku());
  2255. shopErrorPriceRecordEntity.setBarcode(goods.getProdBarcode());
  2256. shopErrorPriceRecordEntity.setVerifier(user.getUserId()+"");
  2257. shopErrorPriceRecordEntity.setCheckTime(new Date());
  2258. shopErrorPriceRecordEntity.setCreaterSn(user.getUserId()+"");
  2259. shopErrorPriceRecordEntity.setCreateTime(new Date());
  2260. shopErrorPriceRecordEntity.setCurrentPrice(goods.getActualPaymentAmount().toString());
  2261. if(e.getMessage().contains("-")){
  2262. String suggestPrice = e.getMessage().split("-")[0];
  2263. String highestPrice = e.getMessage().split("-")[1];
  2264. String lowestPrice = e.getMessage().split("-")[2];
  2265. shopErrorPriceRecordEntity.setSuggestedPrice(suggestPrice);
  2266. shopErrorPriceRecordEntity.setLowestPrice(lowestPrice);
  2267. shopErrorPriceRecordEntity.setHighestPrice(highestPrice);
  2268. }
  2269. shopErrorPriceRecordEntity.setMkaId(dailyActivitiesEntity.getMkaId());
  2270. shopErrorPriceRecordEntities.add(shopErrorPriceRecordEntity);
  2271. }
  2272. }
  2273. private void calculateShopPrice(SysUserEntity user, GoodsEntity goods, List<Map<String, String>> errorPriceSkuList, List<ShopErrorPriceRecordEntity> shopErrorPriceRecordEntities) {
  2274. goods.setDiscountedPrice(new BigDecimal(0));
  2275. BigDecimal retailPrice = goods.getRetailPrice();
  2276. goods.setActualPaymentAmount(retailPrice.setScale(2,RoundingMode.HALF_UP));
  2277. // 计算税费
  2278. try {
  2279. CalculateTax.calculateFinalTax(goods,goods.getActualPaymentAmount(),this).setScale(3,RoundingMode.HALF_UP);
  2280. } catch (Exception e) {
  2281. // 记录有异常的sku
  2282. ShopErrorPriceRecordEntity shopErrorPriceRecordEntity = new ShopErrorPriceRecordEntity();
  2283. shopErrorPriceRecordEntity.setMerchSn(goods.getMerchSn());
  2284. shopErrorPriceRecordEntity.setThirdMerchSn(goods.getThirdPartyMerchCode());
  2285. shopErrorPriceRecordEntity.setShopSn(goods.getStoreId()+"");
  2286. shopErrorPriceRecordEntity.setPriceType(1); // 1 门店商品价格
  2287. shopErrorPriceRecordEntity.setSku(goods.getSku());
  2288. shopErrorPriceRecordEntity.setBarcode(goods.getProdBarcode());
  2289. shopErrorPriceRecordEntity.setVerifier(user.getUserId()+"");
  2290. shopErrorPriceRecordEntity.setCheckTime(new Date());
  2291. shopErrorPriceRecordEntity.setCreaterSn(user.getUserId()+"");
  2292. shopErrorPriceRecordEntity.setCreateTime(new Date());
  2293. shopErrorPriceRecordEntity.setCurrentPrice(goods.getActualPaymentAmount().toString());
  2294. if(e.getMessage().contains("-")){
  2295. String suggestPrice = e.getMessage().split("-")[0];
  2296. String highestPrice = e.getMessage().split("-")[1];
  2297. String lowestPrice = e.getMessage().split("-")[2];
  2298. shopErrorPriceRecordEntity.setSuggestedPrice(suggestPrice);
  2299. shopErrorPriceRecordEntity.setLowestPrice(lowestPrice);
  2300. shopErrorPriceRecordEntity.setHighestPrice(highestPrice);
  2301. }
  2302. shopErrorPriceRecordEntities.add(shopErrorPriceRecordEntity);
  2303. }
  2304. }
  2305. /**
  2306. * 根据产品条码和sku查询保税展示补货的商品
  2307. *
  2308. * @param prodBarcode 商品条码
  2309. * @param sku sku
  2310. * @return 商品信息
  2311. */
  2312. @Override
  2313. public GoodsEntity queryGoodsInfoByProductBarcodeAndSku(String prodBarcode, String sku) {
  2314. return goodsDao.queryGoodsInfoByProductBarcodeAndSku(prodBarcode, sku);
  2315. }
  2316. @Override
  2317. public int updateStockNumberByProductCodeAndSku(GoodsEntity goodsEntity) {
  2318. return goodsDao.updateStockNumberByProductCodeAndSku(goodsEntity);
  2319. }
  2320. }