package com.docus.server.archivefile.service.impl;

import com.docus.core.util.Func;
import com.docus.infrastructure.redis.service.IdService;
import com.docus.infrastructure.web.request.SearchRequest;
import com.docus.infrastructure.web.response.PageResult;
import com.docus.server.archivefile.infrastructure.dao.ITaskConfigDao;
import com.docus.server.archivefile.service.ITaskConfigService;
import com.docus.server.entity.TaskConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

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

@Service
public class TaskConfigServiceImpl implements ITaskConfigService {
    @Resource
    private ITaskConfigDao taskConfigDao;
    @Resource
    private IdService idService;
    @Resource
    private TrackRetryService trackRetryService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateAllPointerDate(String id, Date date) {
        TaskConfig taskConfig = getTaskConfigById(id);
        taskConfig.setAllPointerTime(date);
        updateTaskConfig(taskConfig);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateIncPointerDate(String id, Date date) {
        TaskConfig taskConfig = getTaskConfigById(id);
        taskConfig.setIncPointerTime(date);
        updateTaskConfig(taskConfig);
    }

    @Override
    public TaskConfig getTaskConfigById(String taskConfigId) {
        return taskConfigDao.findById(taskConfigId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateTaskConfig(TaskConfig taskConfig) {
        if (Func.isNull(taskConfig.getId())) {
            taskConfig.setId(idService.getDateSeq());
        }
        return taskConfigDao.saveOrUpdate(taskConfig);
    }

    @Override
    public PageResult<TaskConfig> search(SearchRequest searchRequest) {
        return taskConfigDao.searchTaskConfig(searchRequest);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteTaskConfig(List<Long> ids) {
        return taskConfigDao.deleteByIdList(ids);
    }

    public PageResult<TaskConfig> searchTaskMessage(SearchRequest request) {
        return taskConfigDao.searchTaskConfig(request);
    }

    @Override
    public void searchRetry(SearchRequest searchRequest) throws Exception {
        PageResult<TaskConfig> result = searchTaskMessage(searchRequest);
        actionRetry(result.getList());
    }

    @Override
    public void findRetry(List<Long> ids) throws Exception {
        List<TaskConfig> taskMessages = taskConfigDao.findByIds(ids);
        actionRetry(taskMessages);
    }

    private void actionRetry(List<TaskConfig> list) throws Exception {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }

        for (TaskConfig config : list) {
            trackRetryService.doRetryTaskConfig(config);
        }
    }

}
