GoodsServiceImpl.java 124 KB

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