GoodsServiceImpl.java 138 KB

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