package com.casic.PgInterface.maintenance.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.maintenance.domain.PgAssetPlan;
import com.casic.PgInterface.maintenance.dto.PgAssetPlanStatic;
import com.casic.PgInterface.maintenance.dto.PgAssetPlanDto;
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.Calendar;
import java.util.List;

@Service
public class PgAssetPlanManager extends HibernateEntityDao<PgAssetPlan> {

    public List<PgAssetPlanDto> getPgAssetPlanDtoList(String startDay,String endDay,String inSpectionPlace){
        Criteria criteria=createCriteria(PgAssetPlan.class);
        try {
            if (StringUtils.isNotBlank(inSpectionPlace))
                criteria.add(Restrictions.like("INSPECTION_PRACE", "%" + inSpectionPlace + "%"));

            if (StringUtils.isNotBlank(startDay) && StringUtils.isNotBlank(endDay)) {
                criteria.add(Restrictions.ge("FREQUENCYDATE", DateUtils.sdf_day.parse(startDay)));

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(DateUtils.sdf_day.parse(endDay));
                calendar.add(Calendar.DATE, 1);
                criteria.add(Restrictions.le("FREQUENCYDATE", calendar.getTime()));
            }

            criteria.addOrder(Order.desc("PLAN_NO"));

            List<PgAssetPlan> pgAssetPlanList = criteria.list();

            List<PgAssetPlanDto> pgAssetPlanDtoList = PgAssetPlanDto.ConvertToPgAssetDtoList(pgAssetPlanList);

            if (pgAssetPlanDtoList == null || pgAssetPlanDtoList.size() == 0)
                return null;
            else
                return pgAssetPlanDtoList;
        }
        catch(Exception ex){
            ex.printStackTrace();
            return null;
        }
    }

    public List<PgAssetPlanStatic> getPgAssetPlanStaticLisByTime(
            String startTime, String endTime, String staticType) {

        String sql="";
        String dateType="";
        List<PgAssetPlanStatic> pgAssetPlanStaticList=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.CREATEDATE,'yyyy-MM-dd'), '"+dateType+"') from PG_ASSET_INSPECTION_PLAN t where 1=1";

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

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

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

}
