package cn.com.victorysoft.business.indicator.service;

import cn.com.victorysoft.business.indicator.dao.PumpDeviceDao;
import cn.com.victorysoft.business.indicator.entity.Pump;
import cn.com.victorysoft.business.indicator.entity.PumpDevice;
import cn.com.victorysoft.business.indicator.entity.WaterStation;
import cn.com.victorysoft.business.indicator.param.PumpDeviceParam;
import cn.com.victorysoft.business.indicator.param.PumpParam;
import cn.com.victorysoft.business.indicator.param.WaterStationParam;
import cn.com.victorysoft.business.util.PageUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class PumpDeviceService {

    @Resource
    private PumpDeviceDao pumpDeviceDao;

    public PageInfo<PumpDevice> selectPage(PumpDeviceParam param) {
        PageUtils.startPage(param);
        return new PageInfo<>(this.selectList(param));
    }

    public List<PumpDevice> selectList(PumpDeviceParam param) {
        List<PumpDevice> pumpDevice = pumpDeviceDao.selectList(param);
        for (PumpDevice device : pumpDevice) {
            if (device.getRunTime() == null) continue;
            if (device.getRunTime() > 0) {
                device.setUnitUmber("开");
            } else {
                device.setUnitUmber("关");
            }
        }
        return pumpDevice;
    }

    public PumpDevice selectPumpDevice(PumpDevice param) {
        return pumpDeviceDao.selectPumpDevice(param);
    }

    public int insert(PumpDevice param) {
        // 计算值
        this.calcValue(param);
        param.setCreateOn(new Date());
        return pumpDeviceDao.insert(param);
    }

    public List<WaterStation> selectWaterStations(WaterStationParam param) {
        return pumpDeviceDao.selectWaterStations(param);
    }

    public List<Pump> selectPump(PumpParam param) {
        return pumpDeviceDao.selectPump(param);
    }

    public int updateData(PumpDevice param) {
        // 计算值
        this.calcValue(param);
        return pumpDeviceDao.updateData(param);
    }

    /**
     * 计算值
     */
    public void calcValue(PumpDevice device) {
        // 有效功率=【1000*排量（m³/h）*（出口压力-进口压力）Mpa*100】/3600*102
        if (device.getVolumeHour() != null && device.getPressureIn() != null && device.getPressureOut() != null) {
            device.setEffectivePower(1000 * device.getVolumeHour() * (device.getPressureOut() - device.getPressureIn()) * 100 / 3600 * 102);
        }
        // 输入功率=1.732*电流*电压*功率因数
        if (device.getVoltage() != null && device.getElectricCurrent() != null && device.getPowerFactor() != null) {
            device.setPowerIn(1.732 * device.getElectricCurrent() * device.getVoltage() * device.getPowerFactor());
        }
        // 输出功率=输入功率*效率
        if (device.getPowerIn() != null && device.getEfficiency() != null) {
            device.setPowerOut(device.getPowerIn() * device.getEfficiency());
        }
        // 注水泵效率=有效功率/输出功率
        if (device.getEffectivePower() != null && device.getPowerOut() != null && device.getPowerOut() != 0) {
            device.setPumpEfficiency(device.getEffectivePower() / device.getPowerOut());
        }
        // 耗电=输入功率/小时排量
        if (device.getPowerIn() != null && device.getVolumeHour() != null && device.getVolumeHour() != 0) {
            device.setPowerUsed(device.getPowerIn() / device.getVolumeHour());
        }
        // 10MPa耗电=耗电/分水器压力*10
        if (device.getPowerUsed() != null && device.getPressureSplit() != null && device.getPressureSplit() != 0) {
            device.setPowerUsed10(device.getPowerUsed() / device.getPressureSplit() * 10);
        }
    }
}
