package com.bz.rmcp.dap;
import java.util.Date;
import java.util.*;

import com.alibaba.fastjson.JSON;
import com.zcxk.rmcp.api.dto.readmeter.MeterData;

import com.bz.rmcp.dap.model.DeviceOrigDataDTO;
import com.bz.rmcp.dap.service.impl.DeviceDataServiceImpl;
import com.zcxk.rmcp.core.mongo.MeterReadRecord;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @author linqingwei
 * @date 2021-08-03 18:16
 */
@EnableCaching  //开启缓存
@RunWith(SpringRunner.class)
@SpringBootTest
@ComponentScan(basePackages = {"com.zcxk","com.bz"})
@MapperScan("com.zcxk.rmcp.core.dao")
public class DeviceDataServiceTests {

    @Autowired
    private DeviceDataServiceImpl deviceDataService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MongoTemplate mongoTemplate;

    @Test
    public void DeviceDataTest(){



        Map<String,Object> param = new HashMap<>();
        param.put("currentQuantity",107.4);
        param.put("valveState","1");
        param.put("temp","0");

        Map<Date,String> map = new HashMap<>();
        map.put(new Date(),JSON.toJSONString(param));

        DeviceOrigDataDTO dto = new DeviceOrigDataDTO();
        dto.setId(1L);
        dto.setAgentIdentifier("");
        dto.setDeviceNo("11111111111");
        dto.setMode("");
        dto.setType("");
        dto.setManufacturer("");
        dto.setOriginalDataFormat("");
        dto.setOriginalData("");
        dto.setParsedData(map);
        dto.setEventTime(new Date());
        dto.setReceiveTime(new Date());


        deviceDataService.saveDeviceData(dto);
    }


    @Test
    public void redis(){
        MeterData data = new MeterData();
        data.setReadData(10.5);
        data.setReceiveDate(new Date());

        redisTemplate.opsForValue().set("test:111",data);

        MeterData data1 = (MeterData) redisTemplate.opsForValue().get("test:222");
        System.out.println(data1);
    }

    @Test
    public void buildMeterReadData(){
        MeterReadRecord readRecord = new MeterReadRecord();
        readRecord.setYear(0);
        readRecord.setTenantId("1");
        readRecord.setCategoryId(1);
        readRecord.setCompanyOrgId(1);
        readRecord.setCompanyOrgName("");
        readRecord.setDeptOrgId(1);
        readRecord.setDeptOrgName("");
        readRecord.setCommunityId(1);
        readRecord.setCommunityName("");
        readRecord.setConcentratorId(0);
        readRecord.setConcentratorCode("");
        readRecord.setCollectorId(0);
        readRecord.setCollectorCode("");
        readRecord.setDeviceId(1L);
        readRecord.setDeviceNo("11111");
        readRecord.setLocation("11111");
        readRecord.setProductId(1);
        readRecord.setDeviceModel("");
        readRecord.setChannelNumber("");
        readRecord.setMeterNo("11111");
        readRecord.setFileNo("11111");

        readRecord.setCreateTime(new Date());
        readRecord.setCreatorName("system");
        readRecord.setUpdateTime(new Date());
        List<MeterReadRecord.MeterReadInfo> list = new ArrayList<>();
        MeterReadRecord.MeterReadInfo info1 = new MeterReadRecord.MeterReadInfo();
        info1.setReadDate(20210801);
        info1.setReadTime(new Date().getTime());
        info1.setValveStatus(1);
        info1.setReadData(1.5D);
        info1.setLastValid(1.4D);
        info1.setLastCost(0.1D);
        list.add(info1);

        MeterReadRecord.MeterReadInfo info2 = new MeterReadRecord.MeterReadInfo();
        info2.setReadDate(20210805);
        info2.setReadTime(new Date().getTime());
        info2.setValveStatus(1);
        info2.setReadData(2.6D);
        info2.setLastValid(1.5D);
        info2.setLastCost(1.1D);
        list.add(info2);


        readRecord.setData(list);
        mongoTemplate.insert(readRecord,"rmcp_meter_read_record");
    }

    @Test
    public void updateMeterReadData(){
        Query query = new Query();
        query.addCriteria(Criteria.where("deviceId").is(1L));
        query.addCriteria(Criteria.where("data.readDate").is(20210805));
        Update update = new Update();
        update.set("data.$.readTime",new Date());
        update.set("data.$.readData",3.6D);
        update.set("data.$.valveStatus",1);
        update.set("data.$.lastValid",3.6D);
        update.inc("data.$.lastCost", 0.1);
        long result = mongoTemplate.updateMulti(query,update,"rmcp_meter_read_record").getModifiedCount();
        System.out.println(result);
    }

}
