|
|
@ -1,5 +1,7 @@ |
|
|
|
package com.epmet.service.impl; |
|
|
|
|
|
|
|
import cn.afterturn.easypoi.excel.ExcelExportUtil; |
|
|
|
import cn.afterturn.easypoi.excel.entity.ExportParams; |
|
|
|
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult; |
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
|
|
@ -12,6 +14,7 @@ import com.epmet.commons.tools.constant.StrConstant; |
|
|
|
import com.epmet.commons.tools.dto.result.CustomerStaffInfoCacheResult; |
|
|
|
import com.epmet.commons.tools.exception.EpmetErrorCode; |
|
|
|
import com.epmet.commons.tools.exception.EpmetException; |
|
|
|
import com.epmet.commons.tools.exception.ExceptionUtils; |
|
|
|
import com.epmet.commons.tools.exception.RenException; |
|
|
|
import com.epmet.commons.tools.page.PageData; |
|
|
|
import com.epmet.commons.tools.redis.common.CustomerOrgRedis; |
|
|
@ -21,37 +24,49 @@ import com.epmet.commons.tools.security.dto.TokenDto; |
|
|
|
import com.epmet.commons.tools.utils.ConvertUtils; |
|
|
|
import com.epmet.commons.tools.utils.DateUtils; |
|
|
|
import com.epmet.commons.tools.utils.ExcelPoiUtils; |
|
|
|
import com.epmet.commons.tools.utils.Result; |
|
|
|
import com.epmet.commons.tools.validator.PhoneValidatorUtils; |
|
|
|
import com.epmet.constant.IcCommunitySelfOrganizationConstant; |
|
|
|
import com.epmet.constant.UserDemandConstant; |
|
|
|
import com.epmet.constants.ImportTaskConstants; |
|
|
|
import com.epmet.dao.IcCommunitySelfOrganizationDao; |
|
|
|
import com.epmet.dto.IcCommunitySelfOrganizationDTO; |
|
|
|
import com.epmet.dto.form.AddCommunitySelfOrganizationFormDTO; |
|
|
|
import com.epmet.dto.form.CommunitySelfOrganizationListFormDTO; |
|
|
|
import com.epmet.dto.form.DelCommunitySelfOrganizationFormDTO; |
|
|
|
import com.epmet.dto.form.EditCommunitySelfOrganizationFormDTO; |
|
|
|
import com.epmet.dto.form.*; |
|
|
|
import com.epmet.dto.form.demand.ServiceQueryFormDTO; |
|
|
|
import com.epmet.dto.result.CommunitySelfOrganizationListDTO; |
|
|
|
import com.epmet.dto.result.CommunitySelfOrganizationListResultDTO; |
|
|
|
import com.epmet.dto.result.UploadImgResultDTO; |
|
|
|
import com.epmet.dto.result.demand.OptionDTO; |
|
|
|
import com.epmet.entity.IcCommunitySelfOrganizationEntity; |
|
|
|
import com.epmet.entity.IcCommunitySelfOrganizationPersonnelEntity; |
|
|
|
import com.epmet.excel.CommunitySelfOrgImportExcel; |
|
|
|
import com.epmet.excel.ImportCommunitySelfOrganization; |
|
|
|
import com.epmet.feign.EpmetCommonServiceOpenFeignClient; |
|
|
|
import com.epmet.feign.OssFeignClient; |
|
|
|
import com.epmet.service.IcCommunitySelfOrganizationPersonnelService; |
|
|
|
import com.epmet.service.IcCommunitySelfOrganizationService; |
|
|
|
import com.epmet.service.IcUserDemandRecService; |
|
|
|
import com.github.pagehelper.PageHelper; |
|
|
|
import com.github.pagehelper.PageInfo; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.collections4.CollectionUtils; |
|
|
|
import org.apache.commons.fileupload.FileItem; |
|
|
|
import org.apache.commons.fileupload.FileItemFactory; |
|
|
|
import org.apache.commons.fileupload.disk.DiskFileItemFactory; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.apache.http.entity.ContentType; |
|
|
|
import org.apache.poi.ss.usermodel.Workbook; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
|
import org.springframework.web.multipart.commons.CommonsMultipartFile; |
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.OutputStream; |
|
|
|
import java.util.*; |
|
|
|
import java.util.concurrent.atomic.AtomicBoolean; |
|
|
|
import java.util.concurrent.atomic.AtomicReference; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
@ -64,12 +79,17 @@ import static com.epmet.commons.tools.utils.DateUtils.DATE_PATTERN; |
|
|
|
* @since v1.0.0 2021-11-18 |
|
|
|
*/ |
|
|
|
@Service |
|
|
|
@Slf4j |
|
|
|
public class IcCommunitySelfOrganizationServiceImpl extends BaseServiceImpl<IcCommunitySelfOrganizationDao, IcCommunitySelfOrganizationEntity> implements IcCommunitySelfOrganizationService { |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private IcCommunitySelfOrganizationPersonnelService personnelService; |
|
|
|
@Autowired |
|
|
|
private IcUserDemandRecService icUserDemandRecService; |
|
|
|
@Autowired |
|
|
|
private OssFeignClient ossFeignClient; |
|
|
|
@Autowired |
|
|
|
private EpmetCommonServiceOpenFeignClient commonServiceOpenFeignClient; |
|
|
|
|
|
|
|
@Override |
|
|
|
public PageData<IcCommunitySelfOrganizationDTO> page(Map<String, Object> params) { |
|
|
@ -362,9 +382,9 @@ public class IcCommunitySelfOrganizationServiceImpl extends BaseServiceImpl<IcCo |
|
|
|
* @author zxc |
|
|
|
* @date 2021/11/25 9:03 上午 |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
//@Override
|
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
public void importCommunitySelfOrganization(TokenDto tokenDto, HttpServletResponse response, MultipartFile file) throws IOException { |
|
|
|
public void importCommunitySelfOrganization_old(TokenDto tokenDto, HttpServletResponse response, MultipartFile file) throws IOException { |
|
|
|
ExcelImportResult<ImportCommunitySelfOrganization> testExcelImportResult = ExcelPoiUtils.importExcelMore(file, 0, 2, ImportCommunitySelfOrganization.class); |
|
|
|
List<ImportCommunitySelfOrganization> list = testExcelImportResult.getList(); |
|
|
|
|
|
|
@ -446,6 +466,172 @@ public class IcCommunitySelfOrganizationServiceImpl extends BaseServiceImpl<IcCo |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
/** |
|
|
|
* @Description 社会自组织导入-重新梳理导入逻辑,旧逻辑在上边(importCommunitySelfOrganization_old)的方法 |
|
|
|
* @author sun |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
public void importCommunitySelfOrganization(TokenDto tokenDto, HttpServletResponse response, MultipartFile file, String taskId) throws IOException { |
|
|
|
List<CommunitySelfOrgImportExcel> fileList = new ArrayList<>(); |
|
|
|
CommunitySelfOrgImportExcel excel = null; |
|
|
|
//1.读取Excel数据
|
|
|
|
ExcelImportResult<ImportCommunitySelfOrganization> testExcelImportResult = ExcelPoiUtils.importExcelMore(file, 0, 2, ImportCommunitySelfOrganization.class); |
|
|
|
//2.存在错误行数据时存入错误数据集合中
|
|
|
|
for (ImportCommunitySelfOrganization entity : testExcelImportResult.getFailList()) { |
|
|
|
//打印失败的行 和失败的信息
|
|
|
|
log.warn("第{}行,{}", entity.getRowNum(), entity.getErrorMsg()); |
|
|
|
excel = new CommunitySelfOrgImportExcel(); |
|
|
|
excel.setAgencyName(entity.getOrganizationName()); |
|
|
|
excel.setSocietyName(entity.getOrganizationName()); |
|
|
|
excel.setErrorInfo(entity.getErrorMsg()); |
|
|
|
fileList.add(excel); |
|
|
|
} |
|
|
|
//正确行数据集合
|
|
|
|
List<ImportCommunitySelfOrganization> list = testExcelImportResult.getList(); |
|
|
|
if (CollectionUtils.isNotEmpty(list)){ |
|
|
|
//3.分别判断导入数据是否是当前组织下数据、社会自组织名称是否已存在、手机号是否规范
|
|
|
|
//3-1.查询当前组织下已从存在的社会自组织名称
|
|
|
|
CustomerStaffInfoCacheResult staffInfoCache = CustomerStaffRedis.getStaffInfo(tokenDto.getCustomerId(), tokenDto.getUserId()); |
|
|
|
//查询当前组织下的社会自组织数据
|
|
|
|
List<IcCommunitySelfOrganizationDTO> communitySeltOrgList = baseDao.selectOrgByCustomerId(tokenDto.getCustomerId()); |
|
|
|
//组织信息
|
|
|
|
AgencyInfoCache agencyInfo = CustomerOrgRedis.getAgencyInfo(staffInfoCache.getAgencyId()); |
|
|
|
//3-2.检验数据,把不允许导入的数据剔除
|
|
|
|
Iterator<ImportCommunitySelfOrganization> iterator = list.iterator(); |
|
|
|
while (iterator.hasNext()) { |
|
|
|
AtomicBoolean bl = new AtomicBoolean(false); |
|
|
|
StringBuffer errMsg = new StringBuffer(""); |
|
|
|
ImportCommunitySelfOrganization obj = iterator.next(); |
|
|
|
//社会自组织名称重复
|
|
|
|
communitySeltOrgList.forEach(org -> { |
|
|
|
if (obj.getOrganizationName().equals(org.getOrganizationName())) { |
|
|
|
log.warn(String.format("当前客户下的社会自组织名称已存在,社会自组织名称->%s,行号->%s", obj.getOrganizationName(), obj.getRowNum())); |
|
|
|
errMsg.append("客户下社会自组织名称已存在;"); |
|
|
|
bl.set(true); |
|
|
|
} |
|
|
|
}); |
|
|
|
//手机号不合规
|
|
|
|
boolean m = PhoneValidatorUtils.isMobile(obj.getPrincipalPhone()); |
|
|
|
boolean t = PhoneValidatorUtils.isTel(obj.getPrincipalPhone()); |
|
|
|
if (!m && !t){ |
|
|
|
errMsg.append("手机号码不合法;"); |
|
|
|
bl.set(true); |
|
|
|
} |
|
|
|
if(bl.get()){ |
|
|
|
excel = new CommunitySelfOrgImportExcel(); |
|
|
|
excel.setAgencyName(obj.getOrganizationName()); |
|
|
|
excel.setSocietyName(obj.getOrganizationName()); |
|
|
|
excel.setErrorInfo(errMsg.toString()); |
|
|
|
fileList.add(excel); |
|
|
|
iterator.remove(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
list.forEach(l -> { |
|
|
|
IcCommunitySelfOrganizationEntity e = ConvertUtils.sourceToTarget(l, IcCommunitySelfOrganizationEntity.class); |
|
|
|
e.setCustomerId(tokenDto.getCustomerId()); |
|
|
|
e.setOrgId(agencyInfo.getId()); |
|
|
|
e.setOrgType(IcCommunitySelfOrganizationConstant.ORG_TYPE_AGENCY); |
|
|
|
e.setPid(agencyInfo.getPid()); |
|
|
|
e.setPids(agencyInfo.getPids()); |
|
|
|
e.setOrganizationCreatedTime(l.getOrganizationCreatedTime()); |
|
|
|
baseDao.insert(e); |
|
|
|
if (CollectionUtils.isNotEmpty(l.getPersons())){ |
|
|
|
l.getPersons().forEach(p -> { |
|
|
|
boolean m = PhoneValidatorUtils.isMobile(p.getPersonPhone()); |
|
|
|
boolean t = PhoneValidatorUtils.isTel(p.getPersonPhone()); |
|
|
|
if (!m && !t){ |
|
|
|
throw new EpmetException(EpmetErrorCode.CHECK_PHONE_ERROR.getCode()); |
|
|
|
} |
|
|
|
}); |
|
|
|
List<IcCommunitySelfOrganizationPersonnelEntity> persons = ConvertUtils.sourceToTarget(l.getPersons(), IcCommunitySelfOrganizationPersonnelEntity.class); |
|
|
|
Map<String, List<IcCommunitySelfOrganizationPersonnelEntity>> groupByPhone = persons.stream().collect(Collectors.groupingBy(IcCommunitySelfOrganizationPersonnelEntity::getPersonPhone)); |
|
|
|
List<String> phones = new ArrayList<>(); |
|
|
|
groupByPhone.forEach((k,v) -> { |
|
|
|
if (v.size() > NumConstant.ONE){ |
|
|
|
phones.add(k); |
|
|
|
} |
|
|
|
}); |
|
|
|
if (CollectionUtils.isNotEmpty(phones)){ |
|
|
|
StringBuffer sb = new StringBuffer(); |
|
|
|
phones.forEach(p -> { |
|
|
|
sb.append(p).append(","); |
|
|
|
}); |
|
|
|
String copywriter = sb.toString().substring(NumConstant.ZERO, sb.length() - NumConstant.ONE); |
|
|
|
EpmetErrorCode.EXISTS_SAME_PHONE_ERROR.setMsg(String.format(EpmetErrorCode.EXISTS_SAME_PHONE_ERROR.getMsg(),copywriter)); |
|
|
|
throw new RenException(EpmetErrorCode.EXISTS_SAME_PHONE_ERROR.getCode()); |
|
|
|
} |
|
|
|
persons.forEach(p -> { |
|
|
|
p.setCustomerId(tokenDto.getCustomerId()); |
|
|
|
p.setOrgId(e.getId()); |
|
|
|
}); |
|
|
|
personnelService.insertBatch(persons); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
//4.错误数据生成文件,修改导入任务状态
|
|
|
|
erroeImport(fileList, taskId, tokenDto.getUserId()); |
|
|
|
} |
|
|
|
/** |
|
|
|
* @Author sun |
|
|
|
* @Description 社会自组织导入导入,错误数据生成导入失败文件存到阿里云,修改导入任务为已结束 |
|
|
|
**/ |
|
|
|
private void erroeImport(List<CommunitySelfOrgImportExcel> fileList, String importTaskId, String staffId) throws IOException { |
|
|
|
String url = ""; |
|
|
|
//1.有错误数据则生成错误数据存放文件传到阿里云服务
|
|
|
|
if (!org.springframework.util.CollectionUtils.isEmpty(fileList)) { |
|
|
|
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("导入失败的数据列表", "导入失败的数据列表"), |
|
|
|
CommunitySelfOrgImportExcel.class, fileList); |
|
|
|
|
|
|
|
// 文件名
|
|
|
|
String resultDescFileName = UUID.randomUUID().toString().concat(".xlsx"); |
|
|
|
FileItemFactory factory = new DiskFileItemFactory(16, null); |
|
|
|
FileItem fileItem = factory.createItem("file", ContentType.APPLICATION_OCTET_STREAM.toString(), true, resultDescFileName); |
|
|
|
OutputStream os = fileItem.getOutputStream(); |
|
|
|
Result<UploadImgResultDTO> uploadResult = null; |
|
|
|
try { |
|
|
|
workbook.write(os); |
|
|
|
uploadResult = ossFeignClient.uploadImportTaskDescFile(new CommonsMultipartFile(fileItem)); |
|
|
|
} catch (Exception e) { |
|
|
|
String errormsg = ExceptionUtils.getErrorStackTrace(e); |
|
|
|
log.error("【社会自组织信息导入】上传错误描述文件:{}", errormsg); |
|
|
|
} finally { |
|
|
|
try { |
|
|
|
os.close(); |
|
|
|
} catch (IOException e) { |
|
|
|
String errormsg = ExceptionUtils.getErrorStackTrace(e); |
|
|
|
log.error("【社会自组织信息导入】上传错误描述文件关闭输出流:{}", errormsg); |
|
|
|
} |
|
|
|
try { |
|
|
|
fileItem.delete(); |
|
|
|
} catch (Exception e) { |
|
|
|
String errormsg = ExceptionUtils.getErrorStackTrace(e); |
|
|
|
log.error("【社会自组织信息导入】上传错误描述文件删除临时文件:{}", errormsg); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (uploadResult == null || !uploadResult.success()) { |
|
|
|
log.error("【社会自组织信息导入】调用OSS上传结果描述文件失败"); |
|
|
|
} |
|
|
|
url = uploadResult.getData().getUrl(); |
|
|
|
} |
|
|
|
//2.更新导入任务数据
|
|
|
|
ImportTaskCommonFormDTO importTaskForm = new ImportTaskCommonFormDTO(); |
|
|
|
importTaskForm.setOperatorId(staffId); |
|
|
|
importTaskForm.setBizType(ImportTaskConstants.BIZ_TYPE_PARTY_MEMBER); |
|
|
|
importTaskForm.setTaskId(importTaskId); |
|
|
|
importTaskForm.setResultDescFilePath(url); |
|
|
|
importTaskForm.setProcessStatus(ImportTaskConstants.PROCESS_STATUS_FINISHED_SUCCESS); |
|
|
|
if (StringUtils.isNotBlank(url)) { |
|
|
|
importTaskForm.setProcessStatus(ImportTaskConstants.PROCESS_STATUS_FINISHED_FAIL); |
|
|
|
importTaskForm.setResultDesc("社会自组织导入存在错误数据"); |
|
|
|
} |
|
|
|
Result result = commonServiceOpenFeignClient.finishImportTask(importTaskForm); |
|
|
|
if (!result.success()) { |
|
|
|
throw new RenException(result.getInternalMsg()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public List<IcCommunitySelfOrganizationEntity> queryListById(List<String> communityOrgIds) { |
|
|
|