shiyunteng
2 天以前 c6db287f6c987060d615fd7ef5126511e051d5c6
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
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<DeviceStocktakingPlanMapper, DeviceStocktakingPlan> 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<Long> classArr = new ArrayList<>();
//            for (Long classId:deviceStocktakingPlan.getClassIdList()
//                 ) {
//                List<Long> 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<DeviceStocktakingPlanScope> queryMapper = new QueryWrapper<>();
        queryMapper.eq("plan_id",id);
        List<DeviceStocktakingPlanScope> scopeList = scopeMapper.selectList(queryMapper);
        if (scopeList.size()>0){
            scopeList.stream().forEach(scopeItem->{
                List<DeviceClassVo> 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<StocktatingRecordVo> recordVoList = recordMapper.selectStocktatingResultByPlanId(id);
        return R.ok(recordVoList);
    }
 
    @Override
    public R getByIdNew(Long id) {
        DeviceStocktakingPlanVo vo = baseMapper.selectVoById(id);
        if (vo!=null){
            QueryWrapper<DeviceStocktakingRecord> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("plan_id",id);
            List<DeviceStocktakingRecord> recordList = recordMapper.selectList(queryWrapper);
            vo.setRecordList(recordList);
            QueryWrapper<DeviceStocktakingPlanUser> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.eq("plan_id",id);
            List<DeviceStocktakingPlanUser> userList = userMapper.selectList(userQueryWrapper);
            if (userList.size()>0) {
                List<Long> 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<DeviceStocktakingPlanUser> 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<DeviceStocktakingPlanScope> 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();
    }
}