GoodsServiceImpl.java 119 KB

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