package com.utils;

import com.jfinal.plugin.activerecord.Record;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.*;

public class MarcUtil {


    public static List<Map<String,String>> showMarc(byte[] bytes) {
        List<Map<String,String>> bookList = new ArrayList<Map<String,String>>();
        try {
            // 读取此条数据的总长度
            byte marcB[] = new byte[5];
            for (int i = 0; i < 5; i++) {
                marcB[i] = bytes[i];
            }
            String marcS = new String(marcB);
            int marcLen = Integer.parseInt(marcS);

            // 读取数据基地址
            byte marcB2[] = new byte[5];
            for (int i = 0; i < 5; i++) {
                marcB2[i] = bytes[i + 12];
            }
            String marcS2 = new String(marcB2);
            int dataStart = Integer.parseInt(marcS2);

            int cmLength = dataStart - 24 - 1;
            byte marcB3[] = new byte[cmLength];
            for (int i = 0; i < cmLength; i++) {
                marcB3[i] = bytes[i + 24];
            }
            // 读取记录控制信息
            String marcS3 = new String(marcB3);
            int n = cmLength / 12;
            String controls[] = new String[n];
            for (int i = 0; i < n; i++) {
                controls[i] = marcS3.substring(i * 12, (i + 1) * 12);

            }
            // 读取数据区信息
            int dataLength = marcLen - dataStart - 1;
            byte data[] = new byte[dataLength];
            for (int i = 0; i < dataLength; i++) {
                data[i] = bytes[i + dataStart];
            }
            String OKData[][] = new String[n][2];
            Map<String,String> bookMap =new LinkedHashMap<>();
            for (int i = 0; i < n; i++) {
                OKData[i][0] = controls[i].substring(0, 3);
                int length = Integer.parseInt(controls[i].substring(3, 7));
                int start = Integer.parseInt(controls[i].substring(7));
                //不取每个字段数据最后的结束符
                byte temp[] = new byte[length-1];
                for (int j = start; j < start + length-1; j++) {
                    if(data[j]==31){
                        //分隔符
                        temp[j - start] = 124;
                        //TODO 处理char30
                    }else{
                        temp[j - start] = data[j];
                    }
                }
                OKData[i][1] = new String(temp,"GBK");
                for (String item :OKData[i][1].split("[|]")){
                    if(CommonUtils.isNotNullOrNotEmpty(bookMap.get(OKData[i][0]+item.substring(0,1)))){
                        bookMap.put(OKData[i][0]+item.substring(0,1),bookMap.get(OKData[i][0]+item.substring(0,1))+"|"+item.substring(1));
                    } else {
                        bookMap.put(OKData[i][0]+item.substring(0,1),item.substring(1));
                    }
                }

            }
            bookList.add(bookMap);
        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            return bookList;
        }
    }



    public static void main(String[] args) throws Exception {
        MarcUtil util=new MarcUtil();
        List<String> bookLineStrList =FileUtils.readLines(new File("/Users/tianlumei/Downloads/temp/aa.iso"),"GBK");
        for (String book : bookLineStrList){
          List<Map<String,String>>  bookList=util.showMarc(book.getBytes("GBK"));
          for (Map<String,String> bookMap : bookList) {
              System.out.println(bookMap);
          }

        }
    }

    public static Record mapToRecord(Map map,List<Record> expRecordList) {
        Record record = new Record();
        Set<Map.Entry<String, String>> entries = map.entrySet();
        Iterator<Map.Entry<String, String>> iteratorMap = entries.iterator();
        while (iteratorMap.hasNext()){
            Map.Entry<String, String> next = iteratorMap.next();
            String type = next.getKey();
            String content = next.getValue().trim();
            if(!"".equals(type) && !"".equals(content)) {
                for (int i = 0; i < expRecordList.size(); i ++ ) {
                    Record expRecord = expRecordList.get(i);
                    String exp = expRecord.get("exp");
                    String sysfield = expRecord.get("formname");
                    if(exp.equals(type)){
                        if(sysfield.equals("totalpages") || sysfield.equals("beanPlus.totalpages")){
                            record.set(sysfield,content.indexOf("页")>-1?content.substring(0,content.indexOf("页")):content);
                        } else if(sysfield.equals("price") || sysfield.equals("bean.price")){
                            record.set(sysfield,content.indexOf("元")>-1?content.replace("元",""):
                                    content.indexOf("RMB")>-1?content.replace("RMB",""):content.indexOf("CNY")>-1?content.replace("CNY",""):content);
                        } else {
                            record.set(sysfield,content);
                        }
                    }else{
                        continue;
                    }
                }
            }
        }
        return record;
    }
}
