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

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.gic.haoban.manage.service.service.StaffClerkRelationService;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gic.commons.util.GICMQClientUtil;
import com.gic.haoban.manage.api.dto.CommonMQDTO;
import com.gic.haoban.manage.api.service.HaobanCommonMQApiService;
import com.gic.haoban.manage.service.service.StaffDepartmentRelatedService;
import com.gic.haoban.manage.service.service.WelcomeSendService;
import com.gic.member.api.service.MemberOpenCardBusinessService;
import com.gic.mq.sdk.GicMQClient;
import com.gic.quartz.api.dto.QuartzTaskDTO;
import com.gic.quartz.api.service.QuartzService;

@Service("haobanCommonMQApiService")
public class HaobanCommonMQApiServiceImpl implements HaobanCommonMQApiService {

    private final Logger log = LogManager.getLogger(HaobanCommonMQApiService.class);

    private static GicMQClient mqClient = GICMQClientUtil.getClientInstance();

    @Autowired
    private QuartzService quartzService;
    @Autowired
    private StaffDepartmentRelatedService staffDepartmentRelatedService;
    @Autowired
    private WelcomeSendService welcomeSendService;
    @Autowired
    private MemberOpenCardBusinessService memberOpenCardBusinessService ;
    @Autowired
    private StaffClerkRelationService staffClerkRelationService ;

    @Override
    public void putCommonMessage(CommonMQDTO dto) {
        String message = JSONObject.toJSONString(dto);
        log.info("加入好办通用队列params={}", message);
        try {
            mqClient.sendCommonMessage("haobanCommonRouter", message,
                    "com.gic.haoban.manage.api.service.HaobanCommonMQApiService", "commonHandler");
        } catch (Exception e) {
            log.error("发送MQ异常");
            e.printStackTrace();
        }
    }

    @Override
    public void putCommonDelayMessage(CommonMQDTO dto, int delay) {
        String message = JSONObject.toJSONString(dto);
        log.info("加入好办延时通用队列params={}", message);
        try {
            mqClient.sendMessage("haobanDelayMQ", message, delay);
        } catch (Exception e) {
            log.error("发送MQ异常");
            e.printStackTrace();
        }
    }

    @Override
    public void commonHandler(String message) {
        log.info("接收好办通用队列params={}", message);
        CommonMQDTO dto = JSON.parseObject(message, CommonMQDTO.class);
        // 好办停用,删除定时
        Map<String, String> map = (Map<String, String>) dto.getParams();
        switch (dto.getType()) {
        case 1:
            String enterpriseId = map.get("enterpriseId");
            this.deleteQuartz(enterpriseId);
            break;
        case 2:
            String staffId = map.get("staffId");
            if (StringUtils.isNotBlank(staffId)) {
                this.staffDepartmentRelatedService.repairStaffDepart(staffId);
            }
            break ;
        case 3:
            String memberId = map.get("memberId") ;
            enterpriseId = map.get("enterpriseId") ;
            String clerkId = map.get("clerkId") ;
            this.memberOpenCardBusinessService.memberClerkByQywx(enterpriseId, memberId, clerkId);
            break ;
        default:
            break;
        }
    }

    private void deleteQuartz(String enterpriseId) {
        if (StringUtils.isBlank(enterpriseId)) {
            return;
        }
        log.info("删除好办定时eid={}", enterpriseId);
        // 日报
        QuartzTaskDTO task = new QuartzTaskDTO();
        task.setTaskService("com.gic.haoban.app.daily.api.service.DailyReportTaskApiService");
        task.setTaskMethod("run");
        task.setReferId(enterpriseId);
        this.quartzService.delQuartzTask(task);
    }

    @Override
    public void welcomeMQ(String params) {
        this.welcomeSendService.sendWelcome(params);
    }

    @Override
    public void noticeMemberMQ(String params) {
        log.info("通知会员企微标识={}",params);
        JSONObject json = JSONObject.parseObject(params) ;
        String enterpriseId = json.getString("enterpriseId") ;
        List<String> memberIdList = JSONArray.parseArray(json.getString("memberIdList"),String.class) ;
        this.staffClerkRelationService.delMemberQywxFlag(enterpriseId,memberIdList) ;
    }

    @Override
    public void putToNoticeMemberMQ(String enterpriseId, List<String> memberIdList) {
        JSONObject json = new JSONObject() ;
        json.put("enterpriseId",enterpriseId) ;
        json.put("memberIdList",memberIdList) ;
        try {
            mqClient.sendMessage("haobanNoticeMemberMQ", json.toJSONString());
        } catch (Exception e) {
            log.error("发送MQ异常");
        }
    }
}
