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

import cn.com.tenlion.systembase.pojo.dtos.population.PopulationDTO;
import cn.com.tenlion.systembase.pojo.dtos.school.SchoolDTO;
import cn.com.tenlion.systembase.pojo.dtos.schoolkey.SchoolKeyDTO;
import cn.com.tenlion.systembase.pojo.vos.schoolkey.SchoolKeyVO;
import cn.com.tenlion.systembase.remote.populationmsg.IPopulationMsgService;
import cn.com.tenlion.systembase.service.school.ISchoolService;
import cn.com.tenlion.systembase.service.schoolkey.ISchoolKeyService;
import com.alibaba.fastjson.JSONObject;
import ink.wgink.exceptions.SearchException;
import ink.wgink.module.oauth2.manager.OAuth2ClientTokenManager;
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 FullSyncC0016 implements ApplicationEventPublisherAware {

    private ApplicationEventPublisher applicationEventPublisher;
    @Autowired
    private ISchoolKeyService schoolKeyService;
    @Autowired
    private ISchoolService schoolService;
    @Autowired
    private IPopulationMsgService populationMsgService;
    @Autowired
    KafkaTemplate kafkaTemplate;

    public FullSyncC0016() {}

    @KafkaListener(topics = "C0016FullSync")
    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<SchoolKeyDTO> schoolKeyDTOList = schoolKeyService.list(params);
        System.out.println(schoolKeyDTOList.size());
        if(null != schoolKeyDTOList && schoolKeyDTOList.size() > 0) {
            for(SchoolKeyDTO schoolKeyDTO: schoolKeyDTOList) {
                SchoolDTO schoolDTO = schoolService.get(schoolKeyDTO.getSchoolId());
                SchoolKeyVO schoolKeyVO = setData(schoolKeyDTO, schoolDTO);
                schoolKeyService.saveKafka(schoolKeyDTO.getSchoolKeyId(), schoolKeyVO, schoolDTO);
            }
        }
    }

    @KafkaListener(topics = "C0016")
    public void messageMsg(ConsumerRecord<?, ?> record) {
        JSONObject jsonObject = JSONObject.parseObject(record.value().toString());
        Map<String, Object> params = new HashMap<>();
        if(!"delete".equals(jsonObject.getString("action"))) {
            SchoolKeyDTO schoolKeyDTO = schoolKeyService.get(jsonObject.getString("uid"));
            SchoolDTO schoolDTO = schoolService.get(schoolKeyDTO.getSchoolId());
            SchoolKeyVO schoolKeyVO = setData(schoolKeyDTO, schoolDTO);
            schoolKeyService.saveKafka(schoolKeyDTO.getSchoolKeyId(), schoolKeyVO, schoolDTO);
        }else {
            schoolKeyService.deleteKafka(jsonObject.getString("uid"));
        }
    }

    private SchoolKeyVO setData(SchoolKeyDTO schoolKeyDTO, SchoolDTO schoolDTO) {
        if(null == schoolKeyDTO) {
            throw new SearchException("当前对象为空,请注意查看");
        }
        if(null == schoolDTO) {
            throw new SearchException("当前对象为空,请注意查看");
        }
        SchoolKeyVO schoolKeyVO = new SchoolKeyVO();
        schoolKeyVO.setSchoolName(schoolDTO.getSchoolName());
        PopulationDTO populationDTO = getByIdCardNumber(schoolKeyDTO.getBaseId());
        schoolKeyVO.setFullName(populationDTO.getFullName());
        schoolKeyVO.setNameUsedBefore(populationDTO.getNameUsedBefore());
        schoolKeyVO.setGender(populationDTO.getGenderDictionaryName());
        schoolKeyVO.setDegreeOfHarmName(schoolKeyDTO.getDegreeOfHarmName());
        schoolKeyVO.setIsFollow(schoolKeyDTO.getIsFollow());
        schoolKeyVO.setLocationCode(schoolKeyDTO.getLocationCode());
        schoolKeyVO.setLongitude(schoolKeyDTO.getLongitude());
        schoolKeyVO.setLatitude(schoolKeyDTO.getLatitude());
        schoolKeyVO.setGrid(schoolKeyDTO.getGrid());
        return schoolKeyVO;
    }

    public PopulationDTO getByIdCardNumber(String cardNumber) {
        String url = "http://10.25.242.190:37210/population";
        Map<String, Object> params = new HashMap<>();
        params.put("idCardNumber", cardNumber);
        System.out.println("cardNumber = " + cardNumber);
        String accessToken = OAuth2ClientTokenManager.getInstance().getToken().getAccessToken();
        PopulationDTO populationDTO = populationMsgService.getBaseInfo(url, params, accessToken);
        System.out.println("populationDTO = " + populationDTO.toString());
        return populationDTO;
    }

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

}