wjli
2024-04-08 fc96e4d7fc20dbec5d2d96c6e8c557c1cc3212d3
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
package cn.exrick.xboot.your.controller;
 
import cn.exrick.xboot.core.common.utils.PageUtil;
import cn.exrick.xboot.core.common.utils.ResultUtil;
import cn.exrick.xboot.core.common.utils.SecurityUtil;
import cn.exrick.xboot.core.common.vo.PageVo;
import cn.exrick.xboot.core.common.vo.Result;
import cn.exrick.xboot.core.entity.Department;
import cn.exrick.xboot.core.entity.User;
import cn.exrick.xboot.core.service.DepartmentService;
import cn.exrick.xboot.core.service.UserService;
import cn.exrick.xboot.your.entity.*;
import cn.exrick.xboot.your.service.*;
import cn.exrick.xboot.your.vo.OrderListVo;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;
 
/**
 * @author whj
 */
@Slf4j
@RestController
@Api(tags = "订单接口")
@RequestMapping("/xboot/orderTask")
@Transactional
public class OrderTaskController {
 
    @Autowired
    private IOrderTaskService iOrderTaskService;
 
    @Autowired
    private IAreaService iAreaService;
 
    @Autowired
    private ICustomerService iCustomerService;
 
    @Autowired
    private IOrderDetailService iOrderDetailService;
 
    @Autowired
    private SecurityUtil securityUtil;
 
    @Autowired
    private UserService userService;
 
    @Autowired
    private IAreaSectionService iAreaSectionService;
 
    @Autowired
    private ICarService iCarService;
 
    @Autowired
    private DepartmentService departmentService;
 
    @Autowired
    private IEventLogService iEventLogService;
 
    @Autowired
    private IOrderLogService iOrderLogService;
 
    @Autowired
    private IOrderTaskOriginalService iOrderTaskOriginalService;
 
    @Autowired
    private IOrderDetailOriginalService iOrderDetailOriginalService;
 
 
    @Autowired
    private ICustomerReceiveService iCustomerReceiveService;
 
    boolean b = false;
 
 
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<OrderTask> get(@PathVariable String id) {
 
        OrderTask orderTask = iOrderTaskService.getById(id);
        return new ResultUtil<OrderTask>().setData(orderTask);
    }
 
    @RequestMapping(value = "/getByPage2", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<OrderTask>> getByPage2(PageVo page,String sendDate,String userName,String customerName,Integer status) {
        QueryWrapper<OrderTask> wrapper = new QueryWrapper<>();
        if(StrUtil.isNotEmpty(sendDate)){
            wrapper.eq("order_date",sendDate);
        }
        IPage<OrderTask> data = iOrderTaskService.page(PageUtil.initMpPage(page),wrapper);
        List<OrderTask> records = data.getRecords();
        for (OrderTask record : records) {
            if(record.getAreaSectionId() != null){
                AreaSection byId = iAreaSectionService.getById(record.getAreaSectionId());
                if(byId != null){
                    record.setAreaSectionName(byId.getName());
                }
            }
        }
        data.setRecords(records);
        return new ResultUtil<IPage<OrderTask>>().setData(data);
    }
 
//    public Area getArea(String userId){
//        QueryWrapper<Area> wrapper = new QueryWrapper<Area>();
//        wrapper.eq("user_id",userId);
//        Area area = iAreaService.getOne(wrapper);
//        if(area==null){
//            QueryWrapper<Car> carQueryWrapper = new QueryWrapper<Car>();
//            carQueryWrapper.eq("user_id",userId);
//            Car one = iCarService.getOne(carQueryWrapper);
//            if(one==null){
//                return null;
//            }
//            if(StrUtil.isEmpty(one.getFollowUserId())){
//                return null;
//            }
//
//            QueryWrapper<Area> wrapper3 = new QueryWrapper<Area>();
//            wrapper3.eq("user_id",one.getFollowUserId());
//            area = iAreaService.getOne(wrapper3);
//            return area;
//        }
//        return area;
//    }
 
 
    private String getFormatDate(String sendTime) {
        if (StrUtil.isNotEmpty(sendTime)) {
            return sendTime;
        } else {
            return DateUtil.format(new Date(), "yyyy-MM-dd");
        }
    }
 
    private Date getDate(String sendTime){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        if (StrUtil.isNotEmpty(sendTime)) {
            return new Date();
        } else {
            try {
                return format.parse(sendTime);
            } catch (ParseException e) {
                e.printStackTrace();
                return new Date();
            }
        }
    }
 
    public Area getArea(String userId) {
        QueryWrapper<Car> carQueryWrapper = new QueryWrapper<Car>();
        carQueryWrapper.eq("user_id", userId).or().eq("follow_user_id", userId);
        Car one = iCarService.getOne(carQueryWrapper);
        if (one == null) {
            return null;
        } else {
            QueryWrapper<Area> wrapper = new QueryWrapper<Area>();
            wrapper.eq("car_id", one.getId());
            Area area = iAreaService.getOne(wrapper);
            return area;
        }
    }
 
 
    @RequestMapping(value = "/getByUserId", method = RequestMethod.GET)
    @ApiOperation(value = "获取今日任务统计信息")
    public Result<Object> getAll(String sendTime) {
        Area area = getArea(securityUtil.getCurrUser().getId());
        if (area == null) {
            return ResultUtil.error("请联系管理员绑定该车辆");
        }
 
        QueryWrapper<OrderTask> wrapper2 = new QueryWrapper<OrderTask>();
        String format = getFormatDate(sendTime);
        wrapper2.eq("area_id", area.getId());
        wrapper2.eq("send_date", format);
        List<OrderTask> list = iOrderTaskService.list(wrapper2);
 
        int sum = iOrderTaskService.sum(area.getId(), format);
 
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("count", list.size());
        map.put("sum", sum);
        map.put("name", "");
        if (list.size() > 0) {
            OrderTask orderTask = list.get(0);
            String areaSectionId = orderTask.getAreaSectionId();
            AreaSection a = iAreaSectionService.getById(areaSectionId);
            Area area2 = iAreaService.getById(orderTask.getAreaId());
            map.put("name", area2.getName() + a.getName());
            list.clear();
        }
        return new ResultUtil<Object>().setData(map);
    }
 
    @RequestMapping(value = "/getTodayCount", method = RequestMethod.GET)
    @ApiOperation(value = "获取今日任务统计信息")
    public Result<Object> getAllNew(String sendTime) {
        Area area = getArea(securityUtil.getCurrUser().getId());
        if (area == null) {
            return ResultUtil.error("请联系管理员绑定该车辆");
        }
 
        QueryWrapper<OrderTask> wrapper2 = new QueryWrapper<OrderTask>();
        String format = getFormatDate(sendTime);
        wrapper2.eq("area_id", area.getId());
        wrapper2.eq("send_date", format);
        List<OrderTask> list = iOrderTaskService.list(wrapper2);
 
        int sum = iOrderTaskService.sum(area.getId(), format);
 
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("count", list.size());
        map.put("sum", sum);
        map.put("name", "");
        map.put("sectons", null);
        if (list.size() > 0) {
            List<String> temp = new ArrayList<>();
            List<String> tempIds = new ArrayList<>();
            List<AreaSection> areaSections = new ArrayList<>();
            for(int i=0;i<list.size();i++){
                if(!tempIds.contains(list.get(i).getAreaSectionId())){
                    tempIds.add(list.get(i).getAreaSectionId());
                    AreaSection a = iAreaSectionService.getById(list.get(i).getAreaSectionId());
                    temp.add(a.getName());
                    areaSections.add(a);
                }
            }
            map.put("name", area.getName() + StringUtils.join(temp,"-"));
            map.put("sectons",areaSections);
            temp.clear();
            tempIds.clear();
            //areaSections.clear();
            list.clear();
        }
        return new ResultUtil<Object>().setData(map);
    }
 
   /* @RequestMapping(value = "/getTodayOrder", method = RequestMethod.GET)
    @ApiOperation(value = "获取今日配送任务详情列表")
    public Result<List<OrderTask>> getTodayOrder(String sendTime) {
        Area area = getArea(securityUtil.getCurrUser().getId());
        if (area == null) {
            return ResultUtil.error("请联系管理员绑定该车辆");
        }
 
        QueryWrapper<OrderTask> wrapper2 = new QueryWrapper<OrderTask>();
        String format = getFormatDate(sendTime);
//        wrapper2.eq("a.area_id",area.getId());
//        wrapper2.eq("a.send_date",format);
//        wrapper2.orderByAsc("a.seq").orderByAsc("a.status");
        //List<OrderTask> list = iOrderTaskService.list2(wrapper2);
        wrapper2.eq("area_id", area.getId());
        wrapper2.eq("send_date", format);
        wrapper2.orderByAsc("seq").orderByAsc("status");
        List<OrderTask> list = iOrderTaskService.list(wrapper2);
        for (OrderTask obj : list) {
            obj.setCode(obj.getLinker());
        }
        return new ResultUtil<List<OrderTask>>().setData(list);
    }*/
 
    @RequestMapping(value = "/getTodayOrder", method = RequestMethod.GET)
    @ApiOperation(value = "获取今日配送任务详情列表")
    public Result<List<OrderTask>> getTodayOrder(String sendTime) {
 
        //获取订单列表
        Area area = getArea(securityUtil.getCurrUser().getId());
        if (area == null) {
            return ResultUtil.error("请联系管理员绑定该车辆");
        }
 
        QueryWrapper<OrderTask> wrapper2 = new QueryWrapper<OrderTask>();
        String format = getFormatDate(sendTime);
//        wrapper2.eq("a.area_id",area.getId());
//        wrapper2.eq("a.send_date",format);
//        wrapper2.orderByAsc("a.seq").orderByAsc("a.status");
        //List<OrderTask> list = iOrderTaskService.list2(wrapper2);
        wrapper2.eq("area_id", area.getId());
        wrapper2.eq("send_date", format);
 
        wrapper2.orderByAsc("area_section_id").orderByAsc("seq").orderByAsc("status");
        List<OrderTask> list = iOrderTaskService.list(wrapper2);
 
        return new ResultUtil<List<OrderTask>>().setData(list);
    }
 
    @RequestMapping(value = "/getTodayOrderByHulue", method = RequestMethod.GET)
    @ApiOperation(value = "获取今日配送任务详情列表,忽略某订单,客户端专用接口")
    public Result<OrderListVo> getTodayOrderFromHulue(String orderId, String sendTime,String areaSectionId) {
        //忽略订单
        if (!StrUtil.isEmpty(orderId)) {
            OrderTask orderTask = iOrderTaskService.getById(orderId);
            int seq = orderTask.getSeq();
            orderTask.setSeq(1000+seq);
            iOrderTaskService.saveOrUpdate(orderTask);
        }
        //获取订单列表
        Area area = getArea(securityUtil.getCurrUser().getId());
        if (area == null) {
            return ResultUtil.error("请联系管理员绑定该车辆");
        }
 
        QueryWrapper<OrderTask> wrapper2 = new QueryWrapper<OrderTask>();
        String format = getFormatDate(sendTime);
//        wrapper2.eq("a.area_id",area.getId());
//        wrapper2.eq("a.send_date",format);
//        wrapper2.orderByAsc("a.seq").orderByAsc("a.status");
        //List<OrderTask> list = iOrderTaskService.list2(wrapper2);
        wrapper2.eq("area_id", area.getId());
        wrapper2.eq("send_date", format);
        if(!StrUtil.isEmpty(areaSectionId)){
            wrapper2.eq("area_section_id", areaSectionId);
        }
        wrapper2.orderByAsc("area_section_id").orderByAsc("seq").orderByAsc("status");
        List<OrderTask> list = iOrderTaskService.list(wrapper2);
        int num = 0;
        for (OrderTask obj : list) {
            obj.setCode(obj.getLinker());
            if(obj.getStatus()==1){
                num++;
            }
        }
        String percent = getPercent(num,list.size());
        OrderListVo vo = new OrderListVo();
        vo.setOrderTasks(list);
        vo.setPercent(percent);
        return new ResultUtil<OrderListVo>().setData(vo);
    }
 
    public String getPercent(int num,int totalNum){
        if(totalNum>0){
            BigDecimal number = new BigDecimal(0);
            number=BigDecimal.valueOf((int)num);
            BigDecimal total = new BigDecimal(0);
            total=BigDecimal.valueOf((int)totalNum);
            BigDecimal divide = number.divide(total,2,BigDecimal.ROUND_HALF_UP);
            double bfb = divide.doubleValue();
            int bfbInt = (int)(bfb*100);
            return bfbInt+ "%";
        }else{
            return 0+ "%";
        }
    }
 
    @RequestMapping(value = "/getOneMonthOrderPercent", method = RequestMethod.GET)
    @ApiOperation(value = "获取本月配送百分比")
    public Result<String> getMonthPercent(){
        Area area = getArea(securityUtil.getCurrUser().getId());
        if (area == null) {
            return ResultUtil.error("请联系管理员绑定该车辆");
        }
 
        String todayFormat = DateUtil.format(new Date(), "yyyy-MM-dd");
        String monthFormat = DateUtil.format(DateUtil.beginOfMonth(new Date()),"yyyy-MM-dd");
 
        QueryWrapper<OrderTask> wrapper2 = new QueryWrapper<OrderTask>();
        wrapper2.eq("area_id", area.getId());
        wrapper2.between("send_date",monthFormat,todayFormat);
        int allOrders = iOrderTaskService.count(wrapper2);
 
        wrapper2.eq("status",1);
        int doOrders = iOrderTaskService.count(wrapper2);
 
        return new ResultUtil<String>().setData(getPercent(doOrders,allOrders));
    }
 
    @RequestMapping(value = "/getTodayOrderDetail", method = RequestMethod.GET)
    @ApiOperation(value = "获取当前配送商户详情")
    public Result<OrderTask> getTodayOrderDetail(String orderId, String sendTime) {
        OrderTask orderTask = new OrderTask();
        if (!StrUtil.isEmpty(orderId)) {
            orderTask = iOrderTaskService.getById(orderId);
            if (orderTask.getStatus() != 0) {
                return ResultUtil.error("此商户已配送");
            }
        } else {
            Area area = getArea(securityUtil.getCurrUser().getId());
            if (area == null) {
                return ResultUtil.error("请联系管理员绑定该车辆");
            }
 
            QueryWrapper<OrderTask> wrapper2 = new QueryWrapper<OrderTask>();
            String format = getFormatDate(sendTime);
            wrapper2.eq("area_id", area.getId());
            wrapper2.eq("send_date", format);
            wrapper2.eq("status", 0);
            //wrapper2.orderByAsc("seq");
 
            PageVo page = new PageVo();
            page.setSort("seq");
            page.setOrder("asc");
            page.setPageSize(1);
            page.setPageNumber(0);
            List<OrderTask> list = iOrderTaskService.page(PageUtil.initMpPage(page), wrapper2).getRecords();
            //List<OrderTask> list = iOrderTaskService.list(wrapper2);
            if (list.size() > 0) {
                orderTask = list.get(0);
            } else {
                return new ResultUtil<OrderTask>().setData(null);
            }
        }
 
        orderTask.setCustomer(iCustomerService.getById(orderTask.getCustomerId()));
        QueryWrapper<OrderDetail> wrapper3 = new QueryWrapper<OrderDetail>();
        wrapper3.eq("order_id", orderTask.getId());
        orderTask.setOrderDetails(iOrderDetailService.list(wrapper3));
 
        return new ResultUtil<OrderTask>().setData(orderTask);
    }
 
    @RequestMapping(value = "/getTodayOrderDetailByAreaSection", method = RequestMethod.GET)
    @ApiOperation(value = "获取当前配送商户详情")
    public Result<OrderTask> getTodayOrderDetail(String orderId, String sendTime,String areaSectionId) {
        OrderTask orderTask = new OrderTask();
        if (!StrUtil.isEmpty(orderId)) {
            orderTask = iOrderTaskService.getById(orderId);
            if (orderTask.getStatus() != 0) {
                return ResultUtil.error("此商户已配送");
            }
        } else {
            Area area = getArea(securityUtil.getCurrUser().getId());
            if (area == null) {
                return ResultUtil.error("请联系管理员绑定该车辆");
            }
 
            QueryWrapper<OrderTask> wrapper2 = new QueryWrapper<OrderTask>();
            String format = getFormatDate(sendTime);
            wrapper2.eq("area_id", area.getId());
            wrapper2.eq("send_date", format);
            wrapper2.eq("status", 0);
            if(!StrUtil.isEmpty(areaSectionId)){
                wrapper2.eq("area_section_Id", areaSectionId);
            }
            //wrapper2.orderByAsc("seq");
 
            PageVo page = new PageVo();
            page.setSort("seq");
            page.setOrder("asc");
            page.setPageSize(1);
            page.setPageNumber(0);
            List<OrderTask> list = iOrderTaskService.page(PageUtil.initMpPage(page), wrapper2).getRecords();
            //List<OrderTask> list = iOrderTaskService.list(wrapper2);
            if (list.size() > 0) {
                orderTask = list.get(0);
            } else {
                return new ResultUtil<OrderTask>().setData(null);
            }
        }
 
        orderTask.setCustomer(iCustomerService.getById(orderTask.getCustomerId()));
        QueryWrapper<OrderDetail> wrapper3 = new QueryWrapper<OrderDetail>();
        wrapper3.eq("order_id", orderTask.getId());
        orderTask.setOrderDetails(iOrderDetailService.list(wrapper3));
 
        return new ResultUtil<OrderTask>().setData(orderTask);
    }
 
    @RequestMapping(value = "/getTodayOtherInfo", method = RequestMethod.GET)
    @ApiOperation(value = "获取其它信息")
    public Result<Object> getTodayOtherInfo(String sendTime) {
 
        Area area = getArea(securityUtil.getCurrUser().getId());
        if (area == null) {
            return ResultUtil.error("请联系管理员绑定该车辆");
        }
 
        QueryWrapper<OrderTask> wrapper2 = new QueryWrapper<OrderTask>();
        String format = getFormatDate(sendTime);
        wrapper2.eq("area_id", area.getId());
        wrapper2.eq("send_date", format);
        wrapper2.ne("status", 0);
        wrapper2.orderByAsc("seq");
        int count = iOrderTaskService.count(wrapper2);
 
 
        QueryWrapper<OrderTask> wrapper4 = new QueryWrapper<OrderTask>();
        wrapper4.eq("area_id", area.getId());
        wrapper4.eq("send_date", format);
        int count2 = iOrderTaskService.count(wrapper4);
 
 
        Map<String, Object> map = new HashMap<>();
        map.put("num", count + "/" + count2);
 
        QueryWrapper<OrderTask> wrapper5 = new QueryWrapper<OrderTask>();
        wrapper5.ne("status", 0);
        wrapper5.eq("send_date", format);
 
        PageVo page = new PageVo();
        page.setSort("updateTime");
        page.setOrder("desc");
        page.setPageSize(1);
        page.setPageNumber(0);
        IPage<OrderTask> data = iOrderTaskService.page(PageUtil.initMpPage(page), wrapper5);
        String content = "";
        if (data.getRecords().size() > 0) {
            String userId = data.getRecords().get(0).getUserId();
            User user = userService.get(userId);
 
            QueryWrapper<OrderTask> wrapper6 = new QueryWrapper<OrderTask>();
            wrapper6.eq("user_id", userId);
            wrapper6.eq("send_date", format);
            wrapper6.ne("status", 0);
            int count6 = iOrderTaskService.count(wrapper6);
            if (count6 > 0) {
                content += user.getNickname() + "已配送" + count6 + "单";
            }
        }
        map.put("content", content);
 
        map.put("lng", "");
        map.put("lat", "");
        String deptId = securityUtil.getCurrUser().getDepartmentId();
        if (StrUtil.isNotEmpty(deptId)) {
            Department department = departmentService.get(deptId);
            map.put("lng", department.getLng());
            map.put("lat", department.getLat());
        }
        return new ResultUtil<Object>().setData(map);
    }
 
    @RequestMapping(value = "/addImg", method = RequestMethod.POST)
    @ApiOperation(value = "上传门头照")
    public Object saveOrUpdate(String orderId, String imgUrl) {
        OrderTask orderTask = iOrderTaskService.getById(orderId);
        orderTask.setImg(imgUrl);
        iOrderTaskService.saveOrUpdate(orderTask);
        return ResultUtil.success("添加成功");
    }
 
    @RequestMapping(value = "/signFor", method = RequestMethod.POST)
    @ApiOperation(value = "签收")
    public Object signFor(String orderId, int status, String content, String customerReceiveId, int time, String carId) {
        if (status == 1) {
            if (StrUtil.isEmpty(customerReceiveId)) {
                return ResultUtil.error("正常签收,接货人id必填");
            }
        }
 
        if (StrUtil.isEmpty(carId)) {
            return ResultUtil.error("车辆id不能为空");
        }
        OrderTask orderTask = iOrderTaskService.getById(orderId);
        orderTask.setStatus(status);
        orderTask.setUserId(securityUtil.getCurrUser().getId());
        orderTask.setTime(time);
        orderTask.setCarId(carId);
        if (StrUtil.isNotEmpty(customerReceiveId)) {
            orderTask.setCustomerReceiveId(customerReceiveId);
        }
 
        if (!StrUtil.isEmpty(content)) {
            orderTask.setRemarks(content);
        } else {
            if (status == 2) {
                return ResultUtil.error("请填写异常签收原因");
            }
        }
        iOrderTaskService.saveOrUpdate(orderTask);
 
        Car car = iCarService.getById(orderTask.getCarId());
        EventLog eventLog = new EventLog();
        eventLog.setCarNo(car.getCarNo());
        eventLog.setRefId(orderId);
        eventLog.setType(6);//6:配送完成
        iEventLogService.saveOrUpdate(eventLog);
 
        saveLog();
        return ResultUtil.success("添加成功");
    }
 
    @RequestMapping(value = "/signForX", method = RequestMethod.POST)
    @ApiOperation(value = "签收")
    public Object signForX(String orderId, int status, String content, String customerReceiveId, int time, String carId,String img) {
        if (status == 1) {
            if (StrUtil.isEmpty(customerReceiveId)) {
                return ResultUtil.error("正常签收,接货人id必填");
            }
        }
 
        if (StrUtil.isEmpty(carId)) {
            return ResultUtil.error("车辆id不能为空");
        }
        OrderTask orderTask = iOrderTaskService.getById(orderId);
        orderTask.setStatus(status);
        orderTask.setUserId(securityUtil.getCurrUser().getId());
        orderTask.setTime(time);
        orderTask.setCarId(carId);
        if (StrUtil.isNotEmpty(customerReceiveId)) {
            orderTask.setCustomerReceiveId(customerReceiveId);
        }
        if(StrUtil.isNotEmpty(img)){
            if(StrUtil.isNotEmpty(orderTask.getImg())){
                String imgO = orderTask.getImg();
                orderTask.setImg(imgO+","+img);
            }else{
                orderTask.setImg(","+img);
            }
        }
 
        if (!StrUtil.isEmpty(content)) {
            orderTask.setRemarks(content);
        } else {
            if (status == 2) {
                return ResultUtil.error("请填写异常签收原因");
            }
        }
        iOrderTaskService.saveOrUpdate(orderTask);
 
        Car car = iCarService.getById(orderTask.getCarId());
        EventLog eventLog = new EventLog();
        eventLog.setCarNo(car.getCarNo());
        eventLog.setRefId(orderId);
        eventLog.setType(6);//6:配送完成
        iEventLogService.saveOrUpdate(eventLog);
 
        saveLog();
        return ResultUtil.success("添加成功");
    }
 
    private void saveLog() {
        QueryWrapper<OrderLog> wp = new QueryWrapper<>();
        wp.eq("type", 2);
        OrderLog one = iOrderLogService.getOne(wp);
        if (one != null) {
            one.setNum(one.getNum() + 1);
        } else {
            one = new OrderLog();
            one.setNum(1);
            one.setType(2);
        }
        iOrderLogService.saveOrUpdate(one);
    }
 
    @RequestMapping(value = "/getTaKan", method = RequestMethod.GET)
    @ApiOperation(value = "获取踏勘商户")
    public Result<List<Customer>> getTaKan() {
        Area area = getArea(securityUtil.getCurrUser().getId());
        if (area == null) {
            return ResultUtil.error("请联系管理员绑定该车辆");
        }
 
        QueryWrapper<Customer> wrapper2 = new QueryWrapper<Customer>();
        wrapper2.eq("area_id", area.getId());
        wrapper2.eq("kstatus", 0);
        List<Customer> list = iCustomerService.list(wrapper2);
        return new ResultUtil<List<Customer>>().setData(list);
    }
 
    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<OrderTask>> getByPage(PageVo page) {
 
        IPage<OrderTask> data = iOrderTaskService.page2(PageUtil.initMpPage(page));
        return new ResultUtil<IPage<OrderTask>>().setData(data);
    }
 
    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<OrderTask> saveOrUpdate(OrderTask orderTask) {
 
        if (iOrderTaskService.saveOrUpdate(orderTask)) {
            return new ResultUtil<OrderTask>().setData(orderTask);
        }
        return new ResultUtil<OrderTask>().setErrorMsg("操作失败");
    }
 
    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@RequestParam String[] ids) {
 
        for (String id : ids) {
            iOrderTaskService.removeById(id);
        }
        return ResultUtil.success("批量通过id删除数据成功");
    }
 
    @RequestMapping(value = "/insertOrders", method = RequestMethod.GET)
    @ApiOperation(value = "解析订单")
    public Result<Object> insertOrders(String sendTime) {
        if(b){
            return ResultUtil.error("正在处理");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = null;
        try {
            parse = sdf.parse(sendTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String formatDate = getFormatDate(sendTime);
        QueryWrapper<OrderTask> wrapper2 = new QueryWrapper<OrderTask>();
        wrapper2.eq("order_date", formatDate);
        List<OrderTask> list = iOrderTaskService.list(wrapper2);
        if(list.size() > 0){
            return ResultUtil.error("已存在今日数据");
        }else {
            QueryWrapper<OrderTaskOriginal> queryWrapper = new QueryWrapper<>();
            queryWrapper.between("order_date",DateUtil.beginOfDay(parse),DateUtil.endOfDay(parse));
            List<OrderTaskOriginal> originals = iOrderTaskOriginalService.list(queryWrapper);
            if(originals.size()>0){
                b=true;
            }
            int sumNum = 0;
            for(OrderTaskOriginal original:originals){
                try {
                    OrderTask orderTask = new OrderTask();
                    orderTask.setCode(original.getCode());
                    Area area = getArea2(original.getAreaName());
                    orderTask.setAreaId(area.getId());
                    orderTask.setCarId(area.getCarId());
                    orderTask.setAreaSectionId(getAreaSection(orderTask.getAreaId(),original.getAreaSection()));
                    Customer customer = getCustomer(original.getCustomerCode(), original.getCustomerName(), original.getCustomerPhone(), original.getLinker(), orderTask.getAreaId(), orderTask.getAreaSectionId());
                    orderTask.setCustomerId(customer.getId());
                    orderTask.setCustomerName(customer.getName());
                    orderTask.setLinker(customer.getLinker());
                    orderTask.setEndDate(original.getPacketDate());
                    //orderTask.setSendDate(original.getSendDate());
                    orderTask.setSendDate(DateUtil.offsetDay(original.getOrderDate(),1));
                    orderTask.setOrderDate(original.getOrderDate());
                    if(original.getSeq()!=null){
                        orderTask.setSeq(original.getSeq());
                    }else{
                        orderTask.setSeq(999);
                    }
 
                    orderTask.setNum(original.getNum());
                    sumNum+=original.getNum();
 
                    iOrderTaskService.saveOrUpdate(orderTask);
 
                    updateCustomerRecive(orderTask.getCustomerId(),original.getLinker(),original.getCustomerPhone());
 
                    QueryWrapper<OrderDetailOriginal> wrapper = new QueryWrapper<>();
                    wrapper.eq("order_id",original.getId());
                    List<OrderDetailOriginal> list1 = iOrderDetailOriginalService.list(wrapper);
 
                    List<OrderDetail> detailList = new ArrayList<>();
                    for(OrderDetailOriginal o:list1){
                        OrderDetail orderDetail = new OrderDetail();
                        orderDetail.setNum(o.getNum());
                        orderDetail.setOrderId(orderTask.getId());
                        orderDetail.setProduct(o.getProductName());
                        detailList.add(orderDetail);
                    }
                    iOrderDetailService.saveBatch(detailList);
                }catch (Exception e){
                    System.out.println(original.getId()+"报错了");
                }
            }
 
            OrderLog orderLog = new OrderLog();
            orderLog.setNum(sumNum);
            orderLog.setType(0);
            iOrderLogService.save(orderLog);
            System.out.println("处理订单结束");
            b=false;
 
 
            return ResultUtil.success("导入成功");
        }
 
 
    }
    public void updateCustomerRecive(String customerId,String linker,String phone){
        QueryWrapper<CustomerReceive> wrapper = new QueryWrapper<>();
        wrapper.eq("customer_id",customerId);
        wrapper.eq("name",linker);
        List<CustomerReceive> list = iCustomerReceiveService.list(wrapper);
        if(list.size()==0){
            CustomerReceive one = new CustomerReceive();
            one.setCustomerId(customerId);
            one.setFstatus(0);
            one.setStatus(1);
            one.setName(linker);
            one.setPhone(phone);
            iCustomerReceiveService.saveOrUpdate(one);
        }
    }
 
    public Customer getCustomer(String customerCode,String customerName,String customerPhone,String linker,String areaId,String areaSectionId){
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("licence",customerCode);
        Customer one = iCustomerService.getOne(queryWrapper);
        if(one==null){
            Customer customer = new Customer();
            customer.setLicence(customerCode);
            customer.setName(customerName);
            customer.setPhone(customerPhone);
            customer.setLinker(linker);
            customer.setAreaId(areaId);
            customer.setAreaSectionId(areaSectionId);
            customer.setKstatus(1);
            iCustomerService.saveOrUpdate(customer);
            return customer;
        }
        one.setAreaId(areaId);
        one.setAreaSectionId(areaSectionId);
        iCustomerService.saveOrUpdate(one);
        return one;
    }
 
    public Area getArea2(String name){
        QueryWrapper<Area> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name);
        Area one = iAreaService.getOne(queryWrapper);
        if(one==null){
            Area area = new Area();
            area.setName(name);
            iAreaService.saveOrUpdate(area);
            return area;
        }
        return one;
    }
 
    public String getAreaSection(String areaId,String name){
        QueryWrapper<AreaSection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name);
        queryWrapper.eq("area_id",areaId);
        AreaSection one = iAreaSectionService.getOne(queryWrapper);
        if(one==null){
            AreaSection area = new AreaSection();
            area.setName(name);
            area.setAreaId(areaId);
            iAreaSectionService.saveOrUpdate(area);
            return area.getId();
        }
        return one.getId();
    }
 
 
    public String dateStringFormat(Date strDate) {
        if (strDate == null)
            return "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(strDate)+" 00:00:00";
    }
 
 
    public String dateStringFormat2(Date strDate) {
        if (strDate == null)
            return "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(strDate)+" 23:59:59";
    }
 
    @GetMapping("/outExcel")//  导出的不是中间表
    @ApiOperation(value = "导出签收统计信息表", notes = "导出签收统计信息表")
    public void export1(OrderStatusCount orderStatusCount, HttpServletResponse response) throws IOException {
        List<OrderStatusCount> orderStatus = this.iOrderTaskService.countStatus(orderStatusCount);
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("签收数量统计报表", "UTF-8");
        String fileName2 = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
//        response.setHeader("Content-dispostion", "attachment;filename="+fileName+".xls");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName2 + ".xlsx");
        EasyExcel.write(response.getOutputStream(), OrderStatusCount.class).
                sheet(0).doWrite(orderStatus);
    }
 
    @GetMapping(value = "/count")
    public Result<List<OrderStatusCount>> getAll(OrderStatusCount orderStatusCount) {
        List<OrderStatusCount> data = this.iOrderTaskService.countStatus(orderStatusCount);
        ;
        return new ResultUtil<List<OrderStatusCount>>().setData(data);
    }
    @Autowired
    private ISignCountService iSignCountService;
 
    @RequestMapping(value = "/getStatusCounts", method = RequestMethod.GET)
    @ApiOperation(value = "获取今日任务统计信息")
    public Result<Object> getStatusCounts() {
        System.err.println("执行存储数据定时任务时间: " + LocalDateTime.now());
       OrderStatusCount orderStatusCount = new OrderStatusCount();
        List<OrderStatusCount> StatusCounts = new ArrayList<>();
        List<OrderStatusCount> temps = iOrderTaskService.countStatus(orderStatusCount);
        for (OrderStatusCount statusCount : temps) {
            if("丰南02车".equals(statusCount.getLine())){
                StatusCounts.add(statusCount);
            }
        }
        if (StatusCounts != null && StatusCounts.size() != 0) {
            List<SignCount> list = iSignCountService.list();
            for (OrderStatusCount statusCount : StatusCounts) {
                SignCount signCount = new SignCount();
                boolean flag = false;
                for (SignCount signCountS : list) {
                    if (signCountS.getTaskId().equals(statusCount.getId())) {
                        signCount.setId(signCountS.getId());
                        signCount.setCarName(statusCount.getCarName());
                        signCount.setAmount(statusCount.getAmount());
                        signCount.setSignCount(statusCount.getSignCount());
                        signCount.setAbnormalSign(statusCount.getAbnormalSign());
                        signCount.setBatch(statusCount.getBatch());
                        signCount.setLine(statusCount.getLine());
                        signCount.setNotSignCount(statusCount.getNotSignCount());
                        String SendDate=getBeforeOneDayByStringDate(statusCount.getSendDate());
                        signCount.setSendDate(SendDate);
                        signCount.setProportion(statusCount.getProportion());
                        signCount.setUserName(statusCount.getUserName());
                        signCount.setTaskId(statusCount.getId());
                        iSignCountService.saveOrUpdate(signCount);
                        System.out.println("更新成功");
                        flag = true;
                        break;
                    }
                }
                if (flag) {
                    continue;
                }
                signCount.setCarName(statusCount.getCarName());
                signCount.setAmount(statusCount.getAmount());
                signCount.setSignCount(statusCount.getSignCount());
                signCount.setAbnormalSign(statusCount.getAbnormalSign());
                signCount.setBatch(statusCount.getBatch());
                signCount.setLine(statusCount.getLine());
                signCount.setNotSignCount(statusCount.getNotSignCount());
 
                String SendDate=getBeforeOneDayByStringDate(statusCount.getSendDate());
                signCount.setSendDate(SendDate);
                signCount.setProportion(statusCount.getProportion());
                signCount.setUserName(statusCount.getUserName());
                signCount.setTaskId(statusCount.getId());
                if (iSignCountService.saveOrUpdate(signCount)) {
                    System.out.println("定时存储成功");
                }
            }
        }
        return null;
    }
 
    private String getBeforeOneDayByStringDate(String dateString) {
        String times = "";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date d;
        try {
            d = format.parse(dateString);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(d);
            calendar.add(Calendar.DAY_OF_MONTH, -1);  // 在当前日基础上-1
            String format1 = format.format(calendar.getTime());
            times = format1;
            //获取String类型的时间
 
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return times;
    }
}