package cn.sagacloud.service;

import cn.sagacloud.dao.TaskServiceDao;
import cn.sagacloud.server.algorithm.backstage.model.CreateRequest;
import cn.sagacloud.server.algorithm.backstage.model.CreateResponse;
import cn.sagacloud.server.algorithm.backstage.model.QueryRequest;
import cn.sagacloud.server.algorithm.backstage.model.QueryResponse;
import cn.sagacloud.server.algorithm.models.modelFile.DownloadEntity;
import cn.sagacloud.server.algorithm.models.modelFile.TaskEntity;
import cn.sagacloud.server.algorithm.models.modelFile.TaskResultEntity;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.sybotan.database.SFilter;
import com.sybotan.service.models.requests.SCreateRequest;
import com.sybotan.service.models.requests.SQueryRequest;
import com.sybotan.service.models.responses.SCreateResponse;
import com.sybotan.service.models.responses.SQueryResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

@Component
public class TaskServiceRequest {

    @Autowired
    private TaskServiceDao taskServiceDao;

    /**
     * 保存定时任务实体
     *
     * @param request
     * @return
     */
    public CreateResponse<TaskEntity> createTask(CreateRequest<TaskEntity> request) {
        SCreateRequest<TaskEntity> taskRequest = JSON.parseObject(JSONObject.toJSONString(request), new TypeReference<SCreateRequest<TaskEntity>>() {
        });
        SCreateResponse<TaskEntity> sCreateResponse = taskServiceDao.getTaskService().createList(taskRequest);
        CreateResponse<TaskEntity> response = JSON.parseObject(JSONObject.toJSONString(sCreateResponse), new TypeReference<CreateResponse<TaskEntity>>() {
        });
        return response;
    }

    /**
     * 更新模型文件的url和md5
     *
     * @param request
     * @return
     */
    public CreateResponse<DownloadEntity> createDownload(CreateRequest<DownloadEntity> request) {
        SCreateRequest<DownloadEntity> downloadRequest = JSON.parseObject(JSONObject.toJSONString(request), new TypeReference<SCreateRequest<DownloadEntity>>() {
        });
        SCreateResponse<DownloadEntity> sCreateResponse = taskServiceDao.getDownloadService().createList(downloadRequest);
        CreateResponse<DownloadEntity> response = JSON.parseObject(JSONObject.toJSONString(sCreateResponse), new TypeReference<CreateResponse<DownloadEntity>>() {
        });
        return response;
    }

    /**
     * 查询任务实体
     *
     * @param request
     * @return
     */
    public QueryResponse<TaskEntity> queryTask(QueryRequest request) {
        SQueryRequest taskRequest = JSON.parseObject(JSONObject.toJSONString(request), new TypeReference<SQueryRequest>() {
        });
        SQueryResponse<TaskEntity> sQueryResponse = taskServiceDao.getTaskService().pageQuery(taskRequest, new ArrayList<SFilter>());
        QueryResponse<TaskEntity> queryResponse = JSON.parseObject(JSONObject.toJSONString(sQueryResponse), new TypeReference<QueryResponse<TaskEntity>>() {
        });
        return queryResponse;
    }

    /**
     * 查询任务结果详细信息实体
     *
     * @param request
     * @return
     */
    public QueryResponse<TaskResultEntity> queryTaskResult(QueryRequest request) {
        SQueryRequest taskResultRequest = JSON.parseObject(JSONObject.toJSONString(request), new TypeReference<SQueryRequest>() {
        });
        SQueryResponse<TaskResultEntity> sQueryResponse = taskServiceDao.getTaskResultService().pageQuery(taskResultRequest, new ArrayList<SFilter>());
        QueryResponse<TaskResultEntity> queryResponse = JSON.parseObject(JSONObject.toJSONString(sQueryResponse), new TypeReference<QueryResponse<TaskResultEntity>>() {
        });
        return queryResponse;
    }
}
