package org.well.well.manager;

import org.well.well.base.DeviceTypeEnum;
import org.well.well.core.hibernate.HibernateEntityDao;
import org.well.well.core.util.DateUtils;
import org.well.well.domain.Device;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class DeviceManager extends HibernateEntityDao<Device> {


    public Criteria getCriteria() {
        return getSession().createCriteria(Device.class);
    }


    public Device getDeviceByDevCode(String devcode) {
        try {
            Criteria criteria = getSession().createCriteria(Device.class);
            criteria.add(Restrictions.eq("devCode", devcode));
            criteria.add(Restrictions.eq("valid", true));
            List<Device> devices = criteria.list();
            if (devices != null && devices.size() > 0)
                return devices.get(0);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public void clearOffline(String devCode) {//查找设备状态（1：在线，0：离线）

        try {
            Criteria criteria = getSession().createCriteria(Device.class);
            criteria.add(Restrictions.eq("devCode", devCode));
            criteria.add(Restrictions.eq("valid", true));
            criteria.add(Restrictions.eq("onlineState", "0"));
            List<Device> devices = criteria.list();
            for (Device device : devices) {
                device.setOnlineState("1");
                device.setLogtime(new Date());
                save(device);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void setOfflineBySevenNoData(String devCode) {
        Criteria criteria = getSession().createCriteria(Device.class);
            criteria.add(Restrictions.eq("devCode", devCode));
            criteria.add(Restrictions.eq("valid", true));
            criteria.add(Restrictions.eq("onlineState", "1"));
            List<Device> devices = criteria.list();
            for (Device device : devices) {
                device.setOnlineState("0");
                device.setLogtime(new Date());
                save(device);
        }
    }

    public void findDeviceDataInSevenDays(Device device, String sql) {
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("devCode", device.getDevCode());
            paramMap.put("beg", DateUtils.sdf_day.format(DateUtils.getSevenBeforeDate()));
            List<Object[]> list = getSession().createSQLQuery(sql).setProperties(paramMap).list();
            if (list==null||list.size() == 0) {
                setOfflineBySevenNoData(device.getDevCode());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void updateDeviceStatus() {
        Criteria criteria = getSession().createCriteria(Device.class);
        criteria.add(Restrictions.eq("valid", true));
        criteria.add(Restrictions.eq("onlineState", "1"));
        List<Device> deviceList = criteria.list();
        if (deviceList.size() > 0) {
            for (Device device : deviceList) {
                String devTypeName = device.getDeviceType().getTypeName();
                String sql = "";
                if (devTypeName.equals(DeviceTypeEnum.Liquid.toString())) {
                    sql = "select * from data_liquid " +
                            " where date_format(UPTIME,'%Y-%m-%d')>=:beg and" +
                            " DEVCODE=:devCode";
                    findDeviceDataInSevenDays(device, sql);
                } else if (devTypeName.equals(DeviceTypeEnum.Well.toString())) {
                    sql = "select * from data_wellcover " +
                            " where date_format(LOGTIME,'%Y-%m-%d')>=:beg and" +
                            " DEVCODE=:devCode";
                    findDeviceDataInSevenDays(device, sql);
                } else if (devTypeName.equals(DeviceTypeEnum.Methane.toString())){
                    sql = "select * from data_gas " +
                            " where date_format(UPTIME,'%Y-%m-%d')>=:beg and" +
                            " DEVCODE=:devCode";
                    findDeviceDataInSevenDays(device, sql);
                } else if (devTypeName.equals(DeviceTypeEnum.WasteGas.toString())) {
                    sql = "select * from data_harmful_gas " +
                            " where date_format(UPTIME,'%Y-%m-%d')>=:beg and" +
                            " DEVCODE=:devCode";
                    findDeviceDataInSevenDays(device, sql);
                } else if (devTypeName.equals(DeviceTypeEnum.Methane.toString())) {
                    sql = "select * from data_gas " +
                            " where date_format(UPTIME,'%Y-%m-%d')>=:beg and" +
                            " DEVCODE=:devCode";
                    findDeviceDataInSevenDays(device, sql);
                } else if (devTypeName.equals(DeviceTypeEnum.NoiseDig.toString())) {
                    sql = "select * from data_dig " +
                            " where date_format(UPTIME,'%Y-%m-%d')>=:beg and" +
                            " DEVCODE=:devCode";
                    findDeviceDataInSevenDays(device, sql);
                } else if (devTypeName.equals(DeviceTypeEnum.TempHumi.toString())) {
                    sql = "select * from data_temp_hum " +
                            " where date_format(UPTIME,'%Y-%m-%d')>=:beg and" +
                            " DEVCODE=:devCode";
                    findDeviceDataInSevenDays(device, sql);
                }else if(devTypeName.equals(DeviceTypeEnum.Meter.toString())){
                    sql = "select * from data_meter " +
                            " where date_format(UPTIME,'%Y-%m-%d')>=:beg and" +
                            " DEVCODE=:devCode";
                    findDeviceDataInSevenDays(device, sql);
                }else if(devTypeName.equals(DeviceTypeEnum.FLOWGAUGE.toString())){
                    sql = "select * from data_flowmeter " +
                            " where date_format(logtime,'%Y-%m-%d')>=:beg and" +
                            " DEVCODE=:devCode";
                    findDeviceDataInSevenDays(device, sql);
                }else if(devTypeName.equals(DeviceTypeEnum.RAINGAUGE.toString())){
                    sql = "select * from data_hyetometer " +
                            " where date_format(LOGTIME,'%Y-%m-%d')>=:beg and" +
                            " DEVCODE=:devCode";
                    findDeviceDataInSevenDays(device, sql);
                }
            }
        }
    }
}