package com.ycwl.basic.task;

import cn.hutool.core.date.DateUtil;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.ycwl.basic.facebody.adapter.IFaceBodyAdapter;
import com.ycwl.basic.mapper.FaceMapper;
import com.ycwl.basic.mapper.FaceSampleMapper;
import com.ycwl.basic.mapper.ScenicMapper;
import com.ycwl.basic.model.pc.faceSample.entity.FaceSampleEntity;
import com.ycwl.basic.model.pc.scenic.entity.ScenicConfigEntity;
import com.ycwl.basic.model.pc.scenic.entity.ScenicEntity;
import com.ycwl.basic.model.pc.scenic.req.ScenicReqQuery;
import com.ycwl.basic.model.pc.scenic.resp.ScenicRespVO;
import com.ycwl.basic.repository.ScenicRepository;
import com.ycwl.basic.service.pc.ScenicService;
import com.ycwl.basic.service.task.TaskFaceService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Slf4j
@SpringBootTest
@RunWith(SpringRunner.class)
public class FaceCleanerTest {
    @Autowired
    private FaceMapper faceMapper;
    @Autowired
    private TaskFaceService taskFaceService;
    @Autowired
    private FaceSampleMapper faceSampleMapper;
    @Autowired
    private ScenicMapper scenicMapper;
    @Autowired
    private FaceCleaner faceCleaner;
    @Autowired
    private ScenicService scenicService;
    @Autowired
    private ScenicRepository scenicRepository;

    @Test
    public void testA() {
        ScenicEntity scenic = scenicMapper.get(3980001650692722688L);
        log.info("当前景区{}，开始删除人脸样本", scenic.getId());
        IFaceBodyAdapter adapter = scenicService.getScenicFaceBodyAdapter(scenic.getId());
        int sampleStoreDay = 1;
        Date sampleEndDate = DateUtil.offsetDay(DateUtil.beginOfDay(new Date()), -sampleStoreDay);
        List<FaceSampleEntity> faceSampleList = faceSampleMapper.listEntityBeforeDate(scenic.getId(), sampleEndDate);
        if (faceSampleList.isEmpty()) {
            log.info("当前景区{}，人脸样本为空", scenic.getId());
            return;
        }
        faceSampleList.forEach(faceSample -> {
            boolean success = adapter.deleteFace(String.valueOf(scenic.getId()), faceSample.getId().toString());
            if (success) {
                log.info("当前景区{}，人脸样本ID{}，删除成功", scenic.getId(), faceSample.getId());
                faceSampleMapper.deleteById(faceSample.getId());
            } else {
                log.info("当前景区{}，人脸样本ID{}，删除失败", scenic.getId(), faceSample.getId());
            }
        });
    }

    @Test
    public void testC() {
        faceCleaner.deleteExpireSample();
    }

    @Test
    public void test() {
        ScenicReqQuery scenicQuery = new ScenicReqQuery();
        List<ScenicRespVO> scenicList = scenicMapper.list(scenicQuery);
        scenicList.forEach(scenic -> {
            log.info("当前景区{}，开始删除人脸样本", scenic.getName());
        });
    }

}