TaskServiceImpl.java 6.36 KB
Newer Older
谢恒's avatar
谢恒 committed
1 2
package com.hs.admin.service.impl;

谢恒's avatar
谢恒 committed
3 4
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
谢恒's avatar
谢恒 committed
5
import com.hs.admin.base.PageResult;
谢恒's avatar
谢恒 committed
6 7
import com.hs.admin.bean.*;
import com.hs.admin.dao.JcbDao;
谢恒's avatar
谢恒 committed
8 9
import com.hs.admin.dao.SysTaskDao;
import com.hs.admin.service.TaskService;
谢恒's avatar
谢恒 committed
10 11 12 13 14 15 16 17 18 19 20
import com.hs.admin.util.DateUtil;
import com.hs.admin.util.DbUtil;
import com.hs.admin.util.JsonUtils;
import com.hs.admin.util.LogUtil;
import com.hs.admin.vo.ResponseVO;
import com.hs.admin.vo.ResultVO;
import org.apache.http.client.utils.DateUtils;
import org.quartz.CronExpression;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
谢恒's avatar
谢恒 committed
21
import org.springframework.stereotype.Service;
谢恒's avatar
谢恒 committed
22 23
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
谢恒's avatar
谢恒 committed
24 25

import javax.annotation.Resource;
谢恒's avatar
谢恒 committed
26 27
import java.text.ParseException;
import java.util.*;
谢恒's avatar
谢恒 committed
28 29 30 31 32 33 34

/**
 * @author xieheng
 */
@Service
public class TaskServiceImpl implements TaskService {
    @Resource
谢恒's avatar
谢恒 committed
35
    SysTaskDao sysTaskDao;
谢恒's avatar
谢恒 committed
36
    @Resource
谢恒's avatar
谢恒 committed
37
    JcbDao jcbDao;
谢恒's avatar
谢恒 committed
38 39

    @Override
谢恒's avatar
谢恒 committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    public PageResult<Map> getTaskRecordList(String jcbId, Integer pageNum, Integer pageSize) {
        Integer index = (pageNum - 1) * pageSize;
        Long totalCount = sysTaskDao.getTotalCountById(jcbId);
        List<Map> jcbList = sysTaskDao.findPage(jcbId, index, pageSize);
        Map tmp = jcbList.get(0);
        DataBase dataBase = new DataBase();
        dataBase.setHost(tmp.get("host").toString());
        dataBase.setPort(tmp.get("port").toString());
        dataBase.setName(tmp.get("dbName").toString());
        dataBase.setUsername(tmp.get("dbUsername").toString());
        dataBase.setPassword(tmp.get("dbPwd").toString());
        dataBase.setDbType(tmp.get("dbType").toString());
        List<Map> list = new ArrayList<>();
        for (Map map : jcbList) {
            Map<String, Object> result = new HashMap<>();
            result.put("jcbId", jcbId);
            result.put("jcbName", map.get("jcbName").toString());
            result.put("jobName", map.get("jobName").toString());
            Object contentObj = map.get("content");
            if (!ObjectUtils.isEmpty(contentObj)) {
                String content = contentObj.toString();
                ResponseVO vo = null;
                try {
                    vo = JsonUtils.jsonToBean(content, ResponseVO.class);
                } catch (Exception e) {
                    continue;
                }
                Object params = vo.getParams();
                if (!ObjectUtils.isEmpty(params)) {
                    SysTask task = JsonUtils.jsonToBean(params.toString(), SysTask.class);
                    MqData mqData = JsonUtils.jsonToBean(task.getContent(), MqData.class);
                    String tableName = mqData.getTableName();
                    String cronExpression = task.getCronExpression();
                    if (!ObjectUtils.isEmpty(tableName)) {
                        getTableTime(tableName, dataBase, map);
                    } else {
                        Object createTime = map.get("createTime");
                        Date date = new Date(Long.valueOf(String.valueOf(createTime)));
                        map.put("lastExecuteTime", DateUtil.format(date, "yyyy-MM-dd HH:mm:ss"));
                    }
                    if (!ObjectUtils.isEmpty(cronExpression)) {
                        getNextExecuteTime(cronExpression, map);
                    }
                }
            }
谢恒's avatar
谢恒 committed
85
        }
谢恒's avatar
谢恒 committed
86 87 88 89 90 91
        return new PageResult<>(totalCount, jcbList);
    }

    private Map getNextExecuteTime(String cronExpression, Map map) {
        if (ObjectUtils.isEmpty(cronExpression)) {
            map.put("nextTime", "");
谢恒's avatar
谢恒 committed
92
        } else {
谢恒's avatar
谢恒 committed
93 94 95 96 97 98 99
            try {
                CronExpression cron = new CronExpression(cronExpression);
                Date date = cron.getNextValidTimeAfter(new Date());
                map.put("nextTime", DateUtil.format(date, "yyyy-MM-dd HH:mm:ss"));
            } catch (Exception e) {
                e.printStackTrace();
            }
谢恒's avatar
谢恒 committed
100
        }
谢恒's avatar
谢恒 committed
101
        return map;
谢恒's avatar
谢恒 committed
102 103
    }

谢恒's avatar
谢恒 committed
104 105 106
    private Map getTableTime(String tableName, DataBase dataBase, Map map) {
        JdbcTemplate jdbcTemplate = DbUtil.getJdbcTemplate(dataBase);
        TableLog tableLog = null;
谢恒's avatar
谢恒 committed
107
        try {
谢恒's avatar
谢恒 committed
108 109
            tableLog = jdbcTemplate.queryForObject("select * from table_log where table_name=?", new BeanPropertyRowMapper<>(TableLog.class), tableName);
        } catch (DataAccessException e) {
谢恒's avatar
谢恒 committed
110 111
            e.printStackTrace();
        }
谢恒's avatar
谢恒 committed
112 113 114
        if (!ObjectUtils.isEmpty(tableLog)) {
            Date hsCloudLastTime = tableLog.getHsCloudLastTime();
            map.put("lastExecuteTime", DateUtil.format(hsCloudLastTime, "yyyy-MM-dd HH:mm:ss"));
谢恒's avatar
谢恒 committed
115 116 117 118
        } else {
            Object createTime = map.get("createTime");
            Date date = new Date(Long.valueOf(String.valueOf(createTime)));
            map.put("lastExecuteTime", DateUtil.format(date, "yyyy-MM-dd HH:mm:ss"));
谢恒's avatar
谢恒 committed
119 120
        }
        return map;
谢恒's avatar
谢恒 committed
121 122 123
    }

    @Override
谢恒's avatar
谢恒 committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    public PageResult<Map> getTaskListByCid(Integer cid, String jcbId, Integer pageNum, Integer pageSize) {
        Integer index = (pageNum - 1) * pageSize;
        Long totalCount = sysTaskDao.getRecordTotalCount(cid, jcbId);
        List<Map> jcbList = sysTaskDao.findPageByCid(cid, jcbId, index, pageSize);
        if (!CollectionUtils.isEmpty(jcbList)) {
            for (Map map : jcbList) {
                String ctype = map.get("ctype").toString();
                String content = map.get("content").toString();
                ResponseVO vo = null;
                try {
                    vo = JsonUtils.jsonToBean(content, ResponseVO.class);
                } catch (Exception e) {
                    map.put("content", "");
                    continue;
                }
                ResultVO resultVO = vo.getResultVO();
                if (ObjectUtils.isEmpty(resultVO)) {
                    map.put("content", "");
                } else {
                    //除上传操作不显示result
                    if (!"2".equals(ctype)) {
                        resultVO.setResult(null);
                    }
                    map.put("content", resultVO);
                }
            }
谢恒's avatar
谢恒 committed
150
        }
谢恒's avatar
谢恒 committed
151 152 153 154 155 156
        return new PageResult<>(totalCount, jcbList);
    }

    @Override
    public String getContentById(Integer id) {
        return sysTaskDao.getContentById(id);
谢恒's avatar
谢恒 committed
157 158
    }
}