|  |  | 
 |  |  | package com.xzx.gc.shop.service; | 
 |  |  |  | 
 |  |  | import cn.hutool.core.bean.BeanUtil; | 
 |  |  | import cn.hutool.core.collection.CollUtil; | 
 |  |  | import cn.hutool.core.convert.Convert; | 
 |  |  | import cn.hutool.core.date.DateTime; | 
 |  |  | import cn.hutool.core.date.DateUnit; | 
 |  |  | import cn.hutool.core.date.DateUtil; | 
 |  |  | import cn.hutool.core.util.ObjectUtil; | 
 |  |  | import cn.hutool.core.util.StrUtil; | 
 |  |  | import com.fasterxml.jackson.databind.DeserializationFeature; | 
 |  |  | import com.fasterxml.jackson.databind.ObjectMapper; | 
 |  |  | import com.github.pagehelper.PageHelper; | 
 |  |  | 
 |  |  | import tk.mybatis.mapper.entity.Example; | 
 |  |  |  | 
 |  |  | import javax.annotation.Resource; | 
 |  |  | import java.util.ArrayList; | 
 |  |  | import java.util.HashMap; | 
 |  |  | import java.util.List; | 
 |  |  | import java.util.Map; | 
 |  |  | import java.util.*; | 
 |  |  |  | 
 |  |  | @Service | 
 |  |  | @Transactional | 
 |  |  | 
 |  |  |     ScoreGoodsImagesMapper scoreGoodsImagesMapper; | 
 |  |  |     @Resource | 
 |  |  |     ScoreGoodsStyleMapper scoreGoodsStyleMapper; | 
 |  |  |     @Resource | 
 |  |  |     ScoreGoodsSearchMapper scoreGoodsSearchMapper; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private MqUtil mqUtil; | 
 |  |  | 
 |  |  |         scoreGoodsCategory.setParentId(model.getParentId()); | 
 |  |  |         scoreGoodsCategory.setCreatedBy(model.getCreatedBy()); | 
 |  |  |         scoreGoodsCategory.setCreatedTime(model.getCreatedTime()); | 
 |  |  |         scoreGoodsCategoryMapper.insert(scoreGoodsCategory); | 
 |  |  |         scoreGoodsCategoryMapper.insertSelective(scoreGoodsCategory); | 
 |  |  |         return scoreGoodsCategory.getId(); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | 
 |  |  |         PageHelper.startPage(model.getPage(), model.getLimit()); | 
 |  |  |  | 
 |  |  |         List<QueryGoodsListVo> maps = scoreGoodsMapper.queryGoodsList(name,delFlag); | 
 |  |  |         if(CollUtil.isNotEmpty(maps)){ | 
 |  |  |             for(QueryGoodsListVo queryGoodsListVo : maps){ | 
 |  |  |                 long id = queryGoodsListVo.getId(); | 
 |  |  |                 Integer stock = scoreGoodsSkuMapper.selectScoreGoodsSkuMapperByGoodId(id); | 
 |  |  |                 queryGoodsListVo.setStock(stock); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         PageInfo pageInfo = new PageInfo(maps); | 
 |  |  |         int count = Convert.toInt(pageInfo.getTotal()); | 
 |  |  |         Map<String, Object> map = new HashMap<>(); | 
 |  |  | 
 |  |  |         objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | 
 |  |  |         ScoreGoods scoreGoods = objectMapper.convertValue(model, ScoreGoods.class); | 
 |  |  |         scoreGoods.setDelFlag(Convert.toShort(Constants.DEL_NOT_FLAG)); | 
 |  |  |         scoreGoodsMapper.insert(scoreGoods); | 
 |  |  |         scoreGoodsMapper.insertSelective(scoreGoods); | 
 |  |  |         //样式信息 | 
 |  |  |         List<ScoreGoodsStyleDto> scoreGoodsStyleDtos = model.getScoreGoodsStyleDtos(); | 
 |  |  |         List<ScoreGoodsStyleDto> scoreGoodsStyleDtos = model.getScoreGoodsStyles(); | 
 |  |  |         if(CollUtil.isNotEmpty(scoreGoodsStyleDtos)){ | 
 |  |  |             for(ScoreGoodsStyleDto scoreGoodsStyleDto : scoreGoodsStyleDtos){ | 
 |  |  |                 ScoreGoodsStyle scoreGoodsStyle = objectMapper.convertValue(scoreGoodsStyleDto, ScoreGoodsStyle.class); | 
 |  |  |                 scoreGoodsStyle.setGoodsId(scoreGoods.getId()); | 
 |  |  |                 scoreGoodsStyle.setDelFlag(Convert.toShort(Constants.DEL_NOT_FLAG)); | 
 |  |  |                 scoreGoodsStyleMapper.insert(scoreGoodsStyle); | 
 |  |  |                 scoreGoodsStyleMapper.insertSelective(scoreGoodsStyle); | 
 |  |  |                 //规格信息 | 
 |  |  |                 List<ScoreGoodsSkuDto> scoreGoodsSkuDtos = scoreGoodsStyleDto.getScoreGoodsSkuDtos(); | 
 |  |  |                 List<ScoreGoodsSkuDto> scoreGoodsSkuDtos = scoreGoodsStyleDto.getScoreGoodsSkus(); | 
 |  |  |                 if(CollUtil.isNotEmpty(scoreGoodsSkuDtos)){ | 
 |  |  |                     for(ScoreGoodsSkuDto scoreGoodsSkuDto : scoreGoodsSkuDtos){ | 
 |  |  |                         ScoreGoodsSku scoreGoodsSku = objectMapper.convertValue(scoreGoodsSkuDto, ScoreGoodsSku.class); | 
 |  |  |                         scoreGoodsSku.setGoodsId(scoreGoods.getId()); | 
 |  |  |                         scoreGoodsSku.setStyleId(scoreGoodsStyle.getId()); | 
 |  |  |                         scoreGoodsSku.setDelFlag(Convert.toShort(Constants.DEL_NOT_FLAG)); | 
 |  |  |                         scoreGoodsSkuMapper.insert(scoreGoodsSku); | 
 |  |  |                         scoreGoodsSkuMapper.insertSelective(scoreGoodsSku); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  | 
 |  |  |             ObjectMapper objectMapper = new ObjectMapper(); | 
 |  |  |             objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | 
 |  |  |             viewGoodsVo = objectMapper.convertValue(scoreGoods, ViewGoodsVo.class); | 
 |  |  | //            viewGoodsVo.setCreateTime(scoreGoods.getCreatedTime().toString()); | 
 |  |  |             //轮播图 | 
 |  |  |             List<String> goodsImages = scoreGoodsImagesMapper.selectScoreGoodsImagesByGoodsId(id); | 
 |  |  |             viewGoodsVo.setGoodsImages(goodsImages); | 
 |  |  | 
 |  |  |                     //获取规格 | 
 |  |  |                     Long styleId = goodsStyle.getId(); | 
 |  |  |                     List<ScoreGoodsSkuVo> goodsSkus = scoreGoodsSkuMapper.selectScoreGoodsSkuByGoodsIdAndStyleId(id,styleId); | 
 |  |  |                     scoreGoodsStyleVo.setScoreGoodsSkuVos(goodsSkus); | 
 |  |  |                     scoreGoodsStyleVo.setScoreGoodsSkus(goodsSkus); | 
 |  |  |                     scoreGoodsStyleVos.add(scoreGoodsStyleVo); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |             viewGoodsVo.setScoreGoodsStyleVos(scoreGoodsStyleVos); | 
 |  |  |             viewGoodsVo.setScoreGoodsStyles(scoreGoodsStyleVos); | 
 |  |  |         } | 
 |  |  |         return viewGoodsVo; | 
 |  |  |     } | 
 |  |  | 
 |  |  |     public void updateGoods(UpdateGoodsDto model) { | 
 |  |  |         ObjectMapper objectMapper = new ObjectMapper(); | 
 |  |  |         objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | 
 |  |  |         ScoreGoods scoreGoods = objectMapper.convertValue(model, ScoreGoods.class); | 
 |  |  | //        ScoreGoods scoreGoods = objectMapper.convertValue(model, ScoreGoods.class); | 
 |  |  |         ScoreGoods scoreGoods = BeanUtil.copyProperties(model, ScoreGoods.class); | 
 |  |  |         log.info("抢购时间:{}, {}", scoreGoods.getQgStartTime(), scoreGoods.getQgEndTime()); | 
 |  |  |         scoreGoodsMapper.updateByPrimaryKey(scoreGoods); | 
 |  |  |  | 
 |  |  |         //轮播图 | 
 |  |  | 
 |  |  |                 scoreGoodsImages.setImageUrl(goodImage); | 
 |  |  |                 scoreGoodsImages.setGoodsId(scoreGoods.getId()); | 
 |  |  |                 scoreGoodsImages.setDelFlag(Convert.toShort(Constants.DEL_NOT_FLAG)); | 
 |  |  |                 scoreGoodsImagesMapper.insert(scoreGoodsImages); | 
 |  |  |                 scoreGoodsImagesMapper.insertSelective(scoreGoodsImages); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         List<ScoreGoodsStyleDto> scoreGoodsStyleDtos = model.getScoreGoodsStyleDtos(); | 
 |  |  |         List<ScoreGoodsStyleDto> scoreGoodsStyleDtos = model.getScoreGoodsStyles(); | 
 |  |  |         if(CollUtil.isNotEmpty(scoreGoodsStyleDtos)){ | 
 |  |  |             Example exampleStyle = new Example(ScoreGoodsStyle.class); | 
 |  |  |             Example.Criteria criteriaStyle = exampleStyle.createCriteria(); | 
 |  |  |             criteriaStyle.andEqualTo("goodsId",scoreGoods.getId()); | 
 |  |  |             scoreGoodsStyleMapper.deleteByExample(exampleImages); | 
 |  |  |  | 
 |  |  |             Example exampleSku = new Example(ScoreGoodsSku.class); | 
 |  |  |             Example.Criteria criteriaSku = exampleSku.createCriteria(); | 
 |  |  |             criteriaSku.andEqualTo("goodsId",scoreGoods.getId()); | 
 |  |  |             scoreGoodsSkuMapper.deleteByExample(exampleImages); | 
 |  |  |  | 
 |  |  |             for(ScoreGoodsStyleDto scoreGoodsStyleDto : scoreGoodsStyleDtos){ | 
 |  |  |                 ScoreGoodsStyle scoreGoodsStyle = new ScoreGoodsStyle(); | 
 |  |  |                 scoreGoodsStyle.setId(scoreGoodsStyleDto.getId()); | 
 |  |  |                 scoreGoodsStyle.setName(scoreGoodsStyleDto.getName()); | 
 |  |  |                 scoreGoodsStyle.setGoodsId(scoreGoods.getId()); | 
 |  |  |                 scoreGoodsStyleMapper.updateByPrimaryKeySelective(scoreGoodsStyle); | 
 |  |  |                 scoreGoodsStyleMapper.insertSelective(scoreGoodsStyle); | 
 |  |  |                 //规格 | 
 |  |  |                 List<ScoreGoodsSkuDto> scoreGoodsSkuDtos = scoreGoodsStyleDto.getScoreGoodsSkuDtos(); | 
 |  |  |                 List<ScoreGoodsSkuDto> scoreGoodsSkuDtos = scoreGoodsStyleDto.getScoreGoodsSkus(); | 
 |  |  |                 if(CollUtil.isNotEmpty(scoreGoodsSkuDtos)){ | 
 |  |  |                     for(ScoreGoodsSkuDto scoreGoodsSkuDto : scoreGoodsSkuDtos){ | 
 |  |  |                         ScoreGoodsSku scoreGoodsSku = objectMapper.convertValue(scoreGoodsSkuDto, ScoreGoodsSku.class); | 
 |  |  |                         scoreGoodsSkuMapper.updateByPrimaryKey(scoreGoodsSku); | 
 |  |  |                         scoreGoodsSku.setGoodsId(scoreGoods.getId()); | 
 |  |  |                         scoreGoodsSku.setStyleId(scoreGoodsStyle.getId()); | 
 |  |  |                         scoreGoodsSkuMapper.insertSelective(scoreGoodsSku); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  | 
 |  |  |         }else{ | 
 |  |  |             ScoreGoods scoreGoods = new ScoreGoods(); | 
 |  |  |             scoreGoods.setIsQg(ScoreGoods.ISQG_NO); | 
 |  |  |             scoreGoods.setQgEndTime(null); | 
 |  |  |             scoreGoods.setQgStartTime(null); | 
 |  |  |             Example exampleGoods = new Example(ScoreGoods.class); | 
 |  |  |             Example.Criteria criteriaGoods = exampleGoods.createCriteria(); | 
 |  |  |             criteriaGoods.andEqualTo("id",id); | 
 |  |  | 
 |  |  |  | 
 |  |  |         PageHelper.startPage(xcxGoodsListDto.getPageNo(), xcxGoodsListDto.getPageSize()); | 
 |  |  |         List<XcxGoodsListVo> data = scoreGoodsMapper.selectXcxGoodsList(xcxGoodsListDto); | 
 |  |  |  | 
 |  |  |         if(StrUtil.isNotEmpty(xcxGoodsListDto.getName())){ | 
 |  |  |             Example exampleSearch = new Example(ScoreGoodsSearch.class); | 
 |  |  |             Example.Criteria criteriaSearch = exampleSearch.createCriteria(); | 
 |  |  |             criteriaSearch.andEqualTo("contents",xcxGoodsListDto.getName()); | 
 |  |  |             criteriaSearch.andEqualTo("userId",xcxGoodsListDto.getUserId()); | 
 |  |  |             List<ScoreGoodsSearch> scoreGoodsSearches = scoreGoodsSearchMapper.selectByExample(exampleSearch); | 
 |  |  |             if(CollUtil.isEmpty(scoreGoodsSearches)){ | 
 |  |  |                 ScoreGoodsSearch scoreGoodsSearch = new ScoreGoodsSearch(); | 
 |  |  |                 scoreGoodsSearch.setUserId(xcxGoodsListDto.getUserId()); | 
 |  |  |                 scoreGoodsSearch.setContents(xcxGoodsListDto.getName()); | 
 |  |  |                 scoreGoodsSearch.setCreatedTime(new Date()); | 
 |  |  |                 scoreGoodsSearchMapper.insert(scoreGoodsSearch); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         return new PageInfo<>(data); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public XcxGoodsDetailVo findGoodsDetails(Long id) { | 
 |  |  |         XcxGoodsDetailVo data = scoreGoodsMapper.selectGoodsDetailsById(id); | 
 |  |  |         XcxGoodsListDto dto = new XcxGoodsListDto(); | 
 |  |  |         dto.setId(id); | 
 |  |  |         List<XcxGoodsListVo> list = scoreGoodsMapper.selectXcxGoodsList(dto); | 
 |  |  |  | 
 |  |  |         List<String> images = scoreGoodsImagesMapper.selectScoreGoodsImagesByGoodsId(id); | 
 |  |  |         data.setImages(images); | 
 |  |  |         if (CollUtil.isNotEmpty(list)) { | 
 |  |  |             data.setQuantity(list.get(0).getQuantity()); | 
 |  |  |             data.setStock(list.get(0).getStock()); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         if (ScoreGoods.ISQG_YES.equals(data.getIsQg())) { | 
 |  |  |             if (new Date().before(data.getQgStartTime())) { | 
 |  |  |                 data.setIsStartQg(2); | 
 |  |  |             } else { | 
 |  |  |                 data.setIsStartQg(1); | 
 |  |  |                 Long remain = DateUtil.between(new Date(), data.getQgEndTime(), DateUnit.SECOND, false); | 
 |  |  |                 data.setRemainTime(remain); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         return data; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public List<GoodsSearchVo> goodsSearch(GoodsSearchDto goodsSearchDto) { | 
 |  |  |         return scoreGoodsSearchMapper.goodsSearch(goodsSearchDto); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public void delGoodsSearch(Long id, String userId) { | 
 |  |  |         Example exampleSearch = new Example(ScoreGoodsSearch.class); | 
 |  |  |         Example.Criteria criteriaSearch = exampleSearch.createCriteria(); | 
 |  |  |         criteriaSearch.andEqualTo("id",id); | 
 |  |  |         criteriaSearch.andEqualTo("userId",userId); | 
 |  |  |         scoreGoodsSearchMapper.deleteByExample(exampleSearch); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public void delAllSearch(String userId) { | 
 |  |  |         Example exampleSearch = new Example(ScoreGoodsSearch.class); | 
 |  |  |         Example.Criteria criteriaSearch = exampleSearch.createCriteria(); | 
 |  |  |         criteriaSearch.andEqualTo("userId",userId); | 
 |  |  |         scoreGoodsSearchMapper.deleteByExample(exampleSearch); | 
 |  |  |     } | 
 |  |  | } |