package org.well.well.manager;

import org.well.well.core.hibernate.HibernateEntityDao;
import org.well.well.core.hibernate.HibernateUtils;
import org.well.well.core.hibernate.MatchType;
import org.well.well.core.util.DateUtils;
import org.well.well.domain.Concentrator;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

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

@Service
public class ConcentratorManager extends HibernateEntityDao<Concentrator> {

    public Map saveConcen(String devCode, boolean status, String logTime) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            Concentrator concentrator = null;
            Criteria criteria = getSession().createCriteria(Concentrator.class);
            criteria.add(Restrictions.and(
                    HibernateUtils.buildCriterion("valid", "1", MatchType.EQ),
                    HibernateUtils.buildCriterion("concencode", devCode, MatchType.EQ)));
            List<Concentrator> concentratorList = criteria.list();

            if (concentratorList.size() > 0) {
                for (int i = 0; i < concentratorList.size(); i++) {
                    concentrator = concentratorList.get(i);
                    if (i == 0) {
                        concentrator.setLasttime(DateUtils.sdf4.parse(DateUtils.DateFormat(logTime)));
                        concentrator.setStatus(status?"1":"0");
                    } else {
                        concentrator.setValid("0");
                    }
                    save(concentrator);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("success", false);
            return map;
        }
        return map;
    }
}
