package com.izouma.awesomeadmin.service.impl;

import com.izouma.awesomeadmin.constant.AppConstant;
import com.izouma.awesomeadmin.dao.UsersSessionMapper;
import com.izouma.awesomeadmin.dto.Page;
import com.izouma.awesomeadmin.model.UsersSession;
import com.izouma.awesomeadmin.service.UsersSessionService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class UsersSessionServiceImpl implements UsersSessionService {

    private static Logger logger = Logger.getLogger(UsersSessionServiceImpl.class);

    @Autowired
    private UsersSessionMapper usersSessionMapper;


    @Override
    public List<UsersSession> getUsersSessionList(UsersSession usersSession) {

        logger.info("getUsersSessionList");
        try {
            Map<String, Object> parameter = new HashMap<String, Object>();
            parameter.put("userId", usersSession.getUserId());
            parameter.put("storeId", usersSession.getStoreId());
            parameter.put("type", usersSession.getType());
            parameter.put("beginTime", usersSession.getBeginTime());
            parameter.put("endTime", usersSession.getEndTime());
            return usersSessionMapper.queryAllUsersSession(parameter);
        } catch (Exception e) {
            logger.error("getUsersSessionList", e);
        }
        return null;
    }


    @Override
    public List<UsersSession> queryAllUsersSessionByPage(Page page, UsersSession usersSession) {

        logger.info("queryAllUsersSessionByPage");
        try {
            Map<String, Object> parameter = new HashMap<String, Object>();
            parameter.put(AppConstant.PAGE, page);
            parameter.put("userId", usersSession.getUserId());
            parameter.put("storeId", usersSession.getStoreId());
            parameter.put("type", usersSession.getType());
            parameter.put("beginTime", usersSession.getBeginTime());
            parameter.put("endTime", usersSession.getEndTime());
            return usersSessionMapper.queryAllUsersSessionByPage(parameter);
        } catch (Exception e) {
            logger.error("queryAllUsersSessionByPage", e);
        }
        return null;
    }


    @Override
    public List<UsersSession> getUsersSessionByPage(Page page, UsersSession usersSession) {

        logger.info("getUsersSessionByPage");
        try {

            Map<String, Object> parameter = new HashMap<String, Object>();
            parameter.put("usersSession", usersSession);
            parameter.put(AppConstant.PAGE, page);

            return usersSessionMapper.queryUsersSessionsByPage(parameter);
        } catch (Exception e) {
            logger.error("getUsersSessionByPage", e);
        }
        return null;
    }


    @Override
    public UsersSession getUsersSessionById(String id) {

        logger.info("getUsersSessionById");
        try {

            return usersSessionMapper.selectByPrimaryKey(Integer.valueOf(id));
        } catch (Exception e) {
            logger.error("getUsersSessionById", e);
        }
        return null;
    }


    @Override
    public UsersSession getUsersSession(UsersSession usersSession) {

        logger.info("getUsersSession");
        try {

            return usersSessionMapper.queryUsersSession(usersSession);
        } catch (Exception e) {
            logger.error("getUsersSession", e);
        }
        return null;
    }


    @Override
    public boolean createUsersSession(UsersSession usersSession) {

        logger.info("createUsersSession");
        try {
            UsersSession us = usersSessionMapper.queryUsersSession(usersSession);
            if (us != null) {
                return true;
            }
            usersSession.setCreateTime(new Date());
            int updates = usersSessionMapper.insertSelective(usersSession);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("createUsersSession", e);
        }
        return false;
    }


    @Override
    public boolean deleteUsersSession(String id) {

        logger.info("deleteUsersSession");
        try {

            int updates = usersSessionMapper.delete(id);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("deleteUsersSession", e);
        }
        return false;
    }


    @Override
    public boolean updateUsersSession(UsersSession usersSession) {

        logger.info("updateUsersSession");
        try {

            int updates = usersSessionMapper.updateByPrimaryKeySelective(usersSession);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("updateUsersSession", e);
        }
        return false;
    }

}
