package com.xxl.job.admin.service.impl;

import com.alibaba.fastjson2.JSON;
import com.google.gson.JsonObject;
import com.xxl.job.admin.core.model.XxlJobInfo;
import com.xxl.job.admin.core.thread.JobCompleteHelper;
import com.xxl.job.admin.core.thread.JobRegistryHelper;
import com.xxl.job.admin.core.thread.JobTriggerPoolHelper;
import com.xxl.job.admin.core.trigger.TriggerTypeEnum;
import com.xxl.job.admin.dao.XxlJobInfoDao;
import com.xxl.job.admin.service.XxlJobService;
import com.xxl.job.core.biz.AdminBiz;
import com.xxl.job.core.biz.model.HandleCallbackParam;
import com.xxl.job.core.biz.model.JobInfo;
import com.xxl.job.core.biz.model.RegistryParam;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.util.XxlJobRemotingUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

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

/**
 * @author xuxueli 2017-07-27 21:54:20
 */
@Service
public class AdminBizImpl implements AdminBiz {
    @Resource
    private XxlJobInfoDao xxlJobInfoDao;

    @Resource
    private XxlJobService xxlJobService;


    @Override
    public ReturnT<String> callback(List<HandleCallbackParam> callbackParamList) {
        return JobCompleteHelper.getInstance().callback(callbackParamList);
    }

    @Override
    public ReturnT<String> registry(RegistryParam registryParam) {
        return JobRegistryHelper.getInstance().registry(registryParam);
    }

    @Override
    public ReturnT<String> registryRemove(RegistryParam registryParam) {
        return JobRegistryHelper.getInstance().registryRemove(registryParam);
    }

    @Override
    public ReturnT<String> jobAdd(JobInfo jobInfo) {
        XxlJobInfo xxlJobInfo = new XxlJobInfo();
        BeanUtils.copyProperties(jobInfo, xxlJobInfo);
        //随机选择一个执行器
        return xxlJobService.add(xxlJobInfo);
    }

    @Override
    public ReturnT<String> jobRemove(Integer jobId) {
        return xxlJobService.remove(jobId);
    }

    @Override
    public ReturnT<String> jobStop(Integer jobId) {
        return xxlJobService.stop(jobId);
    }

    @Override
    public ReturnT<String> jobStart(Integer jobId) {
        return xxlJobService.start(jobId);
    }

    @Override
    public ReturnT<String> jobUpdate(JobInfo jobInfo) {
        XxlJobInfo xxlJobInfo = new XxlJobInfo();
        BeanUtils.copyProperties(jobInfo, xxlJobInfo);
        return xxlJobService.update(xxlJobInfo);
    }

    @Override
    public ReturnT<String> jobTrigger(Integer jobId) {
        ReturnT<String> returnT = xxlJobService.getJobById(jobId);
        JobInfo jobInfo = JSON.parseObject(returnT.getContent(), JobInfo.class);
        JobTriggerPoolHelper.trigger(jobId, TriggerTypeEnum.MANUAL, -1, null, jobInfo.getExecutorParam(), "");
        return ReturnT.SUCCESS;
    }

    @Override
    public ReturnT<String> getJobById(Integer jobId) {
        return xxlJobService.getJobById(jobId);
    }

    //    @Override
//    public ReturnT<String> stopByIds(List<Integer> ids) {
//        xxlJobInfoDao.stopByIds(ids);
//        return ReturnT.SUCCESS;
//    }




}
