/*
|
* 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());
|
}
|
}
|
}
|