GoodsServiceImpl.java 124 KB

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