package com.hfkj.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.utils.AmapApiService;
import com.hfkj.common.utils.RequestUtils;
import com.hfkj.dao.SecUserLoginLogMapper;
import com.hfkj.entity.SecUser;
import com.hfkj.entity.SecUserLoginLog;
import com.hfkj.entity.SecUserLoginLogExample;
import com.hfkj.service.SecUserLoginLogService;
import com.hfkj.sysenum.SecUserLoginLogStatusEnum;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @className: SecUserLoginLogServiceImpl
 * @author: HuRui
 * @date: 2024/4/3
 **/
@Service("secUserLoginLogService")
public class SecUserLoginLogServiceImpl implements SecUserLoginLogService {

    @Resource
    private SecUserLoginLogMapper secUserLoginLogMapper;

    @Override
    public void create(SecUserLoginLog userLoginLog) {
        userLoginLog.setCreateTime(new Date());
        secUserLoginLogMapper.insert(userLoginLog);
    }

    @Override
    public void asyncCreateLog(SecUser user, HttpServletRequest request) {
        // 创建一个单线程的线程池
        ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
        // 异步记录登录信息
        singleThreadExecutor.submit(new Runnable() {
            @Override
            public void run() {
                SecUserLoginLog loginLog = new SecUserLoginLog();
                loginLog.setUserId(user.getId());
                loginLog.setUserLoginName(user.getLoginName());
                loginLog.setIp(RequestUtils.getIpAddress(request));
                // 查询ip归属地
                JSONObject ipAddress = AmapApiService.queryIp(loginLog.getIp());
                if (ipAddress != null) {
                    loginLog.setRegionName(StringUtils.isNotBlank(ipAddress.getString("province"))?ipAddress.getString("province"):"未知");
                    loginLog.setCityId(StringUtils.isNotBlank(ipAddress.getString("adcode"))?ipAddress.getString("adcode"):null);
                    loginLog.setCityName(StringUtils.isNotBlank(ipAddress.getString("city"))?ipAddress.getString("city"):"未知");
                    loginLog.setStatus(SecUserLoginLogStatusEnum.status1.getCode());
                } else {
                    loginLog.setCountry("未知");
                    loginLog.setRegionName("未知");
                    loginLog.setCityName("未知");
                    loginLog.setIsp("未知");
                    loginLog.setStatus(SecUserLoginLogStatusEnum.status2.getCode());
                }
                create(loginLog);
            }
        });
        singleThreadExecutor.shutdown();
    }

    @Override
    public List<SecUserLoginLog> getLogList(Map<String, Object> param) {
        SecUserLoginLogExample example = new SecUserLoginLogExample();
        SecUserLoginLogExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getLong(param, "userId") != null) {
            criteria.andUserIdEqualTo(MapUtils.getLong(param, "userId"));
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "userLoginName"))) {
            criteria.andUserLoginNameEqualTo(MapUtils.getString(param, "userLoginName"));
        }

        if (MapUtils.getInteger(param, "status") != null) {
            criteria.andStatusEqualTo(MapUtils.getInteger(param, "status"));
        }

        example.setOrderByClause("create_time desc");
        return secUserLoginLogMapper.selectByExample(example);
    }


}
