package com.casic.PgInterface.devTable.manager;

import com.casic.PgInterface.core.hibernate.HibernateEntityDao;
import com.casic.PgInterface.devTable.domain.PgDevice;
import com.casic.PgInterface.devTable.dto.PgDeviceDto;
import com.casic.PgInterface.devTable.dto.PgDeviceTypeStatisticDto;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yxw on 2017/10/30.
 */
@Service
public class PgDeviceManager extends HibernateEntityDao<PgDevice> {

    //获取当前设备列表
    public List<PgDeviceDto> getAllDevice(String road,String partition,String devType) {

        Criteria criteria = createCriteria(PgDevice.class);

        if(road!=null)
            criteria.add(Restrictions.eq("road",road));
        if(partition!=null)
            criteria.add(Restrictions.eq("partition",partition));
        if(devType!=null)
            criteria.add(Restrictions.eq("devType",devType));

        criteria.add(Restrictions.eq("active", 1));
        criteria.addOrder(Order.desc("id"));
        List<PgDevice> pgDeviceList = criteria.list();

        List<PgDeviceDto> pgDeviceDtoList = new ArrayList<>();
        for (PgDevice pgDevice : pgDeviceList) {
            pgDeviceDtoList.add(new PgDeviceDto(pgDevice));
        }

        return pgDeviceDtoList;
    }

    //根据名字获取当前设备
    public PgDevice getDeviceByAssetCode(String assetCode) {
        Criteria criteria=createCriteria(PgDevice.class);
        criteria.add(Restrictions.eq("assetCode",assetCode));

        List<PgDevice> pgDeviceList=criteria.list();
        if(pgDeviceList.size()==0)
            return null;
        else
            return pgDeviceList.get(0);
    }

    //根据类型获取设备列表
    public List<PgDeviceDto> getDeviceByDevTypee(String devType) {
        Criteria criteria=createCriteria(PgDevice.class);
        criteria.add(Restrictions.eq("devType",devType));

        List<PgDevice> pgDeviceList=criteria.list();
        List<PgDeviceDto> pgDeviceDtoList = new ArrayList<PgDeviceDto>();

        for (PgDevice pgDevice : pgDeviceList) {
            pgDeviceDtoList.add(new PgDeviceDto(pgDevice));
        }
        return pgDeviceDtoList;
    }

    //根据设备编号获取当前设备的信息
    public PgDevice getDeviceBydevCode(String devCode) {
        Criteria criteria=createCriteria(PgDevice.class);
        criteria.add(Restrictions.eq("devCode",devCode));

        List<PgDevice> pgDeviceList=criteria.list();
        if(pgDeviceList.size()==0)
            return null;
        else
            return pgDeviceList.get(0);
    }

    //根据设备类型统计设备资产
    public List<PgDeviceTypeStatisticDto> getDeviceStatisticByDevType(){
        String sql="select device.DEVTYPE,count(*) from PG_DEVICE device group by device.DEVTYPE";

        List<Object[]> objectList=this.getSession().createSQLQuery(sql).list();

        List<PgDeviceTypeStatisticDto> pgDeviceTypeStatisticDtoList = new ArrayList<PgDeviceTypeStatisticDto>();

        for(Object[] objectArray : objectList)
        {
            PgDeviceTypeStatisticDto pgDeviceTypeStatisticDto=new PgDeviceTypeStatisticDto();
            pgDeviceTypeStatisticDto.setDevType(String.valueOf(objectArray[0]));
            pgDeviceTypeStatisticDto.setNum(String.valueOf(objectArray[1]));
            pgDeviceTypeStatisticDtoList.add(pgDeviceTypeStatisticDto);
        }

        return pgDeviceTypeStatisticDtoList;
    }

    //根据分区获取设备列表
    public List<PgDeviceDto> getPgDeviceDtoListByPartition(String partition) {
        Criteria criteria=createCriteria(PgDevice.class);
        criteria.add(Restrictions.eq("partition",partition));

        List<PgDevice> pgDeviceList=criteria.list();
        List<PgDeviceDto> pgDeviceDtoList = new ArrayList<PgDeviceDto>();

        for (PgDevice pgDevice : pgDeviceList) {
            pgDeviceDtoList.add(new PgDeviceDto(pgDevice));
        }
        return pgDeviceDtoList;
    }

    public List<PgDeviceDto> getDeviceByOldDAssetCodeCode(String assetCode) {
        String[] stringList=assetCode.split("\\.");

        String hql="select * from PG_DEVICE device where device.assetCode like '%"+stringList[1]+"%' and device.assetCode like '%"+stringList[2]+"'";

        List<Object[]> pgDevices=(List<Object[]>)this.getSession().createSQLQuery(hql).list();

        List<PgDeviceDto> pgDeviceDtoList = new ArrayList<PgDeviceDto>();

        for (int i = 0; i<pgDevices.size();i++)
        {
            Object[] objects = pgDevices.get(i);
            PgDeviceDto dto = new PgDeviceDto(null);
            dto.setId(objects[0].toString());
            dto.setActive(objects[1].toString());
            dto.setAssetCode(objects[3].toString());
            dto.setAssetName(objects[4].toString());
            dto.setDevCode(objects[5].toString());
            dto.setDevType(objects[6].toString());
            dto.setIp(objects[7].toString());
            dto.setIsOpen(objects[8].toString());
            dto.setPartition(objects[11].toString());
            dto.setPipeGallery(objects[12].toString());
            dto.setPosition(objects[13].toString());
            dto.setRoad(objects[14].toString());

            pgDeviceDtoList.add(dto);
        }
        return pgDeviceDtoList;
    }

}