/**
 *
 */
package com.foc.offline;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.foc.conf.GConstants;
import com.foc.sound.entity.CallRecordsEntity;
import com.foc.sound.entity.SoundRecordingEntity;
import com.foc.sound.service.SoundService;

/**
 * 
 * @team BBDTEK IT Team
 * @author renmb
 * @version 1.0
 * @time  2017年8月9日
 */
public class SoundCheckMain {
    private static List<SoundRecordingEntity> getList(JSONObject result) {

        String status = (String) result.get("status");
        JSONObject dataJson = (JSONObject) result.get("data");
        JSONArray jsonArray = (JSONArray) dataJson.get("list");
        if ("2000".equals(status)) {
            List<SoundRecordingEntity> list = JSONObject.parseArray(jsonArray.toJSONString(),
                    SoundRecordingEntity.class);
            /*
             * SoundRecordingResponseEntity responseEntity =
             * JSON.parseObject(jsonArray, SoundRecordingResponseEntity.class);
             * if (responseEntity != null) {
             * 
             * return responseEntity.getList();
             * 
             * }
             */
            return list;
        }
        return null;
    }
    /**
     * 
     * @author renmb
     * @time  2017年8月9日
     * @param args
     * @throws IOException 
     * @throws FileNotFoundException 
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {
        // TODO Auto-generated method stub

        SoundService service = new SoundService();
        String start = "2017-08-07 14:23:00";
        String end   = "2017-08-07 14:26:00";
        
//        File f = new File("D:\\home\\20170808\\");
//        File[] files = f.listFiles();
//        for(File log:files){
            
//            File log = new File("D:\\home\\201708160010\\sound.20170816001003.page.1.json");
            File log = new File("D:\\home\\201708160010\\sound.20170816001019.page.2.json");
//            File log = new File("D:\\home\\201708160010\\sound.20170816001003.page.2.json");
//            File log = new File("D:\\home\\201708160010\\sound.20170816001003.page.2.json");
//            File log = new File("D:\\home\\201708160010\\sound.20170816001003.page.2.json");
//            File log = new File("D:\\home\\201708160010\\sound.20170816001003.page.2.json");
//            File log = new File("D:\\home\\201708160010\\sound.20170816001003.page.2.json");
//            File log = new File("D:\\home\\201708160010\\sound.20170816001003.page.2.json");
            String text = IOUtils.toString(new FileInputStream(log));
            JSONObject result = JSON.parseObject(text);
            List<SoundRecordingEntity> list = getList(result);
            List<CallRecordsEntity> ls = service.getCallRecords(start, end, GConstants.YES);
            for (SoundRecordingEntity entity : list) {

                String content = entity.getCallNo() + entity.getAcceptNo() + entity.getBeginTime() + entity.getEndTime()
                + entity.getDuration() + entity.getDownloadUrl();
                // 根据MD5生成主键
                String id = DigestUtils.md5Hex(content);
                entity.setId(id);
        
                if("0018789350896".equals(entity.getAcceptNo())){
                    System.out.println(JSON.toJSONString(entity));
                    service.insertData(entity);
                }
                
//                if("76fef4ea009347daec8effb7826b1eae".equals(id)){
//                    System.out.println(log.getName()+"::"+entity.getId()+"::"+entity.getCallNo()+"::" + entity.getAcceptNo());
//
//                    service.insertData(entity);
//                    service.updateRecordsAndPhone(entity, ls, GConstants.YES);
//                }
            }
//        }
        
    }

}
