李林
2023-10-07 658d4927d468c47208fd012d9128b09249c07eff
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
254
/*
 *    Copyright (c) 2018-2025, ztt 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 pig4cloud.com 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: ztt
 */
package com.chinaztt.mes.basic.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.amazonaws.services.s3.model.Bucket;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinaztt.mes.basic.entity.Company;
import com.chinaztt.mes.basic.entity.CompanyAddress;
import com.chinaztt.mes.basic.entity.CompanyContact;
import com.chinaztt.mes.basic.mapper.CompanyAddressMapper;
import com.chinaztt.mes.basic.mapper.CompanyContactMapper;
import com.chinaztt.mes.basic.mapper.CompanyMapper;
import com.chinaztt.mes.basic.service.CompanyService;
import com.chinaztt.mes.common.numgen.NumberGenerator;
import com.chinaztt.ztt.common.oss.OssProperties;
import com.chinaztt.ztt.common.oss.service.OssTemplate;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
 
/**
 * 公司
 *
 * @author liuth
 * @date 2020-09-18 15:02:11
 */
@Service
@AllArgsConstructor
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService {
 
    private final OssProperties ossProperties;
    private final OssTemplate minioTemplate;
    private CompanyAddressMapper companyAddressMapper;
    private CompanyContactMapper companyContactMapper;
    private CompanyMapper companyMapper;
    private NumberGenerator<Company> numberGenerator;
 
    /**
     * 关联删除
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean fullDelete(Long id) {
        companyAddressMapper.delete(Wrappers.<CompanyAddress>lambdaQuery().eq(CompanyAddress::getCompanyId, id));
        companyContactMapper.delete(Wrappers.<CompanyContact>lambdaQuery().eq(CompanyContact::getCompanyId, id));
        baseMapper.deleteById(id);
        return true;
    }
 
    @Override
    public Company getFullById(Long id) {
        return companyMapper.getFullById(id);
    }
 
 
    @Override
    public Company getByStaffId(Long staffId) {
        return companyMapper.getByStaffId(staffId);
    }
 
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean fullSave(Company company) {
        companyMapper.insert(company);
        if (CollectionUtil.isNotEmpty(company.getAddresses())) {
            company.getAddresses().forEach(a -> {
                a.setCompanyId(company.getId());
                companyAddressMapper.insert(a);
            });
        }
        if (CollectionUtil.isNotEmpty(company.getContacts())) {
            company.getContacts().forEach(c -> {
                c.setCompanyId(company.getId());
                companyContactMapper.insert(c);
            });
        }
        return true;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean fullUpdate(Company company) {
        Set<Long> addressIds = new HashSet();
        Set<Long> contactIds = new HashSet();
        companyMapper.updateById(company);
        if (CollectionUtil.isNotEmpty(company.getAddresses())) {
            company.getAddresses().forEach(a -> {
                if (a.getId() == null || a.getId() == 0) {
                    a.setCompanyId(company.getId());
                    companyAddressMapper.insert(a);
                } else {
                    companyAddressMapper.updateById(a);
                }
                addressIds.add(a.getId());
            });
        }
        if (CollectionUtil.isNotEmpty(company.getContacts())) {
            company.getContacts().forEach(c -> {
                if (c.getId() == null || c.getId() == 0) {
                    c.setCompanyId(company.getId());
                    companyContactMapper.insert(c);
                } else {
                    companyContactMapper.updateById(c);
                }
                contactIds.add(c.getId());
            });
        }
        LambdaQueryWrapper<CompanyAddress> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CompanyAddress::getCompanyId, company.getId());
        if (CollectionUtil.isNotEmpty(addressIds)) {
            wrapper.notIn(CompanyAddress::getId, addressIds);
        }
        LambdaQueryWrapper<CompanyContact> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(CompanyContact::getCompanyId, company.getId());
        if (CollectionUtil.isNotEmpty(contactIds)) {
            wrapper2.notIn(CompanyContact::getId, contactIds);
        }
        companyAddressMapper.delete(wrapper);
        companyContactMapper.delete(wrapper2);
        return true;
    }
 
    @Override
    public int deleteContactById(Long id) {
        return companyContactMapper.deleteById(id);
    }
 
    @Override
    public int deleteAddressById(Long id) {
        return companyAddressMapper.deleteById(id);
    }
 
    @Override
    public void clone(Company[] companies) {
        for (Company data : companies) {
            Company company = this.getById(data.getId());
            List<CompanyAddress> addresses = companyAddressMapper.selectList(Wrappers.<CompanyAddress>lambdaQuery().eq(CompanyAddress::getCompanyId, data.getId()));
            List<CompanyContact> contacts = companyContactMapper.selectList(Wrappers.<CompanyContact>lambdaQuery().eq(CompanyContact::getCompanyId, data.getId()));
 
            Company copy = (Company) data.clone();
            copy.setCompanyNo(numberGenerator.getCopyValueOfUniqueField(company.getCompanyNo(), Company::getCompanyNo));
            copy.setAddresses(addresses);
            copy.setContacts(contacts);
 
            this.fullSave(copy);
        }
    }
 
    /**
     * 上传文件
     *
     * @param file 附件
     * @param id   id
     * @return
     */
    @Override
    public boolean uploadFile(MultipartFile file, Long id) {
        //图片存在mes的桶里面如果没有桶 创建一个
        Optional<Bucket> optionalBucket = minioTemplate.getBucket("mes");
        if (optionalBucket.isPresent()) {
            minioTemplate.createBucket("mes");
        }
        Company company = baseMapper.selectById(id);
        String fileName = IdUtil.simpleUUID() + StrUtil.DOT + FileUtil.extName(file.getOriginalFilename());
        try {
            //如果原先存在图片就将原先的图片删掉
            if (StringUtils.isNotBlank(company.getIconName())) {
                minioTemplate.removeObject(ossProperties.getBucketName() + "/company", company.getIconName());
            }
            //上传新的图片
            minioTemplate.putObject(ossProperties.getBucketName() + "/company", fileName, file.getInputStream());
        } catch (Exception e) {
            throw new RuntimeException("上传失败:" + e.getMessage());
        }
        company.setIconName(fileName);
        baseMapper.updateById(company);
        return true;
    }
 
    /**
     * 附件删除
     *
     * @param id id
     * @return
     */
    @Override
    public boolean removeFile(Long id) {
        //图片存在mes的桶里面如果没有桶 创建一个
        Company company = baseMapper.selectById(id);
        if (StringUtils.isBlank(company.getIconName())) {
            throw new RuntimeException("不存在图片");
        }
        try {
            //如果原先存在图片就将原先的图片删掉
            minioTemplate.removeObject(ossProperties.getBucketName() + "/company", company.getIconName());
        } catch (Exception e) {
            throw new RuntimeException("文件移除失败:" + e.getMessage());
        }
        baseMapper.clearIconName(id);
        return true;
    }
 
    /**
     * 读取文件
     *
     * @param id
     * @param response
     */
    @Override
    public void getFile(Long id, HttpServletResponse response) {
        Company company = baseMapper.selectById(id);
        try (InputStream inputStream = minioTemplate.getObject(ossProperties.getBucketName() + "/company", company.getIconName())) {
            response.setContentType("application/octet-stream; charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + company.getIconName());
            IoUtil.copy(inputStream, response.getOutputStream());
        } catch (Exception e) {
            throw new RuntimeException("文件读取失败" + e.getMessage());
        }
    }
}