package com.hfkj.service.user.impl;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.alipay.AliyunService;
import com.hfkj.common.utils.RequestUtils;
import com.hfkj.dao.BsUserLoginLogMapper;
import com.hfkj.entity.BsUser;
import com.hfkj.entity.BsUserLoginLog;
import com.hfkj.entity.BsUserLoginLogExample;
import com.hfkj.service.user.BsUserLoginLogService;
import com.hfkj.sysenum.SecUserLoginLogStatusEnum;
import com.hfkj.sysenum.user.UserLoginPlatform;
import com.hfkj.sysenum.user.UserLoginType;
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: BsUserLoginLogServiceImpl
 * @author: HuRui
 * @date: 2024/5/6
 **/
@Service("userLoginLogService")
public class BsUserLoginLogServiceImpl implements BsUserLoginLogService {
    @Resource
    private BsUserLoginLogMapper userLoginLogMapper;

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

    @Override
    public void asyncCreateLog(UserLoginPlatform loginPlatform, UserLoginType loginType, BsUser user, String agentAppid, HttpServletRequest request) {
        // 创建一个单线程的线程池
        ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
        // 异步记录登录信息
        singleThreadExecutor.submit(new Runnable() {
            @Override
            public void run() {
                BsUserLoginLog loginLog = new BsUserLoginLog();
                loginLog.setPlatformCode(loginPlatform.getCode());
                loginLog.setPlatformName(loginPlatform.getName());
                loginLog.setLoginTypeCode(loginType.getCode());
                loginLog.setLoginTypeName(loginType.getName());
                loginLog.setUserId(user.getId());
                loginLog.setAgentAppid(agentAppid);
                loginLog.setIp(RequestUtils.getIpAddress(request));
                // 查询ip归属地
                JSONObject ipAddress = AliyunService.queryAddress(loginLog.getIp());
                if (ipAddress != null) {
                    loginLog.setCountry(StringUtils.isNotBlank(ipAddress.getString("country"))?ipAddress.getString("country"):"未知");
                    loginLog.setRegionId(StringUtils.isNotBlank(ipAddress.getString("region_id"))?ipAddress.getString("region_id"):null);
                    loginLog.setRegionName(StringUtils.isNotBlank(ipAddress.getString("region"))?ipAddress.getString("region"):"未知");
                    loginLog.setCityId(StringUtils.isNotBlank(ipAddress.getString("city_id"))?ipAddress.getString("city_id"):null);
                    loginLog.setCityName(StringUtils.isNotBlank(ipAddress.getString("city"))?ipAddress.getString("city"):"未知");
                    loginLog.setIsp(StringUtils.isNotBlank(ipAddress.getString("isp"))?ipAddress.getString("isp"):"未知");
                    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<BsUserLoginLog> getLogList(Map<String, Object> param) {
        BsUserLoginLogExample example = new BsUserLoginLogExample();
        BsUserLoginLogExample.Criteria criteria = example.createCriteria();

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

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

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