package com.casic.PgInterface.maintenance.manager;

import com.casic.PgInterface.core.hibernate.HibernateEntityDao;
import com.casic.PgInterface.maintenance.domain.PgAssetInSpectionRecord;
import com.casic.PgInterface.maintenance.dto.PgAssetInSpectionRecordDto;
import com.casic.PgInterface.maintenance.dto.PgAssetInSpectionRecordStatic;
import org.springframework.stereotype.Service;

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

@Service
public class PgAssetInSpectionRecordManager extends HibernateEntityDao<PgAssetInSpectionRecord> {

    public List<PgAssetInSpectionRecordDto> getPgAssetInSpectionRecordLsit(String recordNo, String planName,String startTime,String endTime){
        try {
            String sql="select ID,KEY_ID,PLAN_ID,CREATE_DATE,STATE,CREATOR,PLAN_NAME,RECORD_NO,PLAN_NO," +
                    "RESPONSIBIE_DEPT,INSPECTION_PERSON,PATROL_MAN,OPERATE_DATE,REMARK,BUILD_CABIN,PROJECT," +
                    "START_PILE_NO,END_PILE_NO,INSPECTION_PRACE,OTHER_PATROL_MAN from PG_ASSET_INSPECTION_RECORD 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(create_date,'yyyy-MM-dd')>to_date('"+startTime+"','yyyy-MM-dd')";
            if(endTime!=null)
                sql+=" and to_date(create_date,'yyyy-MM-dd')<to_date('"+endTime+"','yyyy-MM-dd')";

            sql+=" order by id";

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

            List<PgAssetInSpectionRecord> pgAssetInSpectionRecordList=new ArrayList<>();
            for(Object[] objects1:objects){
                PgAssetInSpectionRecord pgAssetInSpectionRecord=new PgAssetInSpectionRecord();

                pgAssetInSpectionRecord.setId(Long.valueOf(objects1[0].toString()));
                pgAssetInSpectionRecord.setKeyId(String.valueOf(objects1[1]));
                pgAssetInSpectionRecord.setPlanId(String.valueOf(objects1[2]));
                pgAssetInSpectionRecord.setCreateDate(String.valueOf(objects1[3]));
                pgAssetInSpectionRecord.setState(String.valueOf(objects1[4]));
                pgAssetInSpectionRecord.setCreator(String.valueOf(objects1[5]));
                pgAssetInSpectionRecord.setPlanName(String.valueOf(objects1[6]));
                pgAssetInSpectionRecord.setRecordNo(String.valueOf(objects1[7]));
                pgAssetInSpectionRecord.setPlanNo(String.valueOf(objects1[8]));
                pgAssetInSpectionRecord.setResponsibleDept(String.valueOf(objects1[9]));
                pgAssetInSpectionRecord.setInspectionPerson(String.valueOf(objects1[10]));
                pgAssetInSpectionRecord.setPatrolMan(String.valueOf(objects1[11]));
                pgAssetInSpectionRecord.setOperateDate(String.valueOf(objects1[12]));
                pgAssetInSpectionRecord.setRemark(String.valueOf(objects1[13]));
                pgAssetInSpectionRecord.setBuildCabin(String.valueOf(objects1[14]));
                pgAssetInSpectionRecord.setProject(String.valueOf(objects1[15]));
                pgAssetInSpectionRecord.setStartPileNo(String.valueOf(objects1[16]));
                pgAssetInSpectionRecord.setEndPileNo(String.valueOf(objects1[17]));
                pgAssetInSpectionRecord.setInspectionPrace(String.valueOf(objects1[18]));
                pgAssetInSpectionRecord.setOtherPatrolMan(String.valueOf(objects1[19]));

                pgAssetInSpectionRecordList.add(pgAssetInSpectionRecord);
            }

            List<PgAssetInSpectionRecordDto> pgAssetInSpectionRecordDtoList=new ArrayList<>();
            for(PgAssetInSpectionRecord pgAssetInSpectionRecord : pgAssetInSpectionRecordList)
            {
                pgAssetInSpectionRecordDtoList.add(PgAssetInSpectionRecordDto.ConvertToDto(pgAssetInSpectionRecord));
            }

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

    public List<PgAssetInSpectionRecordStatic> getPgAssetInSpectionRecordStaticLisByTime(
            String startTime, String endTime, String staticType) {

        String sql="";
        String dateType="";
        List<PgAssetInSpectionRecordStatic> 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.CREATE_DATE,'yyyy-MM-dd'), '"+dateType+"') from PG_ASSET_INSPECTION_RECORD t where 1=1";

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

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

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

}