package com.gic.haoban.manage.service.task;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;
import org.redisson.api.RRateLimiter;
import org.redisson.api.RateIntervalUnit;
import org.redisson.api.RateType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.gic.api.base.commons.Page;
import com.gic.haoban.base.api.common.ServiceResponse;
import com.gic.haoban.manage.api.dto.DealParamMqDTO;
import com.gic.haoban.manage.api.dto.StaffDTO;
import com.gic.haoban.manage.api.dto.WxEnterpriseDTO;
import com.gic.haoban.manage.api.dto.WxEnterpriseQwDTO;
import com.gic.haoban.manage.api.enums.PreDealStatusEnum;
import com.gic.haoban.manage.api.enums.PreDealTypeEnum;
import com.gic.haoban.manage.api.enums.SyncTaskStatusEnum;
import com.gic.haoban.manage.api.service.DealSyncOperationApiService;
import com.gic.haoban.manage.api.service.StaffApiService;
import com.gic.haoban.manage.api.service.WxEnterpriseApiService;
import com.gic.haoban.manage.service.config.Config;
import com.gic.haoban.manage.service.entity.TabHaobanPreDealLog;
import com.gic.haoban.manage.service.entity.TabHaobanSyncTask;
import com.gic.haoban.manage.service.service.PreDealService;
import com.gic.haoban.manage.service.service.SyncTaskService;
import com.gic.redis.data.util.RedisUtil;
import com.gic.wechat.api.dto.qywx.QywxNewUseridDTO;
import com.gic.wechat.api.service.qywx.QywxUserApiService;

/**
 * 企微通讯录同步
 * Created 2020/4/13.
 *
 * @author hua
 */
@Service
public class QywxStaffSyncOperation implements BaseSyncOperation {
    private static final Logger logger = LoggerFactory.getLogger(QywxStaffSyncOperation.class);

    @Autowired
    private PreDealService preDealService;
    @Autowired
    private SyncTaskService syncTaskService;
    @Autowired
    private StaffApiService staffApiService;
    @Autowired
    private DealSyncOperationApiService dealSyncOperationApiService;
    @Override
    public void dealSingleByMq(DealParamMqDTO dealParamMqDTO, TabHaobanPreDealLog dataPre) {
        logger.info("企微通讯录成员同步处理：{}", JSONObject.toJSONString(dealParamMqDTO));
        boolean dealFlag = true;
        String reason = "";
        //处理clerk
        try {
            String taskId = dataPre.getTaskId() ;
            String wxUserId = dealParamMqDTO.getData() ;
            ServiceResponse response = staffApiService.getWxSaveNew(wxUserId, dataPre.getWxEnterpriseId(),true);
            if (response.getCode() != 1) {
                dealFlag = false;
                reason = response.getMessage();
            }
            this.preDealService.addTaskDepartStatusCache(taskId,2);
        } catch (Exception e) {
            logger.info("企微通讯录成员同步失败：{}", e.getMessage(), e);
            reason = "同步异常:";
            dealFlag = false;
        } finally {
            if (!dealFlag) {
                dealException(dealParamMqDTO.getTaskId(), dealParamMqDTO.getData(), dataPre.getEnterpriseId(), reason);
            } else {
                dealSuccess(dealParamMqDTO.getTaskId(), dealParamMqDTO.getData(), dataPre.getEnterpriseId(), dataPre.getWxEnterpriseId());
            }
        }
    }

    @Override
    public void dealException(String taskId, String dataId, String enterpriseId, String reason) {
        logger.info("企微通讯录成员处理失败：t:{},d:{}", taskId, dataId);
        this.preDealService.updateStatusByDataId(taskId, dataId, PreDealTypeEnum.clerk.getVal(), PreDealStatusEnum.exception.getVal(), reason);
    }

    @Override
    public void dealSuccess(String taskId, String dataId, String enterpriseId, String wxEnterpriseId) {
        logger.info("企微通讯录成员处理成功：t:{},d:{}", taskId, dataId);
        this.preDealService.updateStatusByDataId(taskId, dataId, PreDealTypeEnum.clerk.getVal(), PreDealStatusEnum.computed.getVal(), "成功");
    }

    @Override
    public void checkDepartmentTask(String taskId) {
    }

}
