package cn.pluss.platform.user.impl;

import cn.pluss.platform.entity.OperationRecord;
import cn.pluss.platform.entity.UserIpRecord;
import cn.pluss.platform.ipLocation.IpLocationService;
import cn.pluss.platform.mapper.OperationRecordMapper;
import cn.pluss.platform.mapper.UserIpRecordMapper;
import cn.pluss.platform.operationRecord.OperationRecordService;
import cn.pluss.platform.user.UserIpRecordService;
import cn.pluss.platform.util.IpUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.geoip.model.v20200101.DescribeIpv4LocationResponse;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 * 用户操作IP记录表 服务实现类
 * </p>
 *
 * @author Djh
 * @since 2021-09-13
 */
@Service
public class UserIpRecordServiceImpl extends ServiceImpl<UserIpRecordMapper, UserIpRecord> implements UserIpRecordService {

    @Resource
    private OperationRecordMapper opRecordMapper;

    @Autowired
    private IpLocationService ipLocationService;

    @Override
    public void saveRegIp(String userId, String operatorUserId) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ipAddr = IpUtils.getIpAddr(request);
        UserIpRecord entity = new UserIpRecord();
        entity.setUserId(Integer.parseInt(userId));
        entity.setRegIp(ipAddr);
        entity.setRegTime(new Date());
        save(entity);

        OperationRecord opRecord = new OperationRecord();
        opRecord.setOperator(operatorUserId);
        opRecord.setUserId(userId);
        opRecord.setDictValue("APP_REG");
        opRecord.setRemark("用户注册");

        DescribeIpv4LocationResponse location = ipLocationService.getLocation(ipAddr);
        JSONObject extra = (JSONObject) JSON.toJSON(location);
        opRecord.setExtension(extra);

        opRecordMapper.insert(opRecord);
    }

    @Override
    public void saveAuditIp(String userId) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ipAddr = IpUtils.getIpAddr(request);
        UserIpRecord entity = new UserIpRecord();
        entity.setAuditTime(new Date());
        entity.setUserId(Integer.parseInt(userId));
        entity.setAuditIp(ipAddr);
        LambdaUpdateWrapper<UserIpRecord> uWrapper = Wrappers.lambdaUpdate();
        uWrapper.eq(UserIpRecord::getUserId, userId);
        boolean updateFlag = update(entity, uWrapper);
        if (!updateFlag) {
            save(entity);
        }
    }
}
