package com.chinaitop.depot.intelligent.IntelligentDetection.service.impl;

import com.chinaitop.depot.intelligent.IntelligentDetection.mapper.TTimingTaskHistoryMapper;
import com.chinaitop.depot.intelligent.IntelligentDetection.mapper.TTimingTaskMapper;
import com.chinaitop.depot.intelligent.IntelligentDetection.model.TTimingTask;
import com.chinaitop.depot.intelligent.IntelligentDetection.model.TTimingTaskExample;
import com.chinaitop.depot.intelligent.IntelligentDetection.model.TTimingTaskHistory;
import com.chinaitop.depot.intelligent.IntelligentDetection.model.TTimingTaskHistoryExample;
import com.chinaitop.depot.intelligent.IntelligentDetection.service.QuartzManager;
import com.chinaitop.depot.intelligent.IntelligentDetection.service.TTimingTaskService;
import com.chinaitop.depot.intelligent.IntelligentDetection.service.TestTask;
import com.chinaitop.depot.intelligent.utils.DateUtils;
import com.chinaitop.depot.intelligent.utils.JsonToObjectUtils;
import com.chinaitop.depot.intelligent.utils.UuidUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author qingsong.han
 * @description: 策略设置
 * @create 2019-01-03 10:13
 */
@Service
public class TTimingTaskServiceImpl implements TTimingTaskService {

    @Resource
    TTimingTaskMapper tTimingTaskMapper;

    @Resource
    TTimingTaskHistoryMapper tTimingTaskHistoryMapper;

    @Resource
    private QuartzManager quartzManager;


    @Override
    public List<TTimingTask> getList(String tName, String tState, String tCategory) {
        TTimingTaskExample example = new TTimingTaskExample();
        TTimingTaskExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(tName)) {
            criteria.andTNameLike("%" + tName + "%");
        }
        if (StringUtils.isNotBlank(tState)) {
            criteria.andTStateEqualTo(tState);
        }
        if (StringUtils.isNotBlank(tCategory)) {
            criteria.andTCategoryEqualTo(tCategory);
        }
        example.setOrderByClause("t_update_time desc");
        return tTimingTaskMapper.selectByExample(example);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(String taskSchemaJson) {
        if (StringUtils.isNotBlank(taskSchemaJson)) {
            TTimingTask timingTask = (TTimingTask) JsonToObjectUtils.jsonToObject(taskSchemaJson, TTimingTask.class);
            if (StringUtils.isNotBlank(timingTask.gettId())) {
                timingTask.settUpdateTime(new Date()); // 创建时间
                List<String> collect = tTimingTaskMapper.selectStartData().stream().map(t -> t.gettId()).collect(Collectors.toList());
                tTimingTaskMapper.updateByPrimaryKeySelective(timingTask);
                if (timingTask.gettSwitch().equals("1") && StringUtils.isNotBlank(timingTask.gettDetectionCron())) {
                    if (collect.contains(timingTask.gettId())) {
                        quartzManager.updateJob(timingTask.gettId(), timingTask.gettDetectionCron());
                    } else {
                        quartzManager.addJob(timingTask.gettId(), TestTask.class.getName(), timingTask.gettDetectionCron());
                    }
                }else {
                    quartzManager.deleteJob(timingTask.gettId());
                }
            } else {
                String code = UuidUtils.getCode();
                timingTask.settId(code);
//                timingTask.settState("1"); // 0是 1否
                timingTask.settUpdateTime(new Date()); // 创建时间
                tTimingTaskMapper.insert(timingTask);
                //0是关闭，1是启动
                if (timingTask.gettSwitch().equals("1") && StringUtils.isNotBlank(timingTask.gettDetectionCron())) {
                    quartzManager.addJob(timingTask.gettId(), TestTask.class.getName(), timingTask.gettDetectionCron());
                }
            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteById(String id) {
        TTimingTask timingTask = tTimingTaskMapper.selectByPrimaryKey(id);
        if (timingTask.gettSwitch().equals("0") && StringUtils.isNotBlank(timingTask.gettDetectionCron())) {
            quartzManager.deleteJob(timingTask.gettId());
        }
        return tTimingTaskMapper.deleteByPrimaryKey(id);
    }

    @Override
    public TTimingTask getById(String id) {
        return tTimingTaskMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<TTimingTaskHistory> getDetectionHistory(String vcfcode, String time, String tid, String type) {
        TTimingTaskHistoryExample example = new TTimingTaskHistoryExample();
        TTimingTaskHistoryExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(vcfcode)){
            criteria.andVCfCodeEqualTo(vcfcode);
        }
        if (StringUtils.isNotBlank(time)){
            criteria.andTUpdateTimeBetween(DateUtils.stringToData(time+DateUtils.startTime),DateUtils.stringToData(time+DateUtils.endTime));
        }
        if (StringUtils.isNotBlank(tid)){
            criteria.andTIdEqualTo(tid);
        }
        if (StringUtils.isNotBlank(type)){
            criteria.andTCategoryEqualTo(type);
        }
        example.setOrderByClause("t_update_time desc");
        List<TTimingTaskHistory> tTimingTaskHistories = tTimingTaskHistoryMapper.selectByExample(example);
        return tTimingTaskHistories;
    }
}
