package com.casic.PgInterface.devTable.manager;

import com.casic.PgInterface.core.hibernate.HibernateEntityDao;
import com.casic.PgInterface.devTable.domain.PgAlarm;
import com.casic.PgInterface.devTable.domain.PgAlarmType;
import com.casic.PgInterface.devTable.dto.PgAlarmTjDto;
import org.hibernate.Query;
import org.springframework.stereotype.Service;

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

/**
 * Created by yxw on 2017/11/22.
 */
@Service
public class PgAlarmTjManager extends HibernateEntityDao<PgAlarm> {

    private PgAlarmTypeManager pgAlarmTypeManager;

    @Resource
    public void setPgAlarmTypeManager(PgAlarmTypeManager pgAlarmTypeManager) {
        this.pgAlarmTypeManager = pgAlarmTypeManager;
    }

    //
    public List<PgAlarmTjDto> getAlarmTjResultByType(String startDate,String endDate) throws Exception {

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

        List<PgAlarmType> pgAlarmTypeList = pgAlarmTypeManager.getAllAlarmType();
        String sql = "";

        for (int i = 0; i < pgAlarmTypeList.size(); i++) {

            pgAlarmTjDto = new PgAlarmTjDto();
            sql ="select count(*) from PG_ALARM pgAlarm where pgAlarm.alarmTypeId = "+ pgAlarmTypeList.get(i).getId();

            if(startDate!=null&&endDate!=null)
                sql+=" and alarm_date > to_date('"+startDate+"','yyyy-MM-dd') and alarm_date < to_date('"
                        + endDate+"','yyyy-MM-dd')";

            Query query = this.getSession().createSQLQuery(sql);
            int numCount = Integer.valueOf(String.valueOf(query.uniqueResult()));
            pgAlarmTjDto.setAlarmNum(String.valueOf(query.uniqueResult()));
            pgAlarmTjDto.setAlarmType(pgAlarmTypeList.get(i).getAlarmType());
            pgAlarmTjDtoList.add(pgAlarmTjDto);
        }

        return pgAlarmTjDtoList;
    }

    //yyyy-mm-dd
    public List<PgAlarmTjDto> getAlarmByTime(String startTime,String endTime,String statisticType) {

        String sql="";
        String dateType="";
        List<PgAlarmTjDto> pgAlarmTjDtoList=new ArrayList<PgAlarmTjDto>();

        int type=Integer.valueOf(statisticType);
        switch(type) {
            case 0://年
                dateType = "yyyy";
                break;
            case 1://月
                dateType = "yyyy-mm";
                break;
            case 2://周
                dateType = "iw";
                break;
            case 3://日
                dateType="yyyy-mm-dd";
                break;
            default:
                break;
        }
        sql="select count(*), to_char(t.ALARM_DATE, '"+dateType+"') from PG_ALARM t where 1=1";

        if(startTime!=null)
            sql+=" and t.ALARM_DATE >= to_date('"+startTime+"', 'yyyy-mm-dd')";
        if(endTime!=null)
            sql+=" and t.ALARM_DATE <= to_date('"+endTime+"', 'yyyy-mm-dd')";

        sql+=" group by to_char(t.ALARM_DATE, '"+dateType+"')";

        List<Object[]> objects = this.getSession().createSQLQuery(sql).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;
    }

}
