package com.ruoyi.web.controller.chenyanlogin.timer;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.controller.chenyanlogin.service.HuiCunService;
import com.ruoyi.web.controller.chenyanlogin.service.impl.ZhwlCaller;
import com.ruoyi.web.domain.entity.ZhwlDeviceListEntity;
import com.ruoyi.web.service.impl.ZhwlDeviceListServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@Configuration
@EnableScheduling
public class GetHuivCunTimer {

    @Autowired
    private HuiCunService huiCunService;

    @Resource
    private ZhwlDeviceListServiceImpl zhwlDeviceListService;

    //基础数据查询接口
    @Scheduled(cron = "0 0 * * * ? ")
    public  void getBasicData(){
        huiCunService.getBasicData();

    }
    //关爱人群数据查询接口
    @Scheduled(cron = "0 0 * * * ? ")
    public  void getCarePeopleData(){
        huiCunService.getCarePeopleData();
    }

    //事件上报数据查询接口
    @Scheduled(cron = "0 0 * * * ? ")
    public  void getSnapshotReportData(){
        huiCunService.getSnapshotReportData();
    }

    //一键呼叫数据查询接口
    @Scheduled(cron = "0 0 * * * ? ")
    public  void getZhwlDeviceData(){
        //huiCunService.getZhwlDeviceData();
        ZhwlCaller.DeviceListQueryVO queryVO = new ZhwlCaller.DeviceListQueryVO();
        //全量查询
        queryVO.setFullFlag(1);
        try {
            AjaxResult result = huiCunService.getZhwlDeviceData(queryVO);
            if(!ObjectUtils.isEmpty(result) && "200".equals(result.getOrDefault(AjaxResult.CODE_TAG,0).toString())
                    && !ObjectUtils.isEmpty(result.get(AjaxResult.DATA_TAG))) {
                ZhwlCaller.DeviceRespDTO response = (ZhwlCaller.DeviceRespDTO) result.get(AjaxResult.DATA_TAG);
                if(!ObjectUtils.isEmpty(response.getData())) {
                    BeanCopier copier = BeanCopier.create(ZhwlCaller.DeviceRespDTO.DeviceItemDTO.class, ZhwlDeviceListEntity.class, false);
                    for(ZhwlCaller.DeviceRespDTO.DeviceItemDTO item : response.getData()) {
                        ZhwlDeviceListEntity entity = new ZhwlDeviceListEntity();
                        copier.copy(item,entity,null);
                        if(!ObjectUtils.isEmpty(item.getCreateTime())) {
                            entity.setCreateTime(LocalDateTime.parse(item.getCreateTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                        }
                        if(!ObjectUtils.isEmpty(item.getUpdateTime())) {
                            entity.setUpdateTime(LocalDateTime.parse(item.getUpdateTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                        }
                        zhwlDeviceListService.insertOrUpdate(entity);
                    }
                }
            }
        }catch (Exception e) {
            log.error("定时刷新智慧云平台获取到的设备列表信息处理失败，详细异常信息：{}", e.toString());
        }

    }

    //积分排行数据查询接口
    @Scheduled(cron = "0 0 * * * ? ")
    public  void getPointRankData(){
        huiCunService.getPointRankData();
    }

    //文明红榜数据查询接口
    @Scheduled(cron = "0 0 * * * ? ")
    public  void getCivilizedData(){
        huiCunService.getCivilizedData();
    }

    //先进事迹资讯数据查询接口
    @Scheduled(cron = "0 0 * * * ? ")
    public  void getVillageNewsData(){
        huiCunService.getVillageNewsData();
    }

    //同步人口数据查询接口
    @Scheduled(cron = "0 0 * * * ? ")
    public  void syncVillagerData(){
        huiCunService.syncVillagerData(1);
    }

    //同步房屋数据查询接口
    @Scheduled(cron = "0 0 * * * ? ")
    public  void syncHouseData(){
        huiCunService.syncHouseData(1);
    }

    //同步补贴数据查询接口
    @Scheduled(cron = "0 0 * * * ? ")
    public  void syncAllowanceData(){
        huiCunService.syncAllowanceData(1);
    }


}
