package com.xhkjedu.suser.service.system;

import com.alibaba.fastjson.JSON;
import com.xhkjedu.suser.config.ConfigKey;
import com.xhkjedu.suser.mapper.system.UserMapper;
import com.xhkjedu.suser.model.system.TUser;
import com.xhkjedu.suser.model.system.TUserLog;
import com.xhkjedu.vo.system.UserVo;
import com.xhkjedu.utils.N_Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description 登录日志
 * @Author WN
 * @Date 2023/2/20 15:54
 **/
@Service
@Slf4j
public class UserLogService {
    @Resource
    private UserMapper userMapper;

    //保存登录日志


    //用户登录信息同步到云平台
    @Async("asyncPoolTaskExecutor")
    public void saveSyncUserLogin(UserVo userVo, TUser user, TUserLog userLog) {
        Map<String, Object> params = new HashMap<>();
        if(userVo == null){
            //获取当前区域码
            String cloudcode = userMapper.getCloudCode();
            params.put("cloudcode", cloudcode);
            params.put("schoolname", "");
            params.put("userid", user.getUserid());
            params.put("loginname", user.getLoginname());
            params.put("username", "");
            params.put("year",null);

        }else{
            params.put("cloudcode", userVo.getCloudcode());
            params.put("schoolname", userVo.getSchoolname());
            params.put("userid", userVo.getUserid());
            params.put("loginname", userVo.getLoginname());
            params.put("username", userVo.getUsername());
            params.put("year",userVo.getYear());
        }
        params.put("deviceband", user.getUltypeband());
        params.put("devicemodel", user.getUltypemodel());
        params.put("mac", user.getMac());
        params.put("versioncode", user.getVersionnum());
        params.put("systemversion", user.getSystemversion());
        params.put("systembit", user.getSystembit());
        params.put("usertype", userLog.getUsertype());
        params.put("device", userLog.getUltype());
        params.put("logip", userLog.getLogip());
        params.put("logaddress", userLog.getLogaddress());
        params.put("logtime", userLog.getLogtime());
        params.put("loginstate", userLog.getLoginstate());
        params.put("logtxt",userLog.getLogtxt());

        try {
            N_Utils.sendJsonPost(ConfigKey.cloudapi + "rlinfo/save", JSON.toJSONString(params));
        } catch (Exception e) {
            log.error("同步登录日志到云平台失败：" + e.getMessage());
        }
    }
}
