package cn.cslg.pas.service;
import cn.cslg.pas.common.core.base.Constants;
import cn.cslg.pas.common.core.exception.CustomException;
import cn.cslg.pas.common.model.PersonnelVO;
import cn.cslg.pas.common.model.dto.*;
import cn.cslg.pas.common.model.vo.*;
import cn.cslg.pas.common.utils.*;
import cn.cslg.pas.common.utils.SecurityUtils.LoginUtils;
import cn.cslg.pas.domain.*;
import cn.cslg.pas.exception.XiaoShiException;
import cn.cslg.pas.mapper.ProductMapper;
import cn.cslg.pas.mapper.ProjectMapper;
import cn.cslg.pas.mapper.asso.AssoStructurePatentMapper;
import cn.cslg.pas.service.asso.TaskConditionService;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;
/**
*
* 专题库表 服务实现类
*
*
* @author 王岩
* @since 2022-01-24
*/
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class ProjectService extends ServiceImpl {
private final ProjectUserService projectUserService;
private final PatentService patentService;
private final PatentApplicantService patentApplicantService;
private final PatentAgencyService patentAgencyService;
private final PatentAgentService patentAgentService;
private final PatentAgentLinkService patentAgentLinkService;
private final PatentLicensorService patentLicensorService;
private final PatentPledgeService patentPledgeService;
private final PatentAffairService patentAffairService;
private final PatentApplicantLinkService patentApplicantLinkService;
private final PatentApplicantMergeLinkService patentApplicantMergeLinkService;
private final PatentInventorService patentInventorService;
private final PatentInventorLinkService patentInventorLinkService;
private final PatentSimpleFamilyService patentSimpleFamilyService;
private final PatentSimpleFamilyLinkService patentSimpleFamilyLinkService;
private final PatentClassNumberLinkService patentClassNumberLinkService;
private final ProjectPatentLinkService projectPatentLinkService;
private final TaskService taskService;
private final TaskConditionService taskConditionService;
private final PatentImageService patentImageService;
private final ProjectFieldPatentLinkService projectFieldPatentLinkService;
private final ProjectFolderService projectFolderService;
private final ProjectFieldService projectFieldService;
private final ProjectFieldOptionService projectFieldOptionService;
private final ProjectFieldTreeService projectFieldTreeService;
private final ProjectFieldTextService projectFieldTextService;
private final ProjectFileService projectFileService;
private final SystemDictService systemDictService;
private final PatentInstructionService patentInstructionService;
private final PatentInstructionTextService patentInstructionTextService;
private final PatentRightService patentRightService;
private final FileUtils fileUtils;
private final PatentLabelService patentLabelService;
private final CacheUtils cacheUtils;
private final ProjectExportService projectExportService;
private final PatentInventorMergeService patentInventorMergeService;
private final UserService userService;
private final LoginUtils loginUtils;
private final RequestService requestService;
private final AssoStructurePatentMapper assoStructurePatentMapper;
private final ProductMapper productMapper;
private final ProjectMapper projectMapper;
private final IEventService eventService;
private final IAssoEventProjectService assoEventProjectService;
private final CopyService copyService;
public Project getProjectByName(String name) {
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
queryWrapper.eq(Project::getName, name);
queryWrapper.eq(Project::getTenantId, personnelVO.getTenantId());
queryWrapper.last("limit 1");
return this.getOne(queryWrapper);
}
public Project getProjectByName(String name, Integer loginId) {
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
PersonnelVO personnelVO = cacheUtils.getLoginUser(loginId);
queryWrapper.eq(Project::getName, name);
queryWrapper.eq(Project::getTenantId, personnelVO.getTenantId());
queryWrapper.last("limit 1");
return this.getOne(queryWrapper);
}
public IPage getPageList(ProjectVO params) throws IOException {
List projectIds = new ArrayList() {{
add(-1);
}};
//TODO 代码控制不同类型的角色查询权限
PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
if (personnelVO.getRoleType() == null || personnelVO.getRoleType() != 1) {
params.setPersonnelId(loginUtils.getId());
if (personnelVO.getRoleType() != null && personnelVO.getRoleType() == 2) {
params.setTenantId(personnelVO.getTenantId());
}
}
//分页查询专题库列表
IPage pageList = baseMapper.getPageList(new Page<>(params.getCurrent(), params.getSize()), params);
List dataList = pageList.getRecords();
this.setDataList(dataList);
pageList.setRecords(dataList);
return pageList;
}
public List getProjectByIds(List ids) throws IOException {
if (ids == null || ids.size() == 0) {
return new ArrayList<>();
}
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(Project::getId, ids);
//根据ids查询专题库列表
List dataList = this.list(queryWrapper);
//↓根据专题库id查询专题库时,查询到的就是一条,就将当前专题库的产品名字赋值(他妈的两个控制层方法调当前业务层,坑爹)
if (dataList.size() == 1) {
for (Project project : dataList) {
Integer productId = project.getProductId();
if (productId != null) {
ProductVO queryResult = productMapper.getStandardById(productId);
String productName = queryResult.getProductName();
project.setProductName(productName);
}
}
}
try {
//获取所属部门对应信息
String jsonObject = requestService.getDepartmentFromPCS(dataList);
JSONArray jsonArray1 = JSON.parseArray(jsonObject);
List departmentList = jsonArray1.toJavaList(ProjectVO.Department.class);
for (Project project : dataList) {
for (ProjectVO.Department department : departmentList) {
if (project.getDepartmentId() != null) {
if (project.getDepartmentId().equals(department.getId())) {
project.setDepartmentName(department.getDepartmentName());
}
}
}
}
List personnelIds = dataList.stream().map(Project::getPersonnelId).collect(Collectors.toList());
//获取专题库负责人对应信息
String jsonObject1 = requestService.getPersonnelFromPCS(personnelIds);
JSONArray jsonArray = JSON.parseArray(jsonObject1);
List personnelList = jsonArray.toJavaList(ProjectVO.Personnel.class);
for (Project project : dataList) {
for (ProjectVO.Personnel personnel : personnelList) {
if (project.getPersonnelId() != null) {
if (project.getPersonnelId().equals(personnel.getId())) {
project.setPersonnelName(personnel.getPersonnelName());
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
this.setDataList(dataList);
return dataList;
}
public List getProjectByCreateId(Integer createBy) {
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Project::getCreateBy, createBy);
return this.list(queryWrapper);
}
public Project getProjectById(Integer id) {
Project project = this.getById(id);
try {
String res = requestService.getClientByIds(Arrays.asList(project.getClientId()));
JSONArray jsonArray2 = JSON.parseArray(res);
List clientList = jsonArray2.toJavaList(ClientDTO.class);
if (clientList != null && clientList.size() != 0) {
project.setClientName(clientList.get(0).getName());
}
} catch (Exception e) {
}
return project;
}
private void setDataList(List dataList) throws IOException {
//获得委托方的id集合
List clientIds = dataList.stream().map(Project::getClientId).collect(Collectors.toList());
//获得负责人的id集合
List personIds = dataList.stream().map(Project::getPersonnelId).collect(Collectors.toList());
//获得创建人的id集合
List createIds = dataList.stream().map(Project::getCreateBy).collect(Collectors.toList());
personIds.addAll(createIds);
//获取所属部门对应信息
String jsonObject = requestService.getDepartmentFromPCS(dataList);
JSONArray jsonArray1 = JSON.parseArray(jsonObject);
List departmentList = jsonArray1.toJavaList(ProjectVO.Department.class);
//获取专题库负责人对应信息
String jsonObject1 = requestService.getPersonnelFromPCS(personIds);
JSONArray jsonArray = JSON.parseArray(jsonObject1);
List personnelList = jsonArray.toJavaList(ProjectVO.Personnel.class);
//获取专题库委托方对应信息
List clientList = new ArrayList<>();
if (clientIds.size() > 0) {
String jsonObject2 = requestService.getClientByIds(clientIds);
JSONArray jsonArray2 = JSON.parseArray(jsonObject2);
clientList = jsonArray2.toJavaList(ClientDTO.class);
}
for (Project project : dataList) {
//装载负责人名
for (ProjectVO.Personnel personnel : personnelList) {
if (project.getPersonnelId() != null) {
if (project.getPersonnelId().equals(personnel.getId())) {
project.setPersonnelName(personnel.getPersonnelName());
}
}
//装载创建人名
if (project.getCreateBy() != null) {
if (project.getCreateBy().equals(personnel.getId())) {
project.setCreateName(personnel.getPersonnelName());
}
}
}
//装载委托方名
for (ClientDTO clientDTO : clientList) {
if (project.getClientId() != null) {
if (project.getClientId().equals(clientDTO.getId())) {
project.setClientName(clientDTO.getName());
}
}
}
Integer productId = project.getProductId();
//装载产品id
if (productId != null) {
ProductVO queryResult = productMapper.getStandardById(productId);
String productName = queryResult.getProductName();
project.setProductName(productName);
}
//装载部门
for (ProjectVO.Department department : departmentList) {
if (project.getDepartmentId() != null) {
if (project.getDepartmentId().equals(department.getId())) {
project.setDepartmentName(department.getDepartmentName());
}
}
}
}
List systemDictList = systemDictService.getSystemDictListByType(Arrays.asList(Constants.ENTERPRISE_APPLICATION_SCENARIO, Constants.INVESTIGATION_TYPE));
dataList.forEach(item -> {
if (StringUtils.isNotEmpty(item.getScenario())) {
item.setScenarioList(StringUtils.changeStringToInteger(item.getScenario(), ","));
item.setScenarioName(systemDictList.stream().filter(systemDict -> systemDict.getType().equals(Constants.ENTERPRISE_APPLICATION_SCENARIO) && item.getScenarioList().contains(Integer.parseInt(systemDict.getValue()))).map(SystemDict::getLabel).collect(Collectors.toList()));
item.setScenario(null);
}
if (StringUtils.isNotEmpty(item.getType())) {
item.setTypeList(StringUtils.changeStringToInteger(item.getType(), ","));
item.setTypeName(systemDictList.stream().filter(systemDict -> systemDict.getType().equals(Constants.INVESTIGATION_TYPE) && item.getTypeList().contains(Integer.parseInt(systemDict.getValue()))).map(SystemDict::getLabel).collect(Collectors.toList()));
item.setType(null);
} else {
item.setTypeList(new ArrayList());
}
});
}
public List getAllProjectByMySelf() throws IOException {
ProjectVO params = new ProjectVO();
PersonnelVO personnelVO = cacheUtils.getLoginUser(loginUtils.getId());
if (personnelVO.getRoleType() == null || personnelVO.getRoleType() != 1) {
params.setPersonnelId(loginUtils.getId());
if (personnelVO.getRoleType() != null && personnelVO.getRoleType() == 2) {
params.setTenantId(personnelVO.getTenantId());
}
}
List projectList = baseMapper.getListForCount(params);
this.setDataList(projectList);
return projectList;
}
public Map