package com.casic.PgInterface.patroler.manager;

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.patroler.domain.PgLog;
import com.casic.PgInterface.patroler.dto.PgLogDto;
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 yxw on 2017/11/2.
 */
@Service
public class PgLogManager extends HibernateEntityDao<PgLog> {

    private PgUserManager pgPatrolerManager;

    @Resource
    public void setPgPatrolerManager(PgUserManager pgPatrolerManager) {
        this.pgPatrolerManager = pgPatrolerManager;
    }

    //获取当前设备列表
    //TODO LIST getAllLOgs
    public List<PgLogDto> getAllLogs(String userName,String startTime,String endTime) {
        try {
            Criteria criteria = createCriteria(PgLog.class);
            if (StringUtils.isNotBlank(userName)) {
                criteria.add(Restrictions.eq("userId", pgPatrolerManager.getPatrolerByuserName(userName)));
            }

            if (StringUtils.isNotBlank(startTime)) {
                criteria.add(Restrictions.ge("operateTime", 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("operateTime", calendar.getTime()));
            }

            criteria.add(Restrictions.eq("active", 1));
            criteria.addOrder(Order.desc("id"));

            List<PgLog> pgLogList = criteria.list();
            List<PgLogDto> pgLogDtoList = new ArrayList<PgLogDto>();
            for (PgLog pgLog : pgLogList) {
                pgLogDtoList.add(new PgLogDto(pgLog));
            }

            return pgLogDtoList;
        }catch(Exception e)
        {
            return null;
        }
    }


}
