package com.manage.service.ipml;

import com.manage.dao.Power_LogMapper;
import com.manage.entity.Power_Log;
import com.manage.entity.Power_User;
import com.manage.service.LogService;
import com.manage.vo.Power_UserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @ProjectName:
 * @Description:
 * @Param 传输参数
 * @Return
 * @Author: 曾文和
 * @CreateDate: 2019/7/10 10:33
 * @UpdateUser: 曾文和
 * @UpdateDate: 2019/7/10 10:33
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Service
@Transactional
public class LogServiceImpl implements LogService {
    @Autowired
    private Power_LogMapper logMapper;
    @Override
    public void insert(Power_Log log) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        //设置创建者
        if(StringUtils.isBlank(log.getCreater())){
            Power_UserVo user = (Power_UserVo)request.getSession().getAttribute("CURRENT_USER");
            log.setCreater(user.getUserName());
        }
        // 获取系统当前时间
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        log.setCreateDate(fmt.format(new Date()));
        //获取IP
        String ipAddress = getAddressIp(request);
        log.setSysFlag("power");
        log.setIp(ipAddress);
        logMapper.insert(log);
    }

    @Override
    public void autoInsert(Power_Log log) {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        log.setCreateDate(fmt.format(new Date()));
        log.setSysFlag("power");
        logMapper.insert(log);
    }

    @Override
    public void deleteLogById(Integer logId) {
        logMapper.deleteByPrimaryKey(logId);
    }

    @Override
    public Map<String,Object> selectObjectByKey(String tableName, String whereSql) {
        return logMapper.selectObjectByKey(tableName, whereSql);
    }

    @Override
    public int delLogsByDate(String date) {
        return logMapper.delLogsByDate(date);
    }

    @Override
    public void deleteLogByIds(String str) {
        logMapper.deleteLogByIds(str);
    }

    private String getAddressIp( HttpServletRequest request){
        // 获取访问真实IP
        String ipAddress = request.getHeader("x-forwarded-for");
        if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }
        if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr();
            if(ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")){
                //根据网卡取本机配置的IP
                InetAddress inet=null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                ipAddress= inet.getHostAddress();
            }
        }
        //对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
        if(ipAddress!=null && ipAddress.length()>15){ //"***.***.***.***".length() = 15
            if(ipAddress.indexOf(",")>0){
                ipAddress = ipAddress.substring(0,ipAddress.indexOf(","));
            }
        }
        return ipAddress;
    }
}
