陶杰
2024-09-05 87ec61eabf4ffdc020bcf86a38a91cb59a90eaba
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
package com.mzl.flower.service.flower;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.*;
import com.mzl.flower.dto.request.flower.FlowerTagMultipleDTO;
import com.mzl.flower.dto.response.flower.*;
import com.mzl.flower.entity.customer.Collect;
import com.mzl.flower.entity.customer.Customer;
import com.mzl.flower.entity.flower.*;
import com.mzl.flower.entity.payment.Cart;
import com.mzl.flower.entity.payment.Order;
import com.mzl.flower.entity.payment.OrderItem;
import com.mzl.flower.entity.supplier.Station;
import com.mzl.flower.entity.supplier.Supplier;
import com.mzl.flower.entity.supplier.SupplierType;
import com.mzl.flower.mapper.customer.CollectMapper;
import com.mzl.flower.mapper.flower.*;
import com.mzl.flower.mapper.payment.CartMapper;
import com.mzl.flower.mapper.payment.OrderItemMapper;
import com.mzl.flower.mapper.supplier.StationMapper;
import com.mzl.flower.mapper.supplier.SupplierTypeMapper;
import com.mzl.flower.service.BaseService;
import com.mzl.flower.service.customer.BrowseService;
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.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
@Slf4j
@Service
@Transactional
public class FlowerService extends BaseService {
 
    @Autowired
    private FlowerMapper flowerMapper;
 
    @Autowired
    private FlowerCategoryMapper categoryMapper;
 
    @Autowired
    private FlowerParamService paramService;
 
    @Autowired
    private SupplierTypeMapper supplierTypeMapper;
 
    @Autowired
    private StationMapper stationMapper;
 
    @Autowired
    private FlowerCategoryDailyMapper categoryDailyMapper;
 
    @Autowired
    private BrowseService browseService;
 
    @Autowired
    private FlowerZoneMapper flowerZoneMapper;
 
    @Autowired
    private CollectMapper collectMapper;
 
    @Autowired
    private FlowerZoneMapMapper flowerZoneMapMapper;
 
    @Autowired
    private CartMapper cartMapper;
 
    @Autowired
    private OrderItemMapper orderItemMapper;
 
    @Async
    public synchronized void updateFlowerSales(Order order){
        List<OrderItem> items = orderItemMapper.selectList(new QueryWrapper<OrderItem>()
                .eq("order_id", order.getId()));
        if(items != null && items.size() > 0){
            for(OrderItem oi : items){
                flowerMapper.updateFlowerSales(oi.getFlowerId(), oi.getNum());
            }
        }
    }
 
    @Async
    public synchronized void updateCategoryInfo(Long category){
        flowerMapper.updateFlowerCategoryInfo(category);
    }
 
    /**
     * 花农(供应商)提交商品
     *
     * @param dto
     * @return
     */
    public Long commitFlower(FlowerCreateDTO dto){
        Supplier s = getCurrentSupplier();
        if(!"P".equals(s.getStatus())){
            throw new ValidationException("审核未通过,无法操作!");
        }
 
        Flower g = new Flower();
 
        BeanUtils.copyProperties(dto, g);
 
        String userId = SecurityUtils.getUserId();
        g.create(userId);
        g.setBanners(toJSONString(dto.getBannerList()));
        g.setStatus(Constants.FLOWER_STATUS.PENDING.name());
        g.setSupplierId(s.getId());
 
        flowerMapper.insert(g);
 
        FlowerCategory fc = categoryMapper.selectById(g.getCategory());
        if(fc.getParamId() != null){
            List<ParamDTO> params = dto.getParams();
            paramService.insertTableData(fc.getParamId(), g.getId(), params);
        }
 
        return g.getId();
    }
 
    public Long editFlower(FlowerUpdateDTO dto){
        Flower g = flowerMapper.selectById(dto.getId());
        if(Constants.FLOWER_STATUS.PENDING.name().equals(g.getStatus())
            || Constants.FLOWER_STATUS.REJECT.name().equals(g.getStatus())
            || Constants.FLOWER_STATUS.DRAFT.name().equals(g.getStatus())
            || Constants.FLOWER_STATUS.FORCE_OFF.name().equals(g.getStatus())
        ){
            BeanUtils.copyProperties(dto, g);
            g.setBanners(toJSONString(dto.getBannerList()));
        } else {
            g.setPrice(dto.getPrice());
            g.setStock(dto.getStock());
        }
 
        g.update(SecurityUtils.getUserId());
        if(Constants.FLOWER_STATUS.DRAFT.name().equals(g.getStatus())
                || Constants.FLOWER_STATUS.REJECT.name().equals(g.getStatus())
                || Constants.FLOWER_STATUS.FORCE_OFF.name().equals(g.getStatus())
        ){
            g.setStatus(Constants.FLOWER_STATUS.PENDING.name());
        }
        flowerMapper.updateById(g);
 
        FlowerCategory fc = categoryMapper.selectById(g.getCategory());
        if(fc.getParamId() != null){
            List<ParamDTO> params = dto.getParams();
            Map m = paramService.getTableDataById(fc.getParamId(), g.getId());
            if(m == null) {
                paramService.insertTableData(fc.getParamId(), g.getId(), params);
            } else {
                paramService.updateTableData(fc.getParamId(), g.getId(), params);
            }
        }
 
        return g.getId();
    }
 
    public Long editFlowerAdmin(FlowerUpdateAdminDTO dto){
        Flower g = flowerMapper.selectById(dto.getId());
        g.setSales(dto.getSales());
        g.update(SecurityUtils.getUserId());
        flowerMapper.updateById(g);
 
        FlowerCategory fc = categoryMapper.selectById(g.getCategory());
        if(fc.getParamId() != null){
            List<ParamDTO> params = dto.getParams();
            Map m = paramService.getTableDataById(fc.getParamId(), g.getId());
            if(m == null) {
                paramService.insertTableData(fc.getParamId(), g.getId(), params);
            } else {
                paramService.updateTableData(fc.getParamId(), g.getId(), params);
            }
        }
 
        return g.getId();
    }
 
    /**
     * 花农(供应商)商品列表
     *
     * @param page
     * @param dto
     * @return
     */
    public Page<FlowerSupplierListDTO> selectFlowerSupplierList(Page page, FlowerQueryDTO dto, int deleted){
        Supplier s = getCurrentSupplier();
        dto.setStatusList(splitParam(dto.getStatus()));
        List<FlowerSupplierListDTO> ls = flowerMapper.selectFlowerSupplierList(page, deleted, s.getId(), dto);
        page.setRecords(ls);
 
        return page;
    }
 
    /**
     * 花农(供应商)商品详情
     *
     * @param id
     * @return
     */
    public FlowerSupplierDTO getFlowerSupplierDetail(Long id){
        FlowerSupplierDTO dto = new FlowerSupplierDTO();
        Flower f = flowerMapper.selectById(id);
 
        setFlowerInfo(f, dto);
 
        return dto;
    }
 
    private void setFlowerInfo(Flower f, FlowerSupplierDTO dto){
        if(f == null){
            throw new ValidationException("商品未找到");
        }
        BeanUtils.copyProperties(f, dto);
        dto.setBannerList(parseArray(f.getBanners(), String.class));
 
        FlowerCategory fc = categoryMapper.selectById(f.getCategory());
        dto.setCategoryStr(fc.getName());
 
        dto.setParams(getFlowerParams(fc.getParamId(), f.getId()));
    }
 
    private List<ParamItemDTO> getFlowerParams(Long paramId, Long fId){
        List<ParamItemDTO> result = new ArrayList<>();
        if(paramId != null) {
            List<ParamItemDTO> params = paramService.getParamItemListByParamId(paramId);
            setParamResult(paramId, fId, params, result);
        }
 
        return result;
    }
 
    private void setParamResult(Long paramId, Long fId, List<ParamItemDTO> params, List<ParamItemDTO> result){
        Map map = paramService.getTableDataById(paramId, fId);
        if(params != null && params.size() > 0 && map != null){
            for(ParamItemDTO p : params){
                ParamItemDTO it = new ParamItemDTO();
                BeanUtils.copyProperties(p, it);
                it.setValue(map.get(p.getColumn()));
                result.add(it);
            }
        }
    }
 
    public List<ParamItemDTO> getFlowerParams(Long paramId, Long fId
            , Map<Long, List<ParamItemDTO>> paramMap){
        List<ParamItemDTO> result = new ArrayList<>();
        if(paramId != null) {
            List<ParamItemDTO> params = paramMap.get(paramId);
            if(params == null){
                params = paramService.getParamItemListByParamId(paramId);
                paramMap.put(paramId, params);
            }
            setParamResult(paramId, fId, params, result);
        }
 
        return result;
    }
 
    public void setFlowerShown(Long id, Boolean shown){
        Flower f = flowerMapper.selectById(id);
        if(f == null){
            throw new ValidationException("商品不存在");
        }
        f.setShown(shown);
 
        flowerMapper.updateById(f);
    }
 
    /**
     * 运营端 商品列表
     *
     * @param page
     * @param dto
     * @return
     */
    public Page<FlowerListDTO> selectFlowerList(Page page, FlowerQueryDTO dto){
        List<String> statusList = splitParam(dto.getStatus());
        dto.setStatusList(statusList);
        if(statusList != null && statusList.contains(Constants.FLOWER_STATUS.OFF.name())){
            statusList.add(Constants.FLOWER_STATUS.FORCE_OFF.name());
        }
        List<FlowerListDTO> ls = flowerMapper.selectFlowerList(page, dto);
        page.setRecords(ls);
 
        return page;
    }
 
    public FlowerDTO getFlowerDetail(Long id){
        FlowerDTO dto = new FlowerDTO();
        Flower f = flowerMapper.selectById(id);
 
        setFlowerInfo(f, dto);
 
        Supplier s = supplierMapper.selectById(f.getSupplierId());
        dto.setSupplierName(s.getName());
        dto.setSupplierTel(s.getContactTel());
 
        SupplierType t = supplierTypeMapper.selectById(s.getTypeId());
        dto.setSupplierType(t != null ? t.getName() : "");
 
        Station a = stationMapper.selectById(s.getStationId());
        dto.setSupplierStation(a != null ? a.getName() : "");
 
        List<FlowerZoneMap> ls = flowerZoneMapMapper.selectList(new QueryWrapper<FlowerZoneMap>()
                .eq("flower_id", id).orderByAsc("zone_id"));
        List<Long> zoneIdLs = new ArrayList<>();
        if(ls != null && ls.size() > 0){
            for(FlowerZoneMap fzm : ls){
                zoneIdLs.add(fzm.getZoneId());
            }
            List<FlowerZone> zLs = flowerZoneMapper.selectBatchIds(zoneIdLs);
            List<String> zoneIdStrLs = new ArrayList<>();
            List<String> zoneNameLs = new ArrayList<>();
            for(FlowerZone fz : zLs){
                zoneIdStrLs.add(fz.getId() + "");
                zoneNameLs.add(fz.getName());
            }
            dto.setZoneId(String.join(",", zoneIdStrLs));
            dto.setZoneName(String.join(",", zoneNameLs));
        }
 
        //TODO 合伙人加价列表
 
        return dto;
    }
 
    public void auditFlower(Long id, String auditRemarks, String status){
        Flower f = flowerMapper.selectById(id);
        if(f == null){
            throw new ValidationException("商品未找到");
        }
        f.setAuditRemarks(auditRemarks);
        f.setAuditTime(LocalDateTime.now());
        f.setStatus(status);
        flowerMapper.updateById(f);
 
        /*if(Constants.FLOWER_STATUS.REJECT.name().equals(status)){
            //通知花农。只是说花农在小程序可以查看驳回的商品,已包含驳回字段
        }*/
    }
 
    public void auditFlowers(FlowerAuditMultipleDTO dto, String status){
        List<Long> ids = dto.getIds();
        if(ids == null || ids.size() == 0){
            throw new ValidationException("请选择商品");
        }
        for(Long id : ids){
            auditFlower(id, dto.getAuditRemarks(), status);
        }
    }
 
    public void setOffOn(Long id, String status){
        Flower f = flowerMapper.selectById(id);
        if(f == null){
            throw new ValidationException("商品未找到");
        }
        String s = f.getStatus();
        if(Constants.FLOWER_STATUS.FORCE_OFF.name().equals(s)){
            throw new ValidationException("强制下架商品请编辑提交重新审核");
        }
        if(!Constants.FLOWER_STATUS.UP.name().equals(s)
                && !Constants.FLOWER_STATUS.OFF.name().equals(s)){
            throw new ValidationException("无法上下架");
        }
 
        f.setStatus(status);
        flowerMapper.updateById(f);
 
        if(Constants.FLOWER_STATUS.OFF.name().equals(f.getStatus())){
            //清除购物车商品
            cartMapper.delete(new QueryWrapper<Cart>().eq("flower_id", id));
        }
    }
 
    public void deleteFlower(Long id){
        flowerMapper.deleteById(id);
 
        //清除购物车商品
        cartMapper.delete(new QueryWrapper<Cart>().eq("flower_id", id));
    }
 
    public void restoreFlower(Long id){
        Flower f = flowerMapper.selectById(id);
        if(f == null){
            throw new ValidationException("商品未找到");
        }
        f.setDeleted(false);
        f.setStatus(Constants.FLOWER_STATUS.DRAFT.name());
        flowerMapper.updateById(f);
    }
 
    public void editFlowerPrice(FlowerPriceDTO dto){
        Flower f = flowerMapper.selectById(dto.getId());
        if(f == null){
            throw new ValidationException("商品未找到");
        }
        f.setPrice(dto.getPrice());
        flowerMapper.updateById(f);
    }
 
    public void editFlowerStock(FlowerStockDTO dto){
        Flower f = flowerMapper.selectById(dto.getId());
        if(f == null){
            throw new ValidationException("商品未找到");
        }
        f.setStock(dto.getStock());
        flowerMapper.updateById(f);
    }
 
    public void setFlowersTags(FlowerTagMultipleDTO dto){
        List<Long> ids = dto.getIds();
        if(ids == null || ids.size() == 0){
            throw new ValidationException("请选择商品");
        }
 
        for(Long id : ids) {
            Flower f = flowerMapper.selectById(id);
            f.setTags(dto.getTags());
            flowerMapper.updateById(f);
        }
    }
 
    public void setFlowersForceOff(FlowerOffDTO dto){
        List<Long> ids = dto.getIds();
        if(ids == null || ids.size() == 0){
            throw new ValidationException("请选择商品");
        }
 
        for(Long id : ids) {
            Flower f = flowerMapper.selectById(id);
            f.setStatus(Constants.FLOWER_STATUS.FORCE_OFF.name());
            flowerMapper.updateById(f);
 
            //清除购物车商品
            cartMapper.delete(new QueryWrapper<Cart>().eq("flower_id", id));
        }
    }
 
    /**
     * 花店交易大厅商品列表
     *
     * @param page
     * @param dto
     * @return
     */
    public Page<FlowerShowListDTO> selectShowFlowerList(Page page, FlowerShowQueryDTO dto) {
        /*if (StringUtils.isNotEmpty(dto.getColumn())) {
            List<OrderItem> orders = new ArrayList<>();
            OrderItem i = new OrderItem();
            i.setColumn(dto.getColumn());
            i.setAsc(dto.isAsc());
            page.setOrders(orders);
        }*/
        dto.setUserId(SecurityUtils.getUserId());
        dto.setParamId(paramService.getParamIdByCategoryId(dto.getCategory()));
        List<FlowerShowListDTO> ls = flowerMapper.selectFlowerShowList(page, dto);
        if(ls != null && ls.size() > 0){
            Long partnerId = getCurrentCustomerPartner();
            for(FlowerShowListDTO s : ls){
                BigDecimal price = getFinalPrice(partnerId, s.getCategory()
                        , s.getId(), s.getPrice(), s.getLevel());
                s.setPrice(price);
                s.setCollection(s.getCollectCount() > 0);
                s.setShopnum(s.getShopnum() == null ? 0 : s.getShopnum());
 
                if(s.getSales() != null && s.getRealSales() != null){
                    s.setSales(s.getSales() + s.getRealSales());
                }
            }
        }
 
        page.setRecords(ls);
 
        return page;
    }
 
    public FlowerShowDTO getShowFlowerDetail(Long id, Long partnerId){
        FlowerShowDTO dto = new FlowerShowDTO();
        Flower f = flowerMapper.selectById(id);
 
        setFlowerInfo(f, dto);
 
        Supplier s = supplierMapper.selectById(f.getSupplierId());
        dto.setSupplierName(s.getName());
        dto.setSupplierCover(s.getCover());
 
        SupplierType t = supplierTypeMapper.selectById(s.getTypeId());
        dto.setSupplierType(t != null ? t.getName() : "");
 
        Station a = stationMapper.selectById(s.getStationId());
        dto.setSupplierStation(a != null ? a.getName() : "");
 
        Long pId = partnerId;
        if(pId == null){
            pId = getCurrentCustomerPartner();
        }
 
        BigDecimal price = getFinalPrice(pId, dto.getCategory()
                , dto.getId(), dto.getPrice(), dto.getLevel());
        dto.setPrice(price);
 
        String userId = SecurityUtils.getUserId();
        if(StringUtils.isNotEmpty(userId)) {
            int count = collectMapper.selectCount(new QueryWrapper<Collect>()
                    .eq("user_id", userId)
                    .eq("flower_id", id)
            );
            dto.setCollection(count > 0);
 
            Integer num = cartMapper.getCartFlowerCount(userId, id);
            dto.setShopnum(num == null ? 0 : num);
 
            //添加浏览历史日志
            browseService.addBrowseHistory(userId, f.getId());
        }
 
        if(dto.getSales() != null && dto.getRealSales() != null){
            dto.setSales(dto.getSales() + dto.getRealSales());
        }
 
        return dto;
    }
 
    private FlowerCategoryDaily getCategoryDaily(Long categoryId, Long partnerId, LocalDate day){
        QueryWrapper<FlowerCategoryDaily> q = new QueryWrapper<FlowerCategoryDaily>()
                .eq("category_id", categoryId).eq("day", day);
        if(partnerId == null){
            q = q.isNull("partner_id");
        } else {
            q = q.eq("partner_id", partnerId);
        }
        return categoryDailyMapper.selectOne(q);
    }
 
    public void calculateCategoryDaily(Long categoryId, Long partnerId){
        List<Flower> ls = flowerMapper.selectList(new QueryWrapper<Flower>()
                .eq("category", categoryId).eq("status", Constants.FLOWER_STATUS.UP.name()));
        if(ls != null && ls.size() > 0){
            LocalDate preDay = LocalDate.now().plusDays(-1);
            FlowerCategoryDaily pre = getCategoryDaily(categoryId, partnerId, preDay);
 
            int total = ls.size();
            BigDecimal totalAmount = new BigDecimal(0);
            for(Flower f : ls){
                BigDecimal price = getFinalPrice(partnerId, categoryId, f.getId(), f.getPrice(), f.getLevel());
                totalAmount = totalAmount.add(price);
            }
            BigDecimal avePrice = totalAmount.divide(new BigDecimal(total), 2, RoundingMode.HALF_UP);//均价
            BigDecimal avePriceDifference = new BigDecimal(0);//比前一天差价
            double avePriceDifferenceRate = 0D;//差价百分比
            if(pre != null){
                avePriceDifference = avePrice.subtract(pre.getAvePrice());
                if(pre.getAvePrice() != null && pre.getAvePrice().doubleValue() != 0) {
                    BigDecimal rate = avePriceDifference.divide(pre.getAvePrice(), 2, RoundingMode.HALF_UP);
                    avePriceDifferenceRate = rate.doubleValue();
                }
            }
 
            FlowerCategoryDaily cd = getCategoryDaily(categoryId, partnerId, LocalDate.now());
            if (cd == null) {
                cd = new FlowerCategoryDaily();
                cd.setCategoryId(categoryId);
                cd.setPartnerId(partnerId);
                cd.setDay(LocalDate.now());
                cd.create();
                cd.setAvePrice(avePrice);
                cd.setAvePriceDifference(avePriceDifference);
                cd.setAvePriceDifferenceRate(avePriceDifferenceRate);
 
                categoryDailyMapper.insert(cd);
            } else {
                cd.setUpdateTime(LocalDateTime.now());
                cd.setAvePrice(avePrice);
                cd.setAvePriceDifference(avePriceDifference);
                cd.setAvePriceDifferenceRate(avePriceDifferenceRate);
                categoryDailyMapper.updateById(cd);
            }
        }
    }
 
    public Page<FlowerCategoryDailyDTO> getHomeCategoryDaily(Page page){
        Customer c = getCurrentCustomerWithoutCheck();
        Long partnerId = c == null ? null : c.getPartnerId();
        List<FlowerCategoryDailyDTO> ls = categoryMapper.selectHomeCategoryDaily(page, partnerId, LocalDate.now());
 
        page.setRecords(ls);
        return page;
    }
 
    public Page<FlowerShowListDTO> myCollect(Page page, FlowerShowQueryDTO dto) {
        dto.setUserId(SecurityUtils.getUserId());
        if (StringUtils.isNotEmpty(dto.getColumn())) {
            if(dto.getColumn().contains("sales")
                    || dto.getColumn().contains("stock")){
                dto.setColumn(dto.getColumn() + " desc");
            }
        }
 
        dto.setParamId(paramService.getParamIdByCategoryId(dto.getCategory()));
        List<FlowerShowListDTO> flowerShowListDTOS = flowerMapper.myCollect(page, dto);
        page.setRecords(flowerShowListDTOS);
        return page;
    }
 
    public Page<FlowerShowListDTO> myBrowseHistory(Page page, FlowerShowQueryDTO dto) {
        dto.setUserId(SecurityUtils.getUserId());
        /*if (StringUtils.isNotEmpty(dto.getColumn())) {
            List<OrderItem> orders = new ArrayList<>();
            OrderItem i = new OrderItem();
            i.setColumn(dto.getColumn());
            i.setAsc(dto.isAsc());
            page.setOrders(orders);
        }*/
        dto.setParamId(paramService.getParamIdByCategoryId(dto.getCategory()));
        List<FlowerShowListDTO> flowerShowListDTOS = flowerMapper.myBrowseHistory(page, dto);
        page.setRecords(flowerShowListDTOS);
        return page;
    }
 
    public void setFlowerZone(FlowerZoneBatchDTO dto) {
        if(dto.getZoneId()!=null && dto.getZoneId().size()>0){//添加专区
            for (Long zoneId : dto.getZoneId()) {
                for(Long flowerId : dto.getIds()) {
                    FlowerZoneMap fzp = flowerZoneMapMapper.selectOne(new QueryWrapper<FlowerZoneMap>().eq("flower_id", flowerId).eq("zone_id", zoneId));
                    if(fzp==null){
                        fzp = new FlowerZoneMap();
                        fzp.setFlowerId(flowerId);
                        fzp.setZoneId(zoneId);
                        fzp.create(SecurityUtils.getUserId());
                        flowerZoneMapMapper.insert(fzp);
                    }
                }
            }
        }else{ //移除专区
            flowerZoneMapMapper.delete(new QueryWrapper<FlowerZoneMap>().in("flower_id", dto.getIds()));
        }
    }
 
    public void clearInvalid(String userId) {
        List<Long> ids = flowerMapper.searchInvalidCollect(userId);
        if(ids!=null && ids.size()>0){
            collectMapper.deleteBatchIds(ids);
        }
    }
 
    public void setFlowerRecommend(FlowerRecommendMultipleDTO dto) {
        List<Long> ids = dto.getIds();
        if(ids == null || ids.size() == 0){
            throw new ValidationException("请选择商品");
        }
 
        for(Long id : ids) {
            Flower f = flowerMapper.selectById(id);
            f.setRecommend(true);
            flowerMapper.updateById(f);
        }
    }
 
    public void cancelFlowerRecommend(FlowerRecommendMultipleDTO dto) {
        List<Long> ids = dto.getIds();
        if(ids == null || ids.size() == 0){
            throw new ValidationException("请选择商品");
        }
 
        for(Long id : ids) {
            Flower f = flowerMapper.selectById(id);
            f.setRecommend(false);
            flowerMapper.updateById(f);
        }
    }
 
    public Integer getUpFlowerStock(){
        Integer c = flowerMapper.getUpFlowerStock();
        return c == null ? 0 : c;
    }
 
    public void setFlowerRecommendRank(FlowerRecommendRankDTO dto) {
        Flower f = flowerMapper.selectById(dto.getId());
        if(f==null){
            throw new ValidationException("商品不存在");
        }
        f.setRecommendRank(dto.getRecommendRank());
        flowerMapper.updateById(f);
    }
 
    public Page<FlowerListDTO> selectZoneFlowerList(Page page, FlowerZoneQueryDTO dto) {
        List<FlowerListDTO> ls = flowerMapper.selectZoneFlowerList(page, dto);
        page.setRecords(ls);
 
        return page;
    }
 
    public void clearInvalidHistory(String userId) {
 
        List<Long> ids = flowerMapper.searchInvalidHistory(userId);
        if(ids!=null && ids.size()>0){
            browseService.deleteBatchIds(ids);
 
        }
 
    }
}