package com.casic.PgInterface.overtimeInPG.manager;

import com.casic.PgInterface.core.hibernate.HibernateEntityDao;
import com.casic.PgInterface.core.util.StringUtils;
import com.casic.PgInterface.overtimeInPG.domain.PGInOutRec;
import com.casic.PgInterface.overtimeInPG.dto.PGInOutRecDto;
import org.hibernate.Criteria;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class PGInOutRecManager extends HibernateEntityDao<PGInOutRec> {
    public Map<String,Object> getInOutRec(String timeStart ,String timeEnd)
    {
        Map<String,Object> retMap = new HashMap<>();
        String formatStr=null;
        if (timeStart.length()==19)
        {
            formatStr="yyyy-MM-dd HH:mm:ss";
        }
        else
            formatStr="yyyy-MM-dd";

        Criteria criteria = createCriteria(PGInOutRec.class);
//        Disjunction disjunction = Restrictions.disjunction();
        if (StringUtils.isNotBlank(timeStart)&&StringUtils.isNotBlank(timeEnd))
        {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatStr);
            Date dateStart = null;
            Date dateEnd = null;
            try {
                dateStart = simpleDateFormat.parse(timeStart);
                dateEnd = simpleDateFormat.parse(timeEnd);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            criteria.add(Restrictions.or(Restrictions.between("inTime",dateStart,dateEnd),Restrictions.between("outTime",dateStart,dateEnd)));
        }
        criteria.addOrder(Order.desc("id"));
        List<PGInOutRec> list = criteria.list();
        List<PGInOutRecDto> pgInOutRecDtoList = new ArrayList<>();
        pgInOutRecDtoList = PGInOutRecDto.ConvertToDtoList(list);
        retMap.put("msg","success");
        retMap.put("result",pgInOutRecDtoList);
        return retMap;
    }
}
