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 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 page1 = deviceDemandPlanService.pageByScope(page, wrapper); for (DeviceDemandPlan record : page1.getRecords()) { LambdaQueryWrapper totalWrapper = Wrappers.lambdaQuery(); totalWrapper.eq(DeviceDemandTotal::getPlanId,record.getId()); List 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 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) { 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 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 byPlanId = deviceDemandSubService.getByPlanId(l); subs.addAll(byPlanId); } } plan.setAmount(aa); plan.setReleasePerson(name); plan.setStatus(3); plan.setType2(1); 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 subs = new ArrayList<>(); QueryWrapper wrapper = new QueryWrapper<>(); wrapper.lambda() .eq(DeviceDemandPlan::getPlanId,id); List list = deviceDemandPlanService.list(wrapper); List collect = list.stream().map(DeviceDemandPlan::getId).collect(Collectors.toList()); for (Long l : collect) { //合并子项 List byPlanId = deviceDemandSubService.getByPlanId(l); subs.addAll(byPlanId); } List> resultList = new ArrayList<>(); for (Map.Entry> integerListEntry : subs.stream().collect(Collectors.groupingBy(item -> item.getMonth())).entrySet()) { Integer month = integerListEntry.getKey(); List> mapList = new ArrayList<>(); for (Map.Entry> 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 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 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 export(DeviceDemandPlan deviceDemandPlan,Long[] ids) { return deviceDemandPlanService.list(Wrappers.lambdaQuery(deviceDemandPlan).in(ArrayUtil.isNotEmpty(ids), DeviceDemandPlan::getId, ids)); } }