| package com.mzl.flower.service.flower; | 
|   | 
| import com.alibaba.fastjson.JSONObject; | 
| import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
| import com.mzl.flower.base.Node; | 
| import com.mzl.flower.base.cache.CategoryPriceCacheClient; | 
| import com.mzl.flower.config.exception.ValidationException; | 
| import com.mzl.flower.config.security.SecurityUtils; | 
| import com.mzl.flower.constant.Constants; | 
| import com.mzl.flower.dto.request.flower.FlowerCategoryCreateDTO; | 
| import com.mzl.flower.dto.request.flower.FlowerCategoryQueryDTO; | 
| import com.mzl.flower.dto.request.flower.FlowerCategoryUpdateDTO; | 
| import com.mzl.flower.dto.response.flower.FlowerCategoryDTO; | 
| import com.mzl.flower.dto.response.flower.FlowerCategoryShowDTO; | 
| import com.mzl.flower.dto.response.flower.FlowerCategoryTreeDTO; | 
| import com.mzl.flower.entity.customer.Customer; | 
| import com.mzl.flower.entity.flower.Flower; | 
| import com.mzl.flower.entity.flower.FlowerCategory; | 
| import com.mzl.flower.entity.flower.FlowerCategoryDaily; | 
| import com.mzl.flower.entity.menber.Member; | 
| import com.mzl.flower.mapper.flower.FlowerCategoryDailyMapper; | 
| import com.mzl.flower.mapper.flower.FlowerCategoryMapper; | 
| import com.mzl.flower.mapper.flower.FlowerMapper; | 
| import com.mzl.flower.mapper.member.MemberMapper; | 
| import com.mzl.flower.service.BaseService; | 
| import com.mzl.flower.utils.TreeBuilderUtil; | 
| import io.micrometer.core.instrument.util.StringUtils; | 
| import lombok.extern.slf4j.Slf4j; | 
| import org.springframework.beans.BeanUtils; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.stereotype.Service; | 
| import org.springframework.transaction.annotation.Transactional; | 
|   | 
| import java.math.BigDecimal; | 
| import java.time.LocalDate; | 
| import java.util.ArrayList; | 
| import java.util.List; | 
|   | 
| @Slf4j | 
| @Service | 
| @Transactional | 
| public class FlowerCategoryService extends BaseService { | 
|   | 
|     @Autowired | 
|     private FlowerCategoryMapper categoryMapper; | 
|   | 
|     @Autowired | 
|     private FlowerMapper flowerMapper; | 
|   | 
|     @Autowired | 
|     private FlowerCategoryDailyMapper categoryDailyMapper; | 
|   | 
|     @Autowired | 
|     private CategoryPriceCacheClient priceCacheClient; | 
|   | 
|     public Long addCategory(FlowerCategoryCreateDTO dto){ | 
|         FlowerCategory g = new FlowerCategory(); | 
|   | 
|         BeanUtils.copyProperties(dto, g); | 
|         g.setSortBy(dto.getSortBy() == null ? 0 : dto.getSortBy()); | 
|         g.create(SecurityUtils.getUserId()); | 
|   | 
|         if(g.getParentId() != null) { | 
|             FlowerCategory p = categoryMapper.selectById(g.getParentId()); | 
|             g.setParentName(p.getName()); | 
|         } | 
|   | 
|         categoryMapper.insert(g); | 
|   | 
|         return g.getId(); | 
|     } | 
|   | 
|     public Long updateCategory(FlowerCategoryUpdateDTO dto){ | 
|         Long id = dto.getId(); | 
|         FlowerCategory g = categoryMapper.selectById(id); | 
|         Boolean shown = g.getShown(); | 
|         BeanUtils.copyProperties(dto, g); | 
|         g.setShown(shown); | 
|         g.setSortBy(dto.getSortBy() == null ? 0 : dto.getSortBy()); | 
|         g.update(SecurityUtils.getUserId()); | 
|   | 
|         g.setParentName(""); | 
|         if(g.getParentId() != null) { | 
|             FlowerCategory p = categoryMapper.selectById(g.getParentId()); | 
|             g.setParentName(p.getName()); | 
|         } | 
|   | 
|         categoryMapper.updateById(g); | 
|   | 
|         return id; | 
|     } | 
|   | 
|     public FlowerCategoryDTO getCategory(String id){ | 
|         FlowerCategoryDTO dto = new FlowerCategoryDTO(); | 
|         FlowerCategory d = categoryMapper.selectById(id); | 
|         BeanUtils.copyProperties(d, dto); | 
|   | 
|         return dto; | 
|     } | 
|   | 
|     public FlowerCategoryShowDTO getCategoryShow(String id){ | 
|         FlowerCategoryShowDTO dto = new FlowerCategoryShowDTO(); | 
|         FlowerCategory d = categoryMapper.selectById(id); | 
|         BeanUtils.copyProperties(d, dto); | 
|   | 
|         Long partnerId = getCurrentCustomerPartner(); | 
|         LocalDate day = LocalDate.now(); | 
|         QueryWrapper<FlowerCategoryDaily> q = new QueryWrapper<FlowerCategoryDaily>() | 
|                 .eq("category_id", id).eq("day", day); | 
|         if(partnerId == null){ | 
|             q = q.isNull("partner_id"); | 
|         } else { | 
|             q = q.eq("partner_id", partnerId); | 
|         } | 
|         FlowerCategoryDaily cd = categoryDailyMapper.selectOne(q); | 
|         if(cd != null) { | 
|             dto.setAvePrice(cd.getAvePrice()); | 
|             dto.setAvePriceDifference(cd.getAvePriceDifference()); | 
|             dto.setAvePriceDifferenceRate(cd.getAvePriceDifferenceRate()); | 
|         } | 
|   | 
|         return dto; | 
|     } | 
|   | 
|     public List<FlowerCategoryTreeDTO> selectCategoryList(FlowerCategoryQueryDTO dto){ | 
|         return categoryMapper.selectCategoryList(dto); | 
|     } | 
|   | 
|     public List<FlowerCategoryTreeDTO> selectCategoryTree(FlowerCategoryQueryDTO dto) { | 
|         List<FlowerCategoryTreeDTO> treeList = categoryMapper.selectTreeList(dto); | 
|         treeList = (List<FlowerCategoryTreeDTO>) TreeBuilderUtil.buildListToTree(treeList); | 
|         if(treeList != null && treeList.size() > 0){ | 
|             for(FlowerCategoryTreeDTO t : treeList){ | 
|                 List<Node> cLs = t.getChildren(); | 
|                 String ll = t.getLevelLimit(); | 
|                 if(cLs != null && cLs.size() > 0){ | 
|                     for(Node c : cLs){ | 
|                         FlowerCategoryTreeDTO cc = (FlowerCategoryTreeDTO)c; | 
|                         cc.setLevelLimit(ll); | 
|                     } | 
|                 } | 
|             } | 
|         } | 
|   | 
|         return treeList; | 
|     } | 
|   | 
|     public void setCategoryShown(Long categoryId, Boolean shown){ | 
|         FlowerCategory category = categoryMapper.selectById(categoryId); | 
|         if(category == null){ | 
|             throw new ValidationException("分类不存在"); | 
|         } | 
|         category.setShown(shown); | 
|   | 
|         categoryMapper.updateById(category); | 
|     } | 
|   | 
|     public List<FlowerCategoryTreeDTO> selectCustomerCategoryTree(FlowerCategoryQueryDTO dto) { | 
|         dto.setShown(true); | 
|         List<FlowerCategoryTreeDTO> tLs = categoryMapper.selectTreeList(dto); | 
|         List<FlowerCategoryTreeDTO> treeList = new ArrayList<>(); | 
|         List<FlowerCategoryTreeDTO> result = new ArrayList<>(); | 
|         if(tLs != null && tLs.size() > 0){ | 
|             Customer p = getCurrentCustomerWithoutCheck(); | 
|             Long partnerId = p == null ? null : p.getPartnerId(); | 
|             Long levelId = p == null ? null : p.getLevelId(); | 
|             Member member = getMember(levelId); | 
|   | 
|             for(FlowerCategoryTreeDTO t : tLs){ | 
|                 if(t.getParentId() != null && t.getFlowerCount() == 0){ | 
|                     continue; | 
|                 } | 
|   | 
|                 String ppp = priceCacheClient.getPrice(t.getId() + "", partnerId + ""); | 
|                 if(StringUtils.isNotEmpty(ppp)){ | 
|                     JSONObject o = parseObject(ppp, JSONObject.class); | 
|                     t.setPriceLow(o.getBigDecimal("priceLow")); | 
|                     t.setPriceHigh(o.getBigDecimal("priceHigh")); | 
|                     t.setPriceLowMember(calculateMemberPrice(t.getPriceLow(), member)); | 
|                     t.setPriceHighMember(calculateMemberPrice(t.getPriceHigh(), member)); | 
|                 } | 
|   | 
|                 treeList.add(t); | 
|             } | 
|   | 
|             List<FlowerCategoryTreeDTO> rtLs = (List<FlowerCategoryTreeDTO>) TreeBuilderUtil.buildListToTree(treeList); | 
|             for(FlowerCategoryTreeDTO r : rtLs){ | 
|                 if(r.getChildren() != null && r.getChildren().size() > 0){ | 
|                     result.add(r); | 
|                 } | 
|             } | 
|         } | 
|   | 
|         return result; | 
|     } | 
|   | 
|     public void calculateCategoryPriceRange(Long categoryId, Long partnerId){ | 
|         List<Flower> ls = flowerMapper.selectList(new QueryWrapper<Flower>() | 
|                 .eq("category", categoryId) | 
|                 .eq("deleted", 0) | 
|                 .eq("status", Constants.FLOWER_STATUS.UP.name())); | 
|         if(ls != null && ls.size() > 0) { | 
|             BigDecimal priceLow = null; | 
|             BigDecimal priceHigh = null; | 
|             for (Flower f : ls) { | 
|                 BigDecimal price = getFinalPrice(partnerId, categoryId, f.getId(), f.getPrice(), f.getLevel()); | 
|                 if(priceLow == null || priceLow.doubleValue() > price.doubleValue()){ | 
|                     priceLow = price; | 
|                 } | 
|   | 
|                 if(priceHigh == null || priceHigh.doubleValue() < price.doubleValue()){ | 
|                     priceHigh = price; | 
|                 } | 
|             } | 
|   | 
|             JSONObject o = new JSONObject(); | 
|             o.put("priceLow", priceLow); | 
|             o.put("priceHigh", priceHigh); | 
|             priceCacheClient.addPrice(categoryId + "", partnerId + "", toJSONString(o)); | 
|         } | 
|     } | 
|   | 
|     public void deleteCategory(Long id){ | 
|         int tCount = categoryMapper.selectCount(new QueryWrapper<FlowerCategory>().eq("parent_id", id)); | 
|         if(tCount > 0){ | 
|             throw new ValidationException("该分类包含子分类,请先删除子分类!"); | 
|         } | 
|   | 
|         //分类下商品验证 | 
|         int count = flowerMapper.selectCount(new QueryWrapper<Flower>().eq("category", id) | 
|                 .eq("deleted", 0)); | 
|         if(count > 0){ | 
|             throw new ValidationException("该分类包含商品,请先修改商品分类!"); | 
|         } | 
|   | 
|         categoryMapper.deleteById(id); | 
|     } | 
|   | 
| } |