package com.sonic.shark.service.impl;

import com.sonic.common.AppRuntime;
import com.sonic.common.exception.BizExceptionUtils;
import com.sonic.shark.enums.ToastResultCode;
import com.sonic.shark.service.FileUploadLogService;
import com.sonic.shark.service.ImageCheckService;
import com.sonic.shark.utils.MD5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.sonic.shark.utils.Constants.DEFAULT_IMAGE_LIST;

/**
 * @Author code
 * @Date 2021/1/25 13:43
 * @Version 1.0
 */
@Slf4j
@Service
public class ImageCheckServiceImpl implements ImageCheckService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private AppRuntime appRuntime;

    @Autowired
    private FileUploadLogService uploadLogService;


    @Override
    public void addImageCheckResult(String url) {
        //将鉴黄校验通过的 数据保存到redis中 保存1个小时，1小时内提交的数据才是有效的，否则无效，不能上传图片上来
        String urlMd5 = MD5Util.digest(url);
        String redisKey = appRuntime.buildPrefixKey("image", "check", urlMd5);
        log.info("===> addImageCheckResult url : {}, redisKey : {}", url, redisKey);
        stringRedisTemplate.opsForValue().set(redisKey, "1", 24, TimeUnit.HOURS);
    }

    @Override
    public void checkImageIsPassed(String url) {
        if(DEFAULT_IMAGE_LIST.contains(url)) {
            return;
        }
        String urlMd5 = MD5Util.digest(url);
        String redisKey = appRuntime.buildPrefixKey("image", "check", urlMd5);
        Boolean bl = stringRedisTemplate.hasKey(redisKey);
        //无效的图片请求地址
        BizExceptionUtils.check(!bl, "", "Invalid request image address");
    }

    @Override
    public void checkImageIsPassed(List<String> urls) {
        for (String url : urls) {
            checkImageIsPassed(url);
        }
        //确认图片
        confirmImage(null, urls);
    }

    @Override
    public void confirmImage(Long userId, List<String> urls) {
        uploadLogService.confirm(userId, urls);
    }

    @Override
    public void addHumanIdentificationCheckResult(String url) {
        String urlMd5 = MD5Util.digest(url);
        String redisKey = appRuntime.buildPrefixKey("image","humanIdentification","check", urlMd5);
        stringRedisTemplate.opsForValue().set(redisKey, "1", 24, TimeUnit.HOURS);
    }

    @Override
    public void checkHumanIdentificationIsPassed(String url) {
        String urlMd5 = MD5Util.digest(url);
        String redisKey = appRuntime.buildPrefixKey("image","humanIdentification","check", urlMd5);
        Boolean bl = stringRedisTemplate.hasKey(redisKey);
        //无效的图片请求地址
        ToastResultCode.INVALID_REQUEST_IMAGE_ADDRESS.check(!bl);
    }

    @Override
    public void checkHumanIdentificationIsPassed(List<String> urls) {
        for (String url : urls) {
            checkHumanIdentificationIsPassed(url);
        }
    }
}
