package org.well.well.manager;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.well.well.base.DeviceTypeEnum;
import org.well.well.core.hibernate.HibernateEntityDao;
import org.well.well.core.util.DateUtils;
import org.well.well.domain.AlarmJob;
import org.well.well.domain.Device;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AlarmJobManager extends HibernateEntityDao<AlarmJob> {


    private static Map<String, String> deviceAlarmMap = new HashMap<String, String>();

    static {
        deviceAlarmMap.put(DeviceTypeEnum.Liquid.toString(), "YWCX");
        deviceAlarmMap.put(DeviceTypeEnum.Well.toString(), "JGKQ");
        deviceAlarmMap.put(DeviceTypeEnum.Methane.toString(), "RQCX");
        deviceAlarmMap.put(DeviceTypeEnum.NoiseDig.toString(), "KWCX");
        deviceAlarmMap.put(DeviceTypeEnum.WasteGas.toString(), "YHCX");
        deviceAlarmMap.put(DeviceTypeEnum.TempHumi.toString(), "WSCX");
        deviceAlarmMap.put(DeviceTypeEnum.Meter.toString(), "SZCX");
    }

    public Criteria getCriteria() {
        return getSession().createCriteria(Device.class);
    }

    public void updateAlarmJobByDevCode(String devcode, String message) {
        try {
            Criteria criteria = getSession().createCriteria(AlarmJob.class);
            criteria.add(Restrictions.eq("devcode", devcode));
            criteria.add(Restrictions.in("jobStatus", new String[]{"0", "1", "2"}));
            List<AlarmJob> alarmJobList = criteria.list();
            for (AlarmJob alarmJob : alarmJobList) {
                alarmJob.setJobStatus("3");
                alarmJob.setHandleJobTime(new Date());
                alarmJob.setHandleMessage(message);
                alarmJob.setJobBelongTo(null);
                save(alarmJob);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public AlarmJob saveData(String devCode, String wellCode, String devTypeName, String jobType) {
        AlarmJob alarmJob = new AlarmJob();
        alarmJob.setDevcode(devCode);
        alarmJob.setWellCode(wellCode);
        alarmJob.setJobStatus("0");
        alarmJob.setCreateTime(new Date());
        alarmJob.setJobcode(produceJobCode(devTypeName));
        alarmJob.setJogType(jobType);
        save(alarmJob);
        return alarmJob;
    }

    public String produceJobCode(String devTypeName) {
        String pre = deviceAlarmMap.get(devTypeName);
        String dataStr = DateUtils.sdf6.format(new Date());
        String fix = getAlarmJob(pre + dataStr);
        if (StringUtils.isNotBlank(fix)) {
            return pre + dataStr + String.format("%04d", Long.valueOf(fix) + 1);
        } else
            return pre + dataStr + "0001";

    }

    public String getAlarmJob(String jobcode) {
        String fix = "";
        Criteria criteria = getSession().createCriteria(AlarmJob.class);
        criteria.add(Restrictions.like("jobcode", jobcode + "%"));
        criteria.addOrder(Order.desc("jobcode"));
        AlarmJob alarmJob = (AlarmJob) criteria.setMaxResults(1).uniqueResult();
        if (null != alarmJob && alarmJob.getJobcode().length() > 4) {
            fix = alarmJob.getJobcode().substring(alarmJob.getJobcode().length() - 4);
        }
        return fix;
    }

    public AlarmJob getByDevAndType(String devCode, String jobType) {
        Criteria criteria = getSession().createCriteria(AlarmJob.class);
        criteria.add(Restrictions.eq("devcode", devCode));
//        criteria.add(Restrictions.ne("jobStatus", "3"));
        criteria.add(Restrictions.in("jobStatus", new String[]{"0", "1", "2"}));
        criteria.add(Restrictions.eq("jogType", jobType));
        criteria.addOrder(Order.desc("createTime"));
        return (AlarmJob) criteria.setMaxResults(1).uniqueResult();
    }
}