package cn.cslg.pas.service;
import cn.cslg.pas.common.core.base.Constants;
import cn.cslg.pas.common.model.dto.ClientDTO;
import cn.cslg.pas.common.model.vo.ProductVO;
import cn.cslg.pas.common.model.vo.ProjectVO;
import cn.cslg.pas.common.utils.SecurityUtils.LoginUtils;
import cn.cslg.pas.common.utils.StringUtils;
import cn.cslg.pas.domain.Project;
import cn.cslg.pas.domain.SystemDict;
import cn.cslg.pas.domain.Task;
import cn.cslg.pas.common.model.vo.TaskVO;
import cn.cslg.pas.mapper.TaskMapper;
import cn.cslg.pas.common.utils.DateUtils;
import cn.cslg.pas.common.utils.FileUtils;
import cn.cslg.pas.common.utils.Response;
import cn.cslg.pas.common.model.dto.UploadFileDTO;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.io.FileUtil;
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.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
/**
*
* 任务表 服务类
*
*
* @author 王岩
* @since 2022-02-27
*/
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class TaskService extends ServiceImpl {
private final FileUtils fileUtils;
private final LoginUtils loginUtils;
private final RequestService requestService;
private final OutInterfaceService outInterfaceService;
public String getFileUrl(Integer id) {
Task task = this.getById(id);
return fileUtils.getSystemPath() + task.getUrl();
}
public Integer add(UploadFileDTO fileDTO, Integer projectId, Integer productId, Integer total, Integer type, Integer fieldNum, String oldName) {
//创建任务表实体类,准备装载数据 ↓
Task task = new Task();
//任务开始时间
task.setStartTime(DateUtils.getDateTime());
//任务状态(0.队列中 1.进行中 2.成功 3.失败)
task.setStatus(0);
if (projectId != null) {
//专题库id
task.setProjectId(projectId);
}
if (productId != null) {
//产品id
task.setProductId(productId);
}
//文件名称
task.setFileName(fileDTO.getFileName());
//文件路径
task.setUrl(fileDTO.getPath());
//文件的专利总数量
task.setTotal(total);
//文件大小
task.setFileSize(fileDTO.getFileSize());
//任务类型 (1.上传 2导出)
task.setType(type);
//导入导出字段数量
task.setFieldNum(fieldNum);
//创建人id
task.setCreateBy(loginUtils.getId());
//文件原始名称
task.setOldName(oldName);
//数据入任务表
task.insert();
return task.getId();
}
public Task edit(Integer id, Integer status) {
Task task = this.getById(id);
task.setStatus(status);
task.setEndTime(DateUtils.getDateTime());
task.updateById();
return task;
}
public String delete(Integer id) {
Task temp = this.getById(id);
this.removeById(id);
if (StringUtils.isNotEmpty(temp.getUrl())) {
FileUtil.del(fileUtils.getSystemPath(temp.getUrl()));
}
return Response.success(true);
}
public IPage getPageList(TaskVO params) throws IOException {
String createName = params.getCreateName();
//如果此次查询是要根据创建人名称查询则👇
if (createName != null && !createName.equals("")) {
//远程调用权限系统的根据创建人名称模糊查询创建人ids的接口
String res = outInterfaceService.getPersonIdByNamePCS(createName);
List createPersonIds = JSONArray.parseArray(res, Integer.class);
//当未查询到时给创建人ids集合赋值唯一元素id=-1,即查空页返回
if (createPersonIds == null || createPersonIds.size() == 0) {
createPersonIds = new ArrayList<>(Collections.singletonList(-1));
}
params.setCreatePersonIds(createPersonIds);
}
IPage pageList = baseMapper.getPageList(new Page<>(params.getCurrent(), params.getSize()), params);
this.setDataList(pageList.getRecords());
return pageList;
}
public List getQueueList(Integer type, Integer projectId, Integer productId) {
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Task::getCreateBy, loginUtils.getId());
queryWrapper.eq(Task::getType, type);
queryWrapper.in(Task::getStatus, new ArrayList() {{
add(0);
add(1);
}});
if (projectId != null) {
queryWrapper.eq(Task::getProjectId, projectId);
}
if (productId != null) {
queryWrapper.eq(Task::getProductId, productId);
}
queryWrapper.orderByAsc(Task::getStartTime);
return this.list(queryWrapper);
}
public Integer add2(Integer projectId, Long fieldNum, Integer total) {
Task task = new Task();
task.setStatus(0);
task.setProjectId(projectId);
task.setType(2);
task.setTotal(total);
task.setFieldNum(fieldNum.intValue());
task.setCreateBy(loginUtils.getId());
task.setStartTime(DateUtils.getDateTime());
task.insert();
return task.getId();
}
public void updateStatus(Integer id, Integer status, Integer endTime) {
Task task = this.getById(id);
task.setStatus(status);
if (endTime != null) {
task.setEndTime(endTime);
}
task.updateById();
}
public void updateTime(Integer id, Integer status, Integer endTime, String url, String fileName) {
Task task = this.getById(id);
task.setStatus(status);
task.setEndTime(endTime);
task.setFileName(fileName);
task.setUrl(url);
task.updateById();
}
private void setDataList(List taskList) throws IOException {
//获得创建人的id集合
List createIds = taskList.stream().map(Task::getCreateBy).collect(Collectors.toList());
//获取专题库负责人对应信息
String jsonObject1 = requestService.getPersonnelFromPCS(createIds);
JSONArray jsonArray = JSON.parseArray(jsonObject1);
List personnelList = jsonArray.toJavaList(ProjectVO.Personnel.class);
for (Task task : taskList) {
for (ProjectVO.Personnel personnel : personnelList) {
//装载创建人名
if (task.getCreateBy() != null) {
if (task.getCreateBy().equals(personnel.getId())) {
task.setCreateName(personnel.getPersonnelName());
}
}
}
}
}
}