GoodsServiceImpl.java 119 KB

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