package com.gic.haoban.manage.service.service.out.impl;

import com.gic.api.base.commons.ServiceResponse;
import com.gic.haoban.manage.api.service.TestApiService;
import com.gic.haoban.manage.service.service.SecretSettingService;
import com.gic.haoban.manage.service.service.WxEnterpriseService;
import com.gic.quartz.api.dto.QuartzTaskDTO;
import com.gic.quartz.api.service.QuartzService;
import com.gic.wechat.api.dto.qywx.UserDTO;
import com.gic.wechat.api.service.qywx.QywxUserApiService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by tgs on 2020/2/9.
 */
@Service
public class TestServiceImpl implements TestApiService {
    private static final Logger logger = LoggerFactory.getLogger(TestServiceImpl.class);
    @Autowired
    private QywxUserApiService qywxUserApiService;
    @Autowired
    private WxEnterpriseService wxEnterpriseService;
    @Autowired
    private SecretSettingService secretSettingService;

    @Autowired
    private QuartzService quartzService;

    @Override
    public String checkThread(String id, Long expireTime) {
        logger.info("测试-start：{}", id);
        if (expireTime != null && expireTime > 0) {
            logger.info("延时：{}", expireTime);
            try {
                Thread.sleep(expireTime);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        logger.info("测试-end：{}", id);
        return id;
    }

    @Override
    public String listSelfQywxUser(String corpid, String secretVal, String dataId) {
        long t = System.currentTimeMillis();
        List<UserDTO> list = this.qywxUserApiService.listSelfDepartmentUser(corpid, secretVal, dataId, 0);
        logger.info("耗时为===============>{}毫秒", (System.currentTimeMillis() - t));
        if (null != list) {
            logger.info("微信获取部门成员数量为：{}", list.size());
        } else {
            logger.info("微信获取部门成员数量异常");
        }
        return "";
    }

    @Override
    public String listAllQywxUser(String corpid, String secretVal, String dataId) {
        long t = System.currentTimeMillis();
        List<UserDTO> list = this.qywxUserApiService.listSelfDepartmentUser(corpid, secretVal, dataId, 1);
        logger.info("耗时为===============>{}毫秒", (System.currentTimeMillis() - t));
        if (null != list) {
            logger.info("微信获取部门成员数量为：{}", list.size());
        } else {
            logger.info("微信获取部门成员数量异常");
        }
        return "";
    }

    /**
     * 调度器DTO参数构造器
     *
     * @param serviceName : 服务类路径
     * @param methodName  : 方法名
     * @param cron        : cron语法, 执行频率
     * @return : 返回参数dto
     */

    @Override
    public void quartzTaskConvert(String serviceName, String methodName, String cron, String param, String referId) {
        QuartzTaskDTO taskDTO = new QuartzTaskDTO();
        taskDTO.setReferId(StringUtils.isBlank(referId) ? "-1" : referId);
        taskDTO.setTaskService(serviceName);
        taskDTO.setTaskMethod(methodName);
        taskDTO.setTaskParam(param);
        taskDTO.setTaskStatus(1);
        //延迟多少秒执行
        taskDTO.setTaskTimeInfo(cron);
        quartzService.addOrUpdateQuartzTask(taskDTO);
    }

    @Override
    public void delQuartzTask(String serviceName, String methodName, String referId) {
        QuartzTaskDTO taskDTO = new QuartzTaskDTO();
        taskDTO.setTaskService(serviceName);
        taskDTO.setTaskMethod(methodName);
        taskDTO.setReferId(referId);
        quartzService.delQuartzTask(taskDTO);
    }

    @Override
    public ServiceResponse listAllQywxUser(String params) {
        logger.info("JSONOnkec:" + params);
        return ServiceResponse.success(params);
    }
}
