package vip.xiaonuo.Timer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import vip.xiaonuo.common.timer.CommonTimerTaskRunner;
import vip.xiaonuo.modular.energy.energyDevice.entity.EnergyDevice;
import vip.xiaonuo.modular.energy.energyDevice.mapper.EnergyDeviceMapper;
import vip.xiaonuo.modular.energy.energyElectricity.entity.EnergyElectricity;
import vip.xiaonuo.modular.energy.energyElectricity.mapper.EnergyElectricityMapper;
import vip.xiaonuo.modular.energy.energyWater.entity.EnergyWater;
import vip.xiaonuo.modular.energy.energyWater.mapper.EnergyWaterMapper;
import vip.xiaonuo.modular.env.envdevice.entity.EnvDevice;

import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class ProEnergyData implements CommonTimerTaskRunner {

    @Autowired
    private EnergyDeviceMapper deviceMapper;
    @Autowired
    private EnergyElectricityMapper electricityMapper;
    @Autowired
    private EnergyWaterMapper waterMapper;

    @Override
    public void action() {
        QueryWrapper<EnergyDevice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("org_id", "1543842934270394370").eq("delete_flag", "NOT_DELETE")
                .eq("status", 1);
        List<EnergyDevice> energyDevices = deviceMapper.selectList(queryWrapper);
        for (EnergyDevice energyDevice : energyDevices) {
            String waterValue = NumberUtils.getNumFloat(5.0, 20.0);
            String electricityValue = NumberUtils.getNumFloat(10.0, 100.0);

            EnergyWater water = new EnergyWater();
            water.setOrgId(energyDevice.getOrgId());
            water.setUnitId(energyDevice.getInstallPosition());
            water.setDeviceCode(energyDevice.getDeviceCode());
            water.setCreateDate(new Date());
            water.setOriginalDate(new Date());
            water.setWaterValue(waterValue);
            water.setOriginalValue(waterValue);

            EnergyElectricity electricity = new EnergyElectricity();
            electricity.setOrgId(energyDevice.getOrgId());
            electricity.setUnitId(energyDevice.getInstallPosition());
            electricity.setDeviceCode(energyDevice.getDeviceCode());
            electricity.setCreateDate(new Date());
            electricity.setOriginalDate(new Date());
            electricity.setElectricityValue(electricityValue);
            electricity.setOriginalValue(electricityValue);

            electricityMapper.insert(electricity);
            waterMapper.insert(water);
        }
    }
}
