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
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
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.common.security.util.SecurityUtils;
import com.by4cloud.platformx.device.constant.MaxSizeContant;
import com.by4cloud.platformx.device.entity.Device;
import com.by4cloud.platformx.device.entity.DeviceDemandPlan;
import com.by4cloud.platformx.device.entity.DeviceDemandSub;
import com.by4cloud.platformx.device.entity.DeviceDemandTotal;
import com.by4cloud.platformx.device.service.*;
import com.by4cloud.platformx.device.util.NumUtils;
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 pig
 * @date 2025-03-05 14:36:30
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/deviceDemandPlan" )
@Tag(description = "deviceDemandPlan" , name = "设备需求计划主表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class DeviceDemandPlanController {
 
    private final  DeviceDemandPlanService deviceDemandPlanService;
    private final DeviceDemandTotalService deviceDemandTotalService;
    private final DeviceDemandSubService deviceDemandSubService;
    private final JcMaxSizeService maxSizeService;
    private final DeviceService deviceService;
 
    /**
     * 分页查询
     * @param page 分页对象
     * @param deviceDemandPlan 设备需求计划主表
     * @return
     */
    @Operation(summary = "本单位分页查询" , description = "本单位分页查询" )
    @GetMapping("/page" )
    public R getDeviceDemandPlanPage(@ParameterObject Page page, @ParameterObject DeviceDemandPlan deviceDemandPlan) {
        LambdaQueryWrapper<DeviceDemandPlan> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(deviceDemandPlan.getDeclareCompId() !=null,DeviceDemandPlan::getDeclareCompId,deviceDemandPlan.getDeclareCompId());
        wrapper.eq(deviceDemandPlan.getReleasePerson() !=null,DeviceDemandPlan::getReleasePerson,deviceDemandPlan.getReleasePerson());
        wrapper.eq(deviceDemandPlan.getYear() !=null,DeviceDemandPlan::getYear,deviceDemandPlan.getYear());
        wrapper.eq(DeviceDemandPlan::getType2,0);
        wrapper.orderByDesc(DeviceDemandPlan::getCreateTime);
        Page<DeviceDemandPlan> page1 = deviceDemandPlanService.pageByScope(page, wrapper);
        for (DeviceDemandPlan record : page1.getRecords()) {
            LambdaQueryWrapper<DeviceDemandTotal> totalWrapper = Wrappers.lambdaQuery();
            totalWrapper.eq(DeviceDemandTotal::getPlanId,record.getId());
            List<DeviceDemandTotal> list = deviceDemandTotalService.list(totalWrapper);
            record.setTotalList(list);
        }
        return R.ok(page1);
    }
 
    /**
     * 分页查询2
     * @param page 分页对象
     * @param deviceDemandPlan 设备需求计划主表
     * @return
     */
    @Operation(summary = "本单位分页查询" , description = "本单位分页查询" )
    @GetMapping("/page2" )
    public R page2(@ParameterObject Page page, @ParameterObject DeviceDemandPlan deviceDemandPlan) {
        LambdaQueryWrapper<DeviceDemandPlan> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(deviceDemandPlan.getDeclareCompId() !=null,DeviceDemandPlan::getDeclareCompId,deviceDemandPlan.getDeclareCompId());
        wrapper.eq(deviceDemandPlan.getCompId() !=null,DeviceDemandPlan::getCompId,deviceDemandPlan.getCompId());
        wrapper.like(deviceDemandPlan.getReleasePerson() !=null,DeviceDemandPlan::getReleasePerson,deviceDemandPlan.getReleasePerson());
        wrapper.eq(deviceDemandPlan.getYear() !=null,DeviceDemandPlan::getYear,deviceDemandPlan.getYear());
        wrapper.eq(deviceDemandPlan.getStatus() !=null,DeviceDemandPlan::getStatus,deviceDemandPlan.getStatus());
        wrapper.eq(DeviceDemandPlan::getType2,1);
        return R.ok(deviceDemandPlanService.page(page, wrapper));
    }
 
 
    /**
     * 通过id查询设备需求计划主表
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('platformx_deviceDemandPlan_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(deviceDemandPlanService.getById(id));
    }
 
    /**
     * 新增设备需求计划主表
     * @param deviceDemandPlan 设备需求计划主表
     * @return R
     */
    @Operation(summary = "新增设备需求计划主表" , description = "新增设备需求计划主表" )
    @SysLog("新增设备需求计划主表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('platformx_deviceDemandPlan_add')" )
    public R save(@RequestBody DeviceDemandPlan deviceDemandPlan) {
 
        QueryWrapper<DeviceDemandPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("declare_comp_id", deviceDemandPlan.getDeclareCompId());
        queryWrapper.eq("year", deviceDemandPlan.getYear());
        queryWrapper.eq("type",deviceDemandPlan.getType());
        List<DeviceDemandPlan> list = deviceDemandPlanService.list(queryWrapper);
        if (list.size() > 0) {
            if (deviceDemandPlan.getType()==0) {
                return R.failed(deviceDemandPlan.getDeclareCompName() + deviceDemandPlan.getYear() + "年度计划已存在");
            }
            return R.failed(deviceDemandPlan.getDeclareCompName() + deviceDemandPlan.getYear() + "调整计划已存在");
        }
        deviceDemandPlan.setNumber(maxSizeService.nextNo(MaxSizeContant.PLAN_NUM));
        deviceDemandPlan.setType2(0);
        return R.ok(deviceDemandPlanService.save(deviceDemandPlan));
    }
 
    @Operation(summary = "合并计划提交" , description = "合并计划提交" )
    @GetMapping("/mergePlan" )
    public R mergePlan(String ids) {
        List<DeviceDemandSub> subs = new ArrayList<>();
        DeviceDemandPlan plan = new DeviceDemandPlan();
        deviceDemandPlanService.save(plan);
        Double aa = 0d;
        String name = SecurityUtils.getUser().getName();
        if(StringUtils.isNotBlank(ids)){
            String[] split = ids.split(",");
            for (String s : split) {
                long l = Long.parseLong(s);
                DeviceDemandPlan byId = deviceDemandPlanService.getById(l);
                byId.setPlanId(plan.getId());
                byId.setSendDate(new Date());
                deviceDemandPlanService.updateById(byId);
                plan.setYear(byId.getYear());
                plan.setType(byId.getType());
                aa= NumUtils.addDoubles(aa,byId.getAmount());
                //合并子项
                List<DeviceDemandSub> byPlanId = deviceDemandSubService.getByPlanId(l);
                subs.addAll(byPlanId);
            }
        }
        plan.setAmount(aa);
        plan.setReleasePerson(name);
        plan.setStatus(3);
        plan.setType2(1);
        plan.setSendDate(new Date());
        plan.setDeclareCompId(plan.getCompId());
        plan.setDeclareCompName(plan.getCompName());
        if(subs.size()>0){
            deviceDemandPlanService.updateById(plan);
            return R.ok("提交成功");
        }else {
            return R.failed("请选择计划提交");
        }
    }
 
    @Operation(summary = "查看子项详情" , description = "查看子项详情" )
    @GetMapping("/getByMergePlanId/{id}" )
    public R getByMergePlanId(@PathVariable("id" ) Long id) {
        List<DeviceDemandSub> subs = new ArrayList<>();
        QueryWrapper<DeviceDemandPlan> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(DeviceDemandPlan::getPlanId,id);
        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<Map<String,Object>> resultList = new ArrayList<>();
        for (Map.Entry<Integer, List<DeviceDemandSub>> integerListEntry : subs.stream().collect(Collectors.groupingBy(item -> item.getMonth())).entrySet()) {
            Integer month = integerListEntry.getKey();
            List<Map<String,Object>> mapList = new ArrayList<>();
            for (Map.Entry<Long, List<DeviceDemandSub>> entry : integerListEntry.getValue().stream().collect(Collectors.groupingBy(item -> item.getDeviceId())).entrySet()) {
                Long key = entry.getKey();
                int size = entry.getValue().size();
                Device device = deviceService.getById(key);
                Map<String,Object> map = new HashMap<>();
                map.put("deviceNumber",device.getNumber());
                map.put("name",device.getName());
                map.put("count",size);
                map.put("manu",device.getManu());
                map.put("beforeDate",device.getBeforeDate());
                map.put("depreciation",device.getDepreciation());
                map.put("remindDate",device.getRemindDate());
                mapList.add(map);
            }
            Map<String,Object> map = new HashMap<>();
            map.put("month",month);
            map.put("deviceList",mapList);
            resultList.add(map);
 
        }
        return R.ok(resultList);
 
    }
 
    /**
     * 修改设备需求计划主表
     * @param deviceDemandPlan 设备需求计划主表
     * @return R
     */
    @Operation(summary = "修改设备需求计划主表" , description = "修改设备需求计划主表" )
    @SysLog("修改设备需求计划主表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('platformx_deviceDemandPlan_edit')" )
    public R updateById(@RequestBody DeviceDemandPlan deviceDemandPlan) {
        return R.ok(deviceDemandPlanService.updateById(deviceDemandPlan));
    }
 
    /**
     * 通过id删除设备需求计划主表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除设备需求计划主表" , description = "通过id删除设备需求计划主表" )
    @SysLog("通过id删除设备需求计划主表" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('platformx_deviceDemandPlan_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(deviceDemandPlanService.removeBatchByIds(CollUtil.toList(ids)));
    }
 
 
    /**
     * 导出excel 表格
     * @param deviceDemandPlan 查询条件
        * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('platformx_deviceDemandPlan_export')" )
    public List<DeviceDemandPlan> export(DeviceDemandPlan deviceDemandPlan,Long[] ids) {
        return deviceDemandPlanService.list(Wrappers.lambdaQuery(deviceDemandPlan).in(ArrayUtil.isNotEmpty(ids), DeviceDemandPlan::getId, ids));
    }
}