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;
|
}
|
}
|