package com.izouma.awesomeadmin.service.impl;

import java.util.*;

import com.izouma.awesomeadmin.model.UserInfo;
import com.izouma.awesomeadmin.util.MbappUtil;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.izouma.awesomeadmin.constant.AppConstant;
import com.izouma.awesomeadmin.dto.Page;
import com.izouma.awesomeadmin.model.UserUpdateLog;
import com.izouma.awesomeadmin.service.UserUpdateLogService;
import com.izouma.awesomeadmin.dao.UserUpdateLogMapper;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
*  service接口实现类
*/
@Service
public class UserUpdateLogServiceImpl implements UserUpdateLogService{

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

    @Autowired
    private UserUpdateLogMapper userUpdateLogMapper;

    @Override
    public List<UserUpdateLog> getUserUpdateLogList(UserUpdateLog record) {

        logger.info("getUserUpdateLogList");
        try {

        return userUpdateLogMapper.queryAllUserUpdateLog(record);
        } catch (Exception e) {
        logger.error("getUserUpdateLogList", e);
        }

        return null;
    }
    @Override
    public List<UserUpdateLog> getUserUpdateLogByPage(Page page, UserUpdateLog record) {

        logger.info("getUserUpdateLogByPage");
        try {

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

        return userUpdateLogMapper.queryUserUpdateLogByPage(parameter);
        } catch (Exception e) {
        logger.error("getUserUpdateLogByPage", e);
        }

        return null;
    }

    @Override
    public UserUpdateLog getUserUpdateLogById(String id) {

        logger.info("getUserUpdateLogyId");
        try {

            return userUpdateLogMapper.selectByPrimaryKey(Integer.valueOf(id));
        } catch (Exception e) {
        logger.error("getUserUpdateLogById", e);
        }

        return null;
    }

    @Override
    public UserUpdateLog getUserUpdateLog(UserUpdateLog record) {

        logger.info("getUserUpdateLog");
        try {

            return userUpdateLogMapper.queryUserUpdateLog(record);
        } catch (Exception e) {
        logger.error("getUserUpdateLog", e);
        }

        return null;
    }

    @Override
    public boolean createUserUpdateLog(UserUpdateLog record) {

        logger.info("createUserUpdateLog");
        try {

            int updates = userUpdateLogMapper.insertSelective(record);

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

        return false;
    }

    @Override
    public boolean deleteUserUpdateLog(UserUpdateLog record) {

        logger.info("deleteUserUpdateLog");
        try {

             int updates = userUpdateLogMapper.delete(record);

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

        return false;
    }

    @Override
    public boolean updateUserUpdateLog(UserUpdateLog record) {

        logger.info("updateUserUpdateLog");
        try {

            int updates = userUpdateLogMapper.updateByPrimaryKeySelective(record);

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

        return false;
    }

    @Override
    public boolean newUserUpdateLog(UserUpdateLog record) {

        logger.info("newUserUpdateLog");
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

            String ip = MbappUtil.getIpAdderss(request);

            String userName = "未登录操作";
            Subject subject = SecurityUtils.getSubject();
            UserInfo userInfo = (UserInfo) subject.getPrincipal();
            if (userInfo != null) {
                userName = userInfo.getUsername();
            }
            record.setIp(ip);
            record.setCreateUser(userName);


            int updates = userUpdateLogMapper.insertSelective(record);

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

        return false;
    }
}

