package com.casic.PgInterface.construction.manager;

import com.casic.PgInterface.construction.domain.PgConstruction;
import com.casic.PgInterface.construction.dto.*;
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.devTable.domain.PgPartition;
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.List;

/**
 * Created by zxh on 2018/3/23.
 */
@Service
public class PgConstructionManager extends HibernateEntityDao<PgConstruction> {

    @Resource
    private PgPartitionManager pgPartitionManager;

    @Resource
    private PgConstructionTypeManager pgConstructionTypeManager;

    public PgConstruction getPgContructionByName(String name) {
        Criteria criteria = createCriteria(PgConstruction.class);
        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<PgConstruction> getPgContructionByNameOrType(String cs_name, String cs_type, PgPartition pgPartition, String startTime, String endTime) {

        try {
            Criteria criteria = createCriteria(PgConstruction.class);
            criteria.addOrder(Order.desc("id"));
            if (pgPartition != null)
                criteria.add(Restrictions.eq("pgPartitionId", pgPartition));
            if (StringUtils.isNotBlank(cs_name))
                criteria.add(Restrictions.eq("cs_name", cs_name));
            if (StringUtils.isNotBlank(cs_type))
                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()));
            }
            return criteria.list();

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

    public List<PgConstructionDto> getPgContructionDtoByNameOrType(String cs_name, String cs_type, String parName, String startTime, String endTime) {

        PgPartition pgPartition=null;
        if(parName!="")
            pgPartition = pgPartitionManager.getPartitionByParName(parName);

        List<PgConstruction> pgConstructionList = getPgContructionByNameOrType(cs_name, cs_type, pgPartition, startTime, endTime);
        List<PgConstructionDto> pgConstructionDtoList = new ArrayList<PgConstructionDto>();

        for (PgConstruction pgConstruction : pgConstructionList) {
            pgConstructionDtoList.add(new PgConstructionDto(pgConstruction));
        }

        return pgConstructionDtoList;
    }

    //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(!startTime.equals(""))
            sql+=" and t.CS_BEGINTIME >= to_date('" + startTime + "', 'yyyy-mm-dd')";
        if(!endTime.equals(""))
            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(String statisticType){
        List<PgConstructionStiatistic> pgConstructionStiatisticList = new ArrayList<PgConstructionStiatistic>();
        String str="PAPARTITION_ID";

        try {
            String hql="select (select p.PARNAME from pg_partition p where p.id=t.PAPARTITION_ID) as parName," +
                    "count(*) as count from pg_construction t group by t.PAPARTITION_ID";

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

                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(String statisticType) {
        List<PgConstructionStiatistic> pgConstructionStiatisticList = new ArrayList<PgConstructionStiatistic>();

        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="from PgConstruction pgConstruction where " +
                "pgConstruction.pgConstructionTypeId.typeName = '在建'";

        List<PgConstruction> pgConstructionList=(List<PgConstruction>)this.getSession().createQuery(hql).list();
        List<PgConstructionDto> pgConstructionDtoList=new ArrayList<PgConstructionDto>();
        for(PgConstruction pgConstruction : pgConstructionList)
        {
            pgConstructionDtoList.add(new PgConstructionDto(pgConstruction));
        }

        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.ACTIVE, " +
                "(SELECT m.PARNAME FROM PG_PARTITION m WHERE m.ID = t.PAPARTITION_ID) AS 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<PgConstructionDto>();
        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(null);
            pgConstructionDto.setId(String.valueOf(objects.get(i)[0]));
            pgConstructionDto.setCs_name(String.valueOf(objects.get(i)[1]));
            pgConstructionDto.setCs_detail(String.valueOf(objects.get(i)[2]));
            pgConstructionDto.setCs_beginTime(String.valueOf(objects.get(i)[3]));
            pgConstructionDto.setCs_endTime(String.valueOf(objects.get(i)[4]));
            pgConstructionDto.setCs_charge(String.valueOf(objects.get(i)[5]));
            pgConstructionDto.setCs_phone(String.valueOf(objects.get(i)[6]));
            pgConstructionDto.setCs_type(String.valueOf(objects.get(i)[7]));
            pgConstructionDto.setActive(String.valueOf(objects.get(i)[8]));
            pgConstructionDto.setParName(String.valueOf(objects.get(i)[9]));
            pgConstructionDto.setConstructionType(String.valueOf(objects.get(i)[9]));
            pgConstructionDtoList.add(pgConstructionDto);
        }
        return pgConstructionDtoList;
    }

}