Pu Zhibing
5 天以前 f2530067b6cd16a87b76190cf524d2ed75bf9592
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
package com.stylefeng.guns.modular.system.controller.general;
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.SuccessTip;
import com.stylefeng.guns.core.beetl.ShiroExtUtil;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.common.constant.state.ManagerStatus;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.shiro.ShiroUser;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.controller.resp.TAgentResp;
import com.stylefeng.guns.modular.system.controller.util.ExcelUtil;
import com.stylefeng.guns.modular.system.model.*;
import com.stylefeng.guns.modular.system.service.*;
import com.stylefeng.guns.modular.system.util.RedisUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
 
/**
 * 控制器
 *
 * @author fengshuonan
 * @Date 2023-02-20 09:18:08
 */
@Controller
@RequestMapping("")
public class TAgentController extends BaseController {
 
    private String PREFIX = "/system/tAgent/";
 
    @Autowired
    private ITAgentService tAgentService;
 
    @Autowired
    private ITRegionService tRegionService;
 
    @Autowired
    private ITDriverService tDriverService;
    @Autowired
    private ITDriverWorkService tDriverWorkService;
    @Autowired
    private IUserService userService;
    @Autowired
    private RedisUtil redisUtil;
 
    @Autowired
    private ISettlementRecordService settlementRecordService;
 
    @Autowired
    private IRechargeRecordService rechargeRecordService;
 
    @Autowired
    private ITSystemConfigService systemConfigService;
 
    @Autowired
    private ICompanyFundFlowService companyFundFlowService;
 
    @Autowired
    private ShiroExtUtil shiroExtUtil;
 
    @Value("${callbackPath}")
    private String callbackPath;
 
 
    /**
     * 跳转到首页
     */
    @RequestMapping("/tAgent")
    public String index(Model model) {
        model.addAttribute("userType", Objects.requireNonNull(shiroExtUtil.getUser()).getRoleType());
        return PREFIX + "tAgent.html";
    }
 
    /**
     * 跳转到添加
     */
    @RequestMapping("/tAgent/tAgent_add")
    public String tAgentAdd() {
        return PREFIX + "tAgent_add.html";
    }
 
    /**
     * 跳转到修改
     */
    @RequestMapping("/tAgent/tAgent_update/{tAgentId}")
    public String tAgentUpdate(@PathVariable Integer tAgentId, Model model) {
        TAgent tAgent = tAgentService.selectById(tAgentId);
        model.addAttribute("item",tAgent);
        LogObjectHolder.me().set(tAgent);
        User user = userService.selectOne(new EntityWrapper<User>().eq("role_type", 3).eq("object_id", tAgentId));
        model.addAttribute("user", user);
        return PREFIX + "tAgent_edit.html";
    }
 
    /**
     * 跳转详情页面
     */
    @RequestMapping("/tAgent/agentDetail")
    public String agentDetail(Integer agentId, Model model) {
        tAgentService.detail(agentId,model);
        return PREFIX + "tAgentDetail.html";
    }
 
    /**
     * 跳转区域页面
     */
    @RequestMapping("/tAgent/areaDetail")
    public String areaDetail(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);
        if(StringUtils.hasLength(area) && StringUtils.hasLength(areaId)){
            String[] split = area.split("/");
            model.addAttribute("provinceName",split[0]);
            model.addAttribute("cityName",split[1]);
 
            model.addAttribute("provinceId",split1[0]);
            model.addAttribute("cityId",split1[1]);
        }else {
            model.addAttribute("provinceName","");
            model.addAttribute("cityName","");
 
            model.addAttribute("provinceId","");
            model.addAttribute("cityId","");
        }
        return PREFIX + "tAgentArea.html";
    }
 
    /**
     * 获取列表
     */
    @RequestMapping(value = "/tAgent/list")
    @ResponseBody
    public Object list(String principal,String principalPhone,String createTime,Integer status) {
        EntityWrapper<TAgent> wrapper = tAgentService.getAgentWrapper(principal,principalPhone,createTime,status);
        List<TAgent> tAgents = tAgentService.selectList(wrapper);
        // 代理商列表数据封装(导出共用)
        return tAgentService.getAgentResp(tAgents);
    }
 
    /**
     * 获取列表
     */
    @RequestMapping(value = "/tAgent/list-back")
    @ResponseBody
    public Object listBack(String condition) {
        return tAgentService.selectList(null);
    }
 
    /**
     * 新增
     */
    @RequestMapping(value = "/tAgent/add")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    public Object add(TAgent tAgent) {
 
        String[] split1 = tAgent.getArea().split("/");
 
//        int count = tAgentService.selectCount(new EntityWrapper<TAgent>()
//                .eq("provinceName", split1[0])
//                .eq("cityName",split1[1])
//                .eq("districtName",sp));
        int count = tAgentService.selectCount(new EntityWrapper<TAgent>()
                .eq("merchantName", tAgent.getMerchantName()));
        if(count>0){
            return new SuccessTip(500,"该代理商已存在!");
        }
 
        int count1 = userService.selectCount(new EntityWrapper<User>().eq("account", tAgent.getAccount()));
        if (count1 > 0){
            return new SuccessTip(500,"该账号已存在!");
        }
 
        tAgent.setPrincipal(tAgent.getPrincipal().replace(" ",""));
 
 
        String[] split = tAgent.getAreaId().split("/");
        // 查询省市
        // 黑龙江省/大兴安岭地区
        // 702/852
        TRegion province = tRegionService.selectById(split[0]);
        tAgent.setProvinceName(province.getName());
        tAgent.setProvinceCode(province.getCode());
        TRegion city = tRegionService.selectById(split[1]);
        if(Objects.isNull(city)){
            city = tRegionService.selectOne(new EntityWrapper<TRegion>()
                    .eq("name",split[1])
                    .last("LIMIT 1"));
        }
        tAgent.setCityName(city.getName());
        tAgent.setCityCode(city.getCode());
        tAgentService.insert(tAgent);
 
        //添加User对象
        User user = new User();
        user.setAccount(tAgent.getAccount());
        user.setSalt(ShiroKit.getRandomSalt(5));
        user.setPassword(ShiroKit.md5(tAgent.getPassword(), user.getSalt()));
        user.setStatus(ManagerStatus.OK.getCode());
        user.setRoleid("2");
        user.setDeptid(25);
        user.setCreatetime(new Date());
        user.setRoleType(3);
        user.setObjectId(tAgent.getId());
        user.setName(tAgent.getPrincipal());
        user.setSex(1);
        userService.insert(user);
 
        return SUCCESS_TIP;
    }
 
 
 
 
 
 
    /**
     * 删除
     */
    @RequestMapping(value = "/tAgent/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer tAgentId) {
        tAgentService.deleteById(tAgentId);
        return SUCCESS_TIP;
    }
 
    /**
     * 修改用户状态
     */
    @RequestMapping(value = "/tAgent/update-status")
    @ResponseBody
    public Object updateStatus(Integer id,Integer status) {
        TAgent tAgent = tAgentService.selectById(id);
        if(1 == status){
            tAgent.setStatus(2);
        }
        if(2 == status){
            tAgent.setStatus(1);
        }
        tAgentService.updateById(tAgent);
        // 冻结下面所有的司机
        List<TDriver> list = tDriverService.selectList(new EntityWrapper<TDriver>()
                .eq("agentId", id));
        for (TDriver tDriver : list) {
            if(1 == status){
                tDriver.setStatus(2);
                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);
                }
            }
            if(2 == status){
                tDriver.setStatus(1);
            }
        }
        if(!CollectionUtils.isEmpty(list)){
            tDriverService.updateBatchById(list);
        }
        return SUCCESS_TIP;
    }
 
    /**
     * 修改
     */
    @RequestMapping(value = "/tAgent/update")
    @ResponseBody
    public Object update(TAgent tAgent) {
        TAgent tAgent1 = tAgentService.selectOne(new EntityWrapper<TAgent>()
                .eq("merchantName", tAgent.getMerchantName()));
        if(null != tAgent1 && tAgent1.getId().compareTo(tAgent.getId()) != 0){
            return new SuccessTip(500,"该代理商已存在!");
        }
        User user = userService.selectOne(new EntityWrapper<User>().eq("account", tAgent.getAccount()));
        User user1 = userService.selectOne(new EntityWrapper<User>().eq("role_type", 3).eq("object_id", tAgent.getId()));
        if (null != user && user.getId().compareTo(user1.getId()) != 0){
            return new SuccessTip(500,"该账号已存在!");
        }
        tAgent.setPrincipal(tAgent.getPrincipal().replace(" ",""));
        String[] split = tAgent.getAreaId().split("/");
        // 查询省市
        // 黑龙江省/大兴安岭地区
        // 702/852
        TRegion province = tRegionService.selectById(split[0]);
        tAgent.setProvinceName(province.getName());
        tAgent.setProvinceCode(province.getCode());
        TRegion city = tRegionService.selectById(split[1]);
        if(Objects.isNull(city)){
            city = tRegionService.selectOne(new EntityWrapper<TRegion>()
                    .eq("name",split[1])
                    .last("LIMIT 1"));
        }
        tAgent.setCityName(city.getName());
        tAgent.setCityCode(city.getCode());
        tAgentService.updateById(tAgent);
        user1.setAccount(tAgent.getAccount());
        if(ToolUtil.isNotEmpty(tAgent.getPassword())){
            user1.setPassword(ShiroKit.md5(tAgent.getPassword(), user.getSalt()));
        }
        userService.updateById(user1);
        return SUCCESS_TIP;
    }
 
    /**
     * 详情
     */
    @RequestMapping(value = "/tAgent/detail/{tAgentId}")
    @ResponseBody
    public Object detail(@PathVariable("tAgentId") Integer tAgentId) {
        return tAgentService.selectById(tAgentId);
    }
 
 
    @ApiOperation(value = "省市区查询",notes="省市区查询")
    @RequestMapping(value = "/tAgent/area")
    @ResponseBody
    public Object area() {
        return tRegionService.getAreaList();
    }
 
    @ApiOperation(value = "省查询",notes="省查询")
    @RequestMapping(value = "/tAgent/areaProvince")
    @ResponseBody
    public Object areaProvince(Model model) {
        return tRegionService.selectList(new EntityWrapper<TRegion>().eq("parent_id", 0));
    }
 
    @ApiOperation(value = "市区查询",notes="市区查询")
    @RequestMapping(value = "/tAgent/areaCity")
    @ResponseBody
    public Object areaCity(Integer parentId) {
        return tRegionService.selectList(new EntityWrapper<TRegion>().eq("parent_id",parentId));
    }
 
 
    @ApiOperation(value = "导出代理商列表",notes="导出代理商列表")
    @RequestMapping(value = "/tAgent/export")
    @ResponseBody
    public void export(String principal,String principalPhone,Integer status,String createTime, HttpServletResponse response) {
        try {
            Date date = new Date();
            DateFormat format = new SimpleDateFormat("yyyyMMdd");
            String time1 = format.format(date);
            String fileName = "Agent"+time1+".xls";
            String[] title = new String[] {"时间","姓名","联系电话","代理区域","客服电话","订单数量",
                    "有效订单","已发放优惠券","已使用优惠券","累计优惠券金额","司机充值","司机数","状态"};
            EntityWrapper<TAgent> wrapper = tAgentService.getAgentWrapper(principal,principalPhone,createTime,status);
            // 是否异常
            List<TAgent> list = tAgentService.selectList(wrapper);
 
            List<TAgentResp> agentResp = tAgentService.getAgentResp(list);
 
            String[][] values = new String[agentResp.size()][];
            for (int i = 0; i < agentResp.size(); i++) {
                TAgentResp d = agentResp.get(i);
                values[i] = new String[title.length];
                values[i][0] = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(d.getCreateTime());
                values[i][1] = d.getPrincipal();
                values[i][2] = d.getPrincipalPhone();
                values[i][3] = d.getProvinceName()+d.getCityName();
                values[i][4] = StringUtils.hasLength(d.getServiceCalls())?d.getServiceCalls():"";
                values[i][5] = String.valueOf(d.getOrderSum());
                values[i][6] = String.valueOf(d.getValidOrder());
                values[i][7] = String.valueOf(d.getIssuedCoupon());
                values[i][8] = String.valueOf(d.getUsedCoupon());
                values[i][9] = String.valueOf(d.getCouponPriceSum());
                values[i][10] = String.valueOf(d.getDriverRecharge() == null ? "0":d.getDriverRecharge());
                values[i][11] = String.valueOf(d.getDriverCount());
                Integer status1 = d.getStatus();
                if(1 == status1){
                    values[i][12] = "正常";
                }else if(2 == status1){
                    values[i][12] = "冻结";
                }else {
                    values[i][12] = "删除";
                }
            }
            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();
        }
    }
 
 
 
 
 
    public File multipartFileToFile(MultipartFile file, String fileName) throws Exception {
        File toFile = null;
        if (file.equals("") || file.getSize() <= 0) {
            file = null;
        } else {
            InputStream ins = null;
            ins = file.getInputStream();
            toFile = new File("D:\\logs\\" + fileName);
            inputStreamToFile(ins, toFile);
            ins.close();
        }
        return toFile;
 
    }
 
 
    private static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
 
    /**
     * 跳转到企业信息页
     * @return
     */
    @GetMapping("/tAgent/showEnterprisePage")
    public String showEnterprisePage(Model model){
        ShiroUser user = shiroExtUtil.getUser();
        Integer objectId = user.getObjectId();
        Integer roleType = user.getRoleType();
        User user1 = userService.selectById(user.getId());
        TAgent tAgent = new TAgent();
        if(roleType == 1){//平台
            tAgent.setPrincipal(user1.getName());
            tAgent.setPrincipalPhone(user1.getPhone());
            tAgent.setEmail(user1.getEmail());
            tAgent.setMerchantNumber("553021440975");
        }
        if(roleType == 3){//代理商
            tAgent = tAgentService.selectById(objectId);
        }
        model.addAttribute("roleType", roleType);
        model.addAttribute("item", tAgent);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        model.addAttribute("socialCreditCodeExpires", roleType == 1 ? "" :Objects.isNull(tAgent.getSocialCreditCodeExpires())?"": sdf.format(tAgent.getSocialCreditCodeExpires()));
        model.addAttribute("certIdExpires", roleType == 1 ? "" : null == tAgent.getCertIdExpires() ? "" : sdf.format(tAgent.getCertIdExpires()));
        Wrapper<SettlementRecord> settlementRecordWrapper = new EntityWrapper<SettlementRecord>().eq("type", roleType == 1 ? 1 : 2);
        if(roleType == 2){
            settlementRecordWrapper.eq("objectId", objectId);
        }
        List<SettlementRecord> settlementRecords = settlementRecordService.selectList(settlementRecordWrapper.isNotNull("arrivalTime").orderBy("insertTime desc"));
        model.addAttribute("settlementRecords", settlementRecords);
//        InterfaceResponse execute = request.execute(queryBalance, QueryBalance.SERVICE_CODE);
        model.addAttribute("balance", 0);
        model.addAttribute("marketingAmount", 0);
        return PREFIX + "enterprisePage.html";
    }
 
 
 
 
    /**
     * 获取企业流水
     * @param type
     * @param time
     * @return
     */
    @ResponseBody
    @PostMapping("/tAgent/queryCompanyFundFlow")
    public Object queryCompanyFundFlow(Integer type, String time){
        Page<Map<String, Object>> page = new PageFactory<Map<String, Object>>().defaultPage();
        List<Map<String, Object>> list = companyFundFlowService.queryCompanyFundFlow(page, type, time);
        page.setRecords(list);
        return super.packForBT(page);
    }
 
 
    /**
     * 获取代理商汇总数据
     * @param id
     * @param time
     * @return
     */
    @ResponseBody
    @PostMapping("/tAgent/queryAgentSummaryData")
    public Map<String, Object> queryAgentSummaryData(Integer id, String time){
        return tAgentService.queryAgentSummaryData(id, time);
    }
 
 
    /**
     * 获取代理商明细列表
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/tAgent/queryAgentCompanyFundFlow")
    public Object queryAgentCompanyFundFlow(Integer id){
        Page<Map<String, Object>> page = new PageFactory<Map<String, Object>>().defaultPage();
        List<Map<String, Object>> list = companyFundFlowService.queryAgentCompanyFundFlow(page, id);
        page.setRecords(list);
        return super.packForBT(page);
    }
}