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

import java.util.Map;

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.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 ;

    @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);
            }
        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);
    }
}
