package cn.pluss.platform.operationRecord.impl;

import cn.pluss.platform.entity.OperationRecord;
import cn.pluss.platform.ipLocation.IpLocationService;
import cn.pluss.platform.mapper.OperationRecordMapper;
import cn.pluss.platform.operationRecord.OperationRecordService;
import cn.pluss.platform.util.IpUtils;
import cn.pluss.platform.vo.OperationRecordVO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.geoip.model.v20200101.DescribeIpv4LocationResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
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.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class OperationRecordServiceImpl extends ServiceImpl<OperationRecordMapper, OperationRecord> implements OperationRecordService {

    @Autowired
    private IpLocationService ipLocationService;

    @Override
    public List<OperationRecord> selectOne(Map map) {
        return baseMapper.selectOperationRecordOne(map);
    }

    @Override
    public Integer selectParticularsCount(Map map) {
        return baseMapper.selectParticularsCount(map);
    }

    @Override
    public Page<OperationRecordVO> pageVO(Page<OperationRecordVO> page, OperationRecordVO condition) {
        QueryWrapper<OperationRecordVO> qWrapper = Wrappers.query();
        qWrapper.ne("ua.userType", "staff");

        qWrapper.eq(StringUtils.isNotBlank(condition.getUserId()), "opr.userId", condition.getUserId());

        qWrapper.eq(StringUtils.isNotBlank(condition.getIp()), "opr.ip", condition.getIp());

        qWrapper.likeRight(StringUtils.isNotBlank(condition.getUserName()), "ua.userName ->> '$.county'", condition.getCounty());

        qWrapper.likeRight(StringUtils.isNotBlank(condition.getRemark()), "opr.remark", condition.getRemark());

        qWrapper.gt(condition.getCreateTimeStart() != null, "opr.createTime", condition.getCreateTimeStart());

        qWrapper.lt(condition.getCreateTimeEnd() != null, "opr.createTime", condition.getCreateTimeEnd());

        qWrapper.eq(StringUtils.isNotBlank(condition.getIsp()), "opr.extension ->> '$.isp'", condition.getIsp());

        qWrapper.eq(StringUtils.isNotBlank(condition.getCity()), "opr.extension ->> '$.city'", condition.getCity());

        qWrapper.eq(StringUtils.isNotBlank(condition.getProvince()), "opr.extension ->> '$.province'", condition.getProvince());

        qWrapper.eq(StringUtils.isNotBlank(condition.getCountry()), "opr.extension ->> '$.country'", condition.getCountry());

        qWrapper.eq(StringUtils.isNotBlank(condition.getCounty()), "opr.extension ->> '$.county'", condition.getCounty());

        qWrapper.eq(condition.getCount() != null, "t.count", condition.getCount());

        qWrapper.eq(Objects.equals(condition.getIsChina(), "1"), "opr.extension ->> '$.country'", "中国");

        qWrapper.and(Objects.equals(condition.getIsChina(), "0"), wrapper -> {
            wrapper.ne("opr.extension ->> '$.country'", "中国")
                    .isNotNull("opr.extension ->> '$.country'")
            ;
        });

        return baseMapper.selectVOPage(page, qWrapper);
    }

    @Override
    public boolean save(OperationRecord entity) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        if (requestAttributes != null) {
            HttpServletRequest request = requestAttributes.getRequest();
            String ipAddr = IpUtils.getIpAddr(request);
            DescribeIpv4LocationResponse location = ipLocationService.getLocation(ipAddr);

            JSONObject extra;
            if (location == null) {
                extra = new JSONObject();
            } else {
                extra = (JSONObject) JSON.toJSON(location);
            }

            if (entity.getExtension() != null) {
                extra.putAll(entity.getExtension());
            }

            entity.setExtension(extra);
        }

        return super.save(entity);
    }
}
