中扬CRM客户关系管理系统
#
LSH
2023-12-01 7804e0f43c902645e29a5a7fccd2c741f86cd312
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
package com.zy.crm.manager.controller;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.core.annotations.ManagerAuth;
import com.core.common.BaseRes;
import com.core.common.Cools;
import com.core.common.DateUtils;
import com.core.common.R;
import com.core.domain.KeyValueVo;
import com.core.exception.CoolException;
import com.zy.crm.common.entity.PhoneCodeTypeParam;
import com.zy.crm.common.model.SettleDto;
import com.zy.crm.common.web.BaseController;
import com.zy.crm.manager.entity.*;
import com.zy.crm.manager.service.*;
import com.zy.crm.manager.utils.CompanySearchUtils;
import com.zy.crm.system.entity.User;
import com.zy.crm.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
 
@RestController
public class PriOnline2Controller extends BaseController {
 
    @Autowired
    private PriOnline2Service priOnline2Service;
 
    @Autowired
    private PriService priService;
 
    @Autowired
    private PlanService planService;
 
    @Autowired
    private PlanTypeService planTypeService;
 
    @Autowired
    private ProcessPermissionsService processPermissionsService;
 
    @Autowired
    private PriQuoteService priQuoteService;
 
    @Autowired
    private CstmrService cstmrService;
 
    @Autowired
    private UserService userService;
 
    @Autowired
    private PriOnline2FollService priOnline2FollService;
 
    @Autowired
    private SmsCodeService smsCodeService;
 
    String PZH = " ———————————————————————————————————————————————————————————————————————————————— ";
 
    @RequestMapping(value = "/priOnline2/{id}/auth")
    @ManagerAuth
    public R get(@PathVariable("id") String id) {
        PriOnline2 priOnline2 = priOnline2Service.selectById(String.valueOf(id));
        assert priOnline2 != null;
        JSONObject resultObj = JSON.parseObject(JSON.toJSONString(priOnline2));
        // 步骤条相关
        resultObj.put("step", priOnline2.getSettle().equals(priOnline2.getSettleSize()) ? 0 : priOnline2.getSettle() + 1);
        return R.ok().add(resultObj);
    }
 
    @RequestMapping(value = "/priOnline2/viewCheck/{id}/auth")
    @ManagerAuth(memo = "查看询价")
    public R viewCheck(@PathVariable("id") String id) {
        PriOnline2 priOnline = priOnline2Service.selectById(String.valueOf(id));
        if (Cools.isEmpty(priOnline.getCheckData())) {
            return R.error("请先上传询价");
        }
        return R.ok(priOnline);
    }
 
    @RequestMapping(value = "/priOnline2/list/auth")
    @ManagerAuth
    public R list(@RequestParam(defaultValue = "1") Integer curr,
                  @RequestParam(defaultValue = "10") Integer limit,
                  @RequestParam(required = false) String orderByField,
                  @RequestParam(required = false) String orderByType,
                  @RequestParam(required = false) String allSwitch,
                  @RequestParam(required = false) String condition,
                  @RequestParam Map<String, Object> paramSou) {
        EntityWrapper<PriOnline2> wrapper = new EntityWrapper<>();
        wrapper.setSqlSelect("id,title,create_time as createTime,assistant_plan_id as assistantPlanId,host_plan_id as hostPlanId,assistant_host_sign as assistantHostSign,filepath,settle,settle_size as settleSize,item_id as itemId,order_num as orderNum,template_name as templateName,user_id as userId,dept_id as deptId,status,update_time as updateTime,check_data as checkData,update_user_id as updateUserId,member_id as memberId");
//        wrapper.in("member_id", getUserRoleBelongsToUserId("allopen"));
        excludeTrash(paramSou);
        Map<String, Object> param = convertallSwitch(paramSou);
 
        if (!Cools.isEmpty(allSwitch)){
            List<PriOnline2> priOnline2List = priOnline2Service.listByAll(getUserId());
            Page<PriOnline2> page1 = new Page<PriOnline2>(curr, limit).setRecords(priOnline2List);
            page1.setTotal(priOnline2Service.listByAllTotal(getUserId()));
            return R.ok(page1);
        }
 
        convert(param, wrapper);
        allLike(PriOnline2.class, param.keySet(), wrapper, condition);
        wrapper.or().eq("member_id", getUserId());
//        if (!Cools.isEmpty(orderByField)) {
//            wrapper.orderBy(humpToLine(orderByField), "asc".equals(orderByType));
//        }
        wrapper.orderBy("update_time",false);
        wrapper.orderBy("create_time",false);
        return R.ok(priOnline2Service.selectPage(new Page<>(curr, limit), wrapper));
    }
 
    private Map<String, Object>  convertallSwitch(Map<String, Object> map) {
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            if (entry.getKey().equals("allSwitch")) {
                map.remove("allSwitch");
            }
        }
        return map;
    }
    private <T> void convert(Map<String, Object> map, EntityWrapper<T> wrapper) {
        boolean signUserId = false;
        boolean signDeptId = false;
        boolean signHostId = false;
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            if (entry.getKey().equals("dept_id")) {
                signDeptId = true;
                if (String.valueOf(entry.getValue()).equals("19")) {
                    signHostId = true;
                }
            }
        }
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String val = String.valueOf(entry.getValue());
            if (val.contains(RANGE_TIME_LINK)) {
                String[] dates = val.split(RANGE_TIME_LINK);
                wrapper.ge(entry.getKey(), DateUtils.convert(dates[0]));
                wrapper.le(entry.getKey(), DateUtils.convert(dates[1]));
            } else if (entry.getKey().equals("dept_id")) {
                if (!val.equals("19")) {
                    wrapper.eq(entry.getKey(), val);
                }
            } else if (entry.getKey().equals("user_id") && !signDeptId) {
                signUserId = true;
                wrapper.eq(entry.getKey(), val);
            } else {
                wrapper.like(entry.getKey(), val);
            }
        }
        if (!signUserId && !signDeptId) {
            if (getRole().getId() == 1) {
                wrapper.or().eq("host_id", getHostId());
            } else if (getRole().getId() == 2) {
                wrapper.eq("dept_id", getDeptId());
            } else {
                wrapper.eq("user_id", getUserId());
            }
        }
        if (signHostId) {
            wrapper.or().eq("host_id", getHostId());
        }
    }
 
    @RequestMapping(value = "/priOnline2/add/auth")
    @ManagerAuth(memo = "添加核价管理")
    public R add(@RequestBody Map<String, Object> map) {
        PriOnline2 priOnline2 = new PriOnline2();
        priOnline2.setCreateTime(new Date());
        priOnline2.setTitle(map.get("title").toString());
//        priOnline2.setTemplateName(map.get("title").toString());
        priOnline2.setSheetData(map.get("sheetData").toString());
        priOnline2.setItemId(Long.parseLong(map.get("itemId").toString()));
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        priOnline2.setOrderNum(format.format(new Date()));
 
        //业务员
//        Item item = itemService.selectById(priOnline.getItemId());
        Plan plan = planService.selectById(priOnline2.getItemId());
        User userUp = userService.selectById(plan.getUserId());
        priOnline2.setAssistantHostSign(plan.getAssistantHostSign());
        priOnline2.setAssistantPlanId(plan.getAssistantPlanId());
        priOnline2.setHostPlanId(plan.getId());
        if (plan.getAssistantHostSign()==1){
            priOnline2.setHostPlanId(plan.getHostPlanId());
        }
 
 
        //业务员
        priOnline2.setUserId(userUp.getId());
        //业务员部门
        priOnline2.setDeptId(userUp.getDeptId());
        //更新人员
        priOnline2.setUpdateUserId(getUserId());
        //更新时间
        priOnline2.setUpdateTime(new Date());
 
        //状态,未完成
        priOnline2.setStatus(0);
 
        if (plan.getStatus() != 1) {
            return R.error("此规划单状态改变,请刷新页面重新选择!");
        }
 
        Cstmr cstmr = cstmrService.selectById(plan.getCstmrId());
        priOnline2.setTemplateName(cstmr.getName());  //客户信息名称
 
        priOnline2.setSettle(0);
 
//        User manager = new User();
//        try{
//            manager = userService.getDeptManager(getHostId(), getUser().getDeptId());        // 获取部门领导
//        }catch (Exception e){
//            manager = getUser();
//        }
 
//        String node="2-";
//        PlanType planType = planTypeService.selectById(plan.getPlanType());
//        node = node+planType.getType();
//        ProcessPermissions processPermissions = processPermissionsService.selectOne(new EntityWrapper<ProcessPermissions>().eq("process_memo", 5).eq("process", "2-1"));//2:核价流程
//        User manager = userService.selectById(processPermissions.getUserId());       // 获取核价流程节点2确认人
 
        // 获取业务员
        List<String> initNames = new ArrayList<>();
        initNames.add("接取核价任务");
        initNames.add("完成核价");
//        initNames.add("经理审核");
        initNames.add("业务员确认");
        List<User> users = new ArrayList<>();
        users.add(getUser());
        users.add(getUser());
//        users.add(new User());
        users.add(userUp);
        priOnline2.setSettleMsg(JSON.toJSONString(SettleDto.InItFlowPath(users, initNames, users.size())));
        priOnline2.setSettleSize(users.size());
        priOnline2.setPlanId(plan.getId());
        if (users.size()>1){
            priOnline2.setMemberId(users.get(1).getId());
        } else {
            priOnline2.setMemberId(getUserId());
        }
        //设置项目流程
        plan.setStep(2);
        plan.setStatus(2);
        planService.updateById(plan);
 
        priOnline2Service.insert(priOnline2);
 
        // 自动添加跟进人
        for (User user:users){
            List<PriOnline2Foll> priOnline2Folls = priOnline2FollService.selectList(new EntityWrapper<PriOnline2Foll>().eq("pri_online2_id", priOnline2.getId()).eq("user_id", user.getId()));
            if (Cools.isEmpty(priOnline2Folls) || priOnline2Folls.size()==0){
                PriOnline2Foll priOnline2Foll = new PriOnline2Foll();
                priOnline2Foll.setPriOnline2Id(priOnline2.getId());
                priOnline2Foll.setUserId(user.getId());
                if (!priOnline2FollService.insert(priOnline2Foll)) {
                    throw new CoolException("保存失败,请重试");
                }
            }
        }
 
 
        if (plan.getAssistantHostSign()==1){
            Plan plan1 = planService.selectById(plan.getHostPlanId());
            plan1.setStatus2(2);
            planService.updateById(plan1);
        }
 
        return R.ok();
    }
 
    @RequestMapping(value = "/priOnline2/addOther/auth")
    @ManagerAuth(memo = "另存核价管理")
    public R addOther(@RequestBody Map<String, Object> map) {
        if (true){
            return R.error("禁止!");
        }
        PriOnline2 online = priOnline2Service.selectById(Long.parseLong(map.get("id").toString()));
 
        PriOnline2 priOnline2 = new PriOnline2();
        priOnline2.setCreateTime(new Date());
        priOnline2.setTitle(map.get("title").toString());
        priOnline2.setTemplateName(online.getTemplateName());
        priOnline2.setSheetData(map.get("sheetData").toString());
        priOnline2.setItemId(online.getItemId());
 
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        priOnline2.setOrderNum(format.format(new Date()));
        //创建人员
        priOnline2.setUserId(getUserId());
        //更新时间
        priOnline2.setUpdateTime(new Date());
        //更新人员
        priOnline2.setUpdateUserId(getUserId());
        //状态,未完成
        priOnline2.setStatus(0);
        priOnline2.setDeptId(getDeptId());
        //业务员
//        Item item = itemService.selectById(priOnline.getItemId());
        Plan plan = planService.selectById(priOnline2.getItemId());
        priOnline2.setMemberId(plan.getUserId());
 
        priOnline2Service.insert(priOnline2);
        return R.ok();
    }
 
    @RequestMapping(value = "/priOnline2/update/auth")
    @ManagerAuth(memo = "更新核价管理")
    public R update(@RequestBody Map<String, Object> map) {
        PriOnline2 priOnline2 = priOnline2Service.selectById(Long.parseLong(map.get("id").toString()));
        if (priOnline2.getStatus() == 1) {
            return R.error("核价已完成,禁止保存");
        }
        priOnline2.setTitle(map.get("title").toString());
        priOnline2.setSheetData(map.get("sheetData").toString());
        //更新人员
        priOnline2.setUpdateUserId(getUserId());
        //更新时间
        priOnline2.setUpdateTime(new Date());
        priOnline2Service.updateById(priOnline2);
        return R.ok();
    }
 
    //更新状态
    @RequestMapping(value = "/priOnline2/updateForm/auth")
    @ManagerAuth(memo = "更新核价管理状态")
    public R updateForm(Long id, Integer status, String title, String templateName) {
        PriOnline2 priOnline2 = priOnline2Service.selectById(id);
        priOnline2.setStatus(status);
        priOnline2.setUpdateTime(new Date());
        priOnline2.setTitle(title);
        priOnline2.setTemplateName(templateName);
        priOnline2Service.updateById(priOnline2);
        return R.ok();
    }
 
    @RequestMapping(value = "/priOnline2/uploadCheck/auth")
    @ManagerAuth(memo = "上传询价文件")
    public R uploadCheck(@RequestParam("id") Integer id,
                         @RequestParam("checkData") String checkData,
                         @RequestParam("file") MultipartFile[] files) {
        PriOnline2 priOnline2 = priOnline2Service.selectById(id);
        if (priOnline2.getStatus() == 1) {
            return R.error("核价已完成,禁止上传");
        }
        priOnline2.setCheckData(checkData);
 
 
        MultipartFile file = files[0];
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd_HHmmss");
        String path = ClassUtils.getDefaultClassLoader().getResource("excel/uploadCheckData").getPath();
        //文件后缀名
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        //上传文件名
        String filename = format.format(new Date()) + suffix;
        //最终文件路径
        String filepath = path + "/" + filename;
 
 
        //服务器端保存的文件对象
        File serverFile = new File(filepath);
        if (!serverFile.exists()) {
            try {
                //创建文件
                serverFile.createNewFile();
                //将上传的文件写入到服务器端文件内
                file.transferTo(serverFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
 
        //保存文件名
        priOnline2.setCheckDataFile(filename);
        priOnline2Service.updateById(priOnline2);
        return R.ok();
    }
 
    @GetMapping("/priOnline2/checkDataDownload/{filename}")
    public void download(@PathVariable String filename, HttpServletResponse response) {
        try {
            ClassPathResource pathResource = new ClassPathResource("excel/uploadCheckData/" + filename);
            File file = pathResource.getFile();
            InputStream inputStream = pathResource.getInputStream();
            //输出文件
            InputStream fis = new BufferedInputStream(inputStream);
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            response.reset();
 
            //获取文件的名字再浏览器下载页面
            String name = file.getName();
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(name.getBytes(), "iso-8859-1"));
            response.addHeader("Content-Length", "" + file.length());
            OutputStream out = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            out.write(buffer);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    @RequestMapping(value = "/priOnline2/delete/auth")
    @ManagerAuth(memo = "删除核价管理")
    public R delete(Long[] ids) {
        try {
            if (Cools.isEmpty(ids) || ids.length == 0) {
                return R.error();
            }
            for (Long id : ids) {
                int priQuote = priQuoteService.selectCount(new EntityWrapper<PriQuote>().eq("pri_online_id", id));
                if (priQuote != 0) {
                    return R.error("存在关联的报价单,禁止删除!!!");
                }
//                int priSales = priSalesService.selectCount(new EntityWrapper<PriSales>().eq("pri_online2_id", id));
//                if (priSales!=0){
//                    return R.error("存在关联的产品费用明细,禁止删除!!!");
//                }
            }
            for (Long id : ids) {
                PriOnline2 priOnline2 = priOnline2Service.selectById(id);
                Plan plan = planService.selectById(priOnline2.getItemId());
                plan.setStatus(1);
                planService.updateById(plan);
                if (plan.getAssistantHostSign()==1){
                    Plan planHost = planService.selectById(priOnline2.getHostPlanId());
                    planHost.setStatus2(1);
                    planService.updateById(planHost);
                }
            }
 
            priOnline2Service.deleteBatchIds(Arrays.asList(ids));
            return R.ok();
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }
 
    @RequestMapping(value = "/priOnline2/export/auth")
    @ManagerAuth
    public R export(@RequestBody JSONObject param) {
        EntityWrapper<PriOnline2> wrapper = new EntityWrapper<>();
        List<String> fields = JSONObject.parseArray(param.getJSONArray("fields").toJSONString(), String.class);
        Map<String, Object> map = excludeTrash(param.getJSONObject("priOnline"));
        convert(map, wrapper);
        List<PriOnline2> list = priOnline2Service.selectList(wrapper);
        return R.ok(exportSupport(list, fields));
    }
 
    @RequestMapping(value = "/priOnline2Query/auth")
    @ManagerAuth
    public R query(String condition) {
        EntityWrapper<PriOnline2> wrapper = new EntityWrapper<>();
        wrapper.like("order_num", condition);
        wrapper.in("user_id", getUserRoleBelongsToUserId("allopen"));
        Page<PriOnline2> page = priOnline2Service.selectPage(new Page<>(0, 10), wrapper);
        List<Map<String, Object>> result = new ArrayList<>();
        for (PriOnline2 priOnline2 : page.getRecords()) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", priOnline2.getId());
//            map.put("value", priOnline2.getOrderNum() + "/" + priOnline2.getPlanId$() + "/" + priOnline2.getMemberId$());
            map.put("value", priOnline2.getOrderNum() + "/" + priOnline2.getTemplateName());
            result.add(map);
        }
        return R.ok(result);
    }
 
    @RequestMapping(value = "/priOnline2Query2/auth")
    @ManagerAuth
    public R query2(String condition) {
        EntityWrapper<PriOnline2> wrapper = new EntityWrapper<>();
        wrapper.like("order_num", condition);
        wrapper.in("user_id", getUserRoleBelongsToUserId("allopen"));
        Page<PriOnline2> page = priOnline2Service.selectPage(new Page<>(0, 10), wrapper);
        List<Map<String, Object>> result = new ArrayList<>();
        for (PriOnline2 priOnline2 : page.getRecords()) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", priOnline2.getId());
//            map.put("value", priOnline2.getOrderNum() + "/" + priOnline2.getPlanId$() + "/" + priOnline2.getMemberId$());
//            map.put("value", priOnline2.getOrderNum() + "/" + priOnline2.getTemplateName());
            map.put("value", priOnline2.getOrderNum() + "/" + priOnline2.getPlanId$() + "/" + priOnline2.getUser$() + "/" + priOnline2.getTemplateName());
 
            result.add(map);
        }
        return R.ok(result);
    }
 
    @RequestMapping(value = "/priOnline2/check/column/auth")
    @ManagerAuth
    public R query(@RequestBody JSONObject param) {
        Wrapper<PriOnline2> wrapper = new EntityWrapper<PriOnline2>().eq(humpToLine(String.valueOf(param.get("key"))), param.get("val"));
        if (null != priOnline2Service.selectOne(wrapper)) {
            return R.parse(BaseRes.REPEAT).add(getComment(PriOnline2.class, String.valueOf(param.get("key"))));
        }
        return R.ok();
    }
 
    @RequestMapping("/priOnline2/all/get/kv")
    @ManagerAuth
    public R getDataKV(@RequestParam(required = false) String condition) {
        List<KeyValueVo> vos = new ArrayList<>();
        Wrapper<PriOnline2> wrapper = new EntityWrapper<PriOnline2>().andNew().like("id", condition).orderBy("create_time", false);
        priOnline2Service.selectPage(new Page<>(1, 30), wrapper).getRecords().forEach(item -> vos.add(new KeyValueVo(String.valueOf(item.getId()), item.getId())));
        return R.ok().add(vos);
    }
 
    @PostMapping(value = "/priOnline2/approvalEnd/auth")
    @ManagerAuth(memo = "拒绝申请单")
    public R approvalEnd(@RequestParam Long priOnlineId,
                         @RequestParam(required = false) Long plannerId) {
        PriOnline2 priOnline2 = priOnline2Service.selectById(priOnlineId);
        Date now = new Date();
        Long memberId = priOnline2.getMemberId();
        User member = userService.selectById(memberId);
        if (!member.getId().equals(getUserId())) {
            return R.error("抱歉,您没有审核的权限");
        }
        List<SettleDto> planDtos = JSON.parseArray(priOnline2.getSettleMsg(), SettleDto.class);
        List<SettleDto> planDtoList = new ArrayList<>();
        boolean sign = true;
        for (SettleDto dto : planDtos) {
            if (dto.getStep().equals(1)) {
                priOnline2.setMemberId(dto.getUserId());
            }
            if (sign && dto.getUserId().equals(memberId) && priOnline2.getSettle() + 1 == dto.getStep()) {
                if (Cools.isEmpty(dto.getMsg())) {
                    dto.setMsg(DateUtils.convert(now) + " " + member.getNickname() + "回退审批");
                } else {
                    dto.setMsg(dto.getMsg() + PZH + DateUtils.convert(now) + " " + member.getNickname() + "回退审批");
                }
                sign = false;
            }
            if (sign) {
                dto.setMsg(dto.getMsg() + PZH + DateUtils.convert(now) + " " + member.getNickname() + "回退审批");
            }
 
            planDtoList.add(dto);
        }
        priOnline2.setStatus(0);
        priOnline2.setSettle(0);
        priOnline2.setUpdateUserId(getUserId());
        priOnline2.setUpdateTime(now);
 
        priOnline2.setSettleMsg(JSON.toJSONString(planDtoList));
        priOnline2Service.updateById(priOnline2);
        return R.ok("回退成功");
    }
 
    @PostMapping(value = "/priOnline2/approval/auth")
    @ManagerAuth
    public R approvalBusinessTrip(@RequestParam Long priOnlineId,
                                  @RequestParam(required = false) Long plannerId) {
        PriOnline2 priOnline2 = priOnline2Service.selectById(priOnlineId);
        assert priOnline2 != null;
        Date now = new Date();
        Long nextUserId = getUserId();
        User user = userService.selectById(priOnline2.getMemberId());
 
        if (Cools.isEmpty(getUser()) || Cools.isEmpty(user)){
            return R.error("抱歉,您无需确认!!!");
        }
 
        if (!getUserId().equals(user.getId())) {
            return R.error("抱歉,您无需确认!!!");
        }
        // 修改 settle 步骤数据
        List<SettleDto> list = JSON.parseArray(priOnline2.getSettleMsg(), SettleDto.class);
        for (SettleDto dto : list) {
            if (dto.getStep().equals(priOnline2.getSettle())) {
                dto.setCurr(Boolean.TRUE);
            } else if (dto.getStep().equals(priOnline2.getSettle() + 1)) {
                dto.setCurr(Boolean.TRUE);
                if (Cools.isEmpty(dto.getMsg())) {
                    dto.setMsg(user.getNickname() + "审批通过");
                } else {
                    dto.setMsg(dto.getMsg() + PZH + DateUtils.convert(new Date()) + " " + user.getNickname() + "审批通过");
                }
                dto.setTime(DateUtils.convert(new Date()));
            } else if (dto.getStep().equals(priOnline2.getSettle() + 2)) {
                if (Cools.isEmpty(dto.getUserId())){
                    nextUserId= 0L;
                }else {
                    nextUserId=dto.getUserId();
                }
            }
        }
        priOnline2.setSettleMsg(JSON.toJSONString(list));
        // 修改规划单状态
        priOnline2.setSettle(priOnline2.getSettle() + 1);  // 审批通过
        if (user.getId().equals(priOnline2.getUserId())) {
            priOnline2.setStatus(1);
        }
        priOnline2.setUpdateTime(now);
        priOnline2.setUpdateUserId(getUserId());
        priOnline2.setMemberId(nextUserId);
 
        if (!priOnline2Service.updateById(priOnline2)) {
            throw new CoolException("确认失败,请联系管理员");
        }
 
        Plan plan = planService.selectById(priOnline2.getItemId());
        plan.setStatus(3);
        planService.selectById(plan);
 
        if (user.getId().equals(priOnline2.getUserId())) {
            if (priOnline2.getAssistantHostSign()==0){
                User manager = new User();
                try {
                    manager = userService.getDeptManager(getHostId(), getUser().getDeptId());        // 获取部门领导
                } catch (Exception e) {
                    manager = getUser();
                }
                try {
                    priQuoteAdd(priOnline2.getId(), plannerId);
                } catch (Exception e) {
                    return R.error("自动生成报价单失败");
                }
                if (!smsCodeService.sendSmsCodeText(manager.getMobile(), PhoneCodeTypeParam.ALIYUN_M1ABAC630E,getUserId())) {
                    return R.ok("审批成功但短信发送失败!");
                }
            }else {
                try {
                    Plan planHost = planService.selectById(priOnline2.getHostPlanId());
                    planHost.setStatus2(3);
                    planService.selectById(planHost);
                } catch (Exception e) {
                    return R.error("更新主表失败");
                }
            }
        }else {
            User phoneUser = userService.selectById(nextUserId);
            if (!smsCodeService.sendSmsCodeText(phoneUser.getMobile(), PhoneCodeTypeParam.ALIYUN_M1ABAC630E,getUserId())) {
                return R.ok("审批成功但短信发送失败!");
            }
        }
 
        return R.ok("审批成功");
    }
 
    public void priQuoteAdd(Long priOnlineId, Long priType) {
        Pri pri = null;
        if (priType == 1L) {
            pri = priService.selectById(114);
        } else {
            pri = priService.selectById(115);
        }
 
        PriOnline2 priOnline2 = priOnline2Service.selectById(priOnlineId);
 
        PriQuote priQuote = new PriQuote();
        priQuote.setCreateTime(new Date());
        priQuote.setTitle(pri.getTitle());
        priQuote.setTemplateName(priOnline2.getTemplateName());
        priQuote.setSheetData(pri.getSheetData());
        priQuote.setPriOnlineId(priOnlineId);
        priQuote.setItemId(priOnline2.getItemId());
        priQuote.setOrderNum(priOnline2.getOrderNum());
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        priQuote.setInOrderNum(format.format(new Date()));
        //业务员
        Plan plan = planService.selectById(priQuote.getItemId());
        User userUp = userService.selectById(plan.getUserId());
        //业务员
        priQuote.setUserId(userUp.getId());
        //更新人员
        priQuote.setUpdateUserId(getUserId());
        //所属部门
        priQuote.setDeptId(userUp.getDeptId());
        //更新时间
        priQuote.setUpdateTime(new Date());
        //模板
        Integer i = priType == 1L ? 1 : 2;
        priQuote.setTemplate(i.toString());
 
        //设置项目流程
        plan.setStep(3);
 
//        priQuote.setForm(JSON.toJSONString(map));     // 自定义表单内容
        priQuote.setSettle(1);  // 1.开始
        User manager = new User();
        try {
            manager = userService.getDeptManager(getHostId(), getUser().getDeptId());        // 获取部门领导
        } catch (Exception e) {
            manager = getUser();
        }
        priQuote.setMemberId(manager.getId());
 
        ProcessPermissions processPermissions = processPermissionsService.selectOne(new EntityWrapper<ProcessPermissions>().eq("process_memo", 2).eq("process", "3-1"));//2:报价流程
        User president = userService.selectById(processPermissions.getUserId());       // 获取报价流程节点3-1确认人
        priQuote.setSettleMsg(JSON.toJSONString(SettleDto.initPriQuote(plan, manager, president, getUser())));
 
        planService.updateById(plan);
 
        priQuoteService.insert(priQuote);
    }
 
    @PostMapping(value = "/priOnline2/priQuoteAdd/list/auth")
    @ManagerAuth
    public R priOnline2priQuoteAdd(@RequestParam Long planId) {
        List<KeyValueVo> vos = new ArrayList<>();
        vos.add(new KeyValueVo("货架模板", 1L));
        vos.add(new KeyValueVo("集成模板", 2L));
        return R.ok().add(vos);
    }
 
}