GoodsServiceImpl.java 123 KB

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