package cn.com.tenlion.systembase.util.fullsync;

import cn.com.tenlion.systembase.pojo.dtos.keyareacheckrenovation.KeyAreaCheckRenovationDTO;
import cn.com.tenlion.systembase.pojo.dtos.keyareacheckrenovationpatrol.KeyAreaCheckRenovationPatrolDTO;
import cn.com.tenlion.systembase.pojo.vos.keyareacheckrenovationpatrol.KeyAreaCheckRenovationPatrolVO;
import cn.com.tenlion.systembase.service.keyareacheckrenovation.IKeyAreaCheckRenovationService;
import cn.com.tenlion.systembase.service.keyareacheckrenovationpatrol.IKeyAreaCheckRenovationPatrolService;
import com.alibaba.fastjson.JSONObject;
import ink.wgink.exceptions.SearchException;
import ink.wgink.util.map.HashMapUtil;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

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

/**
 * 表单全量数据同步
 * 接收数据整合系统发来的全量数据同步命令, 向数据整合系统发送表单的全部数据
 */
@Component
public class FullSyncC0019 implements ApplicationEventPublisherAware {

    private ApplicationEventPublisher applicationEventPublisher;
    @Autowired
    private IKeyAreaCheckRenovationPatrolService keyAreaCheckRenovationPatrolService;
    @Autowired
    private IKeyAreaCheckRenovationService keyAreaCheckRenovationService;
    @Autowired
    KafkaTemplate kafkaTemplate;

    public FullSyncC0019() {}

    @KafkaListener(topics = "C0019FullSync")
    public void message(ConsumerRecord<?, ?> record) {
        System.out.println("收到全量同步命令");
        JSONObject jsonObject = JSONObject.parseObject(record.value().toString());
        System.out.println(jsonObject.toJSONString());// 表单编号
        System.out.println(jsonObject.getString("tableNumber"));// 表单编号
        System.out.println(jsonObject.getInteger("tableDataCount"));// 整合系统数据量
        /**
         * 1 . 查询本地数据的数量 , 如果本地数据量 > 整合系统数据量
         * 2 . 查询数据库中所有的事件列表
         * 3 . 遍历事件, 封装数据
         * 4 . 发送新增案件消息
         */
        Map<String, Object> params = new HashMap<>();
        List<KeyAreaCheckRenovationPatrolDTO> keyAreaCheckRenovationPatrolDTOList = keyAreaCheckRenovationPatrolService.list(params);
        System.out.println(keyAreaCheckRenovationPatrolDTOList.size());
        if(null != keyAreaCheckRenovationPatrolDTOList && keyAreaCheckRenovationPatrolDTOList.size() > 0) {
            for(KeyAreaCheckRenovationPatrolDTO keyAreaCheckRenovationPatrolDTO: keyAreaCheckRenovationPatrolDTOList) {
                params = HashMapUtil.beanToMap(keyAreaCheckRenovationPatrolDTO);
                KeyAreaCheckRenovationDTO keyAreaCheckRenovationDTO = keyAreaCheckRenovationService.get(keyAreaCheckRenovationPatrolDTO.getKeyAreaCheckRenovationId());
                KeyAreaCheckRenovationPatrolVO keyAreaCheckRenovationPatrolVO = setData(keyAreaCheckRenovationPatrolDTO, keyAreaCheckRenovationDTO);
                String creator = keyAreaCheckRenovationPatrolService.getUserData(params, keyAreaCheckRenovationPatrolVO);
                keyAreaCheckRenovationPatrolService.saveKafka(keyAreaCheckRenovationPatrolDTO.getKeyAreaCheckRenovationPatrolId(), keyAreaCheckRenovationPatrolVO, creator, keyAreaCheckRenovationDTO);
            }
        }
    }

    @KafkaListener(topics = "C0019")
    public void messageMsg(ConsumerRecord<?, ?> record) {
        JSONObject jsonObject = JSONObject.parseObject(record.value().toString());
        Map<String, Object> params;
        if(!"delete".equals(jsonObject.getString("action"))) {
            KeyAreaCheckRenovationPatrolDTO keyAreaCheckRenovationPatrolDTO = keyAreaCheckRenovationPatrolService.get(jsonObject.getString("uid"));
            params = HashMapUtil.beanToMap(keyAreaCheckRenovationPatrolDTO);
            KeyAreaCheckRenovationDTO keyAreaCheckRenovationDTO = keyAreaCheckRenovationService.get(keyAreaCheckRenovationPatrolDTO.getKeyAreaCheckRenovationId());
            KeyAreaCheckRenovationPatrolVO keyAreaCheckRenovationPatrolVO = setData(keyAreaCheckRenovationPatrolDTO, keyAreaCheckRenovationDTO);
            String creator = keyAreaCheckRenovationPatrolService.getUserData(params, keyAreaCheckRenovationPatrolVO);
            keyAreaCheckRenovationPatrolService.saveKafka(keyAreaCheckRenovationPatrolDTO.getKeyAreaCheckRenovationPatrolId(), keyAreaCheckRenovationPatrolVO, creator, keyAreaCheckRenovationDTO);
        }else {
            keyAreaCheckRenovationPatrolService.deleteKafka(jsonObject.getString("uid"));
        }
    }

    private KeyAreaCheckRenovationPatrolVO setData(KeyAreaCheckRenovationPatrolDTO keyAreaCheckRenovationPatrolDTO, KeyAreaCheckRenovationDTO keyAreaCheckRenovationDTO) {
        if(null == keyAreaCheckRenovationPatrolDTO) {
            throw new SearchException("当前对象为空,请注意查看");
        }
        if(null == keyAreaCheckRenovationDTO) {
            throw new SearchException("当前对象为空,请注意查看");
        }
        KeyAreaCheckRenovationPatrolVO keyAreaCheckRenovationPatrolVO = new KeyAreaCheckRenovationPatrolVO();
        keyAreaCheckRenovationPatrolVO.setKeyAreaCheckRenovationName(keyAreaCheckRenovationDTO.getSecurityArea());
        keyAreaCheckRenovationPatrolVO.setPatrolTime(keyAreaCheckRenovationPatrolDTO.getPatrolTime());
        return keyAreaCheckRenovationPatrolVO;
    }

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }

}