package com.casic.PgInterface.devTable.manager;

import com.casic.PgInterface.core.hibernate.HibernateEntityDao;
import com.casic.PgInterface.core.util.DateUtils;
import com.casic.PgInterface.core.util.StringUtils;
import com.casic.PgInterface.devTable.domain.PgAlarm;
import com.casic.PgInterface.devTable.domain.PgAlarmType;
import com.casic.PgInterface.devTable.dto.PgAlarmDto;
import com.casic.PgInterface.devTable.dto.PgAlarmTjDto;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

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

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

    @Resource
    private PgAlarmTypeManager pgAlarmTypeManager;

    //获取当前报警消息列表
    public List<PgAlarmDto> getAllAlarm() {
        Criteria criteria = createCriteria(PgAlarm.class);
//        criteria.add(Restrictions.eq("active", 1));
        criteria.addOrder(Order.desc("id"));

        List<PgAlarm> pgAlarmList = criteria.list();
        List<PgAlarmDto> pgAlarmDtoList = new ArrayList<PgAlarmDto>();
        for (PgAlarm pgAlarm : pgAlarmList) {
            pgAlarmDtoList.add(new PgAlarmDto(pgAlarm));
        }

        return pgAlarmDtoList;
    }

    public List<PgAlarmDto> getAlarmByTypeAndTime(String alarmType,String startTime,String endTime){
        try {
            Criteria criteria = createCriteria(PgAlarm.class);
            if (StringUtils.isNotBlank(alarmType)) {
                criteria.add(Restrictions.eq("alarmTypeId", pgAlarmTypeManager.getAlarmTypeByAlarmType(alarmType)));
            }

            if (StringUtils.isNotBlank(startTime)) {
                criteria.add(Restrictions.ge("alarmDate", DateUtils.sdf_day.parse(startTime)));
            }
            if (StringUtils.isNotBlank(endTime)) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(DateUtils.sdf_day.parse(endTime));
                calendar.add(Calendar.DATE, 1);
                criteria.add(Restrictions.le("alarmDate", calendar.getTime()));
            }
            criteria.add(Restrictions.eq("active", 1));
            criteria.addOrder(Order.desc("id"));

            List<PgAlarm> pgAlarmList=criteria.list();
            List<PgAlarmDto> pgAlarmDtoList=new ArrayList<PgAlarmDto>();
            for(PgAlarm pgAlarm : pgAlarmList)
            {
                pgAlarmDtoList.add(new PgAlarmDto(pgAlarm));
            }

            return pgAlarmDtoList;
        }
        catch(Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }

    public int getAlarmNum(String isDevFault) {
        String hql = "select count(*) from PgAlarm pgAlarm ";

        if (isDevFault.equals("true"))
            hql += " where pgAlarm.alarmTypeId.alarmType = '设备运行状态报警'";
        else
            hql += " where pgAlarm.alarmTypeId.alarmType != '设备运行状态报警'";

        Object object = this.getSession().createQuery(hql).uniqueResult();
        return Integer.valueOf(object.toString());
    }

    public int getMonthAlarmNum(String isDevFault) {

        String hql = "select count(*) from PgAlarm pgAlarm where to_char(pgAlarm.alarmDate, 'yyyy-mm')" +
                " = to_char('" + DateUtils.sdf4.format(new Date()) + "','yyyy-mm')";

        if (isDevFault.equals("true"))
            hql += " and pgAlarm.alarmTypeId.alarmType = '设备运行状态报警'";
        else
            hql += " and pgAlarm.alarmTypeId.alarmType != '设备运行状态报警'";

        Object object = this.getSession().createQuery(hql).uniqueResult();
        return Integer.parseInt(String.valueOf(object));
    }

    public int getWeekAlarmNum(String isDevFault) {
        String hql = "select count(*) from PgAlarm pgAlarm where to_char(pgAlarm.alarmDate, 'iw')" +
                " = to_char('" + DateUtils.sdf4.format(new Date()) + "','iw')";

        if (isDevFault.equals("true"))
            hql += " and pgAlarm.alarmTypeId.alarmType = '设备运行状态报警'";
        else
            hql += " and pgAlarm.alarmTypeId.alarmType != '设备运行状态报警'";

        Object object = this.getSession().createQuery(hql).uniqueResult();
        return Integer.parseInt(String.valueOf(object));
    }

    public List<PgAlarmTjDto> getDayAlarmStatistic(String isDevFault) {

        List<PgAlarmTjDto> pgAlarmTjDtoList = new ArrayList<PgAlarmTjDto>();

        String hql = "select count(*), to_char(pgAlarm.alarmDate, 'yyyy-mm-dd') from PgAlarm pgAlarm where 1=1 ";

        if (isDevFault.equals("true"))
            hql += " and pgAlarm.alarmTypeId.alarmType = '设备运行状态报警'";
        else
            hql += " and pgAlarm.alarmTypeId.alarmType != '设备运行状态报警'";

        hql += " group by to_char(pgAlarm.alarmDate, 'yyyy-mm-dd')";

        List<Object[]> objects = (List<Object[]>)this.getSession().createQuery(hql).list();
        for (int i = 0; i < objects.size(); i++) {
            PgAlarmTjDto pgAlarmTjDto = new PgAlarmTjDto();
            pgAlarmTjDto.setAlarmNum(objects.get(i)[0].toString());
            pgAlarmTjDto.setAlarmDate(objects.get(i)[1].toString());
            pgAlarmTjDtoList.add(pgAlarmTjDto);
        }
        return pgAlarmTjDtoList;
    }

    public List<PgAlarmDto> getAlarmList(String isDevFault) {

        PgAlarmType pgAlarmType=pgAlarmTypeManager.getAlarmTypeByAlarmType("设备运行状态报警");

        Criteria criteria=createCriteria(PgAlarm.class);
        criteria.add(Restrictions.eq("active",1));

        if(isDevFault!=null) {
            if (isDevFault.equals("true"))
                criteria.add(Restrictions.eq("alarmTypeId", pgAlarmType));
            else
                criteria.add(Restrictions.ne("alarmTypeId", pgAlarmType));
        }
        List<PgAlarm> pgAlarmList = criteria.list();
        List<PgAlarmDto> pgAlarmDtoList=new ArrayList<PgAlarmDto>();
        for(PgAlarm pgAlarm : pgAlarmList)
        {
            pgAlarmDtoList.add(new PgAlarmDto(pgAlarm));
        }

        return pgAlarmDtoList;
    }

}
