package org.iii.influx.test.task;

import java.io.IOException;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.iii.influx.config.InfluxDBTemplate;
import org.iii.influx.rest.model.JpyResource;
import org.influxdb.dto.Point;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author yongzan
 * @date 2016/11/14 
 * {@link http://tw.rter.info/json.php?t=currency&q=check&iso=JPY}
 */
@Component
@ConditionalOnExpression("${database.influxdb.jpyrate:false}")
public class JpyRateTask{
    private static final Logger logger = LoggerFactory.getLogger(JpyRateTask.class);
    
    @Autowired
    private InfluxDBTemplate dbTemplate;

    @Autowired
    private ObjectMapper mapper;

    @PostConstruct
    private void initTask(){
        logger.info("task database.influxdb.jpyrate : true");        
    }

    @Scheduled(cron = "0 0/1 * * * ?")
    public void parser() throws IOException {
        String url = "http://tw.rter.info/json.php?t=currency&q=check&iso=JPY";
        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder().url(url).build();
        Response resp = client.newCall(request).execute();
        String response = resp.body().string();
        
        JpyResource json = mapper.readValue(response, JpyResource.class);
        
        List<Point> preProcessPoints = json.getData().stream()
        .filter(predicate -> predicate.size()>=5 )
        .map(mapper -> mapper.stream().collect(Collectors.toList()) )
        .collect(Collectors.toList())
        .stream()
        .map(lists-> newPoint(lists))        
        .collect(Collectors.toList());
        
        dbTemplate.write(preProcessPoints);
    }
    private final String regex = "[-+\"`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？A-z0-9\\s]";
    
    private Point newPoint(List<String> list) {
        return Point.measurement("exchange_rate").time(OffsetDateTime.now().toInstant().toEpochMilli(), TimeUnit.MILLISECONDS)
                .tag("BankTag",   list.get(0).replaceAll(regex, "") )
                .addField("BankRaw", list.get(0))
                .addField("BankName",  list.get(0).replaceAll(regex, "") )
                .addField("BuyIn", list.get(1))
                .addField("BuyOut", list.get(2) )
                .addField("LastTime", list.get(3) )
                .addField("other", list.get(4) )
                .build();
    }

}

@@ -2314,56 +2314,344 @@
     
-%0A    private Point newPoint(List%3CString%3E list) %7B
+private final static String regex_SWIFT=%22(%5Ba-zA-Z%5D%7B4%7D%5Ba-zA-Z%5D%7B2%7D%5Ba-zA-Z0-9%5D%7B2%7D(%5Ba-zA-Z0-9%5D%7B3%7D)?)%22;%0A    //http://tw.fnzcode.com/swift/taiwan/%0A%0A    private Point newPoint(List%3CString%3E list) %7B%0A        boolean filterFlag = list.get(0).matches(regex_SWIFT);%0A        String tag = (filterFlag) ? list.get(0) :list.get(0).replaceAll(regex, %22%22);
 %0A   
@@ -2811,42 +2811,12 @@
 %22,  
- list.get(0).replaceAll(regex, %22%22)
+tag 
  )%0A 
@@ -2907,41 +2907,11 @@
 %22,  
-list.get(0).replaceAll(regex, %22%22)
+tag
  )%0A 

append filter for bank swift code.