无关风月
1 天以前 6999d4114eb6d64d0775e2f9ff00572b0e60ee31
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
package com.ruoyi.web.controller.api;
 
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.TDept;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.model.AssetInventoryRecord;
import com.ruoyi.system.model.AssetMain;
import com.ruoyi.system.query.AssetInventoryListQuery;
import com.ruoyi.system.query.AssetStatisticsListDetailQuery;
import com.ruoyi.system.query.AssetStatisticsListQuery;
import com.ruoyi.system.service.AssetInventoryRecordService;
import com.ruoyi.system.service.AssetMainService;
import com.ruoyi.system.service.AssetTypeService;
import com.ruoyi.system.service.TDeptService;
import com.ruoyi.system.vo.AssetInventoryVO;
import com.ruoyi.system.vo.AssetStatisticsDetailVO;
import com.ruoyi.system.vo.AssetStatisticsVO;
import com.ruoyi.system.vo.asset.AssetTypeTreeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 资产类型表 前端控制器
 * </p>
 *
 * @author WuGuanFengYue
 * @since 2025-09-15
 */
@Api(tags = {"资产-资产汇总-资产出入库-闲置房产"})
@Validated
@RestController
@RequestMapping("/asset-statistics")
@RequiredArgsConstructor
public class AssetStatisticsController {
 
    @Autowired
    private  AssetTypeService assetTypeService;
    @Autowired
    private AssetInventoryRecordService assetInventoryRecordService;
    @Autowired
    private AssetMainService assetMainService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private TDeptService deptService;
 
    @ApiOperation("获取资产类型树形数据")
    @GetMapping("/tree")
    public R<List<AssetTypeTreeVO>> getAssetTypeTree() {
        List<AssetTypeTreeVO> treeData = assetTypeService.getAssetTypeTree();
        return R.ok(treeData);
    }
 
 
 
    public List<Integer> getAllSubDeptIds(Integer parentId) {
        List<Integer> allSubIds = new ArrayList<>();
        allSubIds.add(parentId);
        getSubDeptIdsRecursive(parentId, allSubIds);
        return allSubIds.stream().distinct().collect(Collectors.toList());
    }
    private void getSubDeptIdsRecursive(Integer parentId, List<Integer> allSubIds) {
        // 查询直接下级
        List<Integer> directSubIds = deptService.lambdaQuery().eq(TDept::getParentId, parentId).list()
                .stream().map(TDept::getId).collect(Collectors.toList());
 
        for (Integer subId : directSubIds) {
            allSubIds.add(subId);
            // 递归查询下级的下级
            getSubDeptIdsRecursive(subId, allSubIds);
        }
    }
    @ApiOperation("资产汇总分页列表")
    @PostMapping("/pageList")
    public R<PageInfo<AssetStatisticsVO>> pageList(@RequestBody AssetStatisticsListQuery query) {
        String deptId = tokenService.getLoginUser().getDeptId();
        TDept dept = deptService.getById(tokenService.getLoginUser().getDeptId());
        List<Integer> deptIds = deptService.getAllSubDeptIds(deptId);
        if (dept.getDeptName().contains("资产管理")){
            // 可以查询所有数据
            query.setDeptIds(new ArrayList<>());
        }else{
            if (deptIds.isEmpty()){
                return R.ok(new PageInfo<>());
            }else{
                query.setDeptIds(deptIds);
            }
        }
        return R.ok(assetTypeService.pageList(query));
    }
    @ApiOperation("资产明细")
    @PostMapping("/pageListDetail")
    public R<PageInfo<AssetStatisticsDetailVO>> pageListDetail(@RequestBody AssetStatisticsListDetailQuery query) {
        String deptId = tokenService.getLoginUser().getDeptId();
        TDept dept = deptService.getById(tokenService.getLoginUser().getDeptId());
        List<Integer> deptIds = deptService.getAllSubDeptIds(deptId);
        if (dept.getDeptName().contains("资产管理")){
            // 可以查询所有数据
            query.setDeptIds(new ArrayList<>());
        }else{
            if (deptIds.isEmpty()){
                return R.ok(new PageInfo<>());
            }else{
                query.setDeptIds(deptIds);
            }
        }
        PageInfo<AssetStatisticsDetailVO> res = assetTypeService.pageListDetail(query);
        return R.ok(res);
    }
    @ApiOperation("资产出入库分页列表")
    @PostMapping("/pageListInventory")
    public R<PageInfo<AssetInventoryVO>> pageListInventory(@RequestBody AssetInventoryListQuery query) {
        String deptId = tokenService.getLoginUser().getDeptId();
        TDept dept = deptService.getById(tokenService.getLoginUser().getDeptId());
        List<Integer> deptIds = deptService.getAllSubDeptIds(deptId);
        if (dept.getDeptName().contains("资产管理")){
            // 可以查询所有数据
            query.setDeptIds(new ArrayList<>());
        }else{
            if (deptIds.isEmpty()){
                return R.ok(new PageInfo<>());
            }else{
                query.setDeptIds(deptIds);
            }
        }
        String[] dateList = query.getDate().split(",");
        String month = dateList[1];
        int monthValue = Integer.parseInt(month);
        int year = LocalDateTime.now().getYear();
        LocalDateTime firstDay = LocalDateTime.of(year, monthValue, 1, 0, 0, 0);
        LocalDateTime lastDay = LocalDateTime.of(year, monthValue,
                YearMonth.of(year, monthValue).lengthOfMonth(), 23, 59, 59);
        query.setDateStart(firstDay);
        query.setDateEnd(lastDay);
        List<Integer> assetMainIds = assetInventoryRecordService.lambdaQuery().between(AssetInventoryRecord::getCreateTime, query.getDateStart(), query.getDateEnd())
                .list().stream().map(AssetInventoryRecord::getAssetMainId).collect(Collectors.toList());
        if (assetMainIds.isEmpty()){
            return R.ok(new PageInfo<>());
        }
        if (StringUtils.hasLength(query.getNameOrCode())){
            // 查询出资产名称或者资产编号符合条件的code
            List<Integer> assetTypeIds = assetMainService.lambdaQuery()
                    .in(AssetMain::getOwnershipDeptId, deptIds)
                    .and(wrapper -> wrapper.like(AssetMain::getAssetName, query.getNameOrCode())
                            .or()
                            .like(AssetMain::getAssetCode, query.getNameOrCode()))
                    .list()
                    .stream()
                    .map(AssetMain::getAssetTypeId)
                    .collect(Collectors.toList());
            query.setAssetMainIds(assetTypeIds);
            if (assetTypeIds.isEmpty()){
                return R.ok(new PageInfo<>());
            }
            // 和assetMainIds取交集
            List<Integer> res = assetMainIds.stream().filter(assetTypeIds::contains).collect(Collectors.toList());
            if (res.isEmpty()){
                return R.ok(new PageInfo<>());
            }
            query.setAssetMainIds(res);
        }
        return R.ok(assetTypeService.pageListInventory(query));
    }
    @ApiOperation("资产出入库明细分页列表")
    @PostMapping("/pageListInventoryDetail")
    public R<PageInfo<AssetStatisticsDetailVO>> pageListInventoryDetail(@RequestBody AssetStatisticsListDetailQuery query) {
        String deptId = tokenService.getLoginUser().getDeptId();
        TDept dept = deptService.getById(tokenService.getLoginUser().getDeptId());
        List<Integer> deptIds = deptService.getAllSubDeptIds(deptId);
        if (dept.getDeptName().contains("资产管理")){
            // 可以查询所有数据
            query.setDeptIds(new ArrayList<>());
        }else{
            if (deptIds.isEmpty()){
                return R.ok(new PageInfo<>());
            }else{
                query.setDeptIds(deptIds);
            }
        }
        String[] dateList = query.getDate().split(",");
        String month = dateList[1];
        int monthValue = Integer.parseInt(month);
        int year = LocalDateTime.now().getYear();
        LocalDateTime firstDay = LocalDateTime.of(year, monthValue, 1, 0, 0, 0);
        LocalDateTime lastDay = LocalDateTime.of(year, monthValue,
                YearMonth.of(year, monthValue).lengthOfMonth(), 23, 59, 59);
        query.setDateStart(firstDay);
        query.setDateEnd(lastDay);
        List<Integer> assetMainIds = assetInventoryRecordService.lambdaQuery().between(AssetInventoryRecord::getCreateTime, query.getDateStart(), query.getDateEnd())
                .list().stream().map(AssetInventoryRecord::getAssetMainId).collect(Collectors.toList());
        if (assetMainIds.isEmpty()){
            return R.ok(new PageInfo<>());
        }
        if (StringUtils.hasLength(query.getNameOrCode())){
            // 查询出资产名称或者资产编号符合条件的code
            List<Integer> assetTypeIds = assetMainService.lambdaQuery()
                    .in(AssetMain::getOwnershipDeptId, deptIds)
                    .and(wrapper -> wrapper.like(AssetMain::getAssetName, query.getNameOrCode())
                            .or()
                            .like(AssetMain::getAssetCode, query.getNameOrCode()))
                    .list()
                    .stream()
                    .map(AssetMain::getAssetTypeId)
                    .collect(Collectors.toList());
            query.setAssetMainIds(assetTypeIds);
            if (assetTypeIds.isEmpty()){
                return R.ok(new PageInfo<>());
            }
            // 和assetMainIds取交集
            List<Integer> res = assetMainIds.stream().filter(assetTypeIds::contains).collect(Collectors.toList());
            if (res.isEmpty()){
                return R.ok(new PageInfo<>());
            }
            query.setAssetMainIds(res);
        }
        PageInfo<AssetStatisticsDetailVO> res = assetTypeService.pageListInventoryDetail(query);
        return R.ok(res);
    }
 
    
}