GoodsServiceImpl.java 119 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375
  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. throw new RuntimeException("该品牌不存在,请检查品牌唯一标识符是否正确:"+uniqueIdentifier);
  1082. } else {
  1083. brandCacheData.put("brandId", queryBrand.getId() + "");
  1084. brandCacheData.put("brandName", queryBrand.getName());
  1085. goodsEntity.setBrandId(queryBrand.getId());
  1086. }
  1087. JedisUtil.setMap(uniqueIdentifier, brandCacheData, 0);
  1088. } else {
  1089. goodsEntity.setBrandId(Integer.parseInt(brandId));
  1090. }
  1091. }else{
  1092. Map<String, String> brandCacheData = new HashMap<>();
  1093. // 数据库里面查询
  1094. BrandEntity queryBrand = brandService.queryByUniqueIdentifier(uniqueIdentifier);
  1095. if (queryBrand == null) {
  1096. BrandEntity brandEntity = new BrandEntity();
  1097. brandEntity.setName(brandName);
  1098. brandEntity.setUniqueIdentifier(uniqueIdentifier);
  1099. brandEntity.setSimpleDesc(brandName);
  1100. brandEntity.setIsShow(1);
  1101. brandEntity.setIsNew(0);
  1102. brandService.save(brandEntity);
  1103. brandCacheData.put("brandId", brandEntity.getId() + "");
  1104. brandCacheData.put("brandName", brandName);
  1105. goodsEntity.setBrandId(brandEntity.getId());
  1106. } else {
  1107. brandCacheData.put("brandId", queryBrand.getId() + "");
  1108. brandCacheData.put("brandName", queryBrand.getName());
  1109. goodsEntity.setBrandId(queryBrand.getId());
  1110. }
  1111. JedisUtil.setMap(uniqueIdentifier, brandCacheData, 0);
  1112. }
  1113. }
  1114. private CategoryEntity initCategory(String firstLevel, int parentId, Dict.Level item) {
  1115. CategoryEntity firstCategoryEntity = new CategoryEntity();
  1116. firstCategoryEntity.setName(firstLevel);
  1117. firstCategoryEntity.setParentId(parentId);
  1118. firstCategoryEntity.setIsShow(1);
  1119. firstCategoryEntity.setFrontDesc(firstLevel);
  1120. firstCategoryEntity.setType(0);
  1121. firstCategoryEntity.setLevel(item.getItem());
  1122. return firstCategoryEntity;
  1123. }
  1124. @Override
  1125. @Transactional
  1126. public int uploadExcelByCover(List<GoodsDto> goodsEntityList, int exportDataType) {
  1127. SysUserEntity user = ShiroUtils.getUserEntity();
  1128. String merchSn = user.getMerchSn();
  1129. boolean isFail = false;
  1130. List<String> failSameSkuList = new ArrayList<>(), failHotGoodsSnList = new ArrayList<>(),
  1131. failSuppGoodsSnList = new ArrayList<>(),
  1132. failUnitGoodsSnList = new ArrayList<>(), failNationGoodsSnList = new ArrayList<>(),
  1133. failTypeGoodsSnList = new ArrayList<>(), failMerchGoodsSnList = new ArrayList<>(),
  1134. failMerchUserGoodsSnList = new ArrayList<>();
  1135. // List<String> failGoodsSnList = new ArrayList<>();
  1136. List<String> failGoodsTypeList = new ArrayList<>();
  1137. if (goodsEntityList != null && goodsEntityList.size() > 0) {
  1138. for (int i = 0; i < goodsEntityList.size(); i++) {
  1139. GoodsDto goodsDto = goodsEntityList.get(i);
  1140. GoodsEntity goodsEntity = new GoodsEntity();
  1141. Map<String, Object> valideDate = MapBeanUtil.fromObject(goodsDto);
  1142. ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
  1143. builder.put("goodsSn", "商品编码");
  1144. builder.put("thirdPartyMerchCode", "第三方商户代码");
  1145. builder.put("goodsBizType", "货品业务类型");
  1146. builder.put("name", "商品名称");
  1147. builder.put("isOnSaleStr", "上架");
  1148. builder.put("goodsUnit", "商品单位");
  1149. builder.put("isHotStr", "热销");
  1150. builder.put("prodBarcode", "产品条码");
  1151. builder.put("supplierName", "供应商");
  1152. builder.put("goodsNumber", "商品总库存");
  1153. R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
  1154. if (Integer.valueOf(r.get("code").toString()) != 0) {
  1155. throw new RRException(r.get("msg").toString());
  1156. } else {
  1157. if (!Dict.orderBizType.item_11.getItem().equals(goodsDto.getGoodsBizType())) {
  1158. // 海关信息,普通货物可不添加
  1159. builder.put("goodsRate", "商品税率");
  1160. builder.put("sku", "SKU");
  1161. // builder.put("brand", "产品品牌");
  1162. builder.put("unitName", "计量单位");
  1163. builder.put("oriCntName", "原产国");
  1164. builder.put("cusGoodsCode", "海关商品编码");
  1165. //builder.put("ciqProdModel", "国检规格型号");
  1166. // builder.put("cusDeclEle", "海关申报要素");
  1167. //builder.put("cusRecCode", "海关备案编号");
  1168. }
  1169. r = ValidatorUtil.isEmpty(builder.build(), valideDate);
  1170. if (Integer.valueOf(r.get("code").toString()) != 0) {
  1171. throw new RRException(r.get("msg").toString());
  1172. }
  1173. }
  1174. //业务类型校验
  1175. if(!Dict.orderBizType.item_11.getItem().equalsIgnoreCase(goodsDto.getGoodsBizType())){
  1176. if(!(Dict.orderBizType.item_02.getItem().equalsIgnoreCase(goodsDto.getGoodsBizType())
  1177. || Dict.orderBizType.item_10.getItem().equalsIgnoreCase(goodsDto.getGoodsBizType())
  1178. || Dict.orderBizType.item_00.getItem().equalsIgnoreCase(goodsDto.getGoodsBizType()))){
  1179. isFail = true;
  1180. failTypeGoodsSnList.add(goodsDto.getSku());
  1181. }
  1182. }
  1183. ThirdMerchantBizEntity thirdMerchantBizEntity = thirdMerchantBizDao.getThirdMerchangByCode(goodsDto.getThirdPartyMerchCode());
  1184. if(thirdMerchantBizEntity != null){
  1185. goodsEntity.setMerchSn(thirdMerchantBizEntity.getMerchSn());
  1186. goodsEntity.setThirdPartyMerchCode(thirdMerchantBizEntity.getThirdPartyMerchCode());
  1187. if(!user.getRoleType().equalsIgnoreCase(Dict.roleType.item_1.getItem())) {
  1188. if (!merchSn.equalsIgnoreCase(thirdMerchantBizEntity.getMerchSn())) {
  1189. isFail = true;
  1190. failMerchUserGoodsSnList.add(goodsDto.getGoodsSn());
  1191. }
  1192. }
  1193. }else{//商户不存在
  1194. isFail = true;
  1195. failMerchGoodsSnList.add(goodsDto.getGoodsSn());
  1196. }
  1197. //校验商品信息是否已存在
  1198. Map<String, Object> map = new HashMap<>();
  1199. map.put("isSame", "true");
  1200. map.put("sku", goodsDto.getSku());
  1201. map.put("goodsSn", goodsDto.getGoodsSn());
  1202. map.put("goodsBizType", goodsDto.getGoodsBizType());
  1203. // List<GoodsEntity> list = querySame(map);
  1204. // if (list != null && list.size() != 0) {
  1205. // isFail = true;
  1206. // if(goodsDto.getSku()!=null) {
  1207. // failSameSkuList.add(goodsDto.getSku());
  1208. // }
  1209. // failGoodsSnList.add(goodsDto.getGoodsSn());
  1210. // failGoodsTypeList.add(goodsDto.getGoodsBizType());
  1211. // }
  1212. //校验产品条码是否存在
  1213. goodsEntity.setProdBarcode(goodsDto.getProdBarcode());
  1214. //热销商品校验
  1215. if (Dict.orderBizType.item_02.getItem().equals(goodsDto.getGoodsBizType())
  1216. || Dict.orderBizType.item_10.getItem().equals(goodsDto.getGoodsBizType())) {
  1217. if(goodsDto.getIsHotStr().equalsIgnoreCase("1")){
  1218. isFail = true;
  1219. failHotGoodsSnList.add(goodsDto.getGoodsSn());
  1220. }
  1221. }
  1222. if(thirdMerchantBizEntity != null) {
  1223. SupplierEntity supplierEntity = supplierDao.queryObjectByName(goodsDto.getSupplierName(), thirdMerchantBizEntity.getMerchSn(),thirdMerchantBizEntity.getThirdPartyMerchCode());
  1224. if (supplierEntity == null) {
  1225. //导入没有查到供货商,执行新增
  1226. MerchEntity merchEntity = merchDao.findByMerchSn(thirdMerchantBizEntity.getMerchSn());
  1227. if(null == merchEntity || !StringUtils.isNotEmpty(goodsDto.getSupplierFlag())){
  1228. isFail = true;
  1229. failSuppGoodsSnList.add(goodsDto.getGoodsSn());
  1230. }
  1231. supplierEntity = new SupplierEntity();
  1232. supplierEntity.setLevelMerchSn(merchEntity.getMerchSn());
  1233. supplierEntity.setLevelMerchFlag(merchEntity.getMerchShortName());
  1234. supplierEntity.setThirdPartyMerchCode(thirdMerchantBizEntity.getThirdPartyMerchCode());
  1235. supplierEntity.setChildSupplierName(goodsDto.getSupplierName());
  1236. supplierEntity.setChildSupplierFlag(goodsDto.getSupplierFlag());
  1237. supplierEntity.setIsShow("0");
  1238. supplierDao.save(supplierEntity);
  1239. goodsEntity.setSupplierId(supplierEntity.getId());
  1240. } else {
  1241. goodsEntity.setSupplierId(supplierEntity.getId());
  1242. }
  1243. }
  1244. if (!Dict.orderBizType.item_11.getItem().equals(goodsDto.getGoodsBizType())) {
  1245. SysCusUnitCodeEntity sysCusUnitCodeEntity = sysCusUnitCodeDao.queryObjectByName(goodsDto.getUnitName());
  1246. if (sysCusUnitCodeEntity == null) {
  1247. isFail = true;
  1248. failUnitGoodsSnList.add(goodsDto.getGoodsSn());
  1249. } else {
  1250. goodsEntity.setUnitCode(sysCusUnitCodeEntity.getCode());
  1251. }
  1252. //原产国
  1253. SysCusNationCodeEntity sysCusNationCodeEntity = sysCusNationCodeDao.queryObjectByName(goodsDto.getOriCntName());
  1254. if (sysCusNationCodeEntity == null) {
  1255. isFail = true;
  1256. failNationGoodsSnList.add(goodsDto.getGoodsSn());
  1257. } else {
  1258. goodsEntity.setOriCntCode(sysCusNationCodeEntity.getCode());
  1259. }
  1260. if(goodsDto.getGoodsRate().indexOf("%") != -1){
  1261. goodsDto.setGoodsRate(goodsDto.getGoodsRate().substring(0, goodsDto.getGoodsRate().indexOf("%")));
  1262. }
  1263. goodsEntity.setGoodsRate(BigDecimal.valueOf(Double.valueOf(goodsDto.getGoodsRate())).divide(new BigDecimal(100), 4, BigDecimal.ROUND_HALF_UP));
  1264. }
  1265. goodsEntity.setIsOnSale(Integer.parseInt(goodsDto.getIsOnSaleStr()));
  1266. goodsEntity.setIsHot(Integer.parseInt(goodsDto.getIsHotStr()));
  1267. goodsEntity.setGoodsSn(goodsDto.getGoodsSn());
  1268. goodsEntity.setSku(goodsDto.getSku());
  1269. goodsEntity.setName(goodsDto.getName());
  1270. goodsEntity.setGoodsUnit(goodsDto.getGoodsUnit());
  1271. goodsEntity.setGoodsBizType(goodsDto.getGoodsBizType());
  1272. goodsEntity.setBrand(goodsDto.getBrand());
  1273. goodsEntity.setCusDeclEle(goodsDto.getCusDeclEle());
  1274. goodsEntity.setCusGoodsCode(goodsDto.getCusGoodsCode());
  1275. goodsEntity.setCusRecCode(goodsDto.getCusRecCode());
  1276. goodsEntity.setCiqProdModel(goodsDto.getCiqProdModel());
  1277. goodsEntity.setIsDelete(Integer.parseInt(Dict.isDelete.item_0.getItem()));
  1278. goodsEntity.setIsNew(0);
  1279. goodsEntity.setUpdateUserId(user.getUserId());
  1280. goodsEntity.setAddTime(new Date());
  1281. goodsEntity.setCreateTime(new Date());
  1282. goodsEntity.setUpdateTime(new Date());
  1283. goodsEntity.setModTime(new Date());
  1284. goodsEntity.setGoodsNumber(Integer.parseInt(goodsDto.getGoodsNumber()));
  1285. if(!isFail){
  1286. GoodsEntity goods = goodsDao.queryObjectBySnNew(goodsDto.getGoodsSn());
  1287. MngChangeEntity mngChangeEntity = new MngChangeEntity();
  1288. mngChangeEntity.setThirdPartyMerchCode(goodsEntity.getThirdPartyMerchCode());
  1289. mngChangeEntity.setChangeReason("更新商户商品总库存");
  1290. mngChangeEntity.setCreateTime(new Date());
  1291. mngChangeEntity.setModTime(new Date());
  1292. mngChangeEntity.setCreaterSn(user.getUsername());
  1293. mngChangeEntity.setModerSn(user.getUsername());
  1294. mngChangeEntity.setIsValid(0);
  1295. mngChangeEntity.setMerchSn(goodsEntity.getMerchSn());
  1296. if(goods!=null) {// 修改商品
  1297. mngChangeEntity.setOriginalNum(goods.getGoodsNumber());//原库存数
  1298. mngChangeEntity.setValidNum(goods.getGoodsNumber() + Integer.parseInt(goodsDto.getGoodsNumber()));//可用数
  1299. mngChangeEntity.setChangeNum(Integer.parseInt(goodsDto.getGoodsNumber()));//变化数
  1300. mngChangeEntity.setChangeType(Dict.changeType.item_3.getItem());
  1301. mngChangeEntity.setGoodsId(Integer.parseInt(String.valueOf(goods.getId())));
  1302. goodsEntity.setId(goods.getId());
  1303. goodsEntity.setGoodsNumber(goods.getGoodsNumber()+Integer.parseInt(goodsDto.getGoodsNumber()));
  1304. goodsDao.update(goodsEntity);
  1305. }else{
  1306. mngChangeEntity.setOriginalNum(0);//原库存数
  1307. mngChangeEntity.setValidNum(Integer.parseInt(goodsDto.getGoodsNumber()));//可用数
  1308. mngChangeEntity.setChangeNum(Integer.parseInt(goodsDto.getGoodsNumber()));//变化数
  1309. mngChangeEntity.setChangeType(Dict.changeType.item_2.getItem());
  1310. goodsDao.save(goodsEntity);
  1311. mngChangeEntity.setGoodsId(goodsEntity.getId().intValue());
  1312. }
  1313. mngChangeDao.save(mngChangeEntity);
  1314. // 修改产品
  1315. ProductEntity product = productDao.queryObjectByGoodsIdAndStoreId(String.valueOf(goodsEntity.getId()), "");
  1316. GoodsSpecificationEntity goodsSpecification = new GoodsSpecificationEntity();
  1317. // 普通货物暂不添加商品规格
  1318. if (!Dict.orderBizType.item_11.getItem().equals(goodsDto.getGoodsBizType())) {
  1319. // 添加商品规格
  1320. GoodsSpecificationEntity specificationEntity = goodsSpecificationDao.queryByGoodsId(goodsEntity.getId());
  1321. if(specificationEntity == null) {
  1322. goodsSpecification.setGoodsId(goodsEntity.getId());
  1323. goodsSpecification.setValue(goodsEntity.getCiqProdModel());
  1324. goodsSpecification.setSpecificationId(1);
  1325. goodsSpecificationDao.save(goodsSpecification);
  1326. }else {
  1327. goodsSpecification.setValue(goodsDto.getCiqProdModel());
  1328. goodsSpecification.setId(specificationEntity.getId());
  1329. goodsSpecificationDao.update(goodsSpecification);
  1330. }
  1331. if(product == null){
  1332. product = new ProductEntity();
  1333. product.setGoodsSn(goodsDto.getGoodsSn());
  1334. product.setGoodsId(goodsEntity.getId());
  1335. product.setGoodsDefault(0);
  1336. product.setGoodsNumber(goodsEntity.getGoodsNumber());
  1337. product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
  1338. product.setGoodsSpecificationIds(goodsSpecification.getId().toString());
  1339. productDao.save(product);
  1340. }else{
  1341. product.setGoodsSpecificationIds(goodsSpecification.getId().toString());
  1342. product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
  1343. productDao.update(product);
  1344. }
  1345. }
  1346. }
  1347. }
  1348. ExportExceptionDataEntity exportExceptionDataEntity = new ExportExceptionDataEntity();
  1349. exportExceptionDataEntity.setCreaterSn(user.getUserId().toString());
  1350. exportExceptionDataEntity.setUserId(user.getUserId().intValue());
  1351. exportExceptionDataEntity.setCreateTime(new Date());
  1352. exportExceptionDataEntity.setModTime(new Date());
  1353. exportExceptionDataEntity.setMerchSn(merchSn);
  1354. exportExceptionDataEntity.setStoreId(user.getStoreId());
  1355. if(exportDataType == 1) {
  1356. exportExceptionDataEntity.setExportDataType(Dict.exportDataType.item_1.getItem());
  1357. }else{
  1358. exportExceptionDataEntity.setExportDataType(Dict.exportDataType.item_2.getItem());
  1359. }
  1360. if(failMerchUserGoodsSnList != null && failMerchUserGoodsSnList.size() > 0){
  1361. exportExceptionDataEntity.setExportExceptionData("不能操作除了登录用户以外商户的商品,当前商户编号为【"+merchSn+"】,请检查商品编码【"+failMerchUserGoodsSnList+"】的商品信息");
  1362. goodsUtils.save(exportExceptionDataEntity);
  1363. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1364. }
  1365. if(failMerchGoodsSnList != null && failMerchGoodsSnList.size() > 0){
  1366. exportExceptionDataEntity.setExportExceptionData("第三方商户代码不存在,请在商城配置》第三方商户管理中维护,请检查商品编码【"+failMerchGoodsSnList+"】的商品信息,请先维护再继续操作!");
  1367. goodsUtils.save(exportExceptionDataEntity);
  1368. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1369. }
  1370. // if(failGoodsSnList != null && failGoodsSnList.size() > 0){
  1371. // if(failSameSkuList.size()>0) {
  1372. // exportExceptionDataEntity.setExportExceptionData("不能有重复的商品编码、sku信息!请检查商品编码【" + failGoodsSnList + "】,业务类型【" +
  1373. // failGoodsTypeList + "】,SKU【" + failSameSkuList + "】的商品信息");
  1374. // goodsUtils.save(exportExceptionDataEntity);
  1375. // throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1376. // }else{
  1377. // exportExceptionDataEntity.setExportExceptionData("不能有重复的商品编码、sku信息!请检查商品编码【" + failGoodsSnList + "】,业务类型【" +
  1378. // failGoodsTypeList + "】的商品信息");
  1379. // goodsUtils.save(exportExceptionDataEntity);
  1380. // throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1381. // }
  1382. // }
  1383. if(failTypeGoodsSnList != null && failTypeGoodsSnList.size() > 0){
  1384. exportExceptionDataEntity.setExportExceptionData("货品业务类型只能是【00保税备货、02保税补货、10保税展示】!请检查商品编码【"+failTypeGoodsSnList+"】的商品信息");
  1385. goodsUtils.save(exportExceptionDataEntity);
  1386. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1387. }
  1388. if(failHotGoodsSnList != null && failHotGoodsSnList.size() > 0){
  1389. exportExceptionDataEntity.setExportExceptionData("请检查业务类型为【保税补货或保税展示】的商品,商品编码【"+failHotGoodsSnList+"】的商品不能设置为热销!");
  1390. goodsUtils.save(exportExceptionDataEntity);
  1391. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1392. }
  1393. if(failSuppGoodsSnList != null && failSuppGoodsSnList.size() > 0){
  1394. exportExceptionDataEntity.setExportExceptionData("供应商信息请在商城配置》商品供应商中维护,供应商与商户信息对应,请检查该商品商户信息下的供应商是否维护,不存在的商品编码【" + failSuppGoodsSnList + "】");
  1395. goodsUtils.save(exportExceptionDataEntity);
  1396. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1397. }
  1398. if(failUnitGoodsSnList != null && failUnitGoodsSnList.size() > 0){
  1399. exportExceptionDataEntity.setExportExceptionData("计算单位信息请在商城配置》计算单位中维护,不存在的商品编码【" + failUnitGoodsSnList + "】");
  1400. goodsUtils.save(exportExceptionDataEntity);
  1401. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1402. }
  1403. if(failNationGoodsSnList != null && failNationGoodsSnList.size() > 0){
  1404. exportExceptionDataEntity.setExportExceptionData("原产国信息请在商城配置》原产国中维护,不存在的商品编码【" + failNationGoodsSnList + "】");
  1405. goodsUtils.save(exportExceptionDataEntity);
  1406. throw new RRException("导入数据异常,异常信息请在商品管理》》商品导入异常数据中查看检查");
  1407. }
  1408. }else{
  1409. throw new RRException("导入数据为空,或者检查商品编码数据是否为空");
  1410. }
  1411. return 1;
  1412. }
  1413. @Override
  1414. public GoodsDetailsDto queryGoodsDetailsByProdBarcode(String prodBarcode, String storeId,String sku) {
  1415. return goodsDao.queryGoodsDetailsByProdBarcode(prodBarcode,storeId,sku);
  1416. }
  1417. @Override
  1418. public GoodsPanoramaDto searchGoodsPanoramaDtoByKeyword(String keyword) {
  1419. return goodsDao.searchGoodsPanoramaDtoByKeyword(keyword);
  1420. }
  1421. @Override
  1422. public List<GoodsEntity> queryExportList(Map<String, Object> params) {
  1423. return goodsDao.queryExportList(params);
  1424. }
  1425. /**
  1426. * 查出pdf需要的需要
  1427. *
  1428. * @param sku 商品sku
  1429. * @param storeId
  1430. * @param prodBarcode
  1431. * @return
  1432. */
  1433. @Override
  1434. public PDFGoodsDto queryForPDFData(String sku, String storeId, String prodBarcode) {
  1435. return goodsDao.queryForPDFData(sku,storeId,prodBarcode);
  1436. }
  1437. /**
  1438. * 查询产品价格
  1439. * 改进后的逻辑:
  1440. * @param prodBarcode
  1441. * @param storeId
  1442. * @return
  1443. */
  1444. @Override
  1445. @Transactional
  1446. public synchronized Map<String,Object> calculateGoodsDetail(String prodBarcode, String storeId, String sku) {
  1447. /**
  1448. * 1.首先根据商品条码跟门店id查询是否有库存,没库存直接返回
  1449. */
  1450. GoodsDetailsDto goods = queryGoodsDetailsByProdBarcode(prodBarcode,storeId,sku);
  1451. if(goods == null) {
  1452. return null;
  1453. }
  1454. /*
  1455. * 2. 查询海仓仓库系统,判断库存是否足够
  1456. * */
  1457. sku = org.springframework.util.StringUtils.isEmpty(sku) ? goods.getSku() : sku;
  1458. String wareStockNumberKey = storeId + sku + prodBarcode;
  1459. String warehouseStockMapKey = Constants.WAREHOUSE_STOCK_MAP_KEY + "_" + storeId;
  1460. String redisCacheWareQuantity = JedisUtil.hget(warehouseStockMapKey, wareStockNumberKey);
  1461. Integer wareQuantity = 0;
  1462. Integer exitRegionNumber = goods.getExitRegionNumber();
  1463. String stockNum = goods.getStockNum();
  1464. Integer sellVolume = Objects.isNull(goods.getSellVolume()) ? 1 : goods.getSellVolume();
  1465. if (org.springframework.util.StringUtils.isEmpty(redisCacheWareQuantity)) {
  1466. /* -----------------↓--------------生产请打开注释-------------------↓------------ */
  1467. // WareQueryStockParamDTO wareQueryStockParamDTO = new WareQueryStockParamDTO();
  1468. // List<Criteria> criteriaList = new ArrayList<>();
  1469. // Criteria criteria = new Criteria();
  1470. // criteria.setItemId(goods.getWarehouseSysGoodId());
  1471. // criteria.setItemCode(goods.getSku());
  1472. // criteria.setOwnerCode(goods.getConsignorSn());
  1473. // criteria.setInventoryType(Constants.InventoryType.ZP.getType());
  1474. // criteria.setWarehouseCode(goods.getWarehouseSn());
  1475. // criteriaList.add(criteria);
  1476. // wareQueryStockParamDTO.setCriteriaList(criteriaList);
  1477. // String queryWarehouseStockResponse = null;
  1478. // try {
  1479. // queryWarehouseStockResponse = haiKongWarehouseTemplate.queryWarehouseStock(wareQueryStockParamDTO);
  1480. // } catch (Exception e) {
  1481. // log.error("sku:【{}】,调用海控仓库系统查询库存出现异常!", sku, e);
  1482. // throw new ServiceException(String.format("sku:【%s】,调用海控仓库系统查询库存出现异常!", sku));
  1483. // }
  1484. // log.info("调用海控仓库系统,请求参数,门店:{}、商品条码:{}、商品编码:{},响应数据:{}", storeId, prodBarcode, sku, queryWarehouseStockResponse);
  1485. // if (org.springframework.util.StringUtils.isEmpty(queryWarehouseStockResponse)) {
  1486. // log.error("调用库存系统接口出现错误!返回结果为空!");
  1487. // throw new ServiceException("调用库存系统接口出现错误!");
  1488. // }
  1489. // WareQueryStockResponseDTO wareQueryStockResponseDTO = JacksonUtil.fromStringJson(queryWarehouseStockResponse, WareQueryStockResponseDTO.class);
  1490. /* -----------------↑--------------生产请打开注释------------------↑------------- */
  1491. /* -----------------↓--------------测试数据,生产请注释-------------------↓------------ */
  1492. WareQueryStockResponseDTO wareQueryStockResponseDTO = new WareQueryStockResponseDTO();
  1493. wareQueryStockResponseDTO.setCode("0");
  1494. wareQueryStockResponseDTO.setFlag("success");
  1495. wareQueryStockResponseDTO.setMessage("");
  1496. WareQueryStockResponseDTO.WareQueryStockResponseItemDTO wareQueryStockResponseItemDTO = new WareQueryStockResponseDTO.WareQueryStockResponseItemDTO();
  1497. wareQueryStockResponseItemDTO.setQuantity(3);
  1498. wareQueryStockResponseItemDTO.setItemCode(sku);
  1499. wareQueryStockResponseItemDTO.setProduceCode(prodBarcode);
  1500. List<WareQueryStockResponseDTO.WareQueryStockResponseItemDTO> wareQueryStockResponseItemDTOS = new ArrayList<>();
  1501. wareQueryStockResponseItemDTOS.add(wareQueryStockResponseItemDTO);
  1502. wareQueryStockResponseDTO.setItems(wareQueryStockResponseItemDTOS);
  1503. /* -----------------↑--------------测试数据,生产请注释------------------↑------------- */
  1504. if (Objects.isNull(wareQueryStockResponseDTO)) {
  1505. // log.error("解析一步达库存系统响应数据出现错误!请求响应结果:{}", queryWarehouseStockResponse);
  1506. throw new ServiceException("解析一步达库存系统响应数据出现错误!");
  1507. }
  1508. // 校验库存
  1509. WareQueryStockResponseDTO.WareQueryStockResponseItemDTO itemDTO = wareQueryStockResponseDTO.getItems().get(0);
  1510. // 仓库可用库存
  1511. wareQuantity = itemDTO.getQuantity();
  1512. JedisUtil.hset(warehouseStockMapKey, wareStockNumberKey, String.valueOf(wareQuantity));
  1513. } else {
  1514. wareQuantity = Integer.parseInt(redisCacheWareQuantity);
  1515. }
  1516. // 保税仓库存 + 展销店库存 - 出区数 >= 购买数
  1517. if (!((wareQuantity + Integer.parseInt(stockNum) - exitRegionNumber) > sellVolume)) {
  1518. // 库存不足
  1519. log.error("商品条码:【{}】,sku:【{}】,门店库存:【{}】,保税仓库存:【{}】,出区数:【{}】,该商品仓库库存不足!", prodBarcode, sku, stockNum, wareQuantity, exitRegionNumber);
  1520. throw new ServiceException(String.format("商品条码:【%s】,sku:【%s】,门店库存:【%s】,保税仓库存:【%s】,出区数:【%s】,该商品仓库库存不足!", prodBarcode, sku, stockNum, wareQuantity, exitRegionNumber));
  1521. } else {
  1522. JedisUtil.hset(warehouseStockMapKey, wareStockNumberKey, String.valueOf(wareQuantity - sellVolume));
  1523. }
  1524. goods.setDiscountedPrice(new BigDecimal(0));
  1525. BigDecimal retailPrice = goods.getRetailPrice();
  1526. goods.setActualPaymentAmount(retailPrice.setScale(2,RoundingMode.HALF_UP));
  1527. Map<String,Object> skuActivitiesMap = new HashMap<>();
  1528. // 计算税费
  1529. GoodsEntity goodsEntity = goodsDao.queryByBarcodeAndSku(prodBarcode, goods.getGoodsSn());
  1530. BigDecimal tax = CalculateTax.calculateFinalTax(goodsEntity, goods.getActualPaymentAmount(),this).setScale(3,RoundingMode.HALF_UP);
  1531. goods.setGoodstaxes(tax.toString());
  1532. goods.setSellVolume(1);
  1533. skuActivitiesMap.put("goods",goods);
  1534. return skuActivitiesMap;
  1535. }
  1536. @Override
  1537. public List<Map<String,Object>> selectSkuDetails(String prodBarcode, String storeId) {
  1538. /**
  1539. * 1.首先根据商品条码跟门店id查询是否有库存,没库存直接返回
  1540. */
  1541. List<GoodsDetailsDto> goodsList = goodsDao.queryGoodsSkuList(prodBarcode,storeId);
  1542. List<Map<String,Object>> mapList = new ArrayList<>();
  1543. if (goodsList == null) {
  1544. return null;
  1545. }
  1546. for(GoodsDetailsDto goods : goodsList){
  1547. mapList.add(selectGetSkuDetails(goods,prodBarcode,storeId));
  1548. }
  1549. return mapList;
  1550. }
  1551. public Map<String,Object> selectGetSkuDetails(GoodsDetailsDto goods,String prodBarcode, String storeId) {
  1552. /**
  1553. * 1.首先根据商品条码跟门店id查询是否有库存,没库存直接返回
  1554. */
  1555. goods.setDiscountedPrice(new BigDecimal(0));
  1556. BigDecimal retailPrice = goods.getRetailPrice();
  1557. goods.setActualPaymentAmount(retailPrice.setScale(2,RoundingMode.HALF_UP));
  1558. Map<String,Object> skuActivitiesMap = new HashMap<>();
  1559. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  1560. /**
  1561. * 2.查询当前时间,该门店是否有活动,如果有活动,查询开启了哪些营销方式
  1562. * 参数: 当前时间 门店id
  1563. */
  1564. List<MkActivitiesEntity> mkActivitiesEntityList = mkActivitiesService.queryByNow(storeId,format.format(new Date()));
  1565. if(mkActivitiesEntityList == null || mkActivitiesEntityList.size() == 0){
  1566. // 计算税费
  1567. GoodsEntity goodsEntity = goodsDao.queryByBarcodeAndSku(prodBarcode, goods.getGoodsSn());
  1568. BigDecimal tax = CalculateTax.calculateFinalTax(goodsEntity,goods.getActualPaymentAmount(),this).setScale(3,RoundingMode.HALF_UP);
  1569. goods.setGoodstaxes(tax.toString());
  1570. skuActivitiesMap.put("goods",goods);
  1571. return skuActivitiesMap;
  1572. }
  1573. // 遍历活动集合,查询有哪些活动是开启的
  1574. boolean daily = false,coupon = false,combinationPrice = false
  1575. ,discount = false,fullGift = false,fullReduction = false
  1576. ,getOneFree = false,promotion = false,halfPrice = false;
  1577. List<String> topicList = new ArrayList<>(); // 记录有哪些营销活动的topic
  1578. Map<String,String> mkaIdMap = new HashMap<>(); // 记录topic跟mkaId的关系
  1579. // 将所有的营销活动新增到list中
  1580. for(MkActivitiesEntity mkActivitiesEntity : mkActivitiesEntityList){
  1581. topicList.add(mkActivitiesEntity.getMkaTopic());
  1582. String mkaId = mkaIdMap.putIfAbsent(mkActivitiesEntity.getMkaTopic(), mkActivitiesEntity.getMkaId()+"");
  1583. if(StringUtils.isNotEmpty(mkaId)){
  1584. // mkaId = "'"+mkaId + "','" + mkActivitiesEntity.getMkaId()+"'";
  1585. mkaId += String.format(",%s",mkActivitiesEntity.getMkaId());
  1586. mkaIdMap.put(mkActivitiesEntity.getMkaTopic(),mkaId);
  1587. }
  1588. }
  1589. // 判断有哪些营销活动
  1590. if(topicList.contains("zhjsp")) // 组合价
  1591. combinationPrice = true;
  1592. if(topicList.contains("dz")) // 打折
  1593. discount = true;
  1594. if(topicList.contains("mz")) //满赠
  1595. fullGift = true;
  1596. if(topicList.contains("mj")) // 满减
  1597. fullReduction = true;
  1598. if(topicList.contains("mysy")) // 买一送一
  1599. getOneFree = true;
  1600. if(topicList.contains("rchd")) // 日常活动
  1601. daily = true;
  1602. if(topicList.contains("yhq")) // 优惠券
  1603. coupon = true;
  1604. if(topicList.contains("lscx")) // 临时促销
  1605. promotion = true;
  1606. if(topicList.contains("drjbj"))
  1607. halfPrice = true;
  1608. // 获取未优惠前的商品价格
  1609. retailPrice = goods.getRetailPrice();
  1610. // 根据条码查询商品品牌名称 mall_brand mall_product_store_rela mall_goods
  1611. String brandName = goods.getBrand();
  1612. /**
  1613. * 优先级:临时促销 》买一送一=满赠 》 组合价=日常活动 》 打折=满减 》 优惠券
  1614. */
  1615. /**
  1616. * 组合价的做法就是将参与组合的条码带到收银端
  1617. * 现根据营销活动id跟条形码,查询有哪些参与该条码组合的商品
  1618. *
  1619. *
  1620. * TODO
  1621. */
  1622. if(combinationPrice){
  1623. String mkaId = mkaIdMap.get("zhjsp");
  1624. Map<String,Object> param = new HashMap<>();
  1625. param.put("mkaId",mkaId);
  1626. param.put("prodBarcode",prodBarcode);
  1627. List<MkActivitiesCombinationPriceEntity> combinationPriceList = combinationPriceService.queryList(param);
  1628. if(combinationPriceList != null && combinationPriceList.size() > 0) {
  1629. Map<String, List<MkActivitiesCombinationPriceEntity>> collect =
  1630. combinationPriceList.stream().collect(Collectors.groupingBy(MkActivitiesCombinationPriceEntity::getCombinationType));
  1631. skuActivitiesMap.put("zhjsp",collect);
  1632. }
  1633. }
  1634. format = new SimpleDateFormat("yyyy-MM-dd");
  1635. String nowTime = format.format(new Date());
  1636. /**
  1637. * 满减可能是跟着条码,也可能跟着品牌
  1638. * 根据商品品牌跟商品条码去查询是否有优惠金额
  1639. *
  1640. * 满足金额 购买商品条码 赠品条码
  1641. *
  1642. */
  1643. Map<String,Object> fullReductionMap = new HashMap<>();
  1644. if(fullReduction){
  1645. String mkaId = mkaIdMap.get("mj");
  1646. MkActivitiesFullReductionEntity fullReductionEntity = fullReductionService.queryByCodeOrBrand(mkaId,prodBarcode,brandName,nowTime);
  1647. if(fullReductionEntity != null) {
  1648. if(!StringUtils.isNullOrEmpty(fullReductionEntity.getProductBrand())){
  1649. // 跟着品牌走
  1650. fullReductionMap.put(brandName,fullReductionEntity);
  1651. }else{
  1652. // 跟着条码走
  1653. fullReductionMap.put(fullReductionEntity.getBarcode(), fullReductionEntity);
  1654. }
  1655. skuActivitiesMap.put("mj",fullReductionMap);
  1656. }
  1657. }
  1658. /**
  1659. * 满赠可能是跟着条码,也可能跟着品牌
  1660. * 根据商品品牌跟商品条码去查询是否有满赠
  1661. * 1.先扫买的商品,然后查询出赠送的商品条码
  1662. * 2.先扫赠的商品,然后查询出符合条件的商品条码或者品牌
  1663. * 满足的金额 购买的商品条码或者品牌 赠送的商品条码
  1664. * TODO
  1665. */
  1666. Map<String,Object> fullGiftMap = new HashMap<>();
  1667. if(fullGift){
  1668. String mkaId = mkaIdMap.get("mz");
  1669. MkActivitiesFullGiftEntity giftEntity = fullGiftService.queryByCodeOrBrand(mkaId,prodBarcode,brandName,nowTime);
  1670. if(giftEntity != null) {
  1671. if(giftEntity.getProductBrand() != null){
  1672. // 跟着品牌走
  1673. fullGiftMap.put(brandName,giftEntity);
  1674. }else{
  1675. // 跟着条码走
  1676. fullGiftMap.put(giftEntity.getBarcode(), giftEntity);
  1677. }
  1678. skuActivitiesMap.put("mz",fullGiftMap);
  1679. }
  1680. }
  1681. /**
  1682. * 买一送一可能是跟着条码,也可能跟着品牌
  1683. * 根据商品品牌跟商品条码去查询是否有送的商品
  1684. * 有两个场景
  1685. * 1.先扫买的商品,然后查询出赠送的商品条码
  1686. * 2.先扫赠的商品,然后查询出符合条件的商品条码或者品牌
  1687. * 所以就需要一个map
  1688. * key为购买的商品条码或者商品品牌 value为赠送的商品条码
  1689. * 但是品牌是根据条码查询的,所以最终map里面的结构是
  1690. * key 商品条码 value 赠品条码
  1691. */
  1692. if(getOneFree){
  1693. String mkaId = mkaIdMap.get("mysy");
  1694. MkActivitiesGetOneFreeGoodsEntity getOneFreeGoodsEntity = getOneFreeGoodsService.queryByCodeOrBrand(mkaId,prodBarcode,brandName);
  1695. if(getOneFreeGoodsEntity != null){
  1696. if("无".equals(getOneFreeGoodsEntity.getProductBrand())){
  1697. getOneFreeGoodsEntity.setBrand(false);
  1698. skuActivitiesMap.put("mysy",getOneFreeGoodsEntity);
  1699. }else{
  1700. getOneFreeGoodsEntity.setBrand(true);
  1701. skuActivitiesMap.put("mysy",getOneFreeGoodsEntity);
  1702. }
  1703. }
  1704. }
  1705. // 第二份半价
  1706. if(halfPrice){
  1707. String mkaId = mkaIdMap.get("drjbj");
  1708. MkActivitiesHalfPriceEntity activitiesHalfPriceEntity = halfPriceService.queryByCodeOrBrand(mkaId,prodBarcode);
  1709. if(activitiesHalfPriceEntity != null){
  1710. skuActivitiesMap.put("drjbj",activitiesHalfPriceEntity);
  1711. }
  1712. }
  1713. // --------------------------------------------------------------------------------------
  1714. /**
  1715. * 优惠券跟着条形码走,一般是设置一个标识,然后最后输入优惠券码后,减扣对应的标识,所以返回一个map数组
  1716. */
  1717. if(coupon){
  1718. String mkaId = mkaIdMap.get("yhq");
  1719. MkActivitiesCouponEntity couponEntity = couponService.queryByBarCode(mkaId,prodBarcode,nowTime);
  1720. if(couponEntity != null){ // 优惠券码,优惠金额
  1721. Map<String,Object> returnMap = new HashMap<>();
  1722. returnMap.put(couponEntity.getCouponSn() , couponEntity.getCouponPrice());
  1723. skuActivitiesMap.put("yhq",returnMap);
  1724. }
  1725. }
  1726. /**
  1727. * 打折的价格是与条形码对应的,所以需要根据条形码和营销方式id去查询活动价格
  1728. */
  1729. if(discount){
  1730. String mkaId = mkaIdMap.get("dz");
  1731. MkActivitiesDiscountEntity discountEntity = discountService.queryByBarCode(mkaId,prodBarcode);
  1732. // TODO 可能会直接替代产品价格
  1733. if (discountEntity != null) {
  1734. goods.setActualPaymentAmount(discountEntity.getActivityPrice());
  1735. goods.setRetailPrice(discountEntity.getActivityPrice());
  1736. goods.setActivity("打折");
  1737. }
  1738. }
  1739. /**
  1740. * 日常活动跟着条形码走,优先级比临时促销低,但是高于正常价格
  1741. */
  1742. if(daily){
  1743. String mkaId = mkaIdMap.get("rchd");
  1744. MkDailyActivitiesEntity dailyActivitiesEntity = dailyActivitiesService.queryByBarCode(mkaId,prodBarcode);
  1745. if(dailyActivitiesEntity != null){
  1746. goods.setActualPaymentAmount(dailyActivitiesEntity.getActivityPrice());
  1747. goods.setRetailPrice(dailyActivitiesEntity.getActivityPrice());
  1748. goods.setActivity("日常活动");
  1749. }
  1750. }
  1751. /**
  1752. * 临时促销跟着条形码走,优先级应该最高,所以排到了最下面
  1753. */
  1754. if(promotion){
  1755. String mkaId = mkaIdMap.get("lscx");
  1756. MkActivitiesPromotionEntity promotionEntity = promotionService.queryByBarCode(mkaId,prodBarcode);
  1757. // 如果该商品存在临时促销,直接替换活动价格
  1758. if(promotionEntity != null){
  1759. goods.setActualPaymentAmount(promotionEntity.getActivityPrice());
  1760. goods.setRetailPrice(promotionEntity.getActivityPrice());
  1761. goods.setActivity("临时促销");
  1762. }
  1763. }
  1764. // 计算税费
  1765. GoodsEntity goodsEntity = goodsDao.queryByBarcodeAndSku(prodBarcode, goods.getGoodsSn());
  1766. BigDecimal tax = CalculateTax.calculateFinalTax(goodsEntity,goods.getActualPaymentAmount(),this).setScale(3,RoundingMode.HALF_UP);
  1767. goods.setGoodstaxes(tax.toString());
  1768. skuActivitiesMap.put("goods",goods);
  1769. return skuActivitiesMap;
  1770. }
  1771. /**
  1772. * 根据条形码查询商品
  1773. *
  1774. * @param barCode
  1775. * @return
  1776. */
  1777. @Override
  1778. public GoodsEntity queryByBarcode(String barCode) {
  1779. return goodsDao.queryByBarcode(barCode);
  1780. }
  1781. /**
  1782. * 根据sku查询商品
  1783. *
  1784. * @param sku
  1785. * @return
  1786. */
  1787. @Override
  1788. public GoodsEntity queryBySku(String sku) {
  1789. return goodsDao.queryBySku(sku);
  1790. }
  1791. /* @Override
  1792. @Transactional
  1793. public int uploadExcel(MultipartFile file) {
  1794. SysUserEntity user = ShiroUtils.getUserEntity();
  1795. List<String[]> list = ExcelImport.getExcelData(file);
  1796. // 取门店名称
  1797. StoreEntity storeEntity = storeDao.queryObjectByName(list.get(0)[3]);
  1798. if (null == storeEntity) {
  1799. return 0;
  1800. }
  1801. //去除表头两行、底部合计
  1802. if (list != null && list.size() > 3) {
  1803. ProductStoreRelaEntity storeRelaEntity;
  1804. ProductEntity productEntity;
  1805. for (int i = 2; i < list.size() - 1; i++) {
  1806. String[] item = list.get(i);
  1807. String goodsSn = item[0];
  1808. productEntity = productDao.queryObjectBySn(goodsSn);
  1809. if (StringUtils.isNullOrEmpty(goodsSn)) {
  1810. continue;
  1811. }
  1812. if (null == productEntity || null == productEntity.getId()) {
  1813. continue;
  1814. }
  1815. storeRelaEntity = productStoreRelaDao.queryByStoreIdProductId(storeEntity.getId(), productEntity.getId());
  1816. if (null != storeRelaEntity && null != storeRelaEntity.getId()) {
  1817. storeRelaEntity.setRetailPrice(new BigDecimal(item[6]));
  1818. storeRelaEntity.setStockNum(Integer.valueOf(item[3].replace(".00", "")));
  1819. storeRelaEntity.setStockPrice(new BigDecimal(item[4]));
  1820. productStoreRelaDao.update(storeRelaEntity);
  1821. } else {
  1822. storeRelaEntity = new ProductStoreRelaEntity();
  1823. storeRelaEntity.setGoodsId(productEntity.getGoodsId());
  1824. storeRelaEntity.setProductId(productEntity.getId());
  1825. storeRelaEntity.setRetailPrice(new BigDecimal(item[6]));
  1826. storeRelaEntity.setMarketPrice(new BigDecimal(item[6]));
  1827. storeRelaEntity.setStockNum(Integer.valueOf(item[3]));
  1828. storeRelaEntity.setStockPrice(new BigDecimal(item[4]));
  1829. storeRelaEntity.setStoreId(storeEntity.getId());
  1830. productStoreRelaDao.save(storeRelaEntity);
  1831. }
  1832. }
  1833. }
  1834. return 1;
  1835. }*/
  1836. /**
  1837. * 全量拉取
  1838. */
  1839. @Override
  1840. public void syncOmsHsCodeTask() {
  1841. syncOmsHsCodeSegment(goodsDao.queryNonSyncOmsHsCodeSkuList());
  1842. }
  1843. /**
  1844. * 全量计算
  1845. */
  1846. @Override
  1847. public void syncGoodsRateTask() {
  1848. // 查询出所有商品
  1849. List<GoodsEntity> allGoodsList = goodsDao.queryAllList(null,null);
  1850. syncGoodsRate(allGoodsList);
  1851. }
  1852. /**
  1853. * 还原园区库存
  1854. *
  1855. * @param goodsEntities 数据
  1856. */
  1857. @Override
  1858. public int restoreBatch(List<GoodsEntity> goodsEntities) {
  1859. return goodsDao.restoreBatch(goodsEntities);
  1860. }
  1861. /**
  1862. * 查询库存及sku的信息,查询出来的库存是门店的库存
  1863. *
  1864. * @param prodBarcode 条码
  1865. * @param storeId 门店id
  1866. * @param sku sku
  1867. * @return 商品信息
  1868. */
  1869. @Override
  1870. public GoodsEntity queryGoodsStockByBarcodeAndStoreIdAndSku(String prodBarcode, Integer storeId, String sku) {
  1871. return goodsDao.queryGoodsStockByBarcodeAndStoreIdAndSku(prodBarcode, storeId, sku);
  1872. }
  1873. /**
  1874. * 查询库存及sku的信息,查询出来的库存是门店的库存
  1875. *
  1876. * @param queryGoodsVOList 查询条件,包含商品条码、sku、门店id
  1877. * @return 商品信息
  1878. */
  1879. @Override
  1880. public List<GoodsEntity> queryGoodsStockByQueryGoodsVoList(List<QueryGoodsVO> queryGoodsVOList) {
  1881. return goodsDao.queryGoodsStockByQueryGoodsVoList(queryGoodsVOList);
  1882. }
  1883. /**
  1884. * 选择拉取
  1885. * @param ids
  1886. */
  1887. @Override
  1888. public void syncOmsHsCodeGoode(List<Integer> ids) {
  1889. syncOmsHsCodeSegment(goodsDao.syncOmsHsCodeGoode(ids));
  1890. }
  1891. /**
  1892. * 选择计算
  1893. * @param ids
  1894. */
  1895. @Override
  1896. public void syncGoodsRateGoode(List<Integer> ids) {
  1897. syncGoodsRate(goodsDao.syncGoodsRateGoode(ids));
  1898. }
  1899. public void syncOmsHsCodeSegment(List<String> skuList) {
  1900. int segmentLimitSize = 300;
  1901. if (skuList.size() < segmentLimitSize) {
  1902. this.syncOmsHsCode(skuList);
  1903. } else {
  1904. int segmentSize = skuList.size() % segmentLimitSize == 0 ? skuList.size() / segmentLimitSize : (skuList.size() / segmentLimitSize) + 1;
  1905. List<List<String>> segment = new ArrayList<>();
  1906. for (int i = 0; i < segmentSize; i++) {
  1907. int end = (i == segmentSize - 1) ? skuList.size() : i * segmentLimitSize + segmentLimitSize;
  1908. segment.add(new ArrayList<>(skuList.subList(i * segmentLimitSize, end)));
  1909. }
  1910. for (List<String> tmp : segment) {
  1911. this.syncOmsHsCode(tmp);
  1912. }
  1913. }
  1914. }
  1915. @Transactional
  1916. public void syncOmsHsCode(List<String> skuList) {
  1917. if (Objects.nonNull(skuList) && skuList.size()>0){
  1918. String result = HttpUtil.get("https://oms.ds-bay.com/oms-controller-mgt/pdProductRecord/hsCodeMapBySkuList?skuList=" + skuList);
  1919. //String result = HttpUtil.get("http://183.3.221.143:8080/oms-controller-mgt/pdProductRecord/hsCodeMapBySkuList?skuList=" + skuList);
  1920. //String result = HttpUtil.get("http://127.0.0.1:8080/oms-controller-mgt/pdProductRecord/hsCodeMapBySkuList?skuList=" + skuList);
  1921. Map<String,Map> map = JSON.parseObject(result, Map.class);
  1922. for (String sku : skuList) {
  1923. Map skuInfoMap = map.get(sku);
  1924. if (Objects.nonNull(skuInfoMap)){
  1925. String hsCodeName = (String) skuInfoMap.get("cusGoodsName");
  1926. String hsCode = (String) skuInfoMap.get("cusGoodsCode");
  1927. String prodName = (String) skuInfoMap.get("prodName");
  1928. String prodRecordName = (String) skuInfoMap.get("prodRecordName");
  1929. BigDecimal impConsumTaxRate = (BigDecimal) skuInfoMap.get("impConsumTaxRate");
  1930. BigDecimal valueAddedTaxRate = (BigDecimal) skuInfoMap.get("valueAddedTaxRate");
  1931. BigDecimal legalUnit1Qty = (BigDecimal) skuInfoMap.get("legalUnit1Qty");
  1932. BigDecimal legalUnit2Qty = (BigDecimal) skuInfoMap.get("legalUnit2Qty");
  1933. BigDecimal cosmThresholdValue = (BigDecimal) skuInfoMap.get("cosmThresholdValue");
  1934. if (StringUtils.isNotEmpty(hsCodeName) && StringUtils.isNotEmpty(hsCode)){
  1935. List<GoodsEntity> goodsEntityList = goodsDao.queryListBySku(sku);
  1936. if (Objects.nonNull(goodsEntityList) && goodsEntityList.size()>0){
  1937. for (GoodsEntity goodsEntity : goodsEntityList) {
  1938. goodsEntity.setHsCode(hsCode);
  1939. goodsEntity.setHsCodeName(hsCodeName);
  1940. goodsEntity.setImpConsumTaxRate(impConsumTaxRate);
  1941. goodsEntity.setValueAddedTaxRate(valueAddedTaxRate);
  1942. goodsEntity.setIsSyncHsCode("1");
  1943. goodsEntity.setLegalUnit1Qty(legalUnit1Qty);
  1944. goodsEntity.setProdName(prodName);
  1945. goodsEntity.setProdRecordName(prodRecordName);
  1946. goodsEntity.setLegalUnit2Qty(legalUnit2Qty);
  1947. goodsEntity.setCosmThresholdValue(cosmThresholdValue);
  1948. goodsDao.update(goodsEntity);
  1949. }
  1950. }
  1951. }
  1952. }
  1953. }
  1954. }
  1955. }
  1956. @Transactional
  1957. public void syncGoodsRate(List<GoodsEntity> allGoodsList) {
  1958. String storeId = "163"; // 市场部说所有门店价格一致,并且活动一致,可直接取其中一个门店价格进行计算税率
  1959. for (GoodsEntity goodsEntity : allGoodsList) {
  1960. // 同步过海关商品编码才可以算税率
  1961. if ("1".equals(goodsEntity.getIsSyncHsCode())) {
  1962. // Map<String, Object> goodsDetailMap = calculateGoodsDetail(goodsEntity.getProdBarcode(), storeId);
  1963. // if (Objects.nonNull(goodsDetailMap)) {
  1964. // GoodsDetailsDto goods = (GoodsDetailsDto) goodsDetailMap.get("goods");
  1965. // 顾客给的钱 就是税后价
  1966. // BigDecimal actualPaymentAmount = goods.getActualPaymentAmount(); //随便拿一个门店的当前时间的价格,如果有活动价会使用活动价
  1967. // BigDecimal calculateTax = CalculateTax.calculateTax(goodsEntity, actualPaymentAmount);// 税费
  1968. // BigDecimal goodsRate = calculateTax.divide(actualPaymentAmount, 4, BigDecimal.ROUND_HALF_UP);
  1969. BigDecimal goodsRate =CalculateTax.calculateGoodsRate(goodsEntity);
  1970. goodsEntity.setGoodsRate(goodsRate);
  1971. goodsEntity.setIsSyncHsCode("0");
  1972. goodsDao.update(goodsEntity);
  1973. // }
  1974. }
  1975. }
  1976. }
  1977. @Override
  1978. public void updateTaxErrorRecord(TaxErrorRecordEntity taxErrorRecordEntity) {
  1979. taxErrorRecordDao.update(taxErrorRecordEntity);
  1980. }
  1981. @Override
  1982. public void insertTaxErrorRecord(TaxErrorRecordEntity taxErrorRecordEntity) {
  1983. taxErrorRecordDao.save(taxErrorRecordEntity);
  1984. }
  1985. @Override
  1986. public void updateByEntity(GoodsEntity updateGoods) {
  1987. goodsDao.update(updateGoods);
  1988. }
  1989. @Override
  1990. public void checkGoodsPrice(SysUserEntity user) {
  1991. Map<String, BigDecimal> hsCodeMap = CalculateTax.hsCodeMap;
  1992. Set<String> hdCodeSet = hsCodeMap.keySet();
  1993. List<Map<String,String>> errorPriceSkuList = new LinkedList<>();
  1994. // 保存各种异常价格对象集合
  1995. List<ShopErrorPriceRecordEntity> shopErrorPriceRecordEntities = new LinkedList<>();
  1996. // 查询所有特殊化妆品的商品
  1997. List<GoodsEntity> queryGoodsDetails = goodsDao.querySpecialCosmetics(hdCodeSet);
  1998. for (GoodsEntity goodsEntity : queryGoodsDetails) {
  1999. calculateShopPrice(user,goodsEntity,errorPriceSkuList,shopErrorPriceRecordEntities);
  2000. calculateDailyPrice(user,goodsEntity,errorPriceSkuList,shopErrorPriceRecordEntities);
  2001. calculeatePromotion(user,goodsEntity,errorPriceSkuList,shopErrorPriceRecordEntities);
  2002. }
  2003. if(shopErrorPriceRecordEntities.size() != 0){
  2004. shopErrorPriceRecordService.saveBatch(shopErrorPriceRecordEntities);
  2005. }
  2006. }
  2007. @Override
  2008. public List<GoodsEntity> queryAllList(Integer page,Integer pageSize) {
  2009. return goodsDao.queryAllList(page,pageSize);
  2010. }
  2011. private void calculeatePromotion(SysUserEntity user, GoodsEntity goods, List<Map<String, String>> errorPriceSkuList, List<ShopErrorPriceRecordEntity> shopErrorPriceRecordEntities) {
  2012. String prodBarcode = goods.getProdBarcode();
  2013. String storeId = goods.getStoreId()+"";
  2014. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  2015. /**
  2016. * 2.查询当前时间,该门店是否有活动,如果有活动,查询开启了哪些营销方式
  2017. * 参数: 当前时间 门店id
  2018. */
  2019. MkActivitiesPromotionEntity promotionEntity = promotionService.queryByDateAndBarcode(storeId,format.format(new Date()),"lscx",prodBarcode);
  2020. // 如果该商品存在临时促销,直接替换活动价格
  2021. if(promotionEntity == null){
  2022. return ;
  2023. }
  2024. goods.setActualPaymentAmount(promotionEntity.getActivityPrice());
  2025. goods.setRetailPrice(promotionEntity.getActivityPrice());
  2026. goods.setActivity("临时促销");
  2027. try {
  2028. CalculateTax.calculateFinalTax(goods,goods.getActualPaymentAmount(),this).setScale(3,RoundingMode.HALF_UP);
  2029. } catch (Exception e) {
  2030. ShopErrorPriceRecordEntity shopErrorPriceRecordEntity = new ShopErrorPriceRecordEntity();
  2031. shopErrorPriceRecordEntity.setMerchSn(goods.getMerchSn());
  2032. shopErrorPriceRecordEntity.setThirdMerchSn(goods.getThirdPartyMerchCode());
  2033. shopErrorPriceRecordEntity.setShopSn(goods.getStoreId()+"");
  2034. shopErrorPriceRecordEntity.setPriceType(3); // 3 是临时促销
  2035. shopErrorPriceRecordEntity.setSku(goods.getSku());
  2036. shopErrorPriceRecordEntity.setBarcode(goods.getProdBarcode());
  2037. shopErrorPriceRecordEntity.setVerifier(user.getUserId()+"");
  2038. shopErrorPriceRecordEntity.setCheckTime(new Date());
  2039. shopErrorPriceRecordEntity.setCreaterSn(user.getUserId()+"");
  2040. shopErrorPriceRecordEntity.setCreateTime(new Date());
  2041. shopErrorPriceRecordEntity.setCurrentPrice(goods.getActualPaymentAmount().toString());
  2042. if(e.getMessage().contains("-")){
  2043. String suggestPrice = e.getMessage().split("-")[0];
  2044. String highestPrice = e.getMessage().split("-")[1];
  2045. String lowestPrice = e.getMessage().split("-")[2];
  2046. shopErrorPriceRecordEntity.setSuggestedPrice(suggestPrice);
  2047. shopErrorPriceRecordEntity.setLowestPrice(lowestPrice);
  2048. shopErrorPriceRecordEntity.setHighestPrice(highestPrice);
  2049. }
  2050. shopErrorPriceRecordEntity.setMkaId(promotionEntity.getMkaId());
  2051. shopErrorPriceRecordEntities.add(shopErrorPriceRecordEntity);
  2052. }
  2053. }
  2054. private void calculateDailyPrice(SysUserEntity user, GoodsEntity goods, List<Map<String, String>> errorPriceSkuList, List<ShopErrorPriceRecordEntity> shopErrorPriceRecordEntities) {
  2055. String prodBarcode = goods.getProdBarcode();
  2056. String storeId = goods.getStoreId()+"";
  2057. /**
  2058. * 2.查询当前时间,该门店是否有活动,如果有活动,查询开启了哪些营销方式
  2059. * 参数: 当前时间 门店id
  2060. */
  2061. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  2062. /**
  2063. * 日常活动跟着条形码走,优先级比临时促销低,但是高于正常价格
  2064. */
  2065. MkDailyActivitiesEntity dailyActivitiesEntity = dailyActivitiesService.queryByDateAndBarcode(storeId,format.format(new Date()),"rchd",prodBarcode);
  2066. if(dailyActivitiesEntity == null){
  2067. return ;
  2068. }
  2069. goods.setActualPaymentAmount(dailyActivitiesEntity.getActivityPrice());
  2070. goods.setRetailPrice(dailyActivitiesEntity.getActivityPrice());
  2071. goods.setActivity("日常活动");
  2072. // 计算税费
  2073. try {
  2074. CalculateTax.calculateFinalTax(goods,goods.getActualPaymentAmount(),this).setScale(3,RoundingMode.HALF_UP);
  2075. } catch (Exception e) {
  2076. // 记录有异常的sku
  2077. ShopErrorPriceRecordEntity shopErrorPriceRecordEntity = new ShopErrorPriceRecordEntity();
  2078. shopErrorPriceRecordEntity.setMerchSn(goods.getMerchSn());
  2079. shopErrorPriceRecordEntity.setThirdMerchSn(goods.getThirdPartyMerchCode());
  2080. shopErrorPriceRecordEntity.setShopSn(goods.getStoreId()+"");
  2081. shopErrorPriceRecordEntity.setPriceType(2); // 2 是日常活动
  2082. shopErrorPriceRecordEntity.setSku(goods.getSku());
  2083. shopErrorPriceRecordEntity.setBarcode(goods.getProdBarcode());
  2084. shopErrorPriceRecordEntity.setVerifier(user.getUserId()+"");
  2085. shopErrorPriceRecordEntity.setCheckTime(new Date());
  2086. shopErrorPriceRecordEntity.setCreaterSn(user.getUserId()+"");
  2087. shopErrorPriceRecordEntity.setCreateTime(new Date());
  2088. shopErrorPriceRecordEntity.setCurrentPrice(goods.getActualPaymentAmount().toString());
  2089. if(e.getMessage().contains("-")){
  2090. String suggestPrice = e.getMessage().split("-")[0];
  2091. String highestPrice = e.getMessage().split("-")[1];
  2092. String lowestPrice = e.getMessage().split("-")[2];
  2093. shopErrorPriceRecordEntity.setSuggestedPrice(suggestPrice);
  2094. shopErrorPriceRecordEntity.setLowestPrice(lowestPrice);
  2095. shopErrorPriceRecordEntity.setHighestPrice(highestPrice);
  2096. }
  2097. shopErrorPriceRecordEntity.setMkaId(dailyActivitiesEntity.getMkaId());
  2098. shopErrorPriceRecordEntities.add(shopErrorPriceRecordEntity);
  2099. }
  2100. }
  2101. private void calculateShopPrice(SysUserEntity user, GoodsEntity goods, List<Map<String, String>> errorPriceSkuList, List<ShopErrorPriceRecordEntity> shopErrorPriceRecordEntities) {
  2102. goods.setDiscountedPrice(new BigDecimal(0));
  2103. BigDecimal retailPrice = goods.getRetailPrice();
  2104. goods.setActualPaymentAmount(retailPrice.setScale(2,RoundingMode.HALF_UP));
  2105. // 计算税费
  2106. try {
  2107. CalculateTax.calculateFinalTax(goods,goods.getActualPaymentAmount(),this).setScale(3,RoundingMode.HALF_UP);
  2108. } catch (Exception e) {
  2109. // 记录有异常的sku
  2110. ShopErrorPriceRecordEntity shopErrorPriceRecordEntity = new ShopErrorPriceRecordEntity();
  2111. shopErrorPriceRecordEntity.setMerchSn(goods.getMerchSn());
  2112. shopErrorPriceRecordEntity.setThirdMerchSn(goods.getThirdPartyMerchCode());
  2113. shopErrorPriceRecordEntity.setShopSn(goods.getStoreId()+"");
  2114. shopErrorPriceRecordEntity.setPriceType(1); // 1 门店商品价格
  2115. shopErrorPriceRecordEntity.setSku(goods.getSku());
  2116. shopErrorPriceRecordEntity.setBarcode(goods.getProdBarcode());
  2117. shopErrorPriceRecordEntity.setVerifier(user.getUserId()+"");
  2118. shopErrorPriceRecordEntity.setCheckTime(new Date());
  2119. shopErrorPriceRecordEntity.setCreaterSn(user.getUserId()+"");
  2120. shopErrorPriceRecordEntity.setCreateTime(new Date());
  2121. shopErrorPriceRecordEntity.setCurrentPrice(goods.getActualPaymentAmount().toString());
  2122. if(e.getMessage().contains("-")){
  2123. String suggestPrice = e.getMessage().split("-")[0];
  2124. String highestPrice = e.getMessage().split("-")[1];
  2125. String lowestPrice = e.getMessage().split("-")[2];
  2126. shopErrorPriceRecordEntity.setSuggestedPrice(suggestPrice);
  2127. shopErrorPriceRecordEntity.setLowestPrice(lowestPrice);
  2128. shopErrorPriceRecordEntity.setHighestPrice(highestPrice);
  2129. }
  2130. shopErrorPriceRecordEntities.add(shopErrorPriceRecordEntity);
  2131. }
  2132. }
  2133. /**
  2134. * 根据产品条码和sku查询保税展示补货的商品
  2135. *
  2136. * @param prodBarcode 商品条码
  2137. * @param sku sku
  2138. * @return 商品信息
  2139. */
  2140. @Override
  2141. public GoodsEntity queryGoodsInfoByProductBarcodeAndSku(String prodBarcode, String sku) {
  2142. return goodsDao.queryGoodsInfoByProductBarcodeAndSku(prodBarcode, sku);
  2143. }
  2144. @Override
  2145. public int updateStockNumberByProductCodeAndSku(GoodsEntity goodsEntity) {
  2146. return goodsDao.updateStockNumberByProductCodeAndSku(goodsEntity);
  2147. }
  2148. }