package com.by4cloud.platformx.device.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.by4cloud.platformx.common.core.util.R; import com.by4cloud.platformx.device.constant.CommonStatusContant; import com.by4cloud.platformx.device.entity.*; import com.by4cloud.platformx.device.entity.vo.DeviceClassVo; import com.by4cloud.platformx.device.entity.vo.DeviceStocktakingPlanVo; import com.by4cloud.platformx.device.entity.vo.StocktatingRecordVo; import com.by4cloud.platformx.device.mapper.*; import com.by4cloud.platformx.device.service.DeviceStocktakingPlanService; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; import java.util.List; import java.util.stream.Collectors; /** * 设备盘点计划 * * @author syt * @date 2025-04-24 15:19:13 */ @Service @AllArgsConstructor public class DeviceStocktakingPlanServiceImpl extends ServiceImpl implements DeviceStocktakingPlanService { private final DeviceStocktakingPlanScopeMapper scopeMapper; private final DeviceStocktakingPlanUserMapper userMapper; private final DeviceInventoryMapper inventoryMapper; private final DeviceStocktakingRecordMapper recordMapper; @Override public R saveNew(DeviceStocktakingPlan deviceStocktakingPlan) { deviceStocktakingPlan.setPlanStatus(CommonStatusContant.DEVICE_STOCKTAKING_STATUS_APPROVED); baseMapper.insert(deviceStocktakingPlan); //新增盘点人员 if (deviceStocktakingPlan.getUserIdList().size()>0){ deviceStocktakingPlan.getUserIdList().stream().forEach(userId->{ DeviceStocktakingPlanUser planUser = new DeviceStocktakingPlanUser(); planUser.setPlanId(deviceStocktakingPlan.getId()); planUser.setStocktakingUserId(userId); userMapper.insert(planUser); }); } //新增判断范围 if (deviceStocktakingPlan.getClassIdList().size()>0){ // List classArr = new ArrayList<>(); // for (Long classId:deviceStocktakingPlan.getClassIdList() // ) { // List classes = classMapper.selectEndNode(classId); // //根据分类iD 查询出所有末端分类ID并去重 // classArr = ListUtils.union(classArr,classes).stream().distinct().collect(Collectors.toList()); // } // if (classArr.size()>0){ // classArr.stream().forEach(classId->{ // DeviceStocktakingPlanScope planScope = new DeviceStocktakingPlanScope(); // planScope.setPlanId(deviceStocktakingPlan.getId()); // planScope.setClassId(classId); // scopeMapper.insert(planScope); // }); // } deviceStocktakingPlan.getClassIdList().stream().forEach(classId->{ DeviceStocktakingPlanScope planScope = new DeviceStocktakingPlanScope(); planScope.setPlanId(deviceStocktakingPlan.getId()); planScope.setClassId(classId); scopeMapper.insert(planScope); }); } return R.ok(); } @Override public R approved(Long id) { DeviceStocktakingPlan plan = baseMapper.selectById(id); plan.setPlanStatus(CommonStatusContant.DEVICE_STOCKTAKING_STATUS_GEN); baseMapper.updateById(plan); return R.ok(); } @Override public R genRecord(Long id) { QueryWrapper queryMapper = new QueryWrapper<>(); queryMapper.eq("plan_id",id); List scopeList = scopeMapper.selectList(queryMapper); if (scopeList.size()>0){ scopeList.stream().forEach(scopeItem->{ List deviceClassVos = inventoryMapper.selectListByClassId(scopeItem.getClassId()); if (deviceClassVos.size()>0) { deviceClassVos.stream().forEach(vo->{ DeviceStocktakingRecord record = new DeviceStocktakingRecord(); record.setPlanId(id); record.setClassId(vo.getClassId()); record.setDeviceClassId(vo.getClassId()); record.setDeviceId(vo.getDeviceId()); record.setDeviceName(vo.getName()); record.setDeviceCode(vo.getNumber()); record.setDeviceModels(vo.getSpecification()); record.setSerialNo(vo.getSerialNo()); recordMapper.insert(record); }); } }); } //更新计划状态 DeviceStocktakingPlan plan = baseMapper.selectById(id); plan.setPlanStatus(CommonStatusContant.DEVICE_STOCKTAKING_STATUS_STOCKTATING); baseMapper.updateById(plan); return R.ok(); } @Override public R getResult(Long id) { List recordVoList = recordMapper.selectStocktatingResultByPlanId(id); return R.ok(recordVoList); } @Override public R getByIdNew(Long id) { DeviceStocktakingPlanVo vo = baseMapper.selectVoById(id); if (vo!=null){ QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("plan_id",id); List recordList = recordMapper.selectList(queryWrapper); vo.setRecordList(recordList); QueryWrapper userQueryWrapper = new QueryWrapper<>(); userQueryWrapper.eq("plan_id",id); List userList = userMapper.selectList(userQueryWrapper); if (userList.size()>0) { List userIds = userList.stream().map(DeviceStocktakingPlanUser::getStocktakingUserId).collect(Collectors.toList()); vo.setUserIds(userIds); } } return R.ok(vo); } @Override public R complete(Long id) { DeviceStocktakingPlan plan = baseMapper.selectById(id); plan.setPlanStatus(CommonStatusContant.DEVICE_STOCKTAKING_STATUS_COMPLETE); baseMapper.updateById(plan); return R.ok(); } @Override public R updateByIdNew(DeviceStocktakingPlan deviceStocktakingPlan) { baseMapper.updateById(deviceStocktakingPlan); //新增盘点人员 if (deviceStocktakingPlan.getUserIdList().size()>0){ QueryWrapper userQueryWrapper = new QueryWrapper<>(); userQueryWrapper.eq("plan_id",deviceStocktakingPlan.getId()); userMapper.delete(userQueryWrapper); deviceStocktakingPlan.getUserIdList().stream().forEach(userId->{ DeviceStocktakingPlanUser planUser = new DeviceStocktakingPlanUser(); planUser.setPlanId(deviceStocktakingPlan.getId()); planUser.setStocktakingUserId(userId); userMapper.insert(planUser); }); } //新增判断范围 if (deviceStocktakingPlan.getClassIdList().size()>0){ QueryWrapper scopeQueryWrapper = new QueryWrapper<>(); scopeQueryWrapper.eq("plan_id",deviceStocktakingPlan.getId()); scopeMapper.delete(scopeQueryWrapper); deviceStocktakingPlan.getClassIdList().stream().forEach(classId->{ DeviceStocktakingPlanScope planScope = new DeviceStocktakingPlanScope(); planScope.setPlanId(deviceStocktakingPlan.getId()); planScope.setClassId(classId); scopeMapper.insert(planScope); }); } return R.ok(); } }