zhangwencui
2026-10-29 b2f8320707dc72b2429bfb4fe0b317c11b1e5b7f
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
package com.ruoyi.customerPortal.service;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.basic.mapper.CustomerMapper;
import com.ruoyi.basic.mapper.CustomerUserMapper;
import com.ruoyi.basic.pojo.Customer;
import com.ruoyi.basic.pojo.CustomerUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.customerPortal.dto.ReviewSubmissionDto;
import com.ruoyi.customerPortal.mapper.CustomerSubmissionMapper;
import com.ruoyi.customerPortal.pojo.CustomerSubmission;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.time.LocalDateTime;
import java.time.Year;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
 
@Service
@RequiredArgsConstructor
public class CustomerSubmissionService extends ServiceImpl<CustomerSubmissionMapper, CustomerSubmission> {
    private final CustomerUserMapper customerUserMapper;
    private final CustomerMapper customerMapper;
 
    public Page<CustomerSubmission> listMine(Page<CustomerSubmission> page, Integer reportYear, Integer status) {
        CustomerUser binding = currentBinding();
        LambdaQueryWrapper<CustomerSubmission> query = new LambdaQueryWrapper<CustomerSubmission>()
                .eq(CustomerSubmission::getCustomerId, binding.getCustomerId())
                .eq(CustomerSubmission::getIsDeleted, 0)
                .eq(reportYear != null, CustomerSubmission::getReportYear, reportYear)
                .eq(status != null, CustomerSubmission::getStatus, status)
                .orderByDesc(CustomerSubmission::getReportYear)
                .orderByDesc(CustomerSubmission::getCreateTime);
        return fillCustomerName(page(page, query));
    }
 
    public Page<CustomerSubmission> listManage(Page<CustomerSubmission> page, Long customerId,
                                                Integer reportYear, Integer status) {
        ensureStaff();
        LambdaQueryWrapper<CustomerSubmission> query = new LambdaQueryWrapper<CustomerSubmission>()
                .eq(CustomerSubmission::getIsDeleted, 0)
                .eq(customerId != null, CustomerSubmission::getCustomerId, customerId)
                .eq(reportYear != null, CustomerSubmission::getReportYear, reportYear)
                .eq(status != null, CustomerSubmission::getStatus, status)
                .orderByAsc(CustomerSubmission::getStatus)
                .orderByDesc(CustomerSubmission::getUpdateTime);
        return fillCustomerName(page(page, query));
    }
 
    @Transactional(rollbackFor = Exception.class)
    public CustomerSubmission submit(CustomerSubmission input) {
        CustomerUser binding = currentBinding();
        int currentYear = Year.now().getValue();
        if (input.getReportYear() == null || input.getReportYear() < currentYear - 2
                || input.getReportYear() > currentYear) {
            throw new ServiceException("只能提交最近三个年度的财务材料");
        }
        if (input.getMaterialFiles() == null || input.getMaterialFiles().isBlank()) {
            throw new ServiceException("请至少上传一份材料");
        }
        CustomerSubmission entity = getOne(new LambdaQueryWrapper<CustomerSubmission>()
                .eq(CustomerSubmission::getCustomerId, binding.getCustomerId())
                .eq(CustomerSubmission::getReportYear, input.getReportYear())
                .eq(CustomerSubmission::getIsDeleted, 0), false);
        if (entity != null && entity.getStatus() != null
                && entity.getStatus() != 0 && entity.getStatus() != 3) {
            throw new ServiceException("材料已受理或结果已发布,不能再次修改");
        }
        if (entity == null) {
            entity = new CustomerSubmission();
            entity.setCustomerId(binding.getCustomerId());
            entity.setReportYear(input.getReportYear());
            entity.setSubmitUserId(SecurityUtils.getUserId());
            entity.setSubmitUserName(SecurityUtils.getLoginUser().getNickName());
            entity.setIsDeleted(0);
        }
        entity.setReportName(input.getReportName());
        entity.setMaterialFiles(input.getMaterialFiles());
        entity.setStatus(0);
        entity.setReviewRemark(null);
        entity.setResultText(null);
        entity.setResultFiles(null);
        entity.setPublishUserId(null);
        entity.setPublishUserName(null);
        entity.setPublishTime(null);
        saveOrUpdate(entity);
        return entity;
    }
 
    @Transactional(rollbackFor = Exception.class)
    public void review(ReviewSubmissionDto dto) {
        ensureStaff();
        if (dto.getId() == null || dto.getStatus() == null
                || (dto.getStatus() != 1 && dto.getStatus() != 2 && dto.getStatus() != 3)) {
            throw new ServiceException("审核参数不正确");
        }
        CustomerSubmission entity = getById(dto.getId());
        if (entity == null || Objects.equals(entity.getIsDeleted(), 1)) {
            throw new ServiceException("申报记录不存在");
        }
        Integer oldStatus = entity.getStatus();
        if (dto.getStatus() == 1 && !Objects.equals(oldStatus, 0)) {
            throw new ServiceException("只有待审核材料可以受理");
        }
        if (dto.getStatus() == 2 && !Objects.equals(oldStatus, 1)) {
            throw new ServiceException("只有处理中材料可以发布结果");
        }
        if (dto.getStatus() == 3 && !Objects.equals(oldStatus, 0) && !Objects.equals(oldStatus, 1)) {
            throw new ServiceException("当前状态不能退回");
        }
        if (dto.getStatus() == 3 && (dto.getReviewRemark() == null || dto.getReviewRemark().isBlank())) {
            throw new ServiceException("退回时必须填写原因");
        }
        if (dto.getStatus() == 2 && (dto.getResultText() == null || dto.getResultText().isBlank())
                && (dto.getResultFiles() == null || dto.getResultFiles().isBlank())) {
            throw new ServiceException("发布时请填写结果说明或上传结果文件");
        }
        entity.setStatus(dto.getStatus());
        entity.setResultText(dto.getResultText());
        entity.setResultFiles(dto.getResultFiles());
        entity.setReviewRemark(dto.getReviewRemark());
        if (dto.getStatus() == 2) {
            entity.setPublishUserId(SecurityUtils.getUserId());
            entity.setPublishUserName(SecurityUtils.getLoginUser().getNickName());
            entity.setPublishTime(LocalDateTime.now());
        }
        updateById(entity);
    }
 
    private CustomerUser currentBinding() {
        CustomerUser binding = customerUserMapper.selectOne(new LambdaQueryWrapper<CustomerUser>()
                .eq(CustomerUser::getUserId, SecurityUtils.getUserId()).last("limit 1"));
        if (binding == null) {
            throw new ServiceException("当前账号尚未绑定客户,请联系管理员");
        }
        return binding;
    }
 
    private void ensureStaff() {
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId()) && SecurityUtils.hasRole("customer")) {
            throw new ServiceException("客户账号无权执行此操作");
        }
    }
 
    private Page<CustomerSubmission> fillCustomerName(Page<CustomerSubmission> result) {
        Set<Long> ids = result.getRecords().stream().map(CustomerSubmission::getCustomerId)
                .filter(Objects::nonNull).collect(Collectors.toSet());
        Map<Long, Customer> customerMap = ids.isEmpty() ? Collections.emptyMap() : customerMapper.selectBatchIds(ids)
                .stream().collect(Collectors.toMap(Customer::getId, Function.identity(), (a, b) -> a));
        result.getRecords().forEach(item -> {
            Customer customer = customerMap.get(item.getCustomerId());
            item.setCustomerName(customer == null ? "" : customer.getCustomerName());
        });
        return result;
    }
}