package com.microservices.dms.resourceLibrary.service;

import com.microservices.common.core.utils.DateUtils;
import com.microservices.common.security.utils.SecurityUtils;
import com.microservices.dms.constant.TaskConstant;
import com.microservices.dms.resourceLibrary.domain.*;
import com.microservices.dms.resourceLibrary.domain.vo.KeyValVo;
import com.microservices.dms.resourceLibrary.domain.vo.TaskListVo;
import com.microservices.dms.resourceLibrary.domain.vo.UserActionDataVo;
import com.microservices.dms.resourceLibrary.mapper.TaskResourceLibraryMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

import static com.microservices.dms.constant.TaskConstant.MARKER_SPACE_DOWNLOAD_API_BY_ID;

@Service
@RequiredArgsConstructor
public class TaskResourceLibraryService {
    private final TaskResourceLibraryMapper taskResourceLibraryMapper;

    @Value("${markerSpaceUrl}")
    public String markerSpaceUrl;

    public int addClick(Clicker click) {
        click.setCreatedAt(DateUtils.getNowDate());
        return taskResourceLibraryMapper.insertClicker(click);
    }

    public int addSearcher(Searcher searcher) {
        searcher.setCreatedAt(DateUtils.getNowDate());
        return taskResourceLibraryMapper.insertSearcher(searcher);
    }

    public int addDownloader(Downloader downloader) {
        downloader.setCreatedAt(DateUtils.getNowDate());
        return taskResourceLibraryMapper.insertDownloader(downloader);
    }

    public List<UserActionDataVo> selectClickerCondition(UserActionDataVo actionDataVo) {
        return taskResourceLibraryMapper.selectClickerCondition(actionDataVo);
    }

    public List<UserActionDataVo> selectVisitCondition(UserActionDataVo actionDataVo) {
        return taskResourceLibraryMapper.selectVisitCondition(actionDataVo);
    }

    public List<UserActionDataVo> selectSearcherCondition(UserActionDataVo searcher) {
        return taskResourceLibraryMapper.selectSearcherCondition(searcher);
    }

    public List<UserActionDataVo> selectDownloaderCondition(UserActionDataVo downloader) {
        return taskResourceLibraryMapper.selectDownloaderCondition(downloader);
    }

    public List<UserActionDataVo> selectWatcherCondition(UserActionDataVo watcher) {
        return taskResourceLibraryMapper.selectWatcherCondition(watcher);
    }

    public List<UserActionDataVo> selectPapersCondition(UserActionDataVo paper) {
        return taskResourceLibraryMapper.selectPapersCondition(paper);
    }

    public List<UserActionDataVo> selectTaskJournalsCondition(UserActionDataVo taskJournals) {
        return taskResourceLibraryMapper.selectTaskJournalsCondition(taskJournals);
    }

    public List<TaskListVo> selectTaskListCondition(TaskListVo taskListVo) {
        List<TaskListVo> taskListVos = taskResourceLibraryMapper.selectTaskListCondition(taskListVo);
        for (TaskListVo vo : taskListVos) {
            UserActionDataVo c = new UserActionDataVo();
            c.setItemId(vo.getId());
            c.setItemType(TaskConstant.TASK_TYPE_PAPERS);
            long papers = taskResourceLibraryMapper.countPapersCondition(c);
            c.setItemType(TaskConstant.TASK_TYPE_WATCHERS);
            long watcher = taskResourceLibraryMapper.countWatcherCondition(c);
            c.setItemType(TaskConstant.TASK_TYPE_JOURNALS);
            long clicker = taskResourceLibraryMapper.countClickerCondition(c);
            long downloader = taskResourceLibraryMapper.countDownloaderCondition(c);
            long taskJournals = taskResourceLibraryMapper.countTaskJournalsCondition(c);
            long searcher = taskResourceLibraryMapper.countSearcherCondition(c);

            vo.setTaskClickCount(clicker);
            vo.setTaskWatcherCount(watcher);
            vo.setTaskPapersCount(papers);
            vo.setTaskDownloadCount(downloader);
            vo.setTaskJournalCount(taskJournals);
            vo.setTaskSearchCount(searcher);
        }
        return taskListVos;
    }

    public TaskResourceLibrary selectTaskResourceLibraryById(Long id) {
        return taskResourceLibraryMapper.selectTaskResourceLibraryById(id);
    }


    public List<TaskResourceLibrary> selectTaskResourceLibraryList(TaskResourceLibrary taskResourceLibrary) {
        List<TaskResourceLibrary> list = taskResourceLibraryMapper.selectTaskResourceLibraryList(taskResourceLibrary);
        for (TaskResourceLibrary l : list) {
            List<KeyValVo<String, String>> attachments = taskResourceLibraryMapper.getAttachments(l.getPaperId());
            for (KeyValVo<String, String> a : attachments) {
                Object v = a.getV();
                a.setV(String.format(MARKER_SPACE_DOWNLOAD_API_BY_ID, markerSpaceUrl, v));
            }
            l.setAttachmentList(attachments);
        }
        return list;
    }


    public Long insertTaskResourceLibrary(TaskResourceLibrary taskResourceLibrary) {
        taskResourceLibrary.setCreateTime(DateUtils.getNowDate());
        taskResourceLibrary.setTransferToResultsDate(DateUtils.getNowDate());
        taskResourceLibrary.setCreateBy(SecurityUtils.getUsername());
        taskResourceLibraryMapper.insertTaskResourceLibrary(taskResourceLibrary);
        return taskResourceLibrary.getId();
    }


    public int updateTaskResourceLibrary(TaskResourceLibrary taskResourceLibrary) {
        taskResourceLibrary.setUpdateTime(DateUtils.getNowDate());
        taskResourceLibrary.setUpdateBy(SecurityUtils.getUsername());
        return taskResourceLibraryMapper.updateTaskResourceLibrary(taskResourceLibrary);
    }

    public int deleteTaskResourceLibraryByIds(Long[] ids) {
        return taskResourceLibraryMapper.deleteTaskResourceLibraryByIds(ids);
    }

    public int deleteTaskResourceLibraryById(Long id) {
        return taskResourceLibraryMapper.deleteTaskResourceLibraryById(id);
    }


    /**
     * 获取任务总数
     *
     * @return 任务总数
     */
    public Long getTotalTasks() {
        return taskResourceLibraryMapper.getTotalTasks();
    }

    /**
     * 获取转换成果任务金额
     *
     * @return 转换成果任务金额
     */
    public Double getConvertedTaskAmount() {
        return taskResourceLibraryMapper.getConvertedTaskAmount();
    }

    /**
     * 获取任务转换成果数
     *
     * @return 任务转换成果数
     */
    public Long getConvertedTasksCount() {
        return taskResourceLibraryMapper.getConvertedTasksCount();
    }

    /**
     * 获取任务金额
     *
     * @return 任务金额
     */
    public Double getTaskAmount() {
        return taskResourceLibraryMapper.getTaskAmount();
    }


    public Long getReviewTaskCount() {
        return taskResourceLibraryMapper.getReviewTaskCount();
    }

    public int addFavorite(Favorite favorite) {
        favorite.setCreatedAt(DateUtils.getNowDate());
        return taskResourceLibraryMapper.insertFavorite(favorite);
    }

    public int addWatcher(Watcher watcher) {
        watcher.setCreatedAt(DateUtils.getNowDate());
        return taskResourceLibraryMapper.insertWatcher(watcher);
    }

    public int addSearcher2(Searcher searcher) {
        String[] split = searcher.getSearchIds().split(",");
        Arrays.stream(split).forEach(s -> {
            Searcher e = new Searcher();
            e.setCreatedAt(DateUtils.getNowDate());
            e.setSearchId(Long.parseLong(s));
            e.setUserId(searcher.getUserId());
            e.setExtInfo(searcher.getExtInfo());
            e.setSearchType(searcher.getSearchType());
            taskResourceLibraryMapper.insertSearcher(e);
        });

        return split.length;
    }

    public int delFavorite(Favorite favorite) {
        return taskResourceLibraryMapper.delFavorite(favorite);
    }

    public int delWatcher(Watcher watcher) {
        return taskResourceLibraryMapper.delWatcher(watcher);
    }
}
