GoodsServiceImpl.java 120 KB

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