package com.izouma.awesomeadmin.service.impl;

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

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.izouma.awesomeadmin.constant.AppConstant;
import com.izouma.awesomeadmin.dao.SuperUserMapper;
import com.izouma.awesomeadmin.dto.Page;
import com.izouma.awesomeadmin.model.SuperUser;
import com.izouma.awesomeadmin.service.SuperUserService;

@Service
public class SuperUserServiceImpl implements SuperUserService {

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

    @Autowired
    private SuperUserMapper superUserMapper;


    @Override
    public List <SuperUser> getSuperUserList() {

        logger.info("getSuperUserList");

        return superUserMapper.queryAllUsers();
    }


    @Override
    public List <SuperUser> getSuperUserByPage(Page page) {

        logger.info("getSuperUserByPage");
        Map <String, Object> parameter = new HashMap <String, Object>();
        SuperUser tblUser = new SuperUser();
        parameter.put("tblUser", tblUser);
        parameter.put(AppConstant.PAGE, page);

        return superUserMapper.queryAllUsersByPage(parameter);
    }


    @Override
    public SuperUser getSuperUserById(String id) {

        logger.info("getSuperUserById  id=" + id);
        return superUserMapper.queryUserById(id);
    }


    @Override
    public boolean updateSuperUserInfo(HttpServletRequest request) {

        Boolean isSuccess = false;
        String userId = (String) request.getSession().getAttribute("userId");
        if (userId == null) {
            userId = "7";
        }

        SuperUser userInfo = new SuperUser();
        userInfo.setUserId(Integer.parseInt(userId));
        userInfo.setUserName(request.getParameter("userName"));
        userInfo.setPower(Integer.parseInt(request.getParameter("userName")));

        Map <String, Object> parameter = new HashMap <String, Object>();
        parameter.put("userInfo", userInfo);
        parameter.put("userId", userInfo.getUserId());
        int key = superUserMapper.updateUserInfo(parameter);
        if (key > 0) {
            isSuccess = true;
        }
        return isSuccess;
    }


    @Override
    public boolean createUser(SuperUser superUser) {

        Boolean isSuccess = false;
        int key = superUserMapper.save(superUser);
        if (key > 0) {
            isSuccess = true;
        }
        return isSuccess;
    }


    @Override
    public boolean deleteUser(String userId) {

        Boolean isSuccess = false;
        int key = superUserMapper.delete(userId);
        if (key > 0) {
            isSuccess = true;
        }
        return isSuccess;
    }


    @Override
    public boolean updateUser(SuperUser superUser) {

        boolean isSuccess = false;

        Map <String, Object> parameter = new HashMap <String, Object>();
        parameter.put("userInfo", superUser);
        parameter.put("userId", superUser.getUserId());
        int key = superUserMapper.updateUserInfo(parameter);
        if (key > 0) {
            isSuccess = true;
        }
        return isSuccess;
    }


    @Override
    public SuperUser UserLogin(String userName, String password) {

        logger.info("UserLogin  userName=" + userName + "  password=" + password);

        Map <String, Object> parameter = new HashMap <String, Object>();
        parameter.put("userName", userName);
        parameter.put("password", password);
        return superUserMapper.userLogin(parameter);
    }

}
