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

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.haoban.manage.api.dto.DealParamMqDTO;
import com.gic.haoban.manage.api.dto.qdto.hm.HmQrcodeQDTO;
import com.gic.haoban.manage.api.enums.PreDealTypeEnum;
import com.gic.haoban.manage.api.enums.SyncTaskStatusEnum;
import com.gic.haoban.manage.api.service.hm.HmQrcodeApiService;
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.haoban.manage.service.task.BaseSyncOperation;

@Service
public class QwHmOperation implements BaseSyncOperation {

    private static final Logger logger = LoggerFactory.getLogger(QwHmOperation.class);
    @Autowired
    private HmQrcodeApiService hmQrcodeApiService;
    @Autowired
    private PreDealService preDealService;
    @Autowired
    private SyncTaskService syncTaskService;

    @Override
    public void dealSingleByMq(DealParamMqDTO dealParamMqDTO, TabHaobanPreDealLog dataPre) {
        String taskId = dealParamMqDTO.getTaskId();
        Long hmId = Long.valueOf(dataPre.getDataId());
        logger.info("开始处理活码={},{}",taskId,hmId);
        String clerkIds = dataPre.getDataContent();
        String wxEnterpriseId = dataPre.getWxEnterpriseId();
        String enterpriseId = dataPre.getEnterpriseId();
        List<String> clerkIdList = JSONArray.parseArray(clerkIds, String.class);
        // 删除
        ServiceResponse resp = null ;
        if (clerkIdList.size() == 0) {
            resp = this.hmQrcodeApiService.delById(hmId, 1, null);
        } else {
            HmQrcodeQDTO qdto = new HmQrcodeQDTO();
            qdto.setWxEnterpriseId(wxEnterpriseId);
            qdto.setHmId(hmId);
            qdto.setEnterpriseId(enterpriseId);
            qdto.setClerkIdList(clerkIdList);
            resp = this.hmQrcodeApiService.batchUpdateHm(qdto);
        }
        logger.info("批量修改活码={},{}",hmId,JSON.toJSONString(resp));
        if(resp.isSuccess()) {
            this.preDealService.updateStatusByDataId(taskId, dataPre.getDataId(), 2, null);
        }else {
            this.preDealService.updateStatusByDataId(taskId, dataPre.getDataId(), 3, resp.getMessage());
        }
        this.dealSuccess(taskId, dataPre.getDataId(), enterpriseId, wxEnterpriseId);
    }

    @Override
    public void dealException(String taskId, String dataId, String enterpriseId, String reason) {
    }

    @Override
    public void dealSuccess(String taskId, String dataId, String enterpriseId, String wxEnterpriseId) {
        boolean b = preDealService.checkFriendTask(taskId, PreDealTypeEnum.modify_hm.getVal());
        if (!b) {
            return;
        }
        logger.info("批量修改活码完成={}", taskId);
        TabHaobanSyncTask syncTask = syncTaskService.getSyncTask(taskId);
        if (syncTask.getStatusFlag() == SyncTaskStatusEnum.compute.getVal()) {
            return;
        }
        logger.info("批量修改活码完成,status={}", taskId);
        this.syncTaskService.updateTaskStatus(taskId, SyncTaskStatusEnum.compute.getVal());
    }

    @Override
    public void checkDepartmentTask(String taskId) {
    }

}
