package com.zcxk.sync.mq;

import com.zcxk.core.common.exception.BusinessException;
import com.zcxk.rmcp.core.dao.OrgMapper;
import com.zcxk.rmcp.core.entity.Org;
import com.zcxk.sync.base.BaseSync;
import com.zcxk.sync.constant.MqConstant;
import com.zcxk.sync.enums.SqlType;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

/**
 * @author Andy
 * @version V1.0
 * @description: 同步小区
 * @date 2021/7/28
 **/
@Slf4j
@Component
public class SyncOrgConsumer extends BaseSync<Org> {

    @Resource
    private OrgMapper orgMapper;

    /**
     * 确定同步来的表名字
     */
    private static final String TABLE_NAME = "uims_org";

    /**
    * @author Andy
    * @description mq入口
    * @date 11:33 2021/7/29
    * @param message
    * @return void
    **/
    @RabbitListener(queues = MqConstant.SYNC_ORG_QUEUE)
    @RabbitHandler
    public void exec(Message message) {
        log.info("======================同步org机构数据 begin======================");
        String msg = new String(message.getBody(), StandardCharsets.UTF_8);
        if (StringUtils.isEmpty(msg)) {
            log.info("同步org机构数据 消费为空");
            return;
        }
        try {
            exec(msg);
        } catch (Exception e) {
            log.error("同步org机构数据异常：{}", msg);
        }
        log.info("======================同步org机构数据  end======================");
    }

    /**
    * @author Andy
    * @description 执行入口
    * @date 11:33 2021/7/29
    * @param msg
    * @return void
    **/
    private void exec(String msg) throws Exception {
        try {
            Map<String, List<Org>> mmp = super.stringConvertMap(msg, TABLE_NAME, Org.class);
            mmp.forEach((key, value) -> {
                log.info("同步org机构数据{}操作，需要执行：{}条", key, value.size());
                tableOperation(key, value);
                log.info("同步org机构数据{}操作，执行成功：{}条", key, value.size());
            });
        } catch (BusinessException e) {
            log.error(e.getMsg());
        }
    }

    /**
    * @author Andy
    * @description 执行表操作
    * @date 10:19 2021/7/29
    * @param operationType, orgs
    * @return void
    **/
    private void tableOperation (String operationType, List<Org> orgs){
        switch (SqlType.valueOf(operationType)) {
            case INSERT:
            case UPDATE:
                for (Org org : orgs) {
                    orgMapper.replaceInsert(org);
                    log.info("同步org机构数据，"+ operationType +"操作，操作数据：{}", org.toString());
                }
                break;
            case DELETE:
                for (Org org : orgs) {
                    orgMapper.deleteById(org.getId());
                    log.info("同步org机构数据，DELETE操作，操作数据：Id:{}", org.getId());
                }
                break;
            default:
                return;
        }
    }

}
