package jnpf.attendance.event;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import jnpf.attendance.service.AttendanceUserService;
import jnpf.constants.MessageTopicConstants;
import jnpf.message.model.permission.PermissionRelationOrganizeUserListDTO;
import jnpf.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 组织下人员变更
 */
@Component
@RocketMQMessageListener(
        topic = MessageTopicConstants.PERMISSION_TOPIC,
        consumerGroup = MessageTopicConstants.JNPF_USER_GROUP,
        selectorExpression = MessageTopicConstants.ORGANIZE_RELATION_USER,
        replyTimeout = 600000,
        maxReconsumeTimes = 3,
        nameServer = "${spring.cloud.stream.rocketmq.binder.name-server}"
)
@Slf4j
public class OrganizeUserConsumerMQListener implements RocketMQListener<String> {
    @Autowired
    private AttendanceUserService attendanceUserService;

    @Override
    public void onMessage(String message) {
        if (StringUtil.isEmpty(message)) {
            return;
        }
        log.error("接受组织下人员(批量入职/批量删除/预入职/离职)变更信息{}", message);
        try {
            List<PermissionRelationOrganizeUserListDTO> messageList = JSON.parseArray(message, PermissionRelationOrganizeUserListDTO.class);
            if (CollUtil.isEmpty(messageList) || StringUtil.isEmpty(messageList.get(0).getUserId())) {
                log.error("接受组织下人员无效数据过滤{}", message);
                return;
            }
            //增删改
            attendanceUserService.orgUpdateHandle(messageList);
        } catch (Exception e) {
            log.error("消息解析异常: message={}", message, e);
        }

    }

}
