package com.xzx.gc.shop.service; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.convert.Convert; import cn.hutool.core.util.ObjectUtil; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.xzx.gc.common.constant.Constants; import com.xzx.gc.common.utils.MqUtil; import com.xzx.gc.entity.*; import com.xzx.gc.model.admin.GoodsCategoryModel; import com.xzx.gc.shop.dto.*; import com.xzx.gc.shop.mapper.*; import com.xzx.gc.shop.vo.*; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; 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; @Service @Transactional @Slf4j public class GoodsService { @Resource ScoreGoodsCategoryMapper scoreGoodsCategoryMapper; @Resource ScoreGoodsMapper scoreGoodsMapper; @Resource ScoreGoodsSkuMapper scoreGoodsSkuMapper; @Resource ScoreGoodsImagesMapper scoreGoodsImagesMapper; @Resource ScoreGoodsStyleMapper scoreGoodsStyleMapper; @Autowired private MqUtil mqUtil; public Map queryGoodsCategoryList(QueryGoodsCategoryListDto model) { String name = model.getName(); GoodsCategoryModel goodsCategoryModel = new GoodsCategoryModel(); goodsCategoryModel.setName(name); PageHelper.startPage(model.getPage(), model.getLimit()); List maps = scoreGoodsCategoryMapper.queryGoodsCategoryList(goodsCategoryModel); PageInfo pageInfo = new PageInfo(maps); int count = Convert.toInt(pageInfo.getTotal()); Map map = new HashMap<>(); map.put("data", maps); map.put("count", count); map.put("code", 0); return map; } public Long addGoodsCategory(GoodsCategoryModel model) { ScoreGoodsCategory scoreGoodsCategory = new ScoreGoodsCategory(); scoreGoodsCategory.setName(model.getName()); scoreGoodsCategory.setCategoryIden(model.getCategoryIden()); scoreGoodsCategory.setParentId(model.getParentId()); scoreGoodsCategory.setCreatedBy(model.getCreatedBy()); scoreGoodsCategory.setCreatedTime(model.getCreatedTime()); scoreGoodsCategoryMapper.insert(scoreGoodsCategory); return scoreGoodsCategory.getId(); } public void deleteGoodsCategory(long id) { scoreGoodsCategoryMapper.deleteByPrimaryKey(id); } public ViewGoodsCategoryVo viewGoodsCategoryById(long id) { ScoreGoodsCategory scoreGoodsCategory = scoreGoodsCategoryMapper.selectByPrimaryKey(id); ViewGoodsCategoryVo viewGoodsCategoryVo = new ViewGoodsCategoryVo(); viewGoodsCategoryVo.setId(scoreGoodsCategory.getId()); viewGoodsCategoryVo.setName(scoreGoodsCategory.getName()); viewGoodsCategoryVo.setCategoryIden(scoreGoodsCategory.getCategoryIden()); viewGoodsCategoryVo.setParentId(scoreGoodsCategory.getParentId()); return viewGoodsCategoryVo; } public void updateGoodsCategory(GoodsCategoryModel model) { long id = model.getId(); ScoreGoodsCategory scoreGoodsCategory = scoreGoodsCategoryMapper.selectByPrimaryKey(id); scoreGoodsCategory.setName(model.getName()); scoreGoodsCategory.setCategoryIden(model.getCategoryIden()); scoreGoodsCategory.setParentId(model.getParentId()); scoreGoodsCategoryMapper.updateByPrimaryKey(scoreGoodsCategory); } public Map queryGoodsList(QueryGoodsListDto model) { String name = model.getName(); int delFlag = model.getDelFlag(); ScoreGoods scoreGoods = new ScoreGoods(); scoreGoods.setName(name); scoreGoods.setDelFlag(Convert.toShort(delFlag)); PageHelper.startPage(model.getPage(), model.getLimit()); List maps = scoreGoodsMapper.queryGoodsList(scoreGoods); PageInfo pageInfo = new PageInfo(maps); int count = Convert.toInt(pageInfo.getTotal()); Map map = new HashMap<>(); map.put("data", maps); map.put("count", count); map.put("code", 0); return map; } public Long addGoods(AddGoodsDto model) { //新增商品主信息 ObjectMapper objectMapper = new ObjectMapper(); 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); //样式信息 List scoreGoodsStyleDtos = model.getScoreGoodsStyleDtos(); 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); //规格信息 List scoreGoodsSkuDtos = scoreGoodsStyleDto.getScoreGoodsSkuDtos(); 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); } } } } //轮播图信息 List goodsImages = model.getGoodsImages(); if(CollUtil.isNotEmpty(goodsImages)){ for(String goodImage : goodsImages){ ScoreGoodsImages scoreGoodsImages = new ScoreGoodsImages(); scoreGoodsImages.setImageUrl(goodImage); scoreGoodsImages.setGoodsId(scoreGoods.getId()); scoreGoodsImages.setDelFlag(Convert.toShort(Constants.DEL_NOT_FLAG)); scoreGoodsImagesMapper.insert(scoreGoodsImages); } } return scoreGoods.getId(); } public void deleteGoods(long id) { scoreGoodsMapper.updateDelFlagById(id,Convert.toShort(Constants.DEL_FLAG)); Example exampleStyle = new Example(ScoreGoodsStyle.class); Example.Criteria criteriaStyle = exampleStyle.createCriteria(); criteriaStyle.andEqualTo("goodsId",id); List goodsStyles = scoreGoodsStyleMapper.selectByExample(exampleStyle); if(CollUtil.isNotEmpty(goodsStyles)){ for(ScoreGoodsStyle scoreGoodsStyle : goodsStyles){ scoreGoodsStyleMapper.updateDelFlagById(scoreGoodsStyle.getId(),Convert.toShort(Constants.DEL_FLAG)); } } Example exampleSku = new Example(ScoreGoodsStyle.class); Example.Criteria criteriaSku = exampleSku.createCriteria(); criteriaSku.andEqualTo("goodsId",id); List goodsSkus = scoreGoodsSkuMapper.selectByExample(exampleSku); if(CollUtil.isNotEmpty(goodsSkus)){ for(ScoreGoodsSku scoreGoodsSku : goodsSkus){ scoreGoodsSkuMapper.updateDelFlagById(scoreGoodsSku.getId(),Convert.toShort(Constants.DEL_FLAG)); } } } public ViewGoodsVo viewGoods(long id) { ViewGoodsVo viewGoodsVo = new ViewGoodsVo(); ScoreGoods scoreGoods = scoreGoodsMapper.selectByPrimaryKey(id); if(ObjectUtil.isNotEmpty(scoreGoods)){ ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); viewGoodsVo = objectMapper.convertValue(scoreGoods, ViewGoodsVo.class); //轮播图 List goodsImages = scoreGoodsImagesMapper.selectScoreGoodsImagesByGoodsId(id); viewGoodsVo.setGoodsImages(goodsImages); //样式 List scoreGoodsStyleVos = new ArrayList<>(); List goodsStyles = scoreGoodsStyleMapper.selectScoreGoodsStyleByGoodsId(id); if(CollUtil.isNotEmpty(goodsStyles)){ for(ScoreGoodsStyle goodsStyle : goodsStyles){ ScoreGoodsStyleVo scoreGoodsStyleVo = objectMapper.convertValue(goodsStyle, ScoreGoodsStyleVo.class); //获取规格 Long styleId = goodsStyle.getId(); List goodsSkus = scoreGoodsSkuMapper.selectScoreGoodsSkuByGoodsIdAndStyleId(id,styleId); scoreGoodsStyleVo.setScoreGoodsSkuVos(goodsSkus); scoreGoodsStyleVos.add(scoreGoodsStyleVo); } } viewGoodsVo.setScoreGoodsStyleVos(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); scoreGoodsMapper.updateByPrimaryKey(scoreGoods); //轮播图 Example exampleImages = new Example(ScoreGoodsImages.class); Example.Criteria criteriaImages = exampleImages.createCriteria(); criteriaImages.andEqualTo("goodsId",scoreGoods.getId()); scoreGoodsImagesMapper.deleteByExample(exampleImages); List goodsImages = model.getGoodsImages(); if(CollUtil.isNotEmpty(goodsImages)){ for(String goodImage : goodsImages){ ScoreGoodsImages scoreGoodsImages = new ScoreGoodsImages(); scoreGoodsImages.setImageUrl(goodImage); scoreGoodsImages.setGoodsId(scoreGoods.getId()); scoreGoodsImages.setDelFlag(Convert.toShort(Constants.DEL_NOT_FLAG)); scoreGoodsImagesMapper.insert(scoreGoodsImages); } } List scoreGoodsStyleDtos = model.getScoreGoodsStyleDtos(); if(CollUtil.isNotEmpty(scoreGoodsStyleDtos)){ for(ScoreGoodsStyleDto scoreGoodsStyleDto : scoreGoodsStyleDtos){ ScoreGoodsStyle scoreGoodsStyle = new ScoreGoodsStyle(); scoreGoodsStyle.setId(scoreGoodsStyleDto.getId()); scoreGoodsStyle.setName(scoreGoodsStyleDto.getName()); scoreGoodsStyle.setGoodsId(scoreGoods.getId()); scoreGoodsStyleMapper.updateByPrimaryKeySelective(scoreGoodsStyle); //规格 List scoreGoodsSkuDtos = scoreGoodsStyleDto.getScoreGoodsSkuDtos(); if(CollUtil.isNotEmpty(scoreGoodsSkuDtos)){ for(ScoreGoodsSkuDto scoreGoodsSkuDto : scoreGoodsSkuDtos){ ScoreGoodsSku scoreGoodsSku = objectMapper.convertValue(scoreGoodsSkuDto, ScoreGoodsSku.class); scoreGoodsSkuMapper.updateByPrimaryKey(scoreGoodsSku); } } } } } public void saleGoods(long id, Integer issale) { ScoreGoods scoreGoods = new ScoreGoods(); scoreGoods.setIsSale(issale); Example exampleGoods = new Example(ScoreGoods.class); Example.Criteria criteriaGoods = exampleGoods.createCriteria(); criteriaGoods.andEqualTo("id",id); scoreGoodsMapper.updateByExampleSelective(scoreGoods,exampleGoods); } public void snapUpGoods(SnapUpGoodsDto model) { long id = model.getId(); Integer isQg = model.getIsQg(); if(ScoreGoods.ISQG_YES == isQg){ ScoreGoods scoreGoods = new ScoreGoods(); scoreGoods.setIsQg(ScoreGoods.ISQG_YES); scoreGoods.setQgStartTime(model.getQgStartTime()); scoreGoods.setQgEndTime(model.getQgEndTime()); Example exampleGoods = new Example(ScoreGoods.class); Example.Criteria criteriaGoods = exampleGoods.createCriteria(); criteriaGoods.andEqualTo("id",id); scoreGoodsMapper.updateByExampleSelective(scoreGoods,exampleGoods); }else{ ScoreGoods scoreGoods = new ScoreGoods(); scoreGoods.setIsQg(ScoreGoods.ISQG_NO); Example exampleGoods = new Example(ScoreGoods.class); Example.Criteria criteriaGoods = exampleGoods.createCriteria(); criteriaGoods.andEqualTo("id",id); scoreGoodsMapper.updateByExampleSelective(scoreGoods,exampleGoods); } } }