liujie
13 小时以前 74f8b8074a2fb391b5363b4dca5f99bf31993430
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
package com.stylefeng.guns.modular.system.controller.general;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.SuccessTip;
import com.stylefeng.guns.core.common.constant.state.ManagerStatus;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.util.SinataUtil;
import com.stylefeng.guns.modular.system.controller.resp.TBranchOfficeResp;
import com.stylefeng.guns.modular.system.enums.StatusEnum;
import com.stylefeng.guns.modular.system.model.*;
import com.stylefeng.guns.modular.system.service.*;
import com.stylefeng.guns.modular.system.util.DateUtil;
import com.stylefeng.guns.modular.system.util.RedisUtil;
import com.stylefeng.guns.modular.system.util.ResultUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
 
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
 
/**
 * 控制器
 *
 * @author fengshuonan
 * @Date 2023-02-21 11:17:18
 */
@Controller
@RequestMapping("/tBranchOffice")
public class TBranchOfficeController extends BaseController {
 
    private String PREFIX = "/system/tBranchOffice/";
 
    @Autowired
    private ITBranchOfficeService tBranchOfficeService;
    @Autowired
    private ITSystemConfigService tSystemConfigService;
 
    @Autowired
    private ITRegionService tRegionService;
    @Autowired
    private ITDriverService tDriverService;
    @Autowired
    private ITDriverWorkService tDriverWorkService;
    @Autowired
    private IUserService userService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ITBranchOfficeAreaService branchOfficeAreaService;
 
    /**
     * 跳转到首页
     */
    @RequestMapping("")
    public String index(Model model) {
        model.addAttribute("userType", Objects.requireNonNull(ShiroKit.getUser()).getRoleType());
        return PREFIX + "tBranchOffice.html";
    }
 
    /**
     * 跳转到添加
     */
    @RequestMapping("/tBranchOffice_add")
    public String tBranchOfficeAdd(Model model) {
        List<TRegion> provinceList = tRegionService.selectList(new EntityWrapper<TRegion>().eq("parent_id", 0));
        model.addAttribute("provinceList",provinceList);
        return PREFIX + "tBranchOffice_add.html";
    }
 
    /**
     * 跳转到修改
     */
    @RequestMapping("/tBranchOffice_update/{tBranchOfficeId}")
    public String tBranchOfficeUpdate(@PathVariable Integer tBranchOfficeId, Model model) {
 
        TBranchOffice tBranchOffice = tBranchOfficeService.selectById(tBranchOfficeId);
        TBranchOfficeResp tBranchOfficeResp = new TBranchOfficeResp();
        BeanUtils.copyProperties(tBranchOffice,tBranchOfficeResp);
 
        // 查询区域
//        TRegion city = tRegionService.selectOne(new EntityWrapper<TRegion>().eq("code", tBranchOffice.getCityCode())
//                .last("LIMIT 1"));
//        TRegion district = tRegionService.selectOne(new EntityWrapper<TRegion>().eq("code", tBranchOffice.getDistrictCode())
//                .last("LIMIT 1"));
//
//        if(StringUtils.hasLength(tBranchOffice.getDistrictName()) && !tBranchOffice.getDistrictName().equals(tBranchOffice.getCityName())){
//            tBranchOfficeResp.setArea(tBranchOffice.getProvinceName()+"/"+tBranchOffice.getCityName()+"/"+tBranchOffice.getDistrictName());
//        }else {
//            tBranchOfficeResp.setArea(tBranchOffice.getProvinceName()+"/"+tBranchOffice.getCityName());
//        }
//
//        if(Objects.nonNull(city)){
//            if(Objects.nonNull(district) && !district.getCode().equals(city.getCode())){
//                tBranchOfficeResp.setAreaId(city.getParentId()+"/"+city.getId()+"/"+district.getId());
//            }else {
//                tBranchOfficeResp.setAreaId(city.getParentId()+"/"+city.getId());
//            }
//        }
 
        model.addAttribute("item",tBranchOfficeResp);
        LogObjectHolder.me().set(tBranchOffice);
 
        TSystemConfig tSystemConfig = tSystemConfigService.selectOne(new EntityWrapper<TSystemConfig>().eq("type", 5)
                .eq("companyId", tBranchOfficeId).last("LIMIT 1"));
        if(null != tSystemConfig){
            JSONObject jsonObject = JSONObject.parseObject(tSystemConfig.getContent());
            JSONArray array = jsonObject.getJSONArray("ChargeStandard");
            JSONArray objects = new JSONArray();
            for (int i = 0; i < array.size(); i++) {
                JSONObject jsonObject1 = JSONObject.parseObject(JSONObject.toJSONString(array.get(i)));
                if(i == 0){
                    jsonObject1.put("key",0);
                }else {
                    jsonObject1.put("key",1);
                }
                objects.add(jsonObject1);
            }
            TSystemConfig config = tSystemConfigService.selectOne(new EntityWrapper<TSystemConfig>().eq("type", 8)
                    .eq("companyId", tBranchOfficeId).last("LIMIT 1"));
            JSONObject jsonObj = JSONObject.parseObject(config.getContent());
            Integer zcOne = jsonObj.getInteger("num1");
            String serviceTel = jsonObj.getString("num2");
            Integer zcTwo = jsonObj.getInteger("num3");
            model.addAttribute("zcOne", zcOne);
            model.addAttribute("serviceTel", serviceTel);
            model.addAttribute("zcTwo", zcTwo);
            model.addAttribute("array",objects);
            JSONObject extraCost = JSONObject.parseObject(jsonObject.getString("ExtraCost"));
            model.addAttribute("ExtraCost",extraCost);
        }else{
            model.addAttribute("zcOne",1);
            model.addAttribute("serviceTel", "");
            model.addAttribute("zcTwo", 1);
            model.addAttribute("array",null);
            model.addAttribute("ExtraCost",null);
        }
 
        //省
        List<TRegion> provinceList = tRegionService.selectList(new EntityWrapper<TRegion>().eq("parent_id", 0));
        model.addAttribute("provinceList",provinceList);
 
        //经营区域
        AtomicInteger counter = new AtomicInteger(1);
        List<TBranchOfficeArea> branchOfficeAreaList = branchOfficeAreaService.selectList(new EntityWrapper<TBranchOfficeArea>().eq("branchOfficeId", tBranchOfficeId));
        branchOfficeAreaList.stream().forEach(item->{
            item.setCreateTimeStr(DateUtil.format(item.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
            item.setCounter(counter.getAndIncrement());
        });
        model.addAttribute("scopeList",branchOfficeAreaList);
 
        return PREFIX + "tBranchOffice_edit.html";
    }
 
    /**
     * 跳转到详情
     */
    @RequestMapping("/tBranchOfficeDetail")
    public String tBranchOfficeDetail(Integer tBranchOfficeId, Model model) {
        Integer roleType = Objects.requireNonNull(ShiroKit.getUser()).getRoleType();
        Integer objectId = Objects.requireNonNull(ShiroKit.getUser()).getObjectId();
        tBranchOfficeService.tBranchOfficeDetail(tBranchOfficeId,model);
        TSystemConfig tSystemConfig = tSystemConfigService.selectOne(new EntityWrapper<TSystemConfig>().eq("type", 3)
                .eq("companyType",2)
                .eq("companyId",tBranchOfficeId)
                .last("LIMIT 1"));
        JSONObject jsonObject = JSONObject.parseObject(tSystemConfig.getContent());
//        model.addAttribute("num2",jsonObject.getInteger("num2"));
        Integer num4 = jsonObject.getInteger("num4");
        if(num4 == 1){
            model.addAttribute("num3",jsonObject.getString("num2"));
        }else {
            model.addAttribute("num3",jsonObject.getString("num3"));
        }
 
        TSystemConfig tSystemConfig1 = tSystemConfigService.selectOne(new EntityWrapper<TSystemConfig>().eq("type", 5)
                .eq("companyId", tBranchOfficeId)
                .eq("companyType", 2)
                .last("LIMIT 1"));
        if(null != tSystemConfig1){
            JSONObject jsonObject2 = JSONObject.parseObject(tSystemConfig1.getContent());
            JSONArray array = jsonObject2.getJSONArray("ChargeStandard");
            JSONArray objects = new JSONArray();
            for (int i = 0; i < array.size(); i++) {
                JSONObject jsonObject1 = JSONObject.parseObject(JSONObject.toJSONString(array.get(i)));
                if(i == 0){
                    jsonObject1.put("key",0);
                }else {
                    jsonObject1.put("key",1);
                }
                objects.add(jsonObject1);
            }
            TSystemConfig config = tSystemConfigService.selectOne(new EntityWrapper<TSystemConfig>().eq("type", 8)
                    .eq("companyId", tBranchOfficeId).last("LIMIT 1"));
            JSONObject jsonObj = JSONObject.parseObject(config.getContent());
            Integer zcOne = jsonObj.getInteger("num1");
            String serviceTel = jsonObj.getString("num2");
            Integer zcTwo = jsonObj.getInteger("num3");
            model.addAttribute("zcOne", zcOne);
            model.addAttribute("serviceTel", serviceTel);
            model.addAttribute("zcTwo", zcTwo);
            model.addAttribute("array",objects);
            JSONObject extraCost = JSONObject.parseObject(jsonObject2.getString("ExtraCost"));
            model.addAttribute("ExtraCost",extraCost);
        }else{
            model.addAttribute("zcOne",1);
            model.addAttribute("serviceTel", "");
            model.addAttribute("zcTwo", 1);
            model.addAttribute("array",null);
            model.addAttribute("ExtraCost",null);
        }
        return PREFIX + "tBranchOfficeDetail.html";
    }
 
    /**
     * 跳转到抽成规则
     */
    @RequestMapping("/extractionRules")
    public String extractionRules(Model model,Integer tBranchOfficeId) {
        Integer roleType = ShiroKit.getUser().getRoleType();
        Integer objectId = ShiroKit.getUser().getObjectId();
        model.addAttribute("roleType",roleType);
        model.addAttribute("branchOfficeId",tBranchOfficeId);
        TSystemConfig tSystemConfig = tSystemConfigService.selectOne(new EntityWrapper<TSystemConfig>().eq("type", 3)
                .eq("companyType",1)
                .eq("companyId",1)
                .eq("branchOfficeId",tBranchOfficeId)
                .last("LIMIT 1"));
        if(Objects.nonNull(tSystemConfig)){
            JSONObject jsonObject = JSONObject.parseObject(tSystemConfig.getContent());
            model.addAttribute("num1",jsonObject.getInteger("num1"));
            model.addAttribute("num2",jsonObject.getInteger("num2"));
            model.addAttribute("num3",jsonObject.getString("num3"));
            model.addAttribute("num4",jsonObject.getString("num4"));
            model.addAttribute("num5",jsonObject.getString("num5"));
        }else {
            model.addAttribute("num1",0);
            model.addAttribute("num2",0);
            model.addAttribute("num3",0);
            model.addAttribute("num4",0);
            model.addAttribute("num5",0);
        }
        TSystemConfig tSystemConfig1 = tSystemConfigService.selectOne(new EntityWrapper<TSystemConfig>().eq("type", 3)
                .eq("companyType",2)
                .eq("companyId",tBranchOfficeId)
                .last("LIMIT 1"));
        if(Objects.nonNull(tSystemConfig1)){
            JSONObject jsonObject = JSONObject.parseObject(tSystemConfig1.getContent());
            model.addAttribute("type",jsonObject.getInteger("num4"));
        }else {
            model.addAttribute("type",1);
        }
        return PREFIX + "tSystemConfigExtractionRules.html";
    }
 
    /**
     * 获取列表
     */
    @RequestMapping(value = "/systemConfigSubmit")
    @ResponseBody
    public Object systemConfigSubmit(Integer type,String content,Integer branchOfficeId) {
        Integer roleType = Objects.requireNonNull(ShiroKit.getUser()).getRoleType();
        Integer objectId = Objects.requireNonNull(ShiroKit.getUser()).getObjectId();
        TSystemConfig tSystemConfig = tSystemConfigService.selectOne(new EntityWrapper<TSystemConfig>().eq("type", 3)
                    .eq("companyType",1)
                    .eq("companyId",1)
                    .eq("branchOfficeId",branchOfficeId)
                    .last("LIMIT 1"));
        if(Objects.nonNull(tSystemConfig)){
            tSystemConfig.setContent(content);
            tSystemConfig.setCompanyId(objectId);
            tSystemConfig.setType(type);
            tSystemConfig.setCompanyType(1);
            tSystemConfig.setBranchOfficeId(branchOfficeId);
            tSystemConfigService.updateById(tSystemConfig);
        }else {
            tSystemConfig = new TSystemConfig();
            tSystemConfig.setContent(content);
            tSystemConfig.setCompanyId(objectId);
            tSystemConfig.setType(type);
            tSystemConfig.setCompanyType(1);
            tSystemConfig.setBranchOfficeId(branchOfficeId);
            tSystemConfigService.insert(tSystemConfig);
        }
        return SUCCESS_TIP;
    }
 
    /**
     * 跳转区域页面新增
     */
    @RequestMapping("/areaPageAdd")
    public String areaPageAdd(String area,String areaId,Model model) {
        String[] split1 = areaId.split("/");
        List<TRegion> tRegions = tRegionService.selectList(new EntityWrapper<TRegion>().eq("parent_id", 0));
        model.addAttribute("provinceList",tRegions);
//        List<Integer> provinceIds = tRegions.stream().map(TRegion::getId).collect(Collectors.toList());
//        // 查询市
//        List<TRegion> tRegions1 = tRegionService.selectList(new EntityWrapper<TRegion>().eq("parent_id", split1[0]));
//        model.addAttribute("cityList",tRegions1);
//
//        // 查询区
//        List<Integer> cityIds = tRegions1.stream().map(TRegion::getId).collect(Collectors.toList());
//        List<TRegion> tRegions2 = tRegionService.selectList(new EntityWrapper<TRegion>().in("parent_id", cityIds));
//        model.addAttribute("districtList",tRegions2);
 
        if(StringUtils.hasLength(area) && StringUtils.hasLength(areaId)){
            String[] split = area.split("/");
            model.addAttribute("provinceName",split[0]);
            model.addAttribute("cityName",split[1]);
            if(split.length>2){
                model.addAttribute("districtName",split[2]);
            }else {
                model.addAttribute("districtName","");
            }
 
            model.addAttribute("provinceId",split1[0]);
            model.addAttribute("cityId",split1[1]);
            if(split1.length>2) {
                model.addAttribute("districtId", split1[2]);
            }else {
                model.addAttribute("districtId", "");
            }
 
            List<TRegion> tRegions1 = tRegionService.selectList(new EntityWrapper<TRegion>().eq("parent_id", split1[0]));
            model.addAttribute("cityList",tRegions1);
 
            // 查询区
            List<TRegion> tRegions2 = tRegionService.selectList(new EntityWrapper<TRegion>().in("parent_id", split1[1]));
            model.addAttribute("districtList",tRegions2);
        }else {
            model.addAttribute("cityList",new ArrayList<>());
            model.addAttribute("districtList",new ArrayList<>());
 
            model.addAttribute("provinceName","");
            model.addAttribute("cityName","");
            model.addAttribute("districtName","");
 
            model.addAttribute("provinceId","");
            model.addAttribute("cityId","");
            model.addAttribute("districtId", "");
        }
        return PREFIX + "tBranchOfficeAreaAdd.html";
    }
 
    /**
     * 跳转区域页面编辑
     */
    @RequestMapping("/areaPageUpdate")
    public String areaPageUpdate(String area,String areaId,Model model) {
 
        String[] split = area.split("/");
        model.addAttribute("provinceName",split[0]);
        model.addAttribute("cityName",split[1]);
        if(split.length>2){
            model.addAttribute("districtName",split[2]);
        }else {
            model.addAttribute("districtName","");
        }
 
        String[] split1 = areaId.split("/");
        String provinceId = split1[0];
        model.addAttribute("provinceId",split1[0]);
        model.addAttribute("cityId",split1[1]);
        if(split1.length>2) {
            model.addAttribute("districtId", split1[2]);
        }else {
            model.addAttribute("districtId", "");
        }
 
        List<TRegion> tRegions = tRegionService.selectList(new EntityWrapper<TRegion>().eq("parent_id", 0));
        model.addAttribute("provinceList",tRegions);
//        List<Integer> provinceIds = tRegions.stream().map(TRegion::getId).collect(Collectors.toList());
        // 查询市
        List<TRegion> tRegions1 = tRegionService.selectList(new EntityWrapper<TRegion>().eq("parent_id", provinceId));
        model.addAttribute("cityList",tRegions1);
 
        // 查询区
        List<Integer> cityIds = tRegions1.stream().map(TRegion::getId).collect(Collectors.toList());
        List<TRegion> tRegions2 = tRegionService.selectList(new EntityWrapper<TRegion>().in("parent_id", split1[1]));
        model.addAttribute("districtList",tRegions2);
 
        return PREFIX + "tBranchOfficeAreaUpdate.html";
    }
 
    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String branchOfficeName,String principal,String principalPhone,Integer operatingBusiness ,Integer status) {
        List<TBranchOfficeResp> tBranchOfficeRespList = tBranchOfficeService.getPageList(branchOfficeName,principal,principalPhone,operatingBusiness,status);
        for (TBranchOfficeResp tBranchOfficeResp : tBranchOfficeRespList) {
            tBranchOfficeResp.setUserType(Objects.requireNonNull(ShiroKit.getUser()).getRoleType());
        }
        // 分公司查询优惠券,订单,司机等信息
        tBranchOfficeService.queryOtherInfo(tBranchOfficeRespList);
        return tBranchOfficeRespList;
    }
 
    @ApiOperation(value = "市区查询",notes="市区查询")
    @RequestMapping(value = "/areaCity")
    @ResponseBody
    public Object areaCity(Integer parentId,Model model) {
        List<TRegion> tRegions = tRegionService.selectList(new EntityWrapper<TRegion>().eq("parent_id", parentId));
        /*if(CollectionUtils.isEmpty(tRegions) && StringUtils.hasLength(parentName)){
            TRegion parent = tRegionService.selectOne(new EntityWrapper<TRegion>().eq("name", parentName));
            tRegions = tRegionService.selectList(new EntityWrapper<TRegion>().eq("parent_id", parentId));
        }*/
        model.addAttribute("list",tRegions);
        return tRegions;
    }
 
    /**
     * 启用分公司
     */
    @RequestMapping(value = "/start")
    @ResponseBody
    public Object start(Integer id) {
        // 查询分公司区域
        int count = branchOfficeAreaService.selectCount(new EntityWrapper<TBranchOfficeArea>()
                .eq("branchOfficeId", id));
        if(count == 0){
            return new SuccessTip(500,"请配置区域后再启用该分公司!");
        }
        TBranchOffice tBranchOffice = tBranchOfficeService.selectById(id);
        tBranchOffice.setStatus(StatusEnum.NORMAL.getCode());
        tBranchOfficeService.updateById(tBranchOffice);
        return SUCCESS_TIP;
    }
 
    @RequestMapping(value = "/change")
    @ResponseBody
    public Object change(@RequestParam Integer code) {
        List<TRegion> list = new ArrayList<>();
        if (SinataUtil.isNotEmpty(code)){
            TRegion region = tRegionService.selectOne(new EntityWrapper<TRegion>().eq("code", code));
            list = tRegionService.selectList(new EntityWrapper<TRegion>().eq("parent_id", region.getId()));
        }
        return list;
    }
 
    /**
     * 冻结分公司
     */
    @RequestMapping(value = "/stop")
    @ResponseBody
    public Object updateStatus(Integer id) {
        TBranchOffice tBranchOffice = tBranchOfficeService.selectById(id);
        tBranchOffice.setStatus(StatusEnum.FREEZE.getCode());
        tBranchOfficeService.updateById(tBranchOffice);
        List<TDriver> list = tDriverService.selectList(new EntityWrapper<TDriver>()
                .eq("branchOfficeId", tBranchOffice.getId()));
        for (TDriver tDriver : list) {
            String value = redisUtil.getValue("DRIVER_" + tDriver.getPhone());
            redisUtil.remove(value);
            redisUtil.remove("DRIVER_" + tDriver.getPhone());
            TDriverWork tDriverWork = tDriverWorkService.selectOne(new EntityWrapper<TDriverWork>()
                    .eq("driverId", tDriver.getId())
                    .eq("status", 1)
                    .orderBy("workTime", false)
                    .last("LIMIT 1"));
            if(Objects.nonNull(tDriverWork)){
                tDriverWork.setStatus(2);
                tDriverWork.setOffWorkTime(new Date());
                tDriverWorkService.updateById(tDriverWork);
            }
        }
        // 删除区域
        branchOfficeAreaService.delete(new EntityWrapper<TBranchOfficeArea>().eq("branchOfficeId", id));
        return SUCCESS_TIP;
    }
 
    /**
     * 获取列表
     */
    @RequestMapping(value = "/list-back")
    @ResponseBody
    public Object listBack(String condition) {
        return tBranchOfficeService.selectList(null);
    }
 
    /**
     * 新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    public Object add(TBranchOffice tBranchOffice,String subArr, Integer systemPriceType, String systemPriceContent, Integer isTaxiCar, Integer holiday, String serviceTel) {
        int count = tBranchOfficeService.selectCount(new EntityWrapper<TBranchOffice>().eq("branchOfficeName", tBranchOffice.getBranchOfficeName()).ne("status", 3));
        if(count>0){
            return new SuccessTip(500,"该分公司名称已存在!");
        }
        int count1 = userService.selectCount(new EntityWrapper<User>().eq("account", tBranchOffice.getAccount()).ne("status", 3));
        if (count1 > 0){
            return new SuccessTip(500,"该账号已存在!");
        }
//        Object o = tBranchOfficeService.addOrUpdate(tBranchOffice);
//        if(Objects.nonNull(o)){
//            return o;
//        }
        // 判断区域是否已存在
        Object o = addScopeIsExit(subArr, tBranchOffice);
        if(Objects.nonNull(o)){
            return o;
        }
        tBranchOffice.setPrincipal(tBranchOffice.getPrincipal().replace(" ",""));
        tBranchOffice.setStatus(StatusEnum.NORMAL.getCode());
 
        tBranchOfficeService.insert(tBranchOffice);
 
        //添加User对象
        User user = new User();
        user.setAccount(tBranchOffice.getAccount());
        user.setSalt(ShiroKit.getRandomSalt(5));
        user.setPassword(ShiroKit.md5(tBranchOffice.getPassword(), user.getSalt()));
        user.setStatus(ManagerStatus.OK.getCode());
        user.setRoleid("3");
        user.setDeptid(25);
        user.setCreatetime(new Date());
        user.setRoleType(2);
        user.setObjectId(tBranchOffice.getId());
        user.setName(tBranchOffice.getPrincipal());
        user.setSex(1);
        userService.insert(user);
        //添加价格规则
        TSystemConfig tSystemConfig = new TSystemConfig();
        tSystemConfig.setCompanyId(tBranchOffice.getId());
        tSystemConfig.setType(systemPriceType);
        tSystemConfig.setContent(systemPriceContent);
        tSystemConfig.setCompanyType(2);
        tSystemConfigService.insert(tSystemConfig);
        tSystemConfig = new TSystemConfig();
        tSystemConfig.setType(8);
        tSystemConfig.setCompanyId(tBranchOffice.getId());
        tSystemConfig.setContent("{\"num1\":" + isTaxiCar + ",\"num2\":\"" + serviceTel + "\",\"num3\":" + holiday + "}");
        tSystemConfigService.insert(tSystemConfig);
 
        addScope(subArr,tBranchOffice.getId());
 
        return SUCCESS_TIP;
    }
 
    //添加经营区域
    public Object addScopeIsExit(String subArr,TBranchOffice branchOffice){
        JSONArray jsonArray = JSON.parseArray(subArr);
        List<TRegion> tRegions = tRegionService.selectList(new EntityWrapper<TRegion>());
        branchOfficeAreaService.delete(new EntityWrapper<TBranchOfficeArea>().eq("branchOfficeId", branchOffice.getId()));
        int size = jsonArray.size();
        for (int i = 0; i < size; i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            TBranchOfficeArea branchOfficeArea = branchOfficeAreaService.selectOne(new EntityWrapper<TBranchOfficeArea>().eq("id", jsonObject.getInteger("recordId")).eq("branchOfficeId", branchOffice.getId()));
            String provinceCode = jsonObject.getString("provinceCode");
            String cityCode = jsonObject.getString("cityCode");
            String areaCode = jsonObject.getString("areaCode");
            if(!StringUtils.hasLength(provinceCode) || !StringUtils.hasLength(cityCode) || !StringUtils.hasLength(areaCode)){
                return new SuccessTip(500,"请选择到区域!");
            }
            TRegion area = tRegions.stream().filter(e -> e.getCode().equals(areaCode)).findFirst().get();
            if (SinataUtil.isEmpty(branchOfficeArea)){
                int count = branchOfficeAreaService.selectCount(new EntityWrapper<TBranchOfficeArea>()
                        .eq("areaCode", areaCode));
                if (count>0){
                    return new SuccessTip(500,"该区域已存在!");
                }
            }else{
                int count = branchOfficeAreaService.selectCount(new EntityWrapper<TBranchOfficeArea>()
                        .eq("areaCode", areaCode)
                        .ne("id", branchOfficeArea.getId()));
                if (count>0){
                    return new SuccessTip(500,area.getName()+"该区域已存在!");
                }
            }
        }
        return null;
    }
 
    //添加经营区域
    public void addScope(String subArr,Integer id){
        JSONArray jsonArray = JSON.parseArray(subArr);
        List<TRegion> tRegions = tRegionService.selectList(new EntityWrapper<TRegion>());
        branchOfficeAreaService.delete(new EntityWrapper<TBranchOfficeArea>().eq("branchOfficeId", id));
        int size = jsonArray.size();
        for (int i = 0; i < size; i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            TBranchOfficeArea branchOfficeArea = branchOfficeAreaService.selectOne(new EntityWrapper<TBranchOfficeArea>().eq("id", jsonObject.getInteger("recordId")).eq("branchOfficeId", id));
            String provinceCode = jsonObject.getString("provinceCode");
            String cityCode = jsonObject.getString("cityCode");
            String areaCode = jsonObject.getString("areaCode");
            TRegion province = tRegions.stream().filter(e -> e.getCode().equals(provinceCode)).findFirst().get();
            TRegion city = tRegions.stream().filter(e -> e.getCode().equals(cityCode)).findFirst().get();
            TRegion area = tRegions.stream().filter(e -> e.getCode().equals(areaCode)).findFirst().get();
            if (SinataUtil.isEmpty(branchOfficeArea)){
                branchOfficeArea = new TBranchOfficeArea();
                branchOfficeArea.setProvinceCode(provinceCode);
                branchOfficeArea.setCityCode(cityCode);
                branchOfficeArea.setAreaCode(areaCode);
                branchOfficeArea.setProvinceName(province.getName());
                branchOfficeArea.setCityName(city.getName());
                branchOfficeArea.setAreaName(area.getName());
                branchOfficeArea.setBranchOfficeId(id);
                branchOfficeAreaService.insert(branchOfficeArea);
            }else{
                branchOfficeArea.setProvinceCode(provinceCode);
                branchOfficeArea.setCityCode(cityCode);
                branchOfficeArea.setAreaCode(areaCode);
                branchOfficeArea.setProvinceName(province.getName());
                branchOfficeArea.setCityName(city.getName());
                branchOfficeArea.setAreaName(area.getName());
                branchOfficeAreaService.updateById(branchOfficeArea);
            }
        }
    }
 
    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer tBranchOfficeId) {
        int count = tDriverService.selectCount(new EntityWrapper<TDriver>().eq("branchOfficeId", tBranchOfficeId));
        if(count>0){
            return new SuccessTip(500,"该分公司下有司机,请先删除司机!");
        }
        TBranchOffice tBranchOffice = tBranchOfficeService.selectById(tBranchOfficeId);
        tBranchOffice.setStatus(StatusEnum.DELETE.getCode());
        tBranchOfficeService.updateById(tBranchOffice);
 
        branchOfficeAreaService.delete(new EntityWrapper<TBranchOfficeArea>().eq("branchOfficeId", tBranchOfficeId));
 
        List<User> users = userService.selectList(new EntityWrapper<User>().eq("role_type", 2).eq("object_id", tBranchOfficeId));
        for (User user : users) {
            user.setStatus(3);
            userService.updateById(user);
        }
        tSystemConfigService.delete(new EntityWrapper<TSystemConfig>().eq("type", 5).eq("companyId", tBranchOfficeId));
        return SUCCESS_TIP;
    }
 
    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    public Object update(TBranchOffice tBranchOffice,String subArr, Integer systemPriceType, String systemPriceContent, Integer holiday, Integer isTaxiCar, String serviceTel) {
        TBranchOffice branchOffice = tBranchOfficeService.selectById(tBranchOffice.getId());
        User user = userService.selectOne(new EntityWrapper<User>()
                .eq("role_type", 2)
                .eq("object_id", branchOffice.getId())
                .last("LIMIT 1"));
        // 判断区域是否已存在
        Object o = addScopeIsExit(subArr, tBranchOffice);
        if (o != null){
            return o;
        }
        //判断账号是否已存在
        if (SinataUtil.isNotEmpty(tBranchOffice.getAccount()) && SinataUtil.isNotEmpty(tBranchOffice.getPassword())){
            if(Objects.isNull(user)){
                //添加User对象
                user.setAccount(tBranchOffice.getAccount());
                user.setSalt(ShiroKit.getRandomSalt(5));
                user.setPassword(ShiroKit.md5(tBranchOffice.getPassword(), user.getSalt()));
                user.setStatus(ManagerStatus.OK.getCode());
                user.setRoleid("3");
                user.setDeptid(25);
                user.setCreatetime(new Date());
                user.setRoleType(2);
                user.setObjectId(tBranchOffice.getId());
                user.setName(tBranchOffice.getPrincipal());
                user.setSex(1);
                userService.insert(user);
            }else {
                if (!tBranchOffice.getAccount().equals(user.getAccount())){
                    //判断账号是否已存在
                    int count = userService.selectCount(new EntityWrapper<User>().eq("account", tBranchOffice.getAccount()).ne("status", 3));
                    if (count > 0){
                        return new SuccessTip(500,"该账号已存在!");
                    }
                }
                user.setAccount(tBranchOffice.getAccount());
                user.setPassword(ShiroKit.md5(tBranchOffice.getPassword(), user.getSalt()));
                userService.updateById(user);
            }
        }
 
        TBranchOffice office = tBranchOfficeService.selectOne(new EntityWrapper<TBranchOffice>().eq("branchOfficeName", tBranchOffice.getBranchOfficeName())
                .ne("status", 3).last("LIMIT 1"));
        if(Objects.nonNull(office) && !tBranchOffice.getId().equals(office.getId())){
            return new SuccessTip(500,"该分公司名称已存在!");
        }
//        Object ocr = ocr("E:\\071bf986db0b00355c0ed190bbd3b16.png");
//        System.err.println(ocr);
//        Object o = tBranchOfficeService.addOrUpdate(tBranchOffice);
//        if(Objects.nonNull(o)){
//            return o;
//        }
        tBranchOffice.setPrincipal(tBranchOffice.getPrincipal().replace(" ",""));
        tBranchOfficeService.updateById(tBranchOffice);
 
        TSystemConfig tSystemConfig = tSystemConfigService.selectOne(new EntityWrapper<TSystemConfig>().eq("type", 5).eq("companyId", tBranchOffice.getId()));
        if(null == tSystemConfig){
            tSystemConfig = new TSystemConfig();
            tSystemConfig.setType(systemPriceType);
            tSystemConfig.setCompanyId(tBranchOffice.getId());
            tSystemConfig.setContent(systemPriceContent);
            tSystemConfig.setCompanyType(2);
            tSystemConfigService.insert(tSystemConfig);
        }else{
            tSystemConfig.setContent(systemPriceContent);
            tSystemConfigService.updateById(tSystemConfig);
        }
        tSystemConfig = tSystemConfigService.selectOne(new EntityWrapper<TSystemConfig>().eq("type", 8).eq("companyId", tBranchOffice.getId()));
        if(null == tSystemConfig){
            tSystemConfig = new TSystemConfig();
            tSystemConfig.setType(8);
            tSystemConfig.setCompanyId(tBranchOffice.getId());
            tSystemConfig.setContent("{\"num1\":" + isTaxiCar + ",\"num2\":\"" + serviceTel + "\",\"num3\":" + holiday + "}");
            tSystemConfig.setCompanyType(2);
            tSystemConfigService.insert(tSystemConfig);
        }else{
            tSystemConfig.setContent("{\"num1\":" + isTaxiCar + ",\"num2\":\"" + serviceTel + "\",\"num3\":" + holiday + "}");
            tSystemConfigService.updateById(tSystemConfig);
        }
 
        addScope(subArr,tBranchOffice.getId());
 
        return SUCCESS_TIP;
    }
 
    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{tBranchOfficeId}")
    @ResponseBody
    public Object detail(@PathVariable("tBranchOfficeId") Integer tBranchOfficeId) {
        return tBranchOfficeService.selectById(tBranchOfficeId);
    }
}