package com.casic.PgInterface.construction.manager;

import com.casic.PgInterface.construction.domain.PgConstruction;
import com.casic.PgInterface.construction.domain.PgCsDocument;
import com.casic.PgInterface.construction.dto.*;
import com.casic.PgInterface.core.hibernate.HibernateEntityDao;
import com.casic.PgInterface.core.page.Page;
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.PgPartition;
import com.casic.PgInterface.devTable.dto.PgAlarmDto;
import com.casic.PgInterface.devTable.manager.PgPartitionManager;
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 zxh on 2018/3/23.
 */
@Service
public class PgConstructionManager extends HibernateEntityDao<PgConstruction> {

    @Resource
    private PgConstructionTypeManager pgConstructionTypeManager;

    public PgConstruction getPgContructionByName(String name) {
        Criteria criteria = createCriteria(PgConstruction.class);
        if (StringUtils.isNotBlank(name))
            criteria.add(Restrictions.eq("cs_name", name));
        criteria.add(Restrictions.eq("active", 1));

        List<PgConstruction> pgConstructionList = criteria.list();

        if (pgConstructionList == null||pgConstructionList.size()==0)
            return null;
        else
            return pgConstructionList.get(0);
    }

    public List<PgConstructionDto> getPgContructionDto(String cs_name, String cs_type,
                                                       String startTime, String endTime,
                                                       int page,int rows) {
        try {

            Criteria criteria = createCriteria(PgConstruction.class);
            if(StringUtils.isNotBlank(cs_name))
                criteria.add(Restrictions.eq("cs_name", cs_name));
            if (StringUtils.isNotBlank(cs_type)&&!cs_type.contains("全部"))
                criteria.add(Restrictions.eq("cs_type", cs_type));

            if (StringUtils.isNotBlank(startTime)) {
                criteria.add(Restrictions.ge("cs_beginTime", 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("cs_beginTime", calendar.getTime()));
            }

            criteria.add(Restrictions.eq("active",1));

            criteria.addOrder(Order.desc("id"));
            Page pg = pagedQuery(criteria, page, rows);

            List<PgConstruction> pgConstructionList = (List<PgConstruction>)pg.getResult();
            List<PgConstructionDto> pgConstructionDtoList = new ArrayList<>();
            for (PgConstruction pgConstruction : pgConstructionList) {
                pgConstructionDtoList.add(new PgConstructionDto(pgConstruction,pgConstructionTypeManager));
            }

            return pgConstructionDtoList;
        }
        catch(Exception ex){
            ex.printStackTrace();
            return null;
        }
    }

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

        String sql = "";

        String dateType = "";
        List<PgConstructionStiatistic> pgConstructionStiatisticList = new ArrayList<PgConstructionStiatistic>();

        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 to_char(t.CS_BEGINTIME, '" + dateType + "'),count(*) from PG_CONSTRUCTION t where 1=1 ";

        if(StringUtils.isNotBlank(startTime))
            sql+=" and t.CS_BEGINTIME >= to_date('" + startTime + "', 'yyyy-mm-dd')";
        if(StringUtils.isNotBlank(endTime))
            sql+=" and t.CS_BEGINTIME <= to_date('" + endTime + "', 'yyyy-mm-dd')";

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

        List<Object[]> objects = this.getSession().createSQLQuery(sql).list();
        for (int i = 0; i < objects.size(); i++) {

            PgConstructionDateStatistic pgConstructionDateStatistic = new PgConstructionDateStatistic();

            pgConstructionDateStatistic.setConstructionTime(objects.get(i)[0].toString());
            pgConstructionDateStatistic.setConstructNum(objects.get(i)[1].toString());

            pgConstructionStiatisticList.add(pgConstructionDateStatistic);
        }
        return pgConstructionStiatisticList;
    }

    public List<PgConstructionStiatistic> getStatisticByPar(){
        List<PgConstructionStiatistic> pgConstructionStiatisticList = new ArrayList<PgConstructionStiatistic>();
        String str="PAPARTITION_ID";

        try {
            String hql="select parName as parName," +
                    "count(*) as count from pg_construction t group by t.parName";

            List<Object[]> objects=this.getSession().createSQLQuery(hql).list();
            PgConstructionParStatisticDto pgConstructionParStatisticDto=null;
            for(int i=0;i<objects.size();i++) {
                pgConstructionParStatisticDto = new PgConstructionParStatisticDto();

                if(objects.get(i)[0]==null)
                    pgConstructionParStatisticDto.setConstructPartition("");
                else
                    pgConstructionParStatisticDto.setConstructPartition(objects.get(i)[0].toString());
                pgConstructionParStatisticDto.setConstructNum(objects.get(i)[1].toString());
                pgConstructionStiatisticList.add(pgConstructionParStatisticDto);
            }
            return pgConstructionStiatisticList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<PgConstructionStiatistic> getStatisticByType() {
        List<PgConstructionStiatistic> pgConstructionStiatisticList = new ArrayList<>();

        String str="CS_TYPE";

        try {
            String hql="select pgConstruction." + str + ", count(*) from Pg_Construction pgConstruction "+
                    "where active = 1 group by pgConstruction." + str;

            List<Object[]> objects=this.getSession().createSQLQuery(hql).list();
            PgConstructionTypeStatisticDto pgConstructTypeStatisticDto=null;
            for(int i=0;i<objects.size();i++) {
                pgConstructTypeStatisticDto = new PgConstructionTypeStatisticDto();

                pgConstructTypeStatisticDto.setConstructType(objects.get(i)[0].toString());
                pgConstructTypeStatisticDto.setConstructNum(objects.get(i)[1].toString());
                pgConstructionStiatisticList.add(pgConstructTypeStatisticDto);
            }
            return pgConstructionStiatisticList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<PgConstructionTypeNumStatisticDto> getConstructionNum(){
        String sql="select (select p.typename from pg_construction_type p where p.id=t.pgconstructiontype_id) as typename," +
                "count(*) as count from pg_construction t group by t.pgconstructiontype_id";

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

        List<PgConstructionTypeNumStatisticDto> pgConstructionTypeNumDtoList=new ArrayList<PgConstructionTypeNumStatisticDto>();
        PgConstructionTypeNumStatisticDto pgConstructionTypeNumDto=null;
        for(Object[] objects1:objects){
            pgConstructionTypeNumDto=new PgConstructionTypeNumStatisticDto();
            pgConstructionTypeNumDto.setConstructStatus(String.valueOf(objects1[0]));
            pgConstructionTypeNumDto.setConstructNum(String.valueOf(objects1[1]));

            pgConstructionTypeNumDtoList.add(pgConstructionTypeNumDto);
        }
        return pgConstructionTypeNumDtoList;
    }

    public List<PgConstructionDto> getConstructionToday(){

        String hql="select id,cs_name,cs_detail,cs_beginTime,cs_endTime,cs_charge,cs_phone,cs_type,parName,PGCONSTRUCTIONTYPE_ID from PG_CONSTRUCTION where " +
                "PGCONSTRUCTIONTYPE_ID = (select id from PG_CONSTRUCTION_TYPE where TYPENAME= '在建')";

        List<Object[]> objects=this.getSession().createSQLQuery(hql).list();
        List<PgConstructionDto> pgConstructionDtoList=new ArrayList<>();
        try {
            for (Object[] objects1 : objects) {
                PgConstruction pgConstruction = new PgConstruction();
                pgConstruction.setId(objects1[0] == null ? null : Long.valueOf(objects1[0].toString()));
                pgConstruction.setCs_name(objects1[1] == null ? "" : objects1[1].toString());
                pgConstruction.setCs_detail(objects1[2] == null ? "" : objects1[2].toString());
                pgConstruction.setCs_beginTime(objects1[3] == null ? null : DateUtils.sdf_day.parse(objects1[3].toString()));
                pgConstruction.setCs_endTime(objects1[4] == null ? null : DateUtils.sdf_day.parse(objects1[4].toString()));
                pgConstruction.setCs_charge(objects1[5] == null ? "" : objects1[5].toString());
                pgConstruction.setCs_phone(objects1[6] == null ? "" : objects1[6].toString());
                pgConstruction.setCs_type(objects1[7] == null ? "" : objects1[7].toString());
                pgConstruction.setParName(objects1[8] == null ? "" : objects1[8].toString());
                pgConstruction.setPgConstructionTypeId(objects1[9] == null ? null : Long.valueOf(objects1[9].toString()));

                pgConstructionDtoList.add(new PgConstructionDto(pgConstruction, pgConstructionTypeManager));
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return pgConstructionDtoList;
    }

    public List<PgConstructionDto> getPgConstructionList() {
        String sql = "SELECT t.ID,t.CS_NAME,t.CS_DETAIL,t.CS_BEGINTIME,t.CS_ENDTIME,t.CS_CHARGE,t.CS_PHONE,t.CS_TYPE, " +
                "t.PARNAME, " +
                "(SELECT n.TYPENAME FROM PG_CONSTRUCTION_TYPE n WHERE n.ID = t.PGCONSTRUCTIONTYPE_ID) AS TYPENAME" +
                " FROM PG_CONSTRUCTION t WHERE ACTIVE = 1";

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

        if(objects.size()==0) return null;
        for (int i = 0; i < objects.size() ; i++) {
            PgConstructionDto pgConstructionDto = new PgConstructionDto();

            pgConstructionDto.setId(objects.get(i)[0]==null?null:objects.get(i)[0].toString());
            pgConstructionDto.setCs_name(objects.get(i)[1]==null?"":objects.get(i)[1].toString());
            pgConstructionDto.setCs_detail(objects.get(i)[2]==null?"":objects.get(i)[2].toString());
            pgConstructionDto.setCs_beginTime(objects.get(i)[3]==null? "":objects.get(i)[3].toString());
            pgConstructionDto.setCs_endTime(objects.get(i)[4]==null? "":objects.get(i)[4].toString());
            pgConstructionDto.setCs_charge(objects.get(i)[5]==null?"":objects.get(i)[5].toString());
            pgConstructionDto.setCs_phone(objects.get(i)[6]==null?"":objects.get(i)[6].toString());
            pgConstructionDto.setCs_type(objects.get(i)[7]==null?"":objects.get(i)[7].toString());
            pgConstructionDto.setParName(objects.get(i)[8]==null?"":objects.get(i)[8].toString());
            pgConstructionDto.setConstructionType(objects.get(i)[9]==null?"":objects.get(i)[9].toString());
            pgConstructionDtoList.add(pgConstructionDto);
        }
        return pgConstructionDtoList;
    }

}