package com.casic.PgInterface.maintenance.manager;

import com.casic.PgInterface.core.hibernate.HibernateEntityDao;
import com.casic.PgInterface.maintenance.domain.PgMaintanceRecordHead;
import com.casic.PgInterface.maintenance.dto.PgMaintanceRecordHeadDto;
import com.casic.PgInterface.maintenance.dto.PgMaintanceRecordHeadStatic;
import org.springframework.stereotype.Service;

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

@Service
public class PgMaintanceRecordHeadManager extends HibernateEntityDao<PgMaintanceRecordHead> {


    public List<PgMaintanceRecordHeadDto> getPgMaintanceRecordHeadLsit(String recordNo, String planName,String startTime,String endTime){
        try {
            String sql="select ID,KEY_ID,PLAN_KEY_ID,PLAN_NAME,RECORD_NO,REOAIRMAN,MAINTENANCE_DATE," +
                    "BUILD_CABIN,PROJECT,CREATEDATE,CREATOR from PG_MAINTANCERECORD_HEAD where 1=1";
            if(recordNo!=null)
                sql+=" and RECORD_NO like '%"+recordNo+"%'";
            if(planName!=null)
                sql+=" and PLAN_NAME like '%"+planName+"%'";
            if(startTime!=null)
                sql+=" and to_date(CREATEDATE,'yyyy-MM-dd')>to_date('"+startTime+"','yyyy-MM-dd')";
            if(endTime!=null)
                sql+=" and to_date(CREATEDATE,'yyyy-MM-dd')<to_date('"+endTime+"','yyyy-MM-dd')";

            sql+=" order by id";

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

            List<PgMaintanceRecordHead> pgMaintanceRecordHeadList=new ArrayList<>();
            for(Object[] objects1:objects){
                PgMaintanceRecordHead pgMaintanceRecordHead=new PgMaintanceRecordHead();
                pgMaintanceRecordHead.setId(Long.valueOf(objects1[0].toString()));
                pgMaintanceRecordHead.setKeyId(String.valueOf(objects1[1]));
                pgMaintanceRecordHead.setPlanKeyId(String.valueOf(objects1[2]));
                pgMaintanceRecordHead.setPlanName(String.valueOf(objects1[3]));
                pgMaintanceRecordHead.setRecordNo(String.valueOf(objects1[4]));
                pgMaintanceRecordHead.setReoairMan(String.valueOf(objects1[5]));
                pgMaintanceRecordHead.setMaintenanceDate(String.valueOf(objects1[6]));
                pgMaintanceRecordHead.setBuildCabin(String.valueOf(objects1[7]));
                pgMaintanceRecordHead.setProject(String.valueOf(objects1[8]));
                pgMaintanceRecordHead.setCreateDate(String.valueOf(objects1[9]));
                pgMaintanceRecordHead.setCreator(String.valueOf(objects1[10]));

                pgMaintanceRecordHeadList.add(pgMaintanceRecordHead);
            }

            List<PgMaintanceRecordHeadDto> pgMaintanceRecordHeadDtoList=new ArrayList<>();
            for(PgMaintanceRecordHead pgMaintanceRecordHead : pgMaintanceRecordHeadList)
            {
                pgMaintanceRecordHeadDtoList.add(PgMaintanceRecordHeadDto.ConvertToDto(pgMaintanceRecordHead));
            }

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

    public List<PgMaintanceRecordHeadStatic> getPgMaintanceRecordHeadStaticLisByTime
            (String startTime, String endTime, String staticType) {

        String sql="";
        String dateType="";
        List<PgMaintanceRecordHeadStatic> pgAssetInSpectionRecordStaticList=new ArrayList<>();
        int type=Integer.valueOf(staticType);
        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(to_date(t.MAINTENANCE_DATE,'yyyy-MM-dd'), '"+dateType+"') from PG_MAINTANCERECORD_HEAD t where 1=1";

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

        sql+=" group by to_char(to_date(t.MAINTENANCE_DATE,'yyyy-MM-dd'), '"+dateType+"')";

        List<Object[]> objects = this.getSession().createSQLQuery(sql).list();
        for(int i=0;i<objects.size();i++)
        {
            PgMaintanceRecordHeadStatic pgMaintanceRecordHeadStatic=new PgMaintanceRecordHeadStatic();
            pgMaintanceRecordHeadStatic.setNum(objects.get(i)[0].toString());
            pgMaintanceRecordHeadStatic.setMaintenanceDate(objects.get(i)[1].toString());
            pgAssetInSpectionRecordStaticList.add(pgMaintanceRecordHeadStatic);
        }
        return pgAssetInSpectionRecordStaticList;
    }

}