package com.yihu.hos.datacollect.dao;

import com.yihu.hos.datacollect.model.RsDatapushLog;
import com.yihu.hos.web.framework.constant.DateConvert;
import com.yihu.hos.web.framework.dao.SQLGeneralDAO;
import com.yihu.hos.web.framework.model.DataGridResult;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;

/**
 * Created by hzp on 2016/1/11.
 */
@Repository("DatacollectLogDao")
public class DatacollectLogDao extends SQLGeneralDAO {

    public static final String BEAN_ID = "DatacollectLogDao";

    /**
     * 修改轮询字段
     */
    
    public void updateJobDatasetKeyvalue(String id,String jobDatasetKeyvalue) throws Exception
    {
        String sql = "update rs_job_dataset set job_dataset_keyvalue='"+jobDatasetKeyvalue+"' where id='"+id+"'";
        super.execute(sql);
    }

    /******************************** 推模式日志 ***************************************/
    /**
     * 推数据新增日志
     */
    
    public void addDataPushLog(String type,String success,String content) throws Exception
    {
        RsDatapushLog log = new RsDatapushLog();
        log.setType(type);
        log.setSuccess(success);
        log.setContent(content);
        log.setDatetime(new Date());
        this.saveEntity(log);
    }

    /**
     * 获取日志列表
     */
    public DataGridResult queryDataPushLog(Map<String, Object> conditionMap, Integer page, Integer pageSize) throws Exception
    {
        StringBuilder sb = new StringBuilder();
        sb.append("from RsDatapushLog where 1=1 ");
        if (!StringUtils.isEmpty(conditionMap.get("type"))) {
            sb.append(" and type ='"+conditionMap.get("type")+"'");
        }

        if (!StringUtils.isEmpty(conditionMap.get("datetimeFrom"))) {
            Date time = DateConvert.toDate(conditionMap.get("datetimeFrom").toString());
            time.setHours(0);
            time.setMinutes(0);
            time.setSeconds(0);
            sb.append(" and datetime >= '"+DateConvert.toString(time)+"'");
        }
        if (!StringUtils.isEmpty(conditionMap.get("datetimeTo"))) {
            Date time = DateConvert.toDate(conditionMap.get("datetimeTo").toString());
            time.setHours(0);
            time.setMinutes(0);
            time.setSeconds(0);
            GregorianCalendar gc=new GregorianCalendar();
            gc.setTime(time);
            gc.add(5, 1);
            sb.append(" and datetime < '"+DateConvert.toString(gc.getTime())+"'");
        }
        sb.append("order by datetime desc");
        return getDataGridResult(sb.toString(),page,pageSize);
    }
}
