吉安感知网项目-后端
rain
2026-02-09 66bfbdb9251c22ffa2b5f05595a76a3547a2f5eb
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
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.sxkj.gd.orderdata.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.sxkj.gd.orderdata.dto.GdSupplyDemandDTO;
import org.sxkj.gd.orderdata.entity.GdSupplyDemandAuditAttachmentEntity;
import org.sxkj.gd.orderdata.entity.GdSupplyDemandAuditEntity;
import org.sxkj.gd.orderdata.entity.GdSupplyDemandEntity;
import org.sxkj.gd.orderdata.excel.GdSupplyDemandExcel;
import org.sxkj.gd.orderdata.mapper.GdSupplyDemandMapper;
import org.sxkj.gd.orderdata.param.GdSupplyDemandPageParam;
import org.sxkj.gd.orderdata.service.IGdSupplyDemandAuditAttachmentService;
import org.sxkj.gd.orderdata.service.IGdSupplyDemandAuditService;
import org.sxkj.gd.orderdata.service.IGdSupplyDemandService;
import org.sxkj.gd.orderdata.vo.GdSupplyDemandVO;
import org.sxkj.system.cache.SysCache;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
 
/**
 * 供需需求信息表 服务实现类
 *
 * @author lw
 * @since 2026-01-16
 */
@Service
@AllArgsConstructor
public class GdSupplyDemandServiceImpl extends BaseServiceImpl<GdSupplyDemandMapper, GdSupplyDemandEntity> implements IGdSupplyDemandService {
 
    /**
     * 需求状态常量:0-草稿、1-申请中、2-审核通过、3-拒绝申请
     */
    private static final String STATUS_DRAFT = "0";
    private static final String STATUS_APPLY = "1";
    private static final String STATUS_APPROVED = "2";
    private static final String STATUS_REJECTED = "3";
 
    /**
     * 审核状态常量:0-待审核、1-审核通过、2-审核拒绝
     */
    private static final String AUDIT_PENDING = "0";
    private static final String AUDIT_PASS = "1";
    private static final String AUDIT_REJECT = "2";
 
    private final IGdSupplyDemandAuditService gdSupplyDemandAuditService;
    private final IGdSupplyDemandAuditAttachmentService gdSupplyDemandAuditAttachmentService;
 
    @Override
    public IPage<GdSupplyDemandVO> selectGdSupplyDemandPage(IPage<GdSupplyDemandVO> page, GdSupplyDemandPageParam gdSupplyDemand) {
        List<Long> deptList = new ArrayList<>();
        if (!AuthUtil.isAdministrator()) {
            deptList = SysCache.getDeptChildIds(Long.valueOf(AuthUtil.getDeptId()));
        }
        gdSupplyDemand.setDeptList(deptList);
        return page.setRecords(baseMapper.selectGdSupplyDemandPage(page, gdSupplyDemand));
    }
 
    @Override
    public IPage<GdSupplyDemandEntity> selectGdSupplyDemandList(IPage<GdSupplyDemandEntity> page, GdSupplyDemandDTO gdSupplyDemand) {
        return page.setRecords(baseMapper.selectGdSupplyDemandList(page, gdSupplyDemand));
    }
 
    @Override
    public GdSupplyDemandEntity detailSupplyDemand(GdSupplyDemandDTO gdSupplyDemand) {
        if (Func.isEmpty(gdSupplyDemand)) {
            throw new ServiceException("需求参数不能为空");
        }
        List<Long> deptList = new ArrayList<>();
        if (!AuthUtil.isAdministrator()) {
            deptList = SysCache.getDeptChildIds(Long.valueOf(AuthUtil.getDeptId()));
        }
        GdSupplyDemandEntity detail = baseMapper.selectGdSupplyDemandDetail(gdSupplyDemand, deptList);
        if (detail == null) {
            throw new ServiceException("未查询到数据");
        }
        return detail;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean submitSupplyDemand(GdSupplyDemandDTO gdSupplyDemand) {
        if (Func.isEmpty(gdSupplyDemand)) {
            throw new ServiceException("需求参数不能为空");
        }
        GdSupplyDemandEntity supplyDemand = Objects.requireNonNull(BeanUtil.copy(gdSupplyDemand, GdSupplyDemandEntity.class));
 
        // 获取前端传入的需求状态
        String demandStatus = supplyDemand.getDemandStatus();
        if (StringUtil.isBlank(demandStatus)) {
            throw new ServiceException("需求状态不能为空");
        }
 
        // 校验状态值有效性
        if (!STATUS_DRAFT.equals(demandStatus) && !STATUS_APPLY.equals(demandStatus)) {
            throw new ServiceException("需求状态值无效,仅支持 0(草稿) 或 1(申请中)");
        }
 
        boolean isInsert = Func.isEmpty(supplyDemand.getId());
        Long userId = AuthUtil.getUserId();
        Long deptId = Long.valueOf(AuthUtil.getDeptId());
        supplyDemand.setUpdateUser(userId);
 
        boolean saved;
        if (isInsert) {
            supplyDemand.setCreateUser(userId);
            supplyDemand.setCreateDept(deptId);
            saved = baseMapper.insertGdSupplyDemand(supplyDemand) > 0;
        } else {
            saved = baseMapper.updateGdSupplyDemand(supplyDemand) > 0;
        }
 
        if (!saved) {
            return false;
        }
 
        // 仅当状态为"申请中"(1)时才创建审核记录
        if (STATUS_APPLY.equals(demandStatus)) {
            GdSupplyDemandEntity auditDemand = loadSupplyDemandForAudit(supplyDemand.getId());
            GdSupplyDemandAuditEntity auditEntity = buildAuditEntity(auditDemand, AUDIT_PENDING, null);
            return gdSupplyDemandAuditService.saveSupplyDemandAudit(auditEntity);
        }
 
        // 状态为"草稿"(0)时直接返回成功
        return true;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean approveSupplyDemand(Long demandId, String auditOpinion, List<Long> attachIds) {
        GdSupplyDemandEntity supplyDemand = loadSupplyDemandForAudit(demandId);
        Long userId = AuthUtil.getUserId();
        GdSupplyDemandEntity update = new GdSupplyDemandEntity();
        update.setId(demandId);
        update.setDemandStatus(STATUS_APPROVED);
        update.setUpdateUser(userId);
        boolean updated = baseMapper.updateGdSupplyDemand(update) > 0;
        if (!updated) {
            return false;
        }
        GdSupplyDemandAuditEntity auditEntity = buildAuditEntity(supplyDemand, AUDIT_PASS, auditOpinion);
        boolean auditSaved = gdSupplyDemandAuditService.saveSupplyDemandAudit(auditEntity);
        if (!auditSaved) {
            return false;
        }
        if (Func.isEmpty(attachIds)) {
            return true;
        }
        List<GdSupplyDemandAuditAttachmentEntity> attachments = new ArrayList<>();
        Long deptId = Long.valueOf(AuthUtil.getDeptId());
        attachIds.forEach(attachId -> {
            if (attachId == null) {
                return;
            }
            GdSupplyDemandAuditAttachmentEntity attachment = new GdSupplyDemandAuditAttachmentEntity();
            attachment.setDemandId(demandId);
            attachment.setAttachId(attachId);
            attachment.setAreaCode(supplyDemand.getAreaCode());
            attachment.setCreateUser(userId);
            attachment.setCreateDept(deptId);
            attachments.add(attachment);
        });
        if (attachments.isEmpty()) {
            return true;
        }
        return gdSupplyDemandAuditAttachmentService.saveSupplyDemandAuditAttachments(attachments);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean rejectSupplyDemand(Long demandId, String auditOpinion) {
        if (StringUtil.isBlank(auditOpinion)) {
            throw new ServiceException("拒绝原因不能为空");
        }
        GdSupplyDemandEntity supplyDemand = loadSupplyDemandForAudit(demandId);
        Long userId = AuthUtil.getUserId();
        GdSupplyDemandEntity update = new GdSupplyDemandEntity();
        update.setId(demandId);
        update.setDemandStatus(STATUS_REJECTED);
        update.setUpdateUser(userId);
        boolean updated = baseMapper.updateGdSupplyDemand(update) > 0;
        if (!updated) {
            return false;
        }
        GdSupplyDemandAuditEntity auditEntity = buildAuditEntity(supplyDemand, AUDIT_REJECT, auditOpinion);
        return gdSupplyDemandAuditService.saveSupplyDemandAudit(auditEntity);
    }
 
    private GdSupplyDemandAuditEntity buildAuditEntity(GdSupplyDemandEntity supplyDemand, String auditStatus, String auditOpinion) {
        if (Func.isEmpty(supplyDemand) || supplyDemand.getId() == null) {
            throw new ServiceException("需求数据不存在");
        }
        Long userId = AuthUtil.getUserId();
        Long deptId = Long.valueOf(AuthUtil.getDeptId());
        GdSupplyDemandAuditEntity auditEntity = new GdSupplyDemandAuditEntity();
        auditEntity.setDemandId(supplyDemand.getId());
        auditEntity.setAuditStatus(auditStatus);
        auditEntity.setAuditOpinion(StringUtil.isBlank(auditOpinion) ? null : auditOpinion);
        auditEntity.setAreaCode(supplyDemand.getAreaCode());
        auditEntity.setCreateUser(userId);
        auditEntity.setCreateDept(deptId);
        return auditEntity;
    }
 
    private GdSupplyDemandEntity loadSupplyDemandForAudit(Long demandId) {
        if (demandId == null) {
            throw new ServiceException("需求编号不能为空");
        }
        GdSupplyDemandDTO query = new GdSupplyDemandDTO();
        query.setId(demandId);
        GdSupplyDemandEntity detail = baseMapper.selectGdSupplyDemandDetail(query, SysCache.getDeptChildIds(Long.valueOf(AuthUtil.getDeptId())));
        if (detail == null) {
            throw new ServiceException("需求数据不存在");
        }
        return detail;
    }
 
    @Override
    public List<GdSupplyDemandExcel> exportGdSupplyDemand(Wrapper<GdSupplyDemandEntity> queryWrapper) {
        List<GdSupplyDemandExcel> gdSupplyDemandList = baseMapper.exportGdSupplyDemand(queryWrapper);
        //gdSupplyDemandList.forEach(gdSupplyDemand -> {
        //    gdSupplyDemand.setTypeName(DictCache.getValue(DictEnum.YES_NO, GdSupplyDemand.getType()));
        //});
        return gdSupplyDemandList;
    }
 
}