gongchunyi
2026-04-28 cb3bc6d91595409e138daf6fe08a661bba9d25ee
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
package com.ruoyi.customervisits.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.enums.FileNameType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.customervisits.dto.CustomerVisitsDto;
import com.ruoyi.customervisits.mapper.CustomerVisitsMapper;
import com.ruoyi.customervisits.pojo.CustomerVisits;
import com.ruoyi.customervisits.service.CustomerVisitsService;
import com.ruoyi.other.service.impl.TempFileServiceImpl;
import com.ruoyi.sales.mapper.CommonFileMapper;
import com.ruoyi.sales.pojo.CommonFile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
 
/**
 * @author :yys
 * @date : 2025/8/29 10:27
 */
@Service
@Slf4j
public class CustomerVisitsServiceImpl extends ServiceImpl<CustomerVisitsMapper, CustomerVisits> implements CustomerVisitsService {
 
    @Autowired
    private CustomerVisitsMapper customerVisitsMapper;
 
    @Autowired
    private CommonFileMapper commonFileMapper;
 
    @Autowired
    private TempFileServiceImpl tempFileService;
 
    @Override
    public IPage<CustomerVisitsDto> listPage(Page page, CustomerVisits customerVisits) {
        IPage<CustomerVisitsDto> selectPage = customerVisitsMapper.listPage(page, customerVisits);
        for (CustomerVisitsDto record : selectPage.getRecords()) {
            List<CommonFile> allFiles = commonFileMapper.selectList(new LambdaQueryWrapper<CommonFile>()
                    .eq(CommonFile::getCommonId, record.getId())
                    .eq(CommonFile::getType, FileNameType.CUSTOMER_VISITS.getValue()));
            record.setCommonFileList(allFiles);
        }
        return selectPage;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCustomerVisit(CustomerVisits customerVisits) {
        if (customerVisits == null || customerVisits.getId() == null) {
            return false;
        }
 
        if (StringUtils.isEmpty(customerVisits.getCustomerName())
                || StringUtils.isEmpty(customerVisits.getPurposeVisit())
                || StringUtils.isEmpty(customerVisits.getVisitAddress())
                || StringUtils.isEmpty(customerVisits.getPurposeDate())) {
            return false;
        }
 
        return updateById(customerVisits);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCustomerVisit(CustomerVisitsDto customerVisitsDto) throws Exception {
        CustomerVisits customerVisits = new CustomerVisits();
        BeanUtils.copyProperties(customerVisitsDto, customerVisits);
        boolean updateResult = updateCustomerVisit(customerVisits);
        if (!updateResult) {
            return false;
        }
 
        Long businessId = customerVisits.getId().longValue();
        List<CommonFile> existingFiles = commonFileMapper.selectList(new LambdaQueryWrapper<CommonFile>()
                .eq(CommonFile::getCommonId, businessId)
                .eq(CommonFile::getType, FileNameType.CUSTOMER_VISITS.getValue()));
        Set<Long> retainedFileIds = customerVisitsDto.getCommonFileList() == null
                ? Collections.emptySet()
                : customerVisitsDto.getCommonFileList().stream()
                .map(CommonFile::getId)
                .filter(java.util.Objects::nonNull)
                .collect(Collectors.toSet());
        for (CommonFile commonFile : existingFiles) {
            if (commonFile.getId() == null || retainedFileIds.contains(commonFile.getId())) {
                continue;
            }
            if (commonFile.getUrl() != null && !commonFile.getUrl().isEmpty()) {
                Files.deleteIfExists(Paths.get(commonFile.getUrl()));
            }
            commonFileMapper.deleteById(commonFile.getId());
        }
        tempFileService.migrateTempFilesToFormal(businessId, customerVisitsDto.getTempFileIds(), FileNameType.CUSTOMER_VISITS.getValue());
        return true;
    }
}