puzhibing
2023-02-24 b4d8cb69ff3a3d35a10a7e5c487ff683b31cc9f1
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
package com.stylefeng.guns.modular.system.controller.general;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.shiro.ShiroUser;
import com.stylefeng.guns.modular.system.controller.resp.TDriverResp;
import com.stylefeng.guns.modular.system.controller.util.*;
import com.stylefeng.guns.modular.system.model.*;
import com.stylefeng.guns.modular.system.service.ITAgentService;
import com.stylefeng.guns.modular.system.service.ITBranchOfficeService;
import com.stylefeng.guns.modular.system.service.ITRegionService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.core.util.UuidUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
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 com.stylefeng.guns.modular.system.service.ITDriverService;
 
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * 控制器
 *
 * @author fengshuonan
 * @Date 2023-02-20 09:07:06
 */
@Controller
@RequestMapping("/tDriver")
public class TDriverController extends BaseController {
 
    private String PREFIX = "/system/tDriver/";
 
    @Autowired
    private ITDriverService tDriverService;
 
    @Autowired
    private ITRegionService tRegionService;
 
    @Autowired
    private ITAgentService tAgentService;
 
    @Autowired
    private ITBranchOfficeService tBranchOfficeService;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private HttpUtils httpUtils;
    @Autowired
    private TokenUtils tokenUtils;
 
    /**
     * 跳转到首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "tDriver.html";
    }
 
    /**
     * 跳转到佣金首页
     */
    @RequestMapping("/commission")
    public String commissionIndex() {
        return PREFIX + "tDriverCommission.html";
    }
 
    /**
     * 跳转到添加
     */
    @RequestMapping("/tDriver_add")
    public String tDriverAdd() {
        return PREFIX + "tDriver_add.html";
    }
 
    /**
     * 跳转到修改
     */
    @RequestMapping("/tDriver_update")
    public String tDriverUpdate( Integer tDriverId, Model model) {
        TDriver tDriver = tDriverService.selectById(tDriverId);
        TDriverResp tDriverResp = new TDriverResp();
        BeanUtils.copyProperties(tDriver,tDriverResp);
 
        // 查询邀请人
        TDriver tDriver1 = tDriverService.selectById(tDriver.getInviterId());
        if(Objects.nonNull(tDriver1)){
            tDriverResp.setInviterName(tDriver1.getName());
            tDriverResp.setInviterPhone(tDriver1.getPhone());
        }
 
        // 查询区域
        TRegion district = tRegionService.selectOne(new EntityWrapper<TRegion>().eq("code", tDriver.getAreaCode())
                .last("LIMIT 1"));
        TRegion city = tRegionService.selectOne(new EntityWrapper<TRegion>().eq("code", tDriver.getCityCode())
                .last("LIMIT 1"));
 
        tDriverResp.setArea(tDriver.getProvinceName()+"/"+tDriver.getCityName()+"/"+tDriver.getAreaName());
        if(Objects.nonNull(district) && Objects.nonNull(city)){
            tDriverResp.setAreaId(city.getParentId()+"/"+city.getId()+"/"+district.getId());
        }
 
        model.addAttribute("item",tDriverResp);
        LogObjectHolder.me().set(tDriver);
        return PREFIX + "tDriver_edit.html";
    }
 
    /**
     * 跳转冻结页面
     * @return
     */
    @RequestMapping("/tDriver_start_and_stop")
    public String tAppUserStartAndStop( Integer id,
                                        Model model) {
        TDriver tDriver = tDriverService.selectById(id);
 
        model.addAttribute("id",id);
        model.addAttribute("status",tDriver.getStatus());
        return PREFIX + "tDriverStartAndStop.html";
    }
 
    /**
     * 跳转充值页面
     * @return
     */
    @RequestMapping("/rechargeBalancePage")
    public String rechargeBalancePage( Integer id,
                                        Model model) {
        model.addAttribute("id",id);
        return PREFIX + "tDriverRechargeBalancePage.html";
    }
 
    /**
     * 跳转异常页面
     * @return
     */
    @RequestMapping("/tDriver_exception")
    public String tDriverException(Model model) {
        model.addAttribute("isException",2);
        return PREFIX + "tDriverException.html";
    }
    /**
     * 跳转异常启用冻结页面
     * @return
     */
    @RequestMapping("/tDriverException_start_and_stop")
    public String tDriverExceptionStartAndStop(Integer id,Model model) {
        TDriver tDriver = tDriverService.selectById(id);
        model.addAttribute("id",id);
        model.addAttribute("status",tDriver.getStatus());
        return PREFIX + "tDriverExceptionStartAndStop.html";
    }
 
    /**
     * 跳转审核页面
     * @return
     */
    @RequestMapping("/auditPage")
    public String auditPage( Integer id,
                                       Model model) {
        tDriverService.auditPage(id,model);
        return PREFIX + "tDriverAudit.html";
    }
 
    /**
     * 跳转详情页面
     */
    @RequestMapping("/driverDetail")
    public String driverDetail(Integer tDriverId, Model model) {
        tDriverService.auditPage(tDriverId,model);
        return PREFIX + "tDriverDetail.html";
    }
 
    /**
     * 跳转区域页面
     */
    @RequestMapping("/areaPage")
    public String areaDetail(String area,String areaId,Model model) {
        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>().in("parent_id", provinceIds));
        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]);
            model.addAttribute("districtName",split[2]);
 
            String[] split1 = areaId.split("/");
            model.addAttribute("provinceId",split1[0]);
            model.addAttribute("cityId",split1[1]);
            model.addAttribute("districtId",split1[2]);
        }
        return PREFIX + "tDriverArea.html";
    }
 
    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String createTime,String phone,Integer status) {
        EntityWrapper<TDriver> wrapper = tDriverService.getPageList(createTime,phone,status);
        wrapper.ne("isException",2);
        List<TDriver> tDrivers = tDriverService.selectList(wrapper);
        return tDriverService.getTDriverResp(tDrivers);
    }
    /**
     * 获取佣金列表
     */
    @RequestMapping(value = "/commission/list")
    @ResponseBody
    public Object commissionList(String name,String phone,Integer status) {
        EntityWrapper<TDriver> wrapper = tDriverService.getCommissionPageList(name,phone,status);
        wrapper.ne("isException",2);
        List<TDriver> tDrivers = tDriverService.selectList(wrapper);
        return tDriverService.getTDriverResp(tDrivers);
    }
    /**
     * 获取异常列表
     */
    @RequestMapping(value = "/exceptionList")
    @ResponseBody
    public Object exceptionList(String createTime,String phone,Integer status) {
        EntityWrapper<TDriver> wrapper = tDriverService.getPageList(createTime,phone,status);
        wrapper.eq("isException",2);
        List<TDriver> tDrivers = tDriverService.selectList(wrapper);
        return tDriverService.getTDriverResp(tDrivers);
    }
 
    /**
     * 获取列表
     */
    @RequestMapping(value = "/list-back")
    @ResponseBody
    public Object listBack(String condition) {
        return tDriverService.selectList(null);
    }
    /**
     * ocr
     */
    @RequestMapping(value = "/ocr")
    @ResponseBody
    public Object ocr(String imgUrl) {
        /*String accessToken = redisTemplate.opsForValue().get(TokenUtils.ACCESS_TOKEN_CACHE_KEY);
        if (!StringUtils.hasLength(accessToken)) {
            accessToken = tokenUtils.getSimpleAccessToken();
        }
        File file = new File(imgUrl);
        String url = Constant.OCR_URL.replace("MODE", "scan").replace("ACCESS_TOKEN", accessToken);
        String result = httpUtils.registRequest(url, file);
        JSONObject jsonObject = JSONObject.parseObject(result);
        System.err.println(jsonObject);
        return jsonObject;*/
        return tDriverService.ocr(imgUrl);
    }
 
    /**
     * 审核
     */
    @RequestMapping(value = "/auditSubmit")
    @ResponseBody
    public Object auditSubmit(Integer id,Integer approvalStatus,String approvalNotes) {
        TDriver tDriver = tDriverService.selectById(id);
        tDriver.setApprovalStatus(approvalStatus);
        tDriver.setApprovalNotes(approvalNotes);
        // 审核用户id
        Subject subject = ShiroKit.getSubject();
        ShiroUser shiroUser = (ShiroUser)subject.getPrincipal();
        tDriver.setApprovalUserId(shiroUser.getId());
        tDriver.setApprovalTime(new Date());
        tDriverService.updateById(tDriver);
        return SUCCESS_TIP;
    }
 
    /**
     * 新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(TDriver tDriver) {
 
        int count = tDriverService.selectCount(new EntityWrapper<TDriver>().eq("phone", tDriver.getPhone()));
        if(count>0){
            return "该司机已存在!";
        }
        tDriverService.addOrUpdate(tDriver);
        tDriverService.insert(tDriver);
        return SUCCESS_TIP;
    }
 
    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer tDriverId) {
        tDriverService.deleteById(tDriverId);
        return SUCCESS_TIP;
    }
 
    /**
     * 修改司机状态
     */
    @RequestMapping(value = "/update-status")
    @ResponseBody
    public Object updateStatus(Integer id,Integer status,
                               String remark) {
        TDriver tDriver = tDriverService.selectById(id);
        if(1 == status){
            tDriver.setStatus(2);
        }
        if(2 == status){
            tDriver.setStatus(1);
        }
        tDriver.setRemark(remark);
        tDriverService.updateById(tDriver);
        return SUCCESS_TIP;
    }
 
    /**
     * 充值余额
     */
    @ApiOperation(value = "充值余额")
    @RequestMapping(value = "/recharge-balance")
    @ResponseBody
    public Object rechargeBalance(Integer id, String money) {
        TDriver tDriver = tDriverService.selectById(id);
        tDriver.setBackgroundBalance(new BigDecimal(money).add(tDriver.getBackgroundBalance()));
        tDriverService.updateById(tDriver);
        return SUCCESS_TIP;
    }
 
    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(TDriver tDriver) {
//        Object ocr = ocr("E:\\071bf986db0b00355c0ed190bbd3b16.png");
//        System.err.println(ocr);
        tDriverService.addOrUpdate(tDriver);
        tDriverService.updateById(tDriver);
        return SUCCESS_TIP;
    }
 
    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{tDriverId}")
    @ResponseBody
    public Object detail(@PathVariable("tDriverId") Integer tDriverId) {
        return tDriverService.selectById(tDriverId);
    }
 
    @ApiOperation(value = "省查询",notes="省查询")
    @RequestMapping(value = "/areaProvince")
    @ResponseBody
    public Object areaProvince(Model model) {
        return tRegionService.selectList(new EntityWrapper<TRegion>().eq("parent_id", 0));
    }
 
    @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));
        model.addAttribute("list",tRegions);
        return tRegions;
    }
 
 
 
    @ApiOperation(value = "导出司机列表",notes="导出司机列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."),
    })
    @RequestMapping(value = "/export")
    @ResponseBody
    public void export(String createTime,String phone,Integer status,HttpServletResponse response) {
        try {
            Date date = new Date();
            DateFormat format = new SimpleDateFormat("yyyyMMdd");
            String time1 = format.format(date);
            String fileName = "DriverInfo"+time1+".xls";
            String[] title = new String[] {"编号","姓名","手机号","性别","驾驶证号码","身份证号码",
                    "身份证","所属代理商","所属分公司","钱包余额","累计订单量","当月订单量","积分","拒单次数","状态","添加时间"};
            EntityWrapper<TDriver> wrapper = tDriverService.getPageList(createTime,phone,status);
            // 是否异常
            List<TDriver> list = tDriverService.selectList(wrapper);
 
            List<TDriverResp> tDriverResp = tDriverService.getTDriverResp(list);
 
            String[][] values = new String[tDriverResp.size()][];
            for (int i = 0; i < tDriverResp.size(); i++) {
                TDriverResp d = tDriverResp.get(i);
                values[i] = new String[title.length];
                values[i][0] = d.getCode();
                values[i][1] = d.getName();
                values[i][2] = d.getPhone();
                Integer sex = d.getSex();
                if(Objects.isNull(sex)){
                    values[i][3] = "未知";
                }else if(1 == sex){
                    values[i][3] = "男";
                }else if(2 == sex){
                    values[i][3] = "女";
                }
                values[i][4] = d.getDriverLicenseNumber();
                values[i][5] = d.getIdcard();
                if(StringUtils.hasLength(d.getIdcardFront())){
                    values[i][6] = "已上传";
                }else {
                    values[i][6] = "未上传";
                }
                values[i][7] = d.getAgentName();
                values[i][8] = d.getBranchName();
                values[i][9] = String.valueOf(d.getBalance());
                values[i][10] = String.valueOf(d.getCumulativeOrderCount());
                values[i][11] = String.valueOf(d.getMonthOrderCount());
                values[i][12] = String.valueOf(d.getIntegral());
                values[i][13] = String.valueOf(d.getRefusalCount());
                Integer status1 = d.getStatus();
                if(1 == status1){
                    values[i][14] = "正常";
                }else if(2 == status1){
                    values[i][14] = "冻结";
                }else {
                    values[i][14] = "删除";
                }
                values[i][15] = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(d.getCreateTime());
            }
            HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook("Variance"+time1, title, values, null);
            ExcelUtil.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    @ApiOperation(value = "导出司机异常列表",notes="导出司机异常列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."),
    })
    @RequestMapping(value = "/export-exception")
    @ResponseBody
    public void exportException(String createTime,String phone,Integer status,HttpServletResponse response) {
        try {
            Date date = new Date();
            DateFormat format = new SimpleDateFormat("yyyyMMdd");
            String time1 = format.format(date);
            String fileName = "DriverExceptionInfo"+time1+".xls";
            String[] title = new String[] {"编号","姓名","手机号","性别","驾驶证号码","身份证号码",
                    "身份证","累计订单量","当月订单量","积分","连续未上线(天)","状态","添加时间"};
            EntityWrapper<TDriver> wrapper = tDriverService.getPageList(createTime,phone,status);
            wrapper.eq("isException",2);
            // 是否异常
            List<TDriver> list = tDriverService.selectList(wrapper);
 
            List<TDriverResp> tDriverResp = tDriverService.getTDriverResp(list);
 
            String[][] values = new String[tDriverResp.size()][];
            for (int i = 0; i < tDriverResp.size(); i++) {
                TDriverResp d = tDriverResp.get(i);
                values[i] = new String[title.length];
                values[i][0] = d.getCode();
                values[i][1] = d.getName();
                values[i][2] = d.getPhone();
                Integer sex = d.getSex();
                if(Objects.isNull(sex)){
                    values[i][3] = "未知";
                }else if(1 == sex){
                    values[i][3] = "男";
                }else if(2 == sex){
                    values[i][3] = "女";
                }
                values[i][4] = d.getDriverLicenseNumber();
                values[i][5] = d.getIdcard();
                if(StringUtils.hasLength(d.getIdcardFront())){
                    values[i][6] = "已上传";
                }else {
                    values[i][6] = "未上传";
                }
                values[i][7] = String.valueOf(d.getCumulativeOrderCount());
                values[i][8] = String.valueOf(d.getMonthOrderCount());
                values[i][9] = String.valueOf(d.getIntegral());
                values[i][10] = String.valueOf(Objects.isNull(d.getNotOnlineCount())?0:d.getNotOnlineCount());
                values[i][11] = "异常";
                values[i][12] = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(d.getCreateTime());
            }
            HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook("Variance"+time1, title, values, null);
            ExcelUtil.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}