package com.dayou.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.dayou.entity.GisVisited;
import com.dayou.mapper.GisVisitedMapper;
import com.dayou.service.IGisVisitedService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dayou.utils.IpUtils;
import com.google.gson.JsonObject;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dayou.utils.ExcelUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.ArrayList;
import org.springframework.transaction.annotation.Transactional;
import com.dayou.enums.BatchTaskTypeEnum;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * GIS访问记录 服务实现类
 * </p>
 *
 * @author wucl
 * @since 2023-05-19
 */
@Service
public class GisVisitedServiceImpl extends ServiceImpl<GisVisitedMapper, GisVisited> implements IGisVisitedService {


    @Override
    @SuppressWarnings("unchecked")
    public Page<GisVisited> selectPage(Page page,GisVisited gisVisited){
        LambdaQueryWrapper<GisVisited> condition = new LambdaQueryWrapper<GisVisited>().like(StrUtil.isNotBlank(gisVisited.getRemoteAddr()), GisVisited::getRemoteAddr,gisVisited.getRemoteAddr());
        return this.page(page, condition);

    }


    @Override
    public GisVisited detail(Long id){
        return this.getById(id);
    }

    @Override
    public void add(HttpServletRequest request){
        GisVisited gisVisited = new GisVisited();
        String ip = IpUtils.getIpAddr(request);
        gisVisited.setRemoteIp(ip);
        JSONObject addresses = IpUtils.getAddresses(ip);
        List<String> host = Lists.newArrayList();
        if (addresses!=null){
            host.add((String) addresses.get("pro"));
            host.add((String) addresses.get("city"));
            host.add((String) addresses.get("region"));
            gisVisited.setRemoteAddr((String) addresses.get("addr"));
        }else {
            host.add("Unknown");
            gisVisited.setRemoteAddr("Unknown");
        }
        gisVisited.setRemoteHost(host.toString());
        this.save(gisVisited);
    }

    @Override
    public Boolean update(GisVisited gisVisited){
        return  this.updateById(gisVisited);
    }

    @Override
    public Boolean delete(Long id){
        //逻辑删除
        return this.removeById(id);
    }
}
