package com.stylefeng.guns.modular.system.service.impl;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONArray;
|
import com.baomidou.mybatisplus.mapper.EntityWrapper;
|
import com.baomidou.mybatisplus.mapper.Wrapper;
|
import com.baomidou.mybatisplus.plugins.Page;
|
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
|
import com.stylefeng.guns.core.util.ToolUtil;
|
import com.stylefeng.guns.modular.system.dao.TaskDetailMapper;
|
import com.stylefeng.guns.modular.system.model.PatrolTask;
|
import com.stylefeng.guns.modular.system.model.TaskDetail;
|
import com.stylefeng.guns.modular.system.model.TaskDetailVehicles;
|
import com.stylefeng.guns.modular.system.model.TaskDetailVehiclesChannel;
|
import com.stylefeng.guns.modular.system.model.enums.VideoChannelEnum;
|
import com.stylefeng.guns.modular.system.model.vo.*;
|
import com.stylefeng.guns.modular.system.service.*;
|
import com.stylefeng.guns.modular.system.util.ExcelUtil;
|
import com.stylefeng.guns.modular.system.util.ResultUtil;
|
import com.stylefeng.guns.modular.system.util.quartz.QuartzUtil;
|
import org.apache.poi.hssf.usermodel.*;
|
import org.apache.poi.ss.usermodel.*;
|
import org.apache.poi.ss.util.CellRangeAddress;
|
import org.apache.poi.util.IOUtils;
|
import org.quartz.JobKey;
|
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.format.annotation.DateTimeFormat;
|
import org.springframework.stereotype.Service;
|
|
import javax.servlet.ServletOutputStream;
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.FileInputStream;
|
import java.io.InputStream;
|
import java.net.URL;
|
import java.net.URLConnection;
|
import java.net.URLEncoder;
|
import java.text.SimpleDateFormat;
|
import java.time.format.DateTimeFormatter;
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
/**
|
* @author zhibing.pu
|
* @Date 2024/12/17 20:23
|
*/
|
@Service
|
public class TaskDetailServiceImpl extends ServiceImpl<TaskDetailMapper, TaskDetail> implements ITaskDetailService {
|
|
|
@Autowired
|
private QuartzUtil quartzUtil;
|
|
@Autowired
|
private IPatrolTaskService patrolTaskService;
|
|
@Autowired
|
private ITaskDetailVehiclesService taskDetailVehiclesService;
|
|
@Autowired
|
private ITaskDetailVehiclesChannelService taskDetailVehiclesChannelService;
|
|
@Autowired
|
private IUserService userService;
|
|
|
|
|
|
|
/**
|
* 获取巡查任务列表数据
|
* @param vo
|
* @return
|
*/
|
@Override
|
public List<TaskDetailList> getTaskDetailList(Page<TaskDetailList> pageInfo, TaskDetailListVo vo) {
|
return this.baseMapper.getTaskDetailList(pageInfo, vo);
|
}
|
|
|
/**
|
* 删除任务
|
* @param ids
|
*/
|
@Override
|
public void delTaskDetail(List<String> ids) {
|
this.baseMapper.deleteBatchIds(ids);
|
List<TaskDetail> taskDetails = this.selectBatchIds(ids);
|
for (TaskDetail taskDetail : taskDetails) {
|
JobKey jobKey = new JobKey(taskDetail.getId().toString());
|
quartzUtil.deleteQuartzTask(jobKey);
|
}
|
}
|
|
/**
|
* 获取任务记录列表
|
* @param pageInfo
|
* @param vo
|
* @return
|
*/
|
@Override
|
public List<TaskRecordList> getTaskRecordList(Page<TaskRecordList> pageInfo, TaskRecordListVo vo) {
|
return this.baseMapper.getTaskRecordList(pageInfo, vo);
|
}
|
|
|
/**
|
* 获取任务记录详情
|
* @param id
|
* @return
|
*/
|
@Override
|
public TaskRecordInfo getTaskRecordInfo(Integer id) {
|
TaskDetail taskDetail = this.selectById(id);
|
TaskRecordInfo info = new TaskRecordInfo();
|
info.setId(id);
|
List<TaskDetailVehicles> taskDetailVehiclesList = taskDetailVehiclesService.selectList(new EntityWrapper<TaskDetailVehicles>().eq("task_detail_id", id));
|
info.setVehicleNum(taskDetailVehiclesList.size());
|
long unexecutedQuantity = taskDetailVehiclesList.stream().filter(s -> s.getStatus() == 1).count();
|
info.setUnexecutedQuantity(unexecutedQuantity);
|
PatrolTask patrolTask = patrolTaskService.selectById(taskDetail.getPatrolTaskId());
|
info.setType(patrolTask.getType());
|
info.setName(patrolTask.getName());
|
info.setStatus(taskDetail.getStatus());
|
info.setExecutionTime(taskDetail.getExecutionTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
//执行状态(1=待执行,2=执行中,3=成功,4=离线,5=失败)
|
long offlineNum = taskDetailVehiclesList.stream().filter(s -> s.getStatus() == 4).count();
|
info.setOfflineNum(offlineNum);
|
long normalNum = taskDetailVehiclesList.stream().filter(s -> s.getStatus() == 3).count();
|
info.setNormalNum(normalNum);
|
long abnormalNum = taskDetailVehiclesList.stream().filter(s -> s.getStatus() == 5).count();
|
info.setAbnormalNum(abnormalNum);
|
long underway = taskDetailVehiclesList.stream().filter(s -> s.getStatus() == 2).count();
|
info.setUnderway(underway);
|
return info;
|
}
|
|
|
/**
|
* 获取任务记录中的车船数据
|
* @param vo
|
* @return
|
*/
|
@Override
|
public PictureDetailsVehicle getPictureDetailsVehicle(PictureDetailsVo vo) {
|
PictureDetailsVehicle info = new PictureDetailsVehicle();
|
Wrapper<TaskDetailVehicles> wrapper = new EntityWrapper<TaskDetailVehicles>().eq("task_detail_id", vo.getId());
|
if(ToolUtil.isNotEmpty(vo.getVehicle())){
|
JSONArray jsonArray = JSON.parseArray(vo.getVehicle());
|
List<String> vehicleIdList = new ArrayList<>();
|
for (int i = 0; i < jsonArray.size(); i++) {
|
Integer vehicleId = jsonArray.getJSONObject(i).getInteger("vehicleId");
|
Integer vehicleType = jsonArray.getJSONObject(i).getInteger("vehicleType");
|
vehicleIdList.add(vehicleId + "_" + vehicleType);
|
}
|
wrapper.in("vehicleIdUnique", vehicleIdList);
|
}
|
if(null != vo.getStatus()){
|
wrapper.eq("status", vo.getStatus());
|
}
|
if(null != vo.getSysStatus()){
|
List<TaskDetailVehiclesChannel> sys_status = taskDetailVehiclesChannelService.selectList(new EntityWrapper<TaskDetailVehiclesChannel>().eq("sys_status", vo.getSysStatus()));
|
List<Integer> collect = sys_status.stream().map(TaskDetailVehiclesChannel::getTaskDetailVehiclesId).collect(Collectors.toList());
|
if(collect.size() > 0){
|
wrapper.in("id", collect);
|
}
|
}
|
if(null != vo.getArtificialStatus()){
|
List<TaskDetailVehiclesChannel> sys_status = taskDetailVehiclesChannelService.selectList(new EntityWrapper<TaskDetailVehiclesChannel>().eq("artificial_status", vo.getArtificialStatus()));
|
List<Integer> collect = sys_status.stream().map(TaskDetailVehiclesChannel::getTaskDetailVehiclesId).collect(Collectors.toList());
|
if(collect.size() > 0){
|
wrapper.in("id", collect);
|
}
|
}
|
|
List<TaskDetailVehicles> taskDetailVehiclesList = taskDetailVehiclesService.selectList(wrapper);
|
List<TaskDetailVehicles> vehiclesList = taskDetailVehiclesList.stream().filter(s -> s.getVehicleType() == 1).collect(Collectors.toList());
|
List<VehicleVo> vehicle = new ArrayList<>();
|
for (TaskDetailVehicles taskDetailVehicles : vehiclesList) {
|
VehicleVo vehicleVo = new VehicleVo();
|
BeanUtils.copyProperties(taskDetailVehicles, vehicleVo);
|
vehicle.add(vehicleVo);
|
}
|
info.setVehicle(vehicle);
|
List<TaskDetailVehicles> shipList = taskDetailVehiclesList.stream().filter(s -> s.getVehicleType() == 2).collect(Collectors.toList());
|
List<VehicleVo> ship = new ArrayList<>();
|
for (TaskDetailVehicles taskDetailVehicles : shipList) {
|
VehicleVo vehicleVo = new VehicleVo();
|
BeanUtils.copyProperties(taskDetailVehicles, vehicleVo);
|
ship.add(vehicleVo);
|
}
|
info.setShip(ship);
|
return info;
|
}
|
|
|
|
|
/**
|
* 获取任务记录详情中的通道数据
|
* @param vo
|
* @return
|
*/
|
@Override
|
public List<PictureDetails> getPictureDetails(Page<PictureDetails> pageInfo, PictureDetailsVo vo) {
|
Wrapper<TaskDetailVehicles> wrapper = new EntityWrapper<TaskDetailVehicles>().eq("task_detail_id", vo.getId());
|
if(ToolUtil.isNotEmpty(vo.getVehicle())){
|
JSONArray jsonArray = JSON.parseArray(vo.getVehicle());
|
List<String> vehicleIdList = new ArrayList<>();
|
for (int i = 0; i < jsonArray.size(); i++) {
|
Integer vehicleId = jsonArray.getJSONObject(i).getInteger("vehicleId");
|
Integer vehicleType = jsonArray.getJSONObject(i).getInteger("vehicleType");
|
vehicleIdList.add(vehicleId + "_" + vehicleType);
|
}
|
wrapper.in("vehicleIdUnique", vehicleIdList);
|
}
|
if(null != vo.getStatus()){
|
wrapper.eq("status", vo.getStatus());
|
}
|
List<TaskDetailVehicles> taskDetailVehiclesList = taskDetailVehiclesService.selectList(wrapper);
|
List<Integer> collect = taskDetailVehiclesList.stream().map(TaskDetailVehicles::getId).collect(Collectors.toList());
|
|
List<PictureDetails> pictureDetails = taskDetailVehiclesChannelService.getPictureDetails(pageInfo, collect, vo.getSysStatus(), vo.getArtificialStatus());
|
for (PictureDetails pictureDetail : pictureDetails) {
|
String videoChannel = pictureDetail.getVideoChannel();
|
String name = VideoChannelEnum.getName(Integer.valueOf(videoChannel));
|
pictureDetail.setVideoChannel(name);
|
}
|
return pictureDetails;
|
}
|
|
|
/**
|
* 人工审核
|
* @param vo
|
* @return
|
*/
|
@Override
|
public ResultUtil manualAudit(ManualAuditVo vo) {
|
List<TaskDetailVehiclesChannel> taskDetailVehiclesChannels = taskDetailVehiclesChannelService.selectBatchIds(vo.getId());
|
for (TaskDetailVehiclesChannel taskDetailVehiclesChannel : taskDetailVehiclesChannels) {
|
if(null != taskDetailVehiclesChannel.getArtificialStatus() && 1 == taskDetailVehiclesChannel.getArtificialStatus()){
|
return ResultUtil.error("不能重复审核");
|
}
|
taskDetailVehiclesChannel.setArtificialStatus(vo.getStatus());
|
taskDetailVehiclesChannel.setRemark(vo.getRemark());
|
taskDetailVehiclesChannelService.updateById(taskDetailVehiclesChannel);
|
}
|
return ResultUtil.success();
|
}
|
|
|
/**
|
* 下载任务记录数据
|
* @param ids
|
* @param code
|
* @param status
|
* @param response
|
*/
|
@Override
|
public void downloadTaskRecord(List<String> ids, String code, Integer status, HttpServletResponse response) {
|
List<Map<String, Object>> mapList = this.baseMapper.getDownloadTaskRecord(ids, code, status);
|
try {
|
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
|
HSSFSheet hssfSheet = hssfWorkbook.createSheet();
|
hssfSheet.setDefaultColumnWidth(20);
|
hssfSheet.setDefaultRowHeight((short) 800);
|
//设置单元格合并
|
for (int i = 0; i < 10; i++) {
|
CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 1, i, i);
|
hssfSheet.addMergedRegion(cellRangeAddress);
|
}
|
CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 10, 16);
|
hssfSheet.addMergedRegion(cellRangeAddress);
|
// 创建单元格样式
|
HSSFCellStyle style = hssfWorkbook.createCellStyle();
|
style.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中
|
style.setAlignment(HorizontalAlignment.CENTER); // 水平居中
|
HSSFFont font = hssfWorkbook.createFont();
|
font.setBold(true);
|
style.setFont(font);
|
|
HSSFCellStyle style1 = hssfWorkbook.createCellStyle();
|
style1.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中
|
style1.setAlignment(HorizontalAlignment.CENTER); // 水平居中
|
|
HSSFRow hssfRow = hssfSheet.createRow(0);//设置第一行数据(标题)
|
List<String> titles = Arrays.asList("序号", "任务名称", "任务类型", "执行时间", "执行状态", "车牌号", "运输公司", "年审有效期", "巡检状态", "车辆轨迹图片", "货箱通道");
|
for (int l = 0; l < titles.size(); l++) {
|
HSSFCell hssfCell = hssfRow.createCell(l);
|
hssfCell.setCellType(CellType.STRING);//设置表格类型
|
hssfCell.setCellStyle(style);
|
hssfCell.setCellValue(titles.get(l));
|
}
|
hssfRow = hssfSheet.createRow(1);//设置第一行数据(标题)
|
titles = Arrays.asList("图片", "系统审核", "系统审核时间", "人工审核", "审核意见", "审核人", "审核时间");
|
for (int l = 0; l < titles.size(); l++) {
|
HSSFCell hssfCell = hssfRow.createCell(l + 10);
|
hssfCell.setCellType(CellType.STRING);//设置表格类型
|
hssfCell.setCellStyle(style);
|
hssfCell.setCellValue(titles.get(l));
|
}
|
|
HSSFPatriarch patriarch = hssfSheet.createDrawingPatriarch();
|
for (int i = 0; i < mapList.size(); i++) {
|
hssfRow = hssfSheet.createRow(i + 2);
|
|
Map<String, Object> map = mapList.get(i);
|
//序号
|
HSSFCell hssfCell = hssfRow.createCell(0);
|
hssfCell.setCellType(CellType.STRING);//设置表格类型
|
hssfCell.setCellStyle(style1);
|
hssfCell.setCellValue(i + 1);
|
//任务名称
|
HSSFCell hssfCell1 = hssfRow.createCell(1);
|
hssfCell1.setCellType(CellType.STRING);//设置表格类型
|
hssfCell1.setCellStyle(style1);
|
hssfCell1.setCellValue("");
|
if(null != map.get("name")){
|
hssfCell1.setCellValue(map.get("name").toString());
|
}
|
//任务类型
|
HSSFCell hssfCell2 = hssfRow.createCell(2);
|
hssfCell2.setCellType(CellType.STRING);//设置表格类型
|
hssfCell2.setCellStyle(style1);
|
hssfCell2.setCellValue("");
|
if(null != map.get("taskType")){
|
hssfCell2.setCellValue(map.get("taskType").toString());
|
}
|
//执行时间
|
HSSFCell hssfCell3 = hssfRow.createCell(3);
|
hssfCell3.setCellType(CellType.STRING);//设置表格类型
|
hssfCell3.setCellStyle(style1);
|
hssfCell3.setCellValue("");
|
if(null != map.get("executionTime")){
|
hssfCell3.setCellValue(map.get("executionTime").toString());
|
}
|
//执行状态
|
HSSFCell hssfCell4 = hssfRow.createCell(4);
|
hssfCell4.setCellType(CellType.STRING);//设置表格类型
|
hssfCell4.setCellStyle(style1);
|
hssfCell4.setCellValue("");
|
if(null != map.get("status")){
|
Integer status1 = Integer.valueOf(map.get("status").toString());
|
//执行状态(1=待执行,2=进行中,3=成功,4=失败)
|
hssfCell4.setCellValue(1 == status1 ? "待执行" : 2 == status1 ? "进行中" : 3 == status1 ? "成功" : "失败");
|
}
|
//车牌号
|
HSSFCell hssfCell5 = hssfRow.createCell(5);
|
hssfCell5.setCellType(CellType.STRING);//设置表格类型
|
hssfCell5.setCellStyle(style1);
|
hssfCell5.setCellValue("");
|
if(null != map.get("vehicleNum")){
|
hssfCell5.setCellValue(map.get("vehicleNum").toString());
|
}
|
//运输公司
|
HSSFCell hssfCell6 = hssfRow.createCell(6);
|
hssfCell6.setCellType(CellType.STRING);//设置表格类型
|
hssfCell6.setCellStyle(style1);
|
hssfCell6.setCellValue("");
|
if(null != map.get("companyName")){
|
hssfCell6.setCellValue(map.get("companyName").toString());
|
}
|
//年审有效期
|
HSSFCell hssfCell7 = hssfRow.createCell(7);
|
hssfCell7.setCellType(CellType.STRING);//设置表格类型
|
hssfCell7.setCellStyle(style1);
|
hssfCell7.setCellValue("");
|
if(null != map.get("inspectPeriodEnd")){
|
hssfCell7.setCellValue(map.get("inspectPeriodEnd").toString());
|
}
|
//巡检状态
|
HSSFCell hssfCell8 = hssfRow.createCell(8);
|
hssfCell8.setCellType(CellType.STRING);//设置表格类型
|
hssfCell8.setCellStyle(style1);
|
hssfCell8.setCellValue("");
|
if(null != map.get("vehiclesStatus")){
|
//审核状态(1=未执行,2=正常,3=异常)
|
Integer vehiclesStatus = Integer.valueOf(map.get("vehiclesStatus").toString());
|
hssfCell8.setCellValue(1 == vehiclesStatus ? "未执行" : 2 == vehiclesStatus ? "正常" : "异常");
|
}
|
//车辆轨迹图片
|
HSSFCell hssfCell9 = hssfRow.createCell(9);
|
hssfCell9.setCellType(CellType.STRING);//设置表格类型
|
hssfCell9.setCellStyle(style1);
|
hssfCell9.setCellValue("");
|
//图片
|
if(null != map.get("imageUrl")){
|
try {
|
URL url = new URL(map.get("imageUrl").toString());
|
URLConnection urlConnection = url.openConnection();
|
urlConnection.connect();
|
InputStream inputStream = urlConnection.getInputStream();
|
byte[] bytes = IOUtils.toByteArray(inputStream);
|
int pictureIdx = hssfWorkbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
|
inputStream.close();
|
|
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 10, i + 2, (short) 11, i + 3);
|
//在Excel中添加图片
|
HSSFPicture picture = patriarch.createPicture(anchor, pictureIdx);
|
//设置图片尺寸
|
//picture.resize(0.5, 0.18);
|
}catch (Exception e){
|
e.printStackTrace();
|
}
|
}else{
|
HSSFCell hssfCell10 = hssfRow.createCell(10);
|
hssfCell10.setCellType(CellType.STRING);//设置表格类型
|
hssfCell10.setCellStyle(style1);
|
hssfCell10.setCellValue("");
|
}
|
//系统审核
|
HSSFCell hssfCell11 = hssfRow.createCell(11);
|
hssfCell11.setCellType(CellType.STRING);//设置表格类型
|
hssfCell11.setCellStyle(style1);
|
hssfCell11.setCellValue("");
|
if(null != map.get("sysStatus")){
|
//系统审核状态(1=未执行,2=正常,3=异常)
|
Integer sysStatus = Integer.valueOf(map.get("sysStatus").toString());
|
hssfCell11.setCellValue(1 == sysStatus ? "未执行" : 2 == sysStatus ? "正常" : "异常");
|
}
|
//系统审核时间
|
HSSFCell hssfCell12 = hssfRow.createCell(12);
|
hssfCell12.setCellType(CellType.STRING);//设置表格类型
|
hssfCell12.setCellStyle(style1);
|
hssfCell12.setCellValue("");
|
if(null != map.get("sysCreateTime")){
|
hssfCell12.setCellValue(map.get("sysCreateTime").toString());
|
}
|
//人工审核
|
HSSFCell hssfCell13 = hssfRow.createCell(13);
|
hssfCell13.setCellType(CellType.STRING);//设置表格类型
|
hssfCell13.setCellStyle(style1);
|
hssfCell13.setCellValue("");
|
if(null != map.get("artificialStatus")){
|
//人工审核状态(1=未执行,2=正常,3=异常)
|
Integer artificialStatus = Integer.valueOf(map.get("artificialStatus").toString());
|
hssfCell13.setCellValue(1 == artificialStatus ? "未执行" : 2 == artificialStatus ? "正常" : "异常");
|
}
|
//审核意见
|
HSSFCell hssfCell14 = hssfRow.createCell(14);
|
hssfCell14.setCellType(CellType.STRING);//设置表格类型
|
hssfCell14.setCellStyle(style1);
|
hssfCell14.setCellValue("");
|
if(null != map.get("remark")){
|
hssfCell14.setCellValue(map.get("remark").toString());
|
}
|
//审核人
|
HSSFCell hssfCell15 = hssfRow.createCell(15);
|
hssfCell15.setCellType(CellType.STRING);//设置表格类型
|
hssfCell15.setCellStyle(style1);
|
hssfCell15.setCellValue("");
|
if(null != map.get("artificialUser")){
|
hssfCell15.setCellValue(map.get("artificialUser").toString());
|
}
|
//审核时间
|
HSSFCell hssfCell16 = hssfRow.createCell(16);
|
hssfCell16.setCellType(CellType.STRING);//设置表格类型
|
hssfCell16.setCellStyle(style1);
|
hssfCell16.setCellValue("");
|
if(null != map.get("artificialCreateTime")){
|
hssfCell16.setCellValue(map.get("artificialCreateTime").toString());
|
}
|
}
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("任务记录(" + sdf.format(new Date()) + ").xls", "utf-8"));
|
response.setContentType("application/vnd.ms-excel");
|
ServletOutputStream out = response.getOutputStream();
|
hssfWorkbook.write(out);
|
out.flush();
|
out.close();
|
}catch (Exception e){
|
e.printStackTrace();
|
}
|
}
|
}
|