|  |  |  | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.metadata.IPage; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import org.apache.commons.lang3.StringUtils; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.context.annotation.Lazy; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | import org.springframework.transaction.annotation.Transactional; | 
|---|
|  |  |  | import org.springframework.util.CollectionUtils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.lang.reflect.Field; | 
|---|
|  |  |  | import java.lang.reflect.Method; | 
|---|
|  |  |  | import java.math.BigDecimal; | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.UUID; | 
|---|
|  |  |  | 
|---|
|  |  |  | * @date 2023/09/08 11:39 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Service | 
|---|
|  |  |  | @Slf4j | 
|---|
|  |  |  | public class BaseDataServiceImpl implements BaseDataService { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private BaseDataMapper baseDataMapper; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private MultifileMapper multifileMapper; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | @Lazy | 
|---|
|  |  |  | private SystemDictDataBiz systemDictDataBiz; | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | public static   boolean IS_RUNNING = false; | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional | 
|---|
|  |  |  | public   void dealBaseData() { | 
|---|
|  |  |  | if(IS_RUNNING){ | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | log.info("开始处理源数据=================start==================="); | 
|---|
|  |  |  | IS_RUNNING = true; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | 
|---|
|  |  |  | param.setStatus(Constants.ZERO); | 
|---|
|  |  |  | List<BaseData> allLis=findList(param); | 
|---|
|  |  |  | if(allLis==null || allLis.size() ==0){ | 
|---|
|  |  |  |  | 
|---|
|  |  |  | log.info("开始处理源数据=================end========无待处理数据==========="); | 
|---|
|  |  |  | return ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Brand brand = new Brand(); | 
|---|
|  |  |  | 
|---|
|  |  |  | p.setIsdeleted(Constants.ZERO); | 
|---|
|  |  |  | //查询全部类别属性信息 | 
|---|
|  |  |  | List<BaseCateParam> paramList = baseCateParamMapper.selectList(new QueryWrapper<>(p)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //默認加价系数 | 
|---|
|  |  |  | BigDecimal rate = systemDictDataBiz.getGoodsPriceRate(); | 
|---|
|  |  |  | OssModel ossModel = initOssModel(); | 
|---|
|  |  |  | Date date = new Date(); | 
|---|
|  |  |  | for(BaseData data : allLis){ | 
|---|
|  |  |  | 
|---|
|  |  |  | goods.setStatus(Constants.ZERO); | 
|---|
|  |  |  | goods.setShortPinyin(PinYinUtil.getFirstSpell(goods.getName())); | 
|---|
|  |  |  | goods.setPinyin(PinYinUtil.getFullSpell(goods.getName())); | 
|---|
|  |  |  | goods.setPrice(data.getSaleprice()); | 
|---|
|  |  |  | goods.setZdPrice(data.getPrice()); | 
|---|
|  |  |  | //处理品牌数据 | 
|---|
|  |  |  | Brand bModel = getBrandModelFromList(data.getBrand(),brandList); | 
|---|
|  |  |  | 
|---|
|  |  |  | goods.setBrandId(bModel.getId()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //处理列表数据 | 
|---|
|  |  |  | BaseCategory cModel = getCateModelFromList(data.getBrand(),ossModel,data.getCategoryImg(), cateList); | 
|---|
|  |  |  | if(bModel!= null){ | 
|---|
|  |  |  | goods.setBrandId(bModel.getId()); | 
|---|
|  |  |  | BaseCategory cModel = getCateModelFromList(rate,data.getCategory(),ossModel,data.getCategoryImg(), cateList); | 
|---|
|  |  |  | if(cModel!= null){ | 
|---|
|  |  |  | goods.setCategoryId(cModel.getId()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | goods.setImgurl(getOssImgurl(ossModel, data.getImgurl())); | 
|---|
|  |  |  | goods.setPrice(Constants.formatBigdecimal(data.getSaleprice()).multiply(Constants.formatBigdecimal(cModel.getPriceRate()))); | 
|---|
|  |  |  | //插入新商品数据 | 
|---|
|  |  |  | baseGoodsMapper.insert(goods); | 
|---|
|  |  |  | //处理分类属性 | 
|---|
|  |  |  | dealCateParamAll(cModel.getId(),goods.getId(),data,paramList); | 
|---|
|  |  |  | //处理背景图 | 
|---|
|  |  |  | dealBgImgAll(ossModel,goods.getId(),data); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | data.setEditDate(new Date()); | 
|---|
|  |  |  | data.setStatus(Constants.ONE); | 
|---|
|  |  |  | //更新数据处理状态 | 
|---|
|  |  |  | baseDataMapper.updateById(data); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | log.info("开始处理源数据=================success================处理数据共("+allLis.size()+")个==="); | 
|---|
|  |  |  | }catch (Exception e){ | 
|---|
|  |  |  |  | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | log.info("开始处理源数据=================error==================="+e.getMessage()); | 
|---|
|  |  |  | }finally { | 
|---|
|  |  |  | IS_RUNNING = false; | 
|---|
|  |  |  | log.info("开始处理源数据=================end==================="); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private void dealBgImgAll(OssModel ossModel, Integer id, BaseData data) { | 
|---|
|  |  |  | int i =0; | 
|---|
|  |  |  | i += addImg(i,ossModel,id,data.getBgImg()); | 
|---|
|  |  |  | i += addImg(i,ossModel,id,data.getBgImg2()); | 
|---|
|  |  |  | addImg(i,ossModel,id,data.getBgImg3()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private int addImg(int i,OssModel ossModel, Integer id,String bgImg) { | 
|---|
|  |  |  | if(StringUtils.isNotBlank(bgImg)){ | 
|---|
|  |  |  | String img =getOssImgurl(ossModel,bgImg); | 
|---|
|  |  |  | if(StringUtils.isNotBlank(img)){ | 
|---|
|  |  |  | Multifile f = new Multifile(); | 
|---|
|  |  |  | f.setFileurl(img); | 
|---|
|  |  |  | f.setObjType(Constants.ZERO); | 
|---|
|  |  |  | f.setName(bgImg.substring(bgImg.lastIndexOf("/")+1)); | 
|---|
|  |  |  | f.setType(Constants.ZERO); | 
|---|
|  |  |  | f.setObjId(id); | 
|---|
|  |  |  | f.setCreateDate(new Date()); | 
|---|
|  |  |  | f.setIsdeleted(Constants.ZERO); | 
|---|
|  |  |  | f.setSortnum(i); | 
|---|
|  |  |  | multifileMapper.insert(f); | 
|---|
|  |  |  | return  1; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return  0; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private OssModel initOssModel() { | 
|---|
|  |  |  | 
|---|
|  |  |  | model.setAccessKey(systemDictDataBiz.queryByCode(Constants.OSS, Constants.ACCESS_KEY).getCode()); | 
|---|
|  |  |  | model.setBucket(systemDictDataBiz.queryByCode(Constants.OSS, Constants.BUCKETNAME).getCode()); | 
|---|
|  |  |  | model.setCateFolder(systemDictDataBiz.queryByCode(Constants.OSS, Constants.CATEGORY_IMG).getCode()); | 
|---|
|  |  |  | model.setAccessKey(systemDictDataBiz.queryByCode(Constants.OSS, Constants.GOODS_IMG).getCode()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | model.setGoodsFolder(systemDictDataBiz.queryByCode(Constants.OSS, Constants.GOODS_IMG).getCode()); | 
|---|
|  |  |  | return model; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | String val =(String) item.invoke(data); | 
|---|
|  |  |  | dealCateParam(cateId,goodsId,val.toString(),cateList); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | throw new RuntimeException(e); | 
|---|
|  |  |  | //                    throw new RuntimeException(e); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | if(StringUtils.isBlank(attr)){ | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //        商品名称:火星人F30BX | 
|---|
|  |  |  | //如果属性不为空,处理属性数据 | 
|---|
|  |  |  | int index  = attr.indexOf(":"); | 
|---|
|  |  |  | if(index <=0){ | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | String attrName = attr.substring(0,index); | 
|---|
|  |  |  | if(StringUtils.isBlank(attrName) || StringUtils.equals(attrName, "商品名称") || StringUtils.equals(attrName, "商品编号")){ | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | String val = attr.substring(index+1); | 
|---|
|  |  |  | BaseCateParam cate = getCateParamModelFromList(cateId,attrName, list); | 
|---|
|  |  |  | BaseGoodsParam p = new BaseGoodsParam(); | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private Brand getBrandModelFromList(String name, List<Brand> brandList) { | 
|---|
|  |  |  | if(StringUtils.isBlank(name)){ | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(brandList!=null && brandList.size()>0){ | 
|---|
|  |  |  | for(Brand brand : brandList){ | 
|---|
|  |  |  | if(StringUtils.equals(name,brand.getName())){ | 
|---|
|  |  |  | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private BaseCategory getCateModelFromList(String name,OssModel ossModel, String img,List<BaseCategory> list) { | 
|---|
|  |  |  | private BaseCategory getCateModelFromList(BigDecimal dRate,String name, OssModel ossModel, String img, List<BaseCategory> list) { | 
|---|
|  |  |  | if(StringUtils.isBlank(name)){ | 
|---|
|  |  |  | return  null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(list!=null && list.size()>0){ | 
|---|
|  |  |  | for(BaseCategory p : list){ | 
|---|
|  |  |  | if(StringUtils.equals(name,p.getName())){ | 
|---|
|  |  |  | 
|---|
|  |  |  | BaseCategory model = new BaseCategory(); | 
|---|
|  |  |  | model.setCreateDate(new Date()); | 
|---|
|  |  |  | model.setName(name); | 
|---|
|  |  |  | model.setPriceRate(dRate); | 
|---|
|  |  |  | model.setIsdeleted(Constants.ZERO); | 
|---|
|  |  |  | model.setPinyin(PinYinUtil.getFullSpell(model.getName())); | 
|---|
|  |  |  | model.setShortPinyin(PinYinUtil.getFirstSpell(model.getName())); | 
|---|
|  |  |  | model.setImgurl(getOssImgurl(ossModel,img)); | 
|---|
|  |  |  | //插入新的品牌信息 | 
|---|
|  |  |  | baseCategoryMapper.insert(model); | 
|---|