shiyunteng
6 天以前 fc8c86e7a365d5c6bdc37c2b05b9f83115ac2bc3
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
package com.by4cloud.platformx.device.controller;
 
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.by4cloud.platformx.common.core.util.R;
import com.by4cloud.platformx.common.log.annotation.SysLog;
import com.by4cloud.platformx.device.constant.MaxSizeContant;
import com.by4cloud.platformx.device.entity.*;
import com.by4cloud.platformx.device.service.*;
import com.by4cloud.platformx.device.util.NumUtils;
import com.github.yulichang.adapter.base.ITableInfoAdapter;
import org.springframework.security.access.prepost.PreAuthorize;
import com.by4cloud.platformx.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 年度投资计划主表
 *
 * @author kdq
 * @date 2025-03-28 11:06:50
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/investmentPlan" )
@Tag(description = "investmentPlan" , name = "年度投资计划主表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class InvestmentPlanController {
 
    private final  InvestmentPlanService investmentPlanService;
    private final  InvestmentPlanItemService investmentPlanItemService;
    private final DeviceDemandPlanService deviceDemandPlanService;
    private final DeviceDemandSubService deviceDemandSubService;
    private final JcMaxSizeService maxSizeService;
    private final DeviceService deviceService;
 
    /**
     * 分页查询
     * @param page 分页对象
     * @param investmentPlan 年度投资计划主表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('device_investmentPlan_view')" )
    public R getInvestmentPlanPage(@ParameterObject Page page, @ParameterObject InvestmentPlan investmentPlan) {
        LambdaQueryWrapper<InvestmentPlan> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(investmentPlan.getYear()!=null,InvestmentPlan::getYear,investmentPlan.getYear());
        wrapper.like(StringUtils.isNotBlank(investmentPlan.getName()),InvestmentPlan::getName,investmentPlan.getName());
        wrapper.orderByDesc(InvestmentPlan::getCreateTime);
        return R.ok(investmentPlanService.page(page, wrapper));
    }
 
 
    /**
     * 通过id查询年度投资计划主表
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('device_investmentPlan_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        Map<String,Object> map = new HashMap<>();
        InvestmentPlan plan = investmentPlanService.getById(id);
        QueryWrapper<InvestmentPlanItem> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(InvestmentPlanItem::getPlanId,plan.getId())
                .orderByAsc(InvestmentPlanItem::getMonth);
        List<InvestmentPlanItem> list = investmentPlanItemService.list(wrapper);
        List<Map<String,Object>> resultList = new ArrayList<>();
        for (Map.Entry<Integer, List<InvestmentPlanItem>> listEntry : list.stream().collect(Collectors.groupingBy(item -> item.getMonth())).entrySet()) {
            Integer month = listEntry.getKey();
            List<InvestmentPlanItem> value = listEntry.getValue();
            Map<String,Object> entry = new HashMap<>();
            entry.put("month",month);
            entry.put("value",value);
            resultList.add(entry);
        }
        map.put("plan",plan);
        map.put("items",resultList);
        return R.ok(map);
    }
 
    /**
     * 新增年度投资计划主表
     * @param investmentPlan 年度投资计划主表
     * @return R
     */
    @Operation(summary = "新增年度投资计划主表" , description = "新增年度投资计划主表" )
    @SysLog("新增年度投资计划主表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('device_investmentPlan_add')" )
    public R save(@RequestBody InvestmentPlan investmentPlan) {
        List<DeviceDemandSub> subs = new ArrayList<>();
        investmentPlan.setNumber(maxSizeService.nextNo(MaxSizeContant.TZPLAN_NUM));
        investmentPlanService.save(investmentPlan);
 
        String planIds = investmentPlan.getPlanIds();
        String[] split = planIds.split(",");
        for (String s : split) {
            DeviceDemandPlan demandPlan = deviceDemandPlanService.getById(Long.parseLong(s));
            QueryWrapper<DeviceDemandPlan> wrapper = new QueryWrapper<>();
            wrapper.lambda()
                    .eq(DeviceDemandPlan::getPlanId,demandPlan.getId());
            List<DeviceDemandPlan> list = deviceDemandPlanService.list(wrapper);
            List<Long> collect = list.stream().map(DeviceDemandPlan::getId).collect(Collectors.toList());
            for (Long l : collect) {
                //合并子项
                List<DeviceDemandSub> byPlanId = deviceDemandSubService.getByPlanId(l);
                subs.addAll(byPlanId);
            }
        }
        List<InvestmentPlanItem> items = new ArrayList<>();
        for (Map.Entry<Integer, List<DeviceDemandSub>> integerListEntry : subs.stream().collect(Collectors.groupingBy(item -> item.getMonth())).entrySet()) {
            Integer month = integerListEntry.getKey();
            List<DeviceDemandSub> subList = integerListEntry.getValue();
            for (Map.Entry<Long, List<DeviceDemandSub>> deviceEntry : subList.stream().collect(Collectors.groupingBy(item -> item.getDeviceId())).entrySet()) {
                Long deviceId = deviceEntry.getKey();
                List<DeviceDemandSub> value = deviceEntry.getValue();
                double total = value.stream().mapToDouble(DeviceDemandSub::getPrice).sum();
 
                Device device = deviceService.getById(deviceId);
                InvestmentPlanItem item = new InvestmentPlanItem();
                item.setYear(value.get(0).getYear());
                item.setPlanId(investmentPlan.getId());
                item.setMonth(month);
                item.setDeviceId(deviceId);
                item.setNumber(device.getNumber());
                item.setSpecification(device.getSpecification());
                item.setNum(value.size());
                item.setUnit(device.getUnit());
                item.setPrice(device.getPrice());
                item.setAmount(total);
                items.add(item);
            }
        }
        investmentPlanItemService.saveBatch(items);
        return R.ok();
    }
 
    /**
     * 修改年度投资计划主表
     * @param investmentPlan 年度投资计划主表
     * @return R
     */
    @Operation(summary = "修改年度投资计划主表" , description = "修改年度投资计划主表" )
    @SysLog("修改年度投资计划主表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('device_investmentPlan_edit')" )
    public R updateById(@RequestBody InvestmentPlan investmentPlan) {
        return R.ok(investmentPlanService.updateById(investmentPlan));
    }
 
    /**
     * 通过id删除年度投资计划主表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除年度投资计划主表" , description = "通过id删除年度投资计划主表" )
    @SysLog("通过id删除年度投资计划主表" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('device_investmentPlan_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(investmentPlanService.removeBatchByIds(CollUtil.toList(ids)));
    }
 
 
    /**
     * 导出excel 表格
     * @param investmentPlan 查询条件
        * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('device_investmentPlan_export')" )
    public List<InvestmentPlan> export(InvestmentPlan investmentPlan,Long[] ids) {
        return investmentPlanService.list(Wrappers.lambdaQuery(investmentPlan).in(ArrayUtil.isNotEmpty(ids), InvestmentPlan::getId, ids));
    }
}