package cn.superdesk.uniorder.dubbo.consumer.scheduler;

import cn.superdesk.uniorder.common.enums.thirdpart.jd.SysQuartzScheduleTaskStatementOfAccountIsPauseEnum;
import cn.superdesk.uniorder.common.exception.BadRequestException;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.dao.entity.scheduler.SysQuartzJob;
import cn.superdesk.uniorder.dto.param.scheduler.SysQuartzJobParam;
import cn.superdesk.uniorder.dubbo.facade.scheduler.DubboSysQuartzJobService;
import cn.superdesk.uniorder.executor.scheduler.utils.QuartzManage;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

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

/**
 * @Description: 定时任务
 * @Author: linjinyu
 * @Email linjinyu@avic-s.com
 * @CreateDate: 2020-12-17 16:35:26
 * @UpdateUser: linjinyu
 * @UpdateDate: 2020-12-17 16:35:26
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Component
public class SysQuartzJobDubboConsumerService {

	@Reference(version = "1.0.0", timeout = 10000, retries = -1)
	private DubboSysQuartzJobService dubboSysQuartzJobService;

	@Resource
	private QuartzManage quartzManage;


	/****************************************** extends BaseService 部分 start ******************************************/

	public void addObj(SysQuartzJobParam param) {

		dubboSysQuartzJobService.addObj(param);

	}

	public void updateObj(SysQuartzJobParam param) {

		dubboSysQuartzJobService.updateObj(param);
	}

	public void deleteObj(Long id) {

		dubboSysQuartzJobService.deleteObj(id);
	}

	public SysQuartzJob findById(Long id) {

		return dubboSysQuartzJobService.findById(id);
	}

	public PageInfo<SysQuartzJob> findByPaging(Map<String, Object> conditions, Integer pageNun, Integer pageSize) {

		return dubboSysQuartzJobService.findByPaging(conditions, pageNun, pageSize);
	}

	public List<SysQuartzJob> findAll(Map<String, Object> queryMap) {

		return dubboSysQuartzJobService.findAll(queryMap);
	}

	/****************************************** extends BaseService 部分   end ******************************************/


	public void addSysQuartzJob(SysQuartzJobParam param) {

		dubboSysQuartzJobService.addSysQuartzJob(param);
	}


	public void updateSysQuartzJob(SysQuartzJobParam param) {

		dubboSysQuartzJobService.updateSysQuartzJob(param);
	}


	public void updateIsPause(SysQuartzJob quartzJob) {

		if (quartzJob.getId().equals(1L)) {
			throw new BadRequestException("该任务不可操作");
		}

		if (quartzJob.getIsPause().equals(SysQuartzScheduleTaskStatementOfAccountIsPauseEnum.启用.getKey())) {
			// 恢复
			quartzManage.resumeJob(quartzJob);
		} else {
			// 暂停
			quartzManage.pauseJob(quartzJob);
		}

		quartzJob.setIsPause(quartzJob.getIsPause().equals(SysQuartzScheduleTaskStatementOfAccountIsPauseEnum.启用.getKey()) ?
				SysQuartzScheduleTaskStatementOfAccountIsPauseEnum.暂停.getKey() : SysQuartzScheduleTaskStatementOfAccountIsPauseEnum.启用.getKey());

		dubboSysQuartzJobService.updateSysQuartzJob(quartzJob);
	}


	public void deleteSysQuartzJob(Long id) {
		dubboSysQuartzJobService.deleteSysQuartzJob(id);
	}


	public SysQuartzJob findSysQuartzJobById(Long id) {

		return dubboSysQuartzJobService.findSysQuartzJobById(id);
	}

	public List<SysQuartzJob> findSysQuartzJobByIsPauseEq0() {

		return dubboSysQuartzJobService.findSysQuartzJobByIsPauseEq0();
	}


	public R<List<SysQuartzJob>> pageQuery(Map<String, Object> conditions, int pageNun, int pageSize) {

		return dubboSysQuartzJobService.pageQuery(conditions, pageNun, pageSize);
	}
}
