package com.casic.PgInterface.patroler.manager;

import com.casic.PgInterface.core.hibernate.HibernateEntityDao;
import com.casic.PgInterface.core.http.httpURL;
import com.casic.PgInterface.patroler.domain.PgInPgPerson;
import com.casic.PgInterface.patroler.domain.PgInPgStatus;
import com.casic.PgInterface.patroler.dto.PgInPgInfoDto;
import net.sf.json.JSONObject;
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.List;

/**
 * Created by yxw on 2018/3/22.
 */
@Service
public class PgInPgPersonManager extends HibernateEntityDao<PgInPgPerson> {

    private PgInPgStatusManager pgInPgStatusManager;

    @Resource
    public void setPgInPgStatusManager(PgInPgStatusManager pgInPgStatusManager) {
        this.pgInPgStatusManager = pgInPgStatusManager;
    }

    public PgInPgPerson getPersonByUserName(String userName) {
        List<PgInPgPerson> pgInPgPersonList = null;
        try {
            Criteria criteria = createCriteria(PgInPgPerson.class);
            criteria.add(Restrictions.eq("userName", userName));
            criteria.addOrder(Order.desc("id"));
            pgInPgPersonList = criteria.list();

            if (pgInPgPersonList != null && pgInPgPersonList.size() > 0)
                return pgInPgPersonList.get(0);
            else
                return null;
        } catch (Exception e) {
            return null;
        }
    }

    public List<PgInPgInfoDto> getPgInPersonList(int perType) {

        Criteria criteria = createCriteria(PgInPgPerson.class);
        criteria.addOrder(Order.desc("id"));
        if (perType == 1)
            criteria.add(Restrictions.eq("isInPg", 1));
        List<PgInPgPerson> pgInPgPersonList = criteria.list();

        List<PgInPgInfoDto> pgInPgInfoDtoList = new ArrayList<PgInPgInfoDto>();
        PgInPgStatus pgInPgStatus = new PgInPgStatus();
        int inPgNum = 0;
        for (PgInPgPerson pgInPgPerson : pgInPgPersonList) {
            pgInPgStatus = pgInPgStatusManager.getPgInPgStatusByPerson(pgInPgPerson);
            inPgNum = pgInPgStatusManager.getInPgNumByName(pgInPgPerson);
            pgInPgInfoDtoList.add(new PgInPgInfoDto(pgInPgPerson, pgInPgStatus, inPgNum));
        }

        return pgInPgInfoDtoList;
    }

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

        String sql="";
        String dateType="";
        List<PgInPgInfoDto> pgInPgInfoDtoList=new ArrayList<PgInPgInfoDto>();
        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 count(*), to_char(t.intime, '"+dateType+"') from pg_inpg_status t where t.intime >= " +
                "to_date('"+startTime+"', 'yyyy-mm-dd') and t.intime <= to_date('"+endTime+"', 'yyyy-mm-dd') group " +
                "by to_char(t.intime, '"+dateType+"')";

        List<Object[]> objects = this.getSession().createSQLQuery(sql).list();
        for(int i=0;i<objects.size();i++)
        {
            PgInPgInfoDto pgInPgInfoDto=new PgInPgInfoDto();
            pgInPgInfoDto.setInPgNum(objects.get(i)[0].toString());
            pgInPgInfoDto.setInTime(objects.get(i)[1].toString());
            pgInPgInfoDtoList.add(pgInPgInfoDto);
        }
        return pgInPgInfoDtoList;
    }

    public PgInPgPerson getPgInPgPersonByAssetCode(String assetCode) {
        Criteria criteria = createCriteria(PgInPgPerson.class);
        criteria.add(Restrictions.eq("assetCode", assetCode));
        criteria.addOrder(Order.desc("id"));
        List<PgInPgPerson> pgInPgPersonList = criteria.list();

        if (pgInPgPersonList != null && pgInPgPersonList.size() != 0)
            return pgInPgPersonList.get(0);
        else
            return null;
    }

    public String paramToMap(String id,String hostExternalId,String hostName,String description){

        JSONObject json2=new JSONObject();
        json2.put("ID", id);
        json2.put("hostExternalId",hostExternalId);
        json2.put("hostName",hostName);
        json2.put("description",description);

        return json2.toString();
    }

    public String synchorizedInPgInfo(String id,String hostExternalId,String hostName,String description){

        String paramsJson=paramToMap(id, hostExternalId, hostName, description);
        return  httpURL.httpURLConnectionPOST(paramsJson);
    }

}