package com.zcloud.task;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zcloud.common.utils.DateUtil;
import com.zcloud.modules.job.task.ITask;
import com.zcloud.modules.mkmjDoor.entity.MkmjDoorEntity;
import com.zcloud.modules.mkmjDoor.mapper.MkmjDoorMapper;
import com.zcloud.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.*;

/**
 * 口门数据对接  每天1:00更新
 */
@Component("doorSaveTask")
public class DoorSaveTask implements ITask {
    @Value("${perLoc.url}")
    private String url;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private MkmjDoorMapper mkmjDoorMapper;

    @Override
    public void run(String params) {
        try {
            scheduled();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

//    @Scheduled(cron ="*/30 * * * * ?")//测试
    // @Scheduled(cron ="0 0 1 * * ?")//每天凌晨一点执行一次
    public void scheduled() throws Exception {;
        List<MkmjDoorEntity> allList = new ArrayList<>();
        String type = "0";
        Map<String,Object> result = restTemplate.getForObject(url+"/region/access/obtainBayonetData?type="+type, Map.class);
        Object msg = result.get("msg");
        if (msg!=null && msg.equals("success")){
            ArrayList<LinkedHashMap<String,Object>> data = (ArrayList<LinkedHashMap<String,Object>>)result.get("data");
            for (LinkedHashMap<String,Object> linkedHashMap : data){
                MkmjDoorEntity mkmjDoorEntity = new MkmjDoorEntity();
                mkmjDoorEntity.setDoorId(UuidUtil.get32UUID());
                mkmjDoorEntity.setDooreType(Integer.valueOf(linkedHashMap.get("type").toString()));
                mkmjDoorEntity.setSTATUS(0);
                mkmjDoorEntity.setDoorName(linkedHashMap.get("name").toString());
                mkmjDoorEntity.setLONGITUDE(linkedHashMap.get("longitude").toString());
                mkmjDoorEntity.setLATITUDE(linkedHashMap.get("latitude").toString());
                mkmjDoorEntity.setSourceId(linkedHashMap.get("id").toString());
                mkmjDoorEntity.setCorpinfoId("8854edee3aa94be496cee676b6d4845a"); // 添加企业ID
                mkmjDoorEntity.setCREATOR( "5c61afed41bc4c25acf77aed1391e666"); // 添加人
                mkmjDoorEntity.setCREATTIME( DateUtil.date2Str(new Date())); // 添加时间
                mkmjDoorEntity.setISDELETE("0");	//是否删除
                mkmjDoorEntity.setOPERATOR( "5c61afed41bc4c25acf77aed1391e666");	//修改人
                mkmjDoorEntity.setOPERATTIME( DateUtil.date2Str(new Date()));//修改时间
                allList.add(mkmjDoorEntity);
            }
        }
        type = "1";
        Map<String,Object> result2 = restTemplate.getForObject(url+"/region/access/obtainBayonetData?type="+type, Map.class);
        Object msg2 = result2.get("msg");
        if (msg2!=null && msg2.equals("success")){
            ArrayList<LinkedHashMap<String,Object>> data = (ArrayList<LinkedHashMap<String,Object>>)result2.get("data");
            for (LinkedHashMap<String,Object> linkedHashMap : data){
                MkmjDoorEntity mkmjDoorEntity = new MkmjDoorEntity();
                mkmjDoorEntity.setDoorId( UuidUtil.get32UUID());
                mkmjDoorEntity.setDooreType(Integer.valueOf(linkedHashMap.get("type").toString()));
                mkmjDoorEntity.setSTATUS(0);
                mkmjDoorEntity.setDoorName(linkedHashMap.get("name").toString());
                mkmjDoorEntity.setLONGITUDE(linkedHashMap.get("longitude").toString());
                mkmjDoorEntity.setLATITUDE(linkedHashMap.get("latitude").toString());
                mkmjDoorEntity.setSourceId(linkedHashMap.get("id").toString());
                mkmjDoorEntity.setCorpinfoId("8854edee3aa94be496cee676b6d4845a"); // 添加企业ID
                mkmjDoorEntity.setCREATOR( "5c61afed41bc4c25acf77aed1391e666"); // 添加人
                mkmjDoorEntity.setCREATTIME( DateUtil.date2Str(new Date())); // 添加时间
                mkmjDoorEntity.setISDELETE("0");	//是否删除
                mkmjDoorEntity.setOPERATOR( "5c61afed41bc4c25acf77aed1391e666");	//修改人
                mkmjDoorEntity.setOPERATTIME( DateUtil.date2Str(new Date()));//修改时间
                boolean flag = true;
                for (MkmjDoorEntity p : allList) {
                    if (p.getDoorName().equals(mkmjDoorEntity.getDoorName())){
                        flag = false;
                    }
                }
                if (flag) {
                    allList.add(mkmjDoorEntity);
                }
            }
        }
        LambdaQueryWrapper<MkmjDoorEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MkmjDoorEntity::getCorpinfoId,"8854edee3aa94be496cee676b6d4845a");
        List<MkmjDoorEntity> dataList = mkmjDoorMapper.selectList(lambdaQueryWrapper);
        List<MkmjDoorEntity> addList = new ArrayList<>();
        ArrayList<MkmjDoorEntity> updateList = new ArrayList<>();
        addList.addAll(allList);
        if (dataList.size()>0) {
            for (MkmjDoorEntity mkmjDoorEntity : dataList) {
                for (MkmjDoorEntity data : allList) {
                    if (mkmjDoorEntity.getSourceId().equals(data.getSourceId()) && mkmjDoorEntity.getDoorName().equals(data.getDoorName())) {
                        updateList.add(data);
                        addList.remove(data);
                        continue;
                    }
                }
            }
        }
        if (addList.size()>0) {
            mkmjDoorMapper.insert(addList);
        }
        if (updateList.size()>0) {
            for(MkmjDoorEntity mkmjDoorEntity : updateList){
                LambdaUpdateWrapper<MkmjDoorEntity> updateWrapper = new LambdaUpdateWrapper<>();
                updateWrapper.eq(MkmjDoorEntity::getSourceId,mkmjDoorEntity.getSourceId());
                mkmjDoorMapper.update(mkmjDoorEntity,updateWrapper);
            }
        }
    }


}
