Cui Zhi Feng
2024-09-20 bbce1624d0d4e9063a2ad71b7f54e34298da9b7a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
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);
    }
 
}