package io.loli.box.green;

import io.loli.box.dao.ImgFileRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

/**
 * Created by chocotan on 2016/8/6.
 */
@Configuration
@ConditionalOnProperty(name = "admin.green.enabled", havingValue = "aliyun")
public class AliGreenConfiguration {

    @Bean
    public GreenService greenService() {
        return new AliGreenService();
    }

    @Autowired
    ImgFileRepository imgFileRepository;

    @Bean
    public Object aliGreenJob(GreenService greenService) {
        return new Object() {
            @Scheduled(fixedRate = 60000)
            public void submitTask() {
                imgFileRepository.findByGreenStatus(0).forEach(img -> {
                    String taskId = greenService.asyncDetect("/images/" + img.getShortName());
                    imgFileRepository.updateTaskIdById(taskId, img.getId());
                    imgFileRepository.updateGreenStatusById(1, img.getId());
                });
            }

            @Scheduled(fixedRate = 120000)
            public void getResult() {
                imgFileRepository.findByGreenStatus(1).forEach(img -> {
                    float checkResult = greenService.getCheckResult(img.getGreenTaskId());
                    boolean porn = checkResult > 90;
                    imgFileRepository.updateGreenStatusById(porn ? 3 : 4, img.getId());
                    imgFileRepository.updateGreenPointById(checkResult, img.getId());
                });
            }
        };
    }
}

@@ -980,32 +980,65 @@
 rEach(img -%3E %7B%0D%0A
+                    try %7B%0D%0A%0D%0A    
                 
@@ -1129,32 +1129,36 @@
                 
+    
 imgFileRepositor
@@ -1211,32 +1211,36 @@
                 
+  
+  
 imgFileRepositor
@@ -1273,32 +1273,190 @@
  img.getId());%0D%0A
+                    %7D catch (Throwable throwable) %7B%0D%0A                        imgFileRepository.updateGreenStatusById(2, img.getId());%0D%0A                    %7D%0D%0A
                 
@@ -1625,32 +1625,63 @@
 rEach(img -%3E %7B%0D%0A
+                    try %7B%0D%0A    
                 
@@ -1748,32 +1748,36 @@
 reenTaskId());%0D%0A
+    
                 
@@ -1826,32 +1826,36 @@
                 
+    
 imgFileRepositor
@@ -1893,9 +1893,9 @@
 3 : 
-4
+5
 , im
@@ -1919,32 +1919,36 @@
                 
+    
 imgFileRepositor
@@ -1990,32 +1990,190 @@
  img.getId());%0D%0A
+                    %7D catch (Throwable throwable) %7B%0D%0A                        imgFileRepository.updateGreenStatusById(4, img.getId());%0D%0A                    %7D%0D%0A
                 

Add exception handle