Analysis.java 7.97 KB
package com.example.demo.service;

import com.example.demo.SaticScheduleTask;
import com.example.demo.model.TJRL;
import com.example.demo.model.TJRLDB;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Component
public class Analysis {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    private static Date startDate = subtractTime(new Date(), -300000);


    @Value("${ftp.oldFile}")
    private  String oldFile;


    /**
     * 获取路径下的所有文件/文件夹  /读取文件 返回时间和carid
     *
     * @param files 需要遍历的文件夹路径
     * @return
     */
    public  List<TJRLDB> getAllFile(List<File> files) {
        List<TJRLDB> listMap = new ArrayList<>();
        List<String> list = new ArrayList<String>();
        for (File file : files) {
            if (!file.isDirectory()) {
                String name=file.getName().substring(0,10);
                String absolutePath = file.getAbsolutePath();
                String time = (absolutePath.substring(absolutePath.indexOf("GJ"), absolutePath.indexOf("GJ") + 16)).replace("GJ", "");
                Date nowTime = null;
                try {
                    nowTime = new SimpleDateFormat("yyyyMMddHHmmss").parse(time);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                //if (belongCalendar(nowTime)) { //判断是否在对应时间内
                    list.add(file.getAbsolutePath());
                    List<String> strings = txt2String(new File(file.getAbsolutePath()));//读取文件
                    for (int i = 0; i < strings.size(); i++) {
                        String text = strings.get(i);
                        Map<String, Object> map = new HashMap<>();
                        TJRLDB tjrl=new TJRLDB();
                        Long val = Long.parseLong(text.substring(30, 40));
                        String cardId = Long.toHexString(val).toUpperCase();
                        tjrl.setTJRLDRVCRDID(cardId);//司售卡号 DriverCardId签到卡片ID号,BCD编码,司售卡卡号
                        //map.put("DriverCardType", text.substring(40, 42));//DriverCardType HEX格式,签到卡的卡型,BCD编码
                        //map.put("CHECKINDATE", text.substring(42, 54));//签到时间YYYYMMDDhhmm,BCD编码
                        //map.put("CHECKOUTCOUNT", text.substring(54, 60));//签到总次数,BCD编码
                        tjrl.setTJRLTXFG(text.substring(60, 62));//交易类型  Txn Flag HEX格式,交易标志,其中88为正常交易,99为锁卡交易,BCD编码,定义见附录1交易类型对照表
                        String TJRLPOSID=text.substring(62, 68);
                        tjrl.setTJRLPOSID(TJRLPOSID);//HEX格式,消费交易流水号,BCD编码
                        //map.put("CITYCODE", text.substring(68, 72));    //城市代码号,BCD编码
                        tjrl.setTJRLCARDNO(text.substring(72, 82));//CARDID消费卡的ID号,BCD编码
                        tjrl.setTJRLCDKIND(text.substring(82, 84));//Card Type HEX格式,消费卡卡型,BCD编码
                        tjrl.setTJRLCDBAL(text.substring(84, 92));//交易前余额 BalBef消费卡交易前金额,BCD编码 BCD编码之前最高位1表示负数,0表示正数
                        tjrl.setTJRLAMT(text.substring(92, 100));//交易金额 TxnAmount本次消费交易金额,BCD编码
                        String TJRLRDATE=text.substring(100, 108);
                        try {
                            tjrl.setTJRLRDATE(new SimpleDateFormat("yyyy-MM-dd").
                                    format(new SimpleDateFormat("yyyyMMdd").parse(text.substring(100, 108))));
                            //交易日期 本次消费交易日期,BCD编码YYYYMMDD
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        String TJRLRTIME=text.substring(108, 114);
                        try {
                            tjrl.setTJRLRTIME(new SimpleDateFormat("HH:mm:ss").
                                    format(new SimpleDateFormat("HHmmss").parse(text.substring(108, 114))));
                            //交易时间 Txn Time本次消费交易时间,BCD编码hhmmss
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        //map.put("COUNT", text.substring(114, 120));    //交易计数器,BCD编码
                        tjrl.setTJRLORGAMT(text.substring(120, 124));//交易原始金额Orignal Txn Amount
                        //map.put("CARDSPEC", text.substring(124, 126));   //卡规范,BCD编码 01—建设部卡 02—交通部卡 03-交通部二维码
                        String TAC=text.substring(126, 134);//HEX格式,交易认证码,BCD编码
                        String TRAD_ID=TAC+TJRLPOSID+TJRLRDATE+TJRLRTIME;
                        tjrl.setTRAD_ID(TRAD_ID);////pos交易数据需要加上 trad_id 用tac+pos流水+交易日期+交易时间

                        listMap.add(tjrl);
                    }
                //}
              /*  Analysis analysis=new Analysis();
                analysis.setOldFile(oldFile);*/
                File f2=new File(oldFile+File.separator+name);
                if(!f2.exists()&&!f2.isDirectory()){
                    f2.mkdir();
                }
                File f=new File(oldFile+File.separator+name+File.separator+file.getName());
                file.renameTo(f);
            }
        }
        return listMap;
    }


    /**
     * 读取文件内容
     */
    public static List<String> txt2String(File file) {
        List<String> list = new ArrayList<>();
        StringBuilder result = new StringBuilder();
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
            String s = null;
            while ((s = br.readLine()) != null) {//使用readLine方法,一次读一行
                list.add(s);
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }


    /**
     * 加减对应时间后的日期
     *
     * @param date   需要加减时间的日期
     * @param amount 加减的时间(毫秒)
     * @return 加减对应时间后的日期
     */

    private static Date subtractTime(Date date, int amount) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String strTime = sdf.format(date.getTime() + amount);
            Date time = sdf.parse(strTime);
            return time;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 判断时间是否在时间段内
     *
     * @param nowTime
     * @return
     */
    public static boolean belongCalendar(Date nowTime) {
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startDate);

        Calendar end = Calendar.getInstance();
        end.setTime(new Date());

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

   /* @Value("${ftp.oldFile}")
    public  void setOldFile(String oldFile) {
        Analysis.oldFile = oldFile;
    }*/

}