xiangpei
2025-02-24 31cba27f3f8ef2cdb02f1fa0d3df7b41605a3ba5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
package com.ycl.service.impl;
 
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycl.common.base.Result;
import com.ycl.common.config.SysConfig;
import com.ycl.common.constant.Constants;
import com.ycl.common.core.domain.BaseEntity;
import com.ycl.common.core.domain.entity.SysDictData;
import com.ycl.common.enums.business.FileTypeEnum;
import com.ycl.common.enums.business.ImportanceTypeEnum;
import com.ycl.common.enums.business.ProjectCategoryEnum;
import com.ycl.common.enums.business.ProjectStatusEnum;
import com.ycl.common.exception.base.BaseException;
import com.ycl.common.utils.CopyUtils;
import com.ycl.common.utils.DateUtils;
import com.ycl.common.utils.SecurityUtils;
import com.ycl.common.utils.StringUtils;
import com.ycl.common.utils.bean.BeanUtils;
import com.ycl.common.utils.excel.OutputExcelUtils;
import com.ycl.common.utils.file.FileUploadUtils;
import com.ycl.common.utils.file.FileUtils;
import com.ycl.common.utils.poi.ExcelUtil;
import com.ycl.constant.ProjectConstant;
import com.ycl.domain.entity.*;
import com.ycl.domain.excel.ProjectExcelTemplate;
import com.ycl.domain.form.*;
import com.ycl.domain.query.ProjectExportQuery;
import com.ycl.domain.query.ProjectInfoQuery;
import com.ycl.domain.vo.*;
import com.ycl.framework.utils.PageUtil;
import com.ycl.listener.excel.ProjectImportListener;
import com.ycl.mapper.*;
import com.ycl.service.*;
import com.ycl.system.mapper.SysDeptMapper;
import com.ycl.system.service.ISysDeptService;
import com.ycl.system.service.ISysDictDataService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.codec.Charsets;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
/**
 * 项目管理基础信息表 服务实现类
 *
 * @author flq
 * @since 2024-11-22
 */
@Service
@RequiredArgsConstructor
public class ProjectInfoServiceImpl extends ServiceImpl<ProjectInfoMapper, ProjectInfo> implements ProjectInfoService {
 
    private final ProjectInfoMapper projectInfoMapper;
    private final ProjectInfoWinUnitService projectInfoWinUnitService;
    private final ProjectInfoYearPlanService projectInfoYearPlanService;
    private final ProjectInvestmentFundingMapper investmentFundingMapper;
    private final ProjectInvestmentInfoMapper investmentInfoMapper;
    private final ProjectInvestmentPolicyComplianceMapper policyMapper;
    private final ProjectUnitRegistrationInfoMapper unitRegistrationInfoMapper;
    private final FileService fileService;
    private final FileMapper fileMapper;
    private final PlanMapper planMapper;
    private final ProjectInvestmentInfoServiceImpl projectInvestmentInfoServiceImpl;
    private final ProjectInvestmentFundingServiceImpl projectInvestmentFundingServiceImpl;
    private final ProjectInvestmentPolicyComplianceServiceImpl projectInvestmentPolicyComplianceServiceImpl;
    private final ProjectUnitRegistrationInfoServiceImpl projectUnitRegistrationInfoServiceImpl;
    private final SysDeptMapper sysDeptMapper;
    private final ISysDictDataService dictDataService;
 
 
    /**
     * 添加
     *
     * @param form
     * @return
     */
    @Override
    public Result add(ProjectInfoForm form) {
        //添加基本信息
        ProjectInfo entity = ProjectInfoForm.getEntityByForm(form, null);
        Long userId = SecurityUtils.getUserId();
//        Long deptId = SecurityUtils.getDeptId();
//        entity.setProjectOwnerUnit(deptId);
        entity.setCreateBy(userId);
        entity.setUpdateBy(userId);
        //如果是储备项目(未开工项目)不需要审核
        if (ProjectConstant.COMMIT.equals(entity.getUsedStatus()) && ProjectStatusEnum.PENDDING.getType().equals(entity.getProjectStatus())) {
            entity.setUsedStatus(ProjectConstant.PASS);
        }
        if(!checkProjectNameAndIdIsUnique(entity,null)){
            throw new RuntimeException("项目重复,项目名称、项目码已存在");
        }
        baseMapper.insert(entity);
        addPlan(entity.getId()); //添加计划表
        //添加文件
        List<File> fileList = form.getFileList();
        fileList.forEach(item -> {
            item.setBusId(entity.getId());
            item.setType(FileTypeEnum.PROJECT_INFO);
        });
        fileService.saveBatch(fileList);
 
        //添加项目中标单位
        saveUnit(entity.getId(), form.getWinUnitList());
        //添加年度投资计划
        saveYearPlan(entity.getId(), form.getYearPlanList());
        return Result.ok("添加成功").data(entity.getId());
    }
 
    /**
     * 判断项目名和项目码是否唯一
     * @param entity from true 来自add() false 来自edit()
     * @return
     */
    public boolean  checkProjectNameAndIdIsUnique(ProjectInfo entity,Long updateId){
        QueryWrapper<ProjectInfo> queryWrapper = new QueryWrapper<ProjectInfo>();
        if (updateId == null) {
 
            queryWrapper.eq("project_name", entity.getProjectName());
            queryWrapper.eq("deleted",0);
            if (StringUtils.isNotEmpty(entity.getProjectCode())) {
                queryWrapper.eq("project_code", entity.getProjectCode());
            }
 
            List<ProjectInfo> project = projectInfoMapper.selectList(queryWrapper);
 
            return project == null || project.isEmpty();
        }else {
 
            List<ProjectInfo> project = projectInfoMapper.checkProjectNameAndIdIsUnique(updateId,entity.getProjectName(),entity.getProjectCode());
 
            return project == null || project.isEmpty();
        }
 
    }
 
    public void addPlan(Long projectInfoId) {
        Plan plan = new Plan();
        plan.setProjectInfoId(projectInfoId);
        plan.setReportStatus(1);
        plan.setMonthStatus(1);
        plan.setSeasonStatus(1);
        plan.setYearStatus(1);
        plan.setDeleted(0);
        planMapper.insert(plan);
    }
 
    /**
     * 修改
     *
     * @param form
     * @return
     */
    @Override
    public Result update(ProjectInfoForm form) {
        ProjectInfo entity = baseMapper.selectById(form.getId());
 
        // 为空抛IllegalArgumentException,做全局异常处理
        Assert.notNull(entity, "记录不存在");
        ProjectInfoForm.getEntityByForm(form, entity);
        Long userId = SecurityUtils.getUserId();
        entity.setUpdateBy(userId);
        //如果是储备项目(未开工项目)不需要审核
        if (ProjectConstant.COMMIT.equals(entity.getUsedStatus()) && ProjectStatusEnum.PENDDING.getType().equals(entity.getProjectStatus())) {
            entity.setUsedStatus(ProjectConstant.PASS);
        }
        // 管理员修改的直接通过
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            entity.setUsedStatus(ProjectConstant.PASS);
        }
        if(!checkProjectNameAndIdIsUnique(entity,form.getId())){
            throw new RuntimeException("项目重复,项目名称、项目码已存在");
        }
        //更新项目信息
        baseMapper.updateById(entity);
        List<File> fileList = form.getFileList();
        fileList.forEach(item -> {
            item.setId(null);
            item.setBusId(entity.getId());
            item.setType(FileTypeEnum.PROJECT_INFO);
        });
        //删除原有文件
        QueryWrapper<File> fileQueryWrapper = new QueryWrapper<>();
        fileQueryWrapper.eq("type", FileTypeEnum.PROJECT_INFO.getType());
        fileQueryWrapper.eq("bus_id", entity.getId());
        fileMapper.delete(fileQueryWrapper);
        //替换成现有
        fileService.saveBatch(fileList);
 
        //修改项目中标单位
        //删除原数据
        QueryWrapper<ProjectInfoWinUnit> unitWrapper = new QueryWrapper<>();
        unitWrapper.eq("project_info_id", form.getId());
        projectInfoWinUnitService.remove(unitWrapper);
        //添加项目中标单位
        saveUnit(entity.getId(), form.getWinUnitList());
 
        //修改年度投资计划
        //删除原数据
        QueryWrapper<ProjectInfoYearPlan> yearPlanWrapper = new QueryWrapper<>();
        unitWrapper.eq("project_info_id", form.getId());
        projectInfoYearPlanService.remove(yearPlanWrapper);
        //添加年度投资计划
        saveYearPlan(entity.getId(), form.getYearPlanList());
        return Result.ok("修改成功");
    }
 
    private void saveUnit(Long id, List<ProjectInfoWinUnitForm> projectInfoWinUnitForm) {
        List<ProjectInfoWinUnit> projectInfoWinUnits = new ArrayList<>();
        for (ProjectInfoWinUnitForm winUnitForm : projectInfoWinUnitForm) {
            ProjectInfoWinUnit projectInfoWinUnit = ProjectInfoWinUnitForm.getEntityByForm(winUnitForm, null);
            projectInfoWinUnit.setId(null);
            projectInfoWinUnit.setProjectInfoId(id);
            projectInfoWinUnits.add(projectInfoWinUnit);
        }
        if (!CollectionUtils.isEmpty(projectInfoWinUnits)) {
            projectInfoWinUnitService.saveBatch(projectInfoWinUnits);
        }
    }
 
    private void saveYearPlan(Long id, List<ProjectInfoYearPlanForm> yearPlanList) {
        List<ProjectInfoYearPlan> projectInfoYearPlans = new ArrayList<>();
        for (ProjectInfoYearPlanForm yearPlanForm : yearPlanList) {
            ProjectInfoYearPlan yearPlan = ProjectInfoYearPlanForm.getEntityByForm(yearPlanForm, null);
            yearPlan.setId(null);
            yearPlan.setProjectInfoId(id);
            projectInfoYearPlans.add(yearPlan);
        }
        if (!CollectionUtils.isEmpty(projectInfoYearPlans)) {
            projectInfoYearPlanService.saveBatch(projectInfoYearPlans);
        }
    }
 
    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Override
    public Result remove(List<String> ids) {
        baseMapper.deleteBatchIds(ids);
        return Result.ok("删除成功");
    }
 
    /**
     * id删除
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result removeById(Long id) {
        investmentFundingMapper.delete(new QueryWrapper<ProjectInvestmentFunding>().eq("project_id", id));
        investmentInfoMapper.delete(new QueryWrapper<ProjectInvestmentInfo>().eq("project_id", id));
        policyMapper.delete(new QueryWrapper<ProjectInvestmentPolicyCompliance>().eq("project_id", id));
        unitRegistrationInfoMapper.delete(new QueryWrapper<ProjectUnitRegistrationInfo>().eq("project_id", id));
        projectInfoWinUnitService.remove(new QueryWrapper<ProjectInfoWinUnit>().eq("project_info_id", id));
        projectInfoYearPlanService.remove(new QueryWrapper<ProjectInfoYearPlan>().eq("project_info_id", id));
 
        List<String> types = new ArrayList<>();
        types.add(FileTypeEnum.PROJECT_INFO.getType());
        types.add(FileTypeEnum.INVEST_POLICY.getType());
        types.add(FileTypeEnum.DOCUMENT_INFO.getType());
        fileMapper.delete(new QueryWrapper<File>().eq("bus_id", id).in("type", types));
        baseMapper.deleteById(id);
        return Result.ok("删除成功");
    }
 
    /**
     * 分页查询,TODO 项目中心的分页使用单独的接口,去掉写死的逻辑换成dept or查询
     *
     * @param query
     * @return
     */
    @Override
    public Result page(ProjectInfoQuery query) {
        if (query.getProjectStartTime() != null) {
            query.setProjectStartTime(DateUtils.getDayStart(query.getProjectStartTime()));
        }
        if (query.getProjectEndTime() != null) {
            query.setProjectEndTime(DateUtils.getDayEnd(query.getProjectEndTime()));
        }
        String projectCategory = query.getProjectCategory();
        //实施阶段有两个值放在sql处理
        if (ProjectCategoryEnum.RESERVE.getType().equals(projectCategory)) {
            query.setProjectStatus(ProjectCategoryEnum.RESERVE.getStatus());
            query.setReserveOrPrevious(ProjectCategoryEnum.RESERVE.getCode());
        } else if (ProjectCategoryEnum.PREVIOUS.getType().equals(projectCategory)) {
            query.setProjectStatus(ProjectCategoryEnum.PREVIOUS.getStatus());
            query.setReserveOrPrevious(ProjectCategoryEnum.PREVIOUS.getCode());
        } else if (ProjectCategoryEnum.FINISH.getType().equals(projectCategory)) {
            query.setProjectStatus(ProjectCategoryEnum.FINISH.getStatus());
        } else if (ProjectCategoryEnum.EXCEPTION.getType().equals(projectCategory)) {
            //TODO
            //先查出异常流程或者异常进度或者异常计划的projectId和异常种类
            //通过projectId查出项目数据
            //补充相应的异常数据(异常种类、异常流程节点等)
            List<ProjectVO> list = new ArrayList<>();
            return Result.ok().data(list).total(0);
        }
 
        Long userId = SecurityUtils.getUserId();
        String ancestors = sysDeptMapper.selectAncestors(userId);
        String[] ancestorArr = ancestors.split(",");
        List<String> ancestorList = Arrays.stream(ancestorArr).collect(Collectors.toList());
        ancestorList.add(SecurityUtils.getDeptId() + "");
        if (!CollectionUtils.isEmpty(ancestorList) && ancestorList.contains("101")) {
            query.setProjectOwnerUnit(SecurityUtils.getDeptId());
            //业主
            query.setRole(0);
        } else if (!CollectionUtils.isEmpty(ancestorList) && ancestorList.contains("102")) {
            query.setCompetentDepartment(SecurityUtils.getDeptId());
            //审批 对应主管部门
            query.setRole(1);
        }
 
        IPage<ProjectInfoVO> page = PageUtil.getPage(query, ProjectInfoVO.class);
        baseMapper.getPage(query, page);
        List<ProjectInfoVO> records = page.getRecords();
        List<ProjectVO> list = new ArrayList<>();
        records.forEach(vo -> {
            ProjectInfoVO.transform(vo);
            ProjectVO projectVO = new ProjectVO();
            copyToProjectVO(vo, projectVO);
            List<Long> departmentList = vo.getCompetentDepartmentList();
            projectVO.setAuditRole(SecurityUtils.isAdmin(SecurityUtils.getUserId()) || (!CollectionUtils.isEmpty(departmentList) && departmentList.contains(SecurityUtils.getDeptId())));
            // 翻译项目阶段
            String phase = ProjectCategoryEnum.getPhaseByProjectStatus(projectVO.getProjectStatus(), projectVO.getProcessId() != null);
            projectVO.setProjectPhase(phase);
            if (StringUtils.isNotEmpty(projectVO.getProjectSubType())) {
                // 翻译项目子类型
                SysDictData dictData = dictDataService.selectDictDataByValue(projectVO.getProjectType(), projectVO.getProjectSubType(), "sys_project_type");
                if (Objects.nonNull(dictData)) {
                    projectVO.setProjectSubType(dictData.getDictLabel());
                }
            }
            list.add(projectVO);
        });
        return Result.ok().data(list).total(page.getTotal());
    }
 
    private void copyToProjectVO(ProjectInfoVO vo, ProjectVO projectVO) {
        //忽略null值的复制
        CopyUtils.copyNoNullProperties(vo, projectVO);
        if (vo.getProjectInvestmentInfo() != null)
            CopyUtils.copyNoNullProperties(vo.getProjectInvestmentInfo(), projectVO);
        if (vo.getProjectUnitRegistrationInfo() != null)
            CopyUtils.copyNoNullProperties(vo.getProjectUnitRegistrationInfo(), projectVO);
        if (vo.getProjectInvestmentPolicyCompliance() != null)
            CopyUtils.copyNoNullProperties(vo.getProjectInvestmentPolicyCompliance(), projectVO);
        if (vo.getProjectInvestmentFunding() != null)
            CopyUtils.copyNoNullProperties(vo.getProjectInvestmentFunding(), projectVO);
    }
 
    /**
     * 根据id查找
     *
     * @param id
     * @return
     */
    @Override
    public Result detail(Integer id) {
        ProjectInfo entity = baseMapper.getById(id);
        Assert.notNull(entity, "记录不存在");
        ProjectInfoVO vo = ProjectInfoVO.getVoByEntity(entity, null);
        List<Long> departmentList = vo.getCompetentDepartmentList();
        vo.setAuditRole(SecurityUtils.isAdmin(SecurityUtils.getUserId()) || (!CollectionUtils.isEmpty(departmentList) && departmentList.contains(SecurityUtils.getDeptId())));
        QueryWrapper<File> fileQueryWrapper = new QueryWrapper<>();
        fileQueryWrapper.eq("type", FileTypeEnum.PROJECT_INFO.getType());
        fileQueryWrapper.eq("bus_id", vo.getId());
 
        List<File> files = fileMapper.selectList(fileQueryWrapper);
        vo.setFileList(files);
 
        //中标单位
        List<ProjectInfoWinUnitVO> unitVOS = new ArrayList<>();
        List<ProjectInfoWinUnit> unitList = projectInfoWinUnitService.list(new QueryWrapper<ProjectInfoWinUnit>().eq("project_info_id", id));
        for (ProjectInfoWinUnit projectInfoWinUnit : unitList) {
            ProjectInfoWinUnitVO unitVO = ProjectInfoWinUnitVO.getVoByEntity(projectInfoWinUnit, null);
            unitVO.setId(null);
            unitVOS.add(unitVO);
        }
        vo.setWinUnitList(unitVOS);
        //年度投资计划
        List<ProjectInfoYearPlanVO> yearPlanVOS = new ArrayList<>();
        List<ProjectInfoYearPlan> yearPlaList = projectInfoYearPlanService.list(new QueryWrapper<ProjectInfoYearPlan>().eq("project_info_id", id));
        for (ProjectInfoYearPlan yearPlan : yearPlaList) {
            ProjectInfoYearPlanVO yearPlanVO = ProjectInfoYearPlanVO.getVoByEntity(yearPlan, null);
            yearPlanVO.setId(null);
            yearPlanVOS.add(yearPlanVO);
        }
        vo.setYearPlanList(yearPlanVOS);
        return Result.ok().data(vo);
    }
 
    /**
     * 列表
     *
     * @return
     */
    @Override
    public Result all() {
        List<ProjectInfo> entities = baseMapper.selectList(null);
        List<ProjectInfoVO> vos = entities.stream()
                .map(entity -> ProjectInfoVO.getVoByEntity(entity, null))
                .collect(Collectors.toList());
        return Result.ok().data(vos);
    }
 
    @Override
    public IndexCountVO getIndexCount(IndexDTO indexDTO) {
        // {"proPhaseCountVO":[{"type":"储备规划阶段","count":0,"amount":"0.00","text":"储"},
        // {"type":"项目前期阶段","count":0,"amount":"0.00","text":"新"},
        // {"type":"实施阶段","count":0,"amount":"0.00","text":"建"},{"type":"竣工投用阶段","count":0,"amount":"0.00","text":"竣"}],
        // "impTypeCountVO":[{"type":"一般项目","count":0,"amount":"0.00","text":"普"},
        // {"type":"县重点项目","count":0,"amount":"0.00","text":"县"},{"type":"市重点项目","count":0,"amount":"0.00","text":"市"},
        // {"type":"省重点项目","count":0,"amount":"0.00","text":"省"}]}}
        IndexCountVO indexCountVO = new IndexCountVO();
        List<IndexProPhaseCountVO> proPhaseCountVO = new ArrayList<>();
        List<ProjectVO> projectVOS = baseMapper.homeCount(new BaseEntity());
        List<ProjectVO> reserve = new ArrayList<>();
        List<ProjectVO> previous = new ArrayList<>();
        List<ProjectVO> implement = new ArrayList<>();
        List<ProjectVO> finish = new ArrayList<>();
        List<ProjectVO> normal = new ArrayList<>();
        List<ProjectVO> province = new ArrayList<>();
        List<ProjectVO> city = new ArrayList<>();
        List<ProjectVO> county = new ArrayList<>();
        projectVOS.forEach(item -> {
            if (ProjectCategoryEnum.RESERVE.getDesc().equals(ProjectCategoryEnum.getPhaseByProjectStatus(item.getProjectStatus(), item.getProcessId() != null))) {
                reserve.add(item);
            } else if (ProjectCategoryEnum.PREVIOUS.getDesc().equals(ProjectCategoryEnum.getPhaseByProjectStatus(item.getProjectStatus(), item.getProcessId() != null))) {
                previous.add(item);
            } else if (ProjectCategoryEnum.IMPLEMENT.getDesc().equals(ProjectCategoryEnum.getPhaseByProjectStatus(item.getProjectStatus(), item.getProcessId() != null))) {
                implement.add(item);
            } else if (ProjectCategoryEnum.FINISH.getDesc().equals(ProjectCategoryEnum.getPhaseByProjectStatus(item.getProjectStatus(), item.getProcessId() != null))) {
                finish.add(item);
            }
 
            if (ImportanceTypeEnum.PROVINCIAL_KEY.getType().equals(item.getImportanceType())) {
                province.add(item);
            } else if (ImportanceTypeEnum.SUINING_KEY.getType().equals(item.getImportanceType())) {
                city.add(item);
            } else if (ImportanceTypeEnum.SHEHONG_KEY.getType().equals(item.getImportanceType())) {
                county.add(item);
            } else // if  (ImportanceTypeEnum.NORMAL.getType().equals(item.getImportanceType()))
            {
                normal.add(item);
            }
        });
        //单位先默认为元
        proPhaseCountVO.add(new IndexProPhaseCountVO("储备规划阶段", reserve.size(), reserve.stream().
                filter(item -> !StringUtils.isEmpty(item.getTotalInvestment()))
                .map(item -> new BigDecimal(item.getTotalInvestment()).setScale(2, RoundingMode.HALF_UP))
                .reduce(BigDecimal.ZERO, BigDecimal::add).divide(new BigDecimal("10000"), 2, RoundingMode.HALF_UP).toString(), "储"));
 
        proPhaseCountVO.add(new IndexProPhaseCountVO("项目前期阶段", previous.size(), previous.stream().
                filter(item -> !StringUtils.isEmpty(item.getTotalInvestment()))
                .map(item -> new BigDecimal(item.getTotalInvestment()).setScale(2, RoundingMode.HALF_UP))
                .reduce(BigDecimal.ZERO, BigDecimal::add).divide(new BigDecimal("10000"), 2, RoundingMode.HALF_UP).toString(), "新"));
 
        proPhaseCountVO.add(new IndexProPhaseCountVO("实施阶段", implement.size(), implement.stream().
                filter(item -> !StringUtils.isEmpty(item.getTotalInvestment()))
                .map(item -> new BigDecimal(item.getTotalInvestment()).setScale(2, RoundingMode.HALF_UP))
                .reduce(BigDecimal.ZERO, BigDecimal::add).divide(new BigDecimal("10000"), 2, RoundingMode.HALF_UP).toString(), "建"));
 
        proPhaseCountVO.add(new IndexProPhaseCountVO("竣工投用阶段", finish.size(), finish.stream().
                filter(item -> !StringUtils.isEmpty(item.getTotalInvestment()))
                .map(item -> new BigDecimal(item.getTotalInvestment()).setScale(2, RoundingMode.HALF_UP))
                .reduce(BigDecimal.ZERO, BigDecimal::add).divide(new BigDecimal("10000"), 2, RoundingMode.HALF_UP).toString(), "竣"));
 
        indexCountVO.setProPhaseCountVO(proPhaseCountVO);
 
        List<IndexImpTypeCountVO> impTypeCountVO = new ArrayList<>();
        impTypeCountVO.add(new IndexImpTypeCountVO("一般项目", normal.size(), normal.stream().
                filter(item -> !StringUtils.isEmpty(item.getTotalInvestment()))
                .map(item -> new BigDecimal(item.getTotalInvestment()).setScale(2, RoundingMode.HALF_UP))
                .reduce(BigDecimal.ZERO, BigDecimal::add).divide(new BigDecimal("10000"), 2, RoundingMode.HALF_UP).toString(), "普"));
 
        impTypeCountVO.add(new IndexImpTypeCountVO("县重点项目", county.size(), county.stream().
                filter(item -> !StringUtils.isEmpty(item.getTotalInvestment()))
                .map(item -> new BigDecimal(item.getTotalInvestment()).setScale(2, RoundingMode.HALF_UP))
                .reduce(BigDecimal.ZERO, BigDecimal::add).divide(new BigDecimal("10000"), 2, RoundingMode.HALF_UP).toString(), "县"));
 
        impTypeCountVO.add(new IndexImpTypeCountVO("市重点项目", city.size(), city.stream().
                filter(item -> !StringUtils.isEmpty(item.getTotalInvestment()))
                .map(item -> new BigDecimal(item.getTotalInvestment()).setScale(2, RoundingMode.HALF_UP))
                .reduce(BigDecimal.ZERO, BigDecimal::add).divide(new BigDecimal("10000"), 2, RoundingMode.HALF_UP).toString(), "市"));
 
        impTypeCountVO.add(new IndexImpTypeCountVO("省重点项目", province.size(), province.stream().
                filter(item -> !StringUtils.isEmpty(item.getTotalInvestment()))
                .map(item -> new BigDecimal(item.getTotalInvestment()).setScale(2, RoundingMode.HALF_UP))
                .reduce(BigDecimal.ZERO, BigDecimal::add).divide(new BigDecimal("10000"), 2, RoundingMode.HALF_UP).toString(), "省"));
        indexCountVO.setImpTypeCountVO(impTypeCountVO);
        return indexCountVO;
    }
 
    @Override
    public Map<String, Integer> countExceptionProject(IndexDTO indexDTO) {
        Map<String, Integer> map = new HashMap<>();
        map.put("processExceptionProject", 0);
        return map;
    }
 
    @Override
    public Result docDetail(Integer id) {
        DocumentInfoForm documentInfoForm = new DocumentInfoForm();
        QueryWrapper<File> fileQueryWrapper = new QueryWrapper<>();
        fileQueryWrapper.eq("type", FileTypeEnum.DOCUMENT_INFO.getType());
        fileQueryWrapper.eq("bus_id", id);
        List<File> files = fileMapper.selectList(fileQueryWrapper);
        documentInfoForm.setFileList(files);
        return Result.ok().data(documentInfoForm);
    }
 
    @Override
    public Result addDoc(DocumentInfoForm form) {
        List<File> fileList = form.getFileList();
        fileList.forEach(item -> {
            item.setId(null);
            item.setBusId(form.getProjectId());
            item.setType(FileTypeEnum.DOCUMENT_INFO);
        });
        //删除原有文件
        QueryWrapper<File> fileQueryWrapper = new QueryWrapper<>();
        fileQueryWrapper.eq("type", FileTypeEnum.DOCUMENT_INFO.getType());
        fileQueryWrapper.eq("bus_id", form.getProjectId());
        fileMapper.delete(fileQueryWrapper);
        //替换成现有
        fileService.saveBatch(fileList);
        return Result.ok();
    }
 
    @Override
    public Result getManagerFlag(Integer recordId) {
        ProjectInfo projectInfo = baseMapper.selectById(recordId);
        // 判断当前用户id是否在主管列表中
        String competentDepartment = projectInfo.getCompetentDepartment();
        if (StringUtils.isEmpty(competentDepartment)) {
            return Result.ok().data(false);
        }
        List<String> list = Arrays.asList(competentDepartment.split(","));
        // 获得当前用户id
        Long userId = SecurityUtils.getUserId();
        if (list.contains(userId.toString())) {
            return Result.ok().data(true);
        } else {
            return Result.ok().data(false);
        }
    }
 
    @Override
    public void export(HttpServletResponse response, ProjectExportQuery query) throws IOException {
        List<ProjectVO> data = baseMapper.selectProjectDetailByIds(query.getDataIdList());
        List<ProjectExcelTemplate> excelList = new ArrayList<>();
        //字典作翻译
        data.forEach(item -> {
            ProjectExcelTemplate excel = new ProjectExcelTemplate();
            BeanUtils.copyProperties(item, excel);
            //项目阶段
            excel.setProjectPhase(ProjectCategoryEnum.getPhaseByProjectStatus(item.getProjectStatus(), item.getProcessId() != null));
            excelList.add(excel);
        });
        Set<Integer> indexes = OutputExcelUtils.getSelectFields(query.getFieldList(), ProjectExcelTemplate.class);
        //不需要附件
        if (!query.getRequireFile()) {
            try (ServletOutputStream outputStream = response.getOutputStream()) {
                EasyExcel.write(outputStream, ProjectExcelTemplate.class).includeColumnIndexes(indexes).sheet("项目列表").doWrite(excelList);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                throw new BaseException("导出失败:" + e.getMessage());
            }
        } else {
            //需要附件,导出为zip
            response.setContentType("application/zip");
            response.setCharacterEncoding(Charsets.UTF_8.name());
            String fileName = URLEncoder.encode("项目列表", Charsets.UTF_8);
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".zip");
 
            java.io.File tempDir = null;
            try {
                //查出附件
                List<File> projectInfoFiles = fileMapper.selectList(new QueryWrapper<File>()
                        .in("bus_id", data.stream().map(ProjectVO::getId).collect(Collectors.toList()))
                        .and(q -> q.eq("type", FileTypeEnum.PROJECT_INFO.getType()).or().eq("type", FileTypeEnum.DOCUMENT_INFO.getType()))
                );
                List<File> policyFiles = fileMapper.selectList(new QueryWrapper<File>()
                        .in("bus_id", data.stream().map(ProjectVO::getPolicyId).collect(Collectors.toList()))
                        .eq("type", FileTypeEnum.INVEST_POLICY.getType())
                );
                projectInfoFiles.addAll(policyFiles);
                projectInfoFiles.forEach(file -> {
                    data.forEach(item -> {
                        if (FileTypeEnum.PROJECT_INFO.equals(file.getType())) {
                            if (file.getBusId().equals(item.getId())) {
                                //审批计划书
                                item.setApprovalPlan(file.getOriginalName());
                            }
                        } else if (FileTypeEnum.DOCUMENT_INFO.equals(file.getType())) {
                            if (file.getBusId().equals(item.getId())) {
                                //相关文书
                                item.setDocuments(file.getOriginalName());
                            }
                        } else if (FileTypeEnum.INVEST_POLICY.equals(file.getType())) {
                            if (file.getBusId().equals(item.getPolicyId())) {
                                //符合产业政策附件
                                item.setPolicyComplianceAttachment(file.getOriginalName());
                            }
                        }
                    });
 
                });
                // 创建临时目录
                tempDir = Files.createTempDirectory("temp").toFile();
 
                java.io.File templateDir = new java.io.File(tempDir, "template");
                if (!templateDir.exists()) {
                    templateDir.mkdirs();
                }
 
                // 创建 Excel 文件
                java.io.File excelFile = new java.io.File(templateDir, "excel.xlsx");
                if (!excelFile.exists()) {
                    excelFile.createNewFile();
                }
 
                // 写入 Excel 模板数据
                try (FileOutputStream fileOutputStream = new FileOutputStream(excelFile, false)) {
                    EasyExcel.write(fileOutputStream, ProjectExcelTemplate.class).sheet("项目列表").doWrite(excelList);
                }
 
                // 创建附件目录
                java.io.File attachmentDir = new java.io.File(templateDir, "attachment");
                if (!attachmentDir.exists()) {
                    attachmentDir.mkdirs();
                }
 
                //将附件复制导目录下
                for (File item : projectInfoFiles) {
                    String url = item.getUrl();
                    if (StringUtils.isBlank(url)) continue;
                    url = url.replaceFirst(Constants.RESOURCE_PREFIX, SysConfig.getProfile());
                    // 直接使用File类处理系统路径
                    java.io.File sourceFile = new java.io.File(url);
                    if (!sourceFile.exists() || !sourceFile.isFile()) {
                        // 处理文件不存在或不是文件的情况
                        continue; // 或者记录日志、抛出异常等
                    }
                    // 目标文件路径,使用attachmentDir作为父目录,并保持原文件名
                    java.io.File targetFile = new java.io.File(attachmentDir, sourceFile.getName());
                    // 复制文件
                    try {
                        Files.copy(sourceFile.toPath(), targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
                    } catch (IOException e) {
                        log.error(e.getMessage());
                    }
                }
                // 打包 ZIP 文件
                java.io.File zipFile = ZipUtil.zip(templateDir);
                byte[] zipBytes = Files.readAllBytes(zipFile.toPath());
 
                // 将 ZIP 文件写入响应
                try (ServletOutputStream outputStream = response.getOutputStream()) {
                    outputStream.write(zipBytes);
                }
            } finally {
                OutputExcelUtils.deleteDirectoryOrFile(tempDir);
            }
        }
    }
 
    @Override
    public Result updateUsedStatus(Integer id, Integer usedStatus) {
        ProjectInfo entity = baseMapper.selectById(id);
        // 为空抛IllegalArgumentException,做全局异常处理
        Assert.notNull(entity, "记录不存在");
        entity.setUsedStatus(usedStatus);
        baseMapper.updateById(entity);
        return Result.ok("删除成功");
    }
 
    @Transactional
    @Override
    public void importProject(MultipartFile file) {
        java.io.File tempZip = null;
        java.io.File tempDir = null;
        try {
            tempZip = java.io.File.createTempFile("temp", ".zip");
            try (InputStream inputStream = file.getInputStream()) {
                Files.copy(inputStream, tempZip.toPath(), StandardCopyOption.REPLACE_EXISTING);
            }
 
            tempDir = Files.createTempDirectory("temp-dir").toFile();
 
            try {
                ZipUtil.unzip(tempZip, tempDir, CharsetUtil.CHARSET_UTF_8);
            } catch (IORuntimeException e) {
                log.error("解压失败,尝试使用GBK编码解压...");
                ZipUtil.unzip(tempZip, tempDir, CharsetUtil.CHARSET_GBK);
            }
 
            Path path = tempDir.toPath();
            ProjectImportListener projectImportListener = new ProjectImportListener();
            //需要存入数据库
            ArrayList<File> attachments = new ArrayList<>();
            //实际的文件
            List<java.io.File> files = new ArrayList<>();
            try (Stream<Path> pathStream = Files.walk(path)) {
                pathStream.forEach(filePath -> {
                    if (Files.isDirectory(filePath) && filePath.getFileName().toString().equals("attachment")) {
                        //附件处理
                        try (Stream<Path> stream = Files.walk(filePath)) {
                            stream.forEach(attachmentPath -> {
                                if (Files.isRegularFile(attachmentPath)) {
                                    files.add(attachmentPath.toFile());
                                }
                            });
                        } catch (IOException e) {
                            log.error(e.getMessage());
                            throw new RuntimeException(e);
                        }
                    } else if (Files.isRegularFile(filePath) && filePath.getFileName().toString().equals("excel.xlsx")) {
                        //导入数据处理
                        try (FileInputStream fileInputStream = new FileInputStream(filePath.toFile())) {
                            ExcelUtil.importExcel(fileInputStream, ProjectExcelTemplate.class, projectImportListener);
                        } catch (IOException e) {
                            log.error(e.getMessage());
                            throw new RuntimeException(e);
                        }
                    }
                });
            }
            /* 把附件存入上传路径 */
            //上传文件路径
            String filePath = SysConfig.getUploadPath();
            files.forEach(attachmentFile -> {
                try {
                    //上传
                    String url = FileUploadUtils.uploadIOFile(filePath, attachmentFile);
                    //存放的文件名会加上_时间戳的后缀
                    String newName = FileUtils.getName(url);
                    attachments.add(new File()
                            .setUrl(url)
                            .setName(newName)
                            .setOriginalName(attachmentFile.getName()));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
 
            Map<String, Long> projectInfoMap = projectImportListener.getProjectInfoMap();
            Map<String, Long> investmentProjectPolicyComplianceMap = projectImportListener.getInvestmentProjectPolicyComplianceMap();
            Map<String, Long> documentsMap = projectImportListener.getDocumentsMap();
            //补充type和busId字段
            attachments.forEach(attachment -> {
                String fileName = attachment.getOriginalName();
                if (ObjectUtil.isNotNull(projectInfoMap.get(fileName))) {
                    attachment.setBusId(projectInfoMap.get(fileName));
                    attachment.setType(FileTypeEnum.PROJECT_INFO);
                } else if (ObjectUtil.isNotNull(investmentProjectPolicyComplianceMap.get(fileName))) {
                    attachment.setBusId(investmentProjectPolicyComplianceMap.get(fileName));
                    attachment.setType(FileTypeEnum.INVEST_POLICY);
                } else if (ObjectUtil.isNotNull(documentsMap.get(fileName))) {
                    attachment.setBusId(documentsMap.get(fileName));
                    attachment.setType(FileTypeEnum.DOCUMENT_INFO);
                }
            });
 
            // 添加项目附件关联表
            fileService.saveBatch(attachments);
        } catch (IOException e) {
            log.error(e.getMessage());
            throw new RuntimeException(e);
        } finally {
            deleteDirectoryOrFile(tempZip);
            deleteDirectoryOrFile(tempDir);
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result editProject(ProjectForm form) {
        Long projectId = form.getProjectInfoForm().getId();
        if (ObjectUtil.isNotNull(form.getProjectInfoForm())) {
            if (ObjectUtil.isNull(form.getProjectInfoForm().getId())) {
                //新增
                projectId = (Long) ((ProjectInfoServiceImpl) AopContext.currentProxy()).add(form.getProjectInfoForm()).get("data");
            } else {
                //更新
                ((ProjectInfoServiceImpl) AopContext.currentProxy()).update(form.getProjectInfoForm());
            }
        }
        if (ObjectUtil.isNotNull(form.getProjectInvestmentInfoForm())) {
            if (ObjectUtil.isNull(form.getProjectInvestmentInfoForm().getId())) {
                form.getProjectInvestmentInfoForm().setProjectId(projectId);
                projectInvestmentInfoServiceImpl.add(form.getProjectInvestmentInfoForm());
            } else {
                projectInvestmentInfoServiceImpl.update(form.getProjectInvestmentInfoForm());
            }
        }
        if (ObjectUtil.isNotNull(form.getProjectInvestmentFundingForm())) {
            if (ObjectUtil.isNull(form.getProjectInvestmentFundingForm().getId())) {
                form.getProjectInvestmentFundingForm().setProjectId(projectId);
                projectInvestmentFundingServiceImpl.add(form.getProjectInvestmentFundingForm());
            } else {
                projectInvestmentFundingServiceImpl.update(form.getProjectInvestmentFundingForm());
            }
        }
        if (ObjectUtil.isNotNull(form.getProjectInvestmentPolicyComplianceForm())) {
            if (ObjectUtil.isNull(form.getProjectInvestmentPolicyComplianceForm().getId())) {
                form.getProjectInvestmentPolicyComplianceForm().setProjectId(projectId);
                projectInvestmentPolicyComplianceServiceImpl.add(form.getProjectInvestmentPolicyComplianceForm());
            } else {
                projectInvestmentPolicyComplianceServiceImpl.update(form.getProjectInvestmentPolicyComplianceForm());
            }
        }
        if (ObjectUtil.isNotNull(form.getProjectUnitRegistrationInfoForm())) {
            if (ObjectUtil.isNull(form.getProjectUnitRegistrationInfoForm().getId())) {
                form.getProjectUnitRegistrationInfoForm().setProjectId(projectId);
                projectUnitRegistrationInfoServiceImpl.add(form.getProjectUnitRegistrationInfoForm());
            } else {
                projectUnitRegistrationInfoServiceImpl.update(form.getProjectUnitRegistrationInfoForm());
            }
        }
        if (ObjectUtil.isNotNull(form.getDocumentInfoForm())) {
            if (ObjectUtil.isNull(form.getDocumentInfoForm().getProjectId())) {
                return Result.error("请先保存投资管理基本信息");
            } else {
                addDoc(form.getDocumentInfoForm());
            }
        }
 
        return Result.ok("提交成功");
    }
 
    @Override
    public Result searchByKey(String wordKey) {
        Wrapper wrapper = null;
        if (!StringUtils.isEmpty(wordKey)) {
            wrapper = Wrappers.<ProjectInfo>lambdaQuery().like(ProjectInfo::getProjectName, wordKey).or().like(ProjectInfo::getProjectCode, wordKey);
        }
        List<ProjectInfo> list = baseMapper.selectList(wrapper);
        return Result.ok().data(list);
    }
 
    private static void deleteDirectoryOrFile(java.io.File file) {
        if (ObjectUtil.isNull(file)) {
            return;
        }
 
        if (file.isDirectory()) {
            java.io.File[] files = file.listFiles();
            if (files != null) {
                for (java.io.File f : files) {
                    deleteDirectoryOrFile(f);
                }
            }
        }
        file.delete();
    }
 
    /**
     * 项目名称校验重复
     *
     * @return
     */
    public void checkProjectNameUnique(ProjectInfo projectInfo) {
        if (StrUtil.isBlank(projectInfo.getProjectName())) {
            throw new BaseException("项目名称不能为空");
        }
        Assert.isTrue(baseMapper.selectCount(Wrappers.<ProjectInfo>lambdaQuery().eq(ProjectInfo::getProjectName, projectInfo.getProjectName())) == 0, "项目名称重复");
    }
 
}