ParseTask.java 3.2 KB
package com.sunyo.office.task;

import com.sunyo.office.model.Person;
import com.tianbo.util.Bean.Bean2Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.util.*;

@Slf4j
@Component
public class ParseTask {

    //读取目录
    @Value("${custom.readDir}")
    private  String readDir;

    @Autowired
    private Configuration freemakerTemplate;

    private static ParseTask parseTask;

    @PostConstruct //通过@PostConstruct实现初始化bean之前进行的操作
    public void init() {
        parseTask = this;
        parseTask.readDir = this.readDir;
        parseTask.freemakerTemplate = this.freemakerTemplate;
    }


    @Scheduled(fixedRate = 50000)
    public void parse(){
        File fileDirectory = new File(parseTask.readDir);
        List<File> files = (List) FileUtils.listFiles(fileDirectory, new String[]{"lrmx"}, false);
        List<Person> personList = new ArrayList<>();
        if (files!=null && !files.isEmpty()){
            log.info("开始读取导入目录");
            for (File file:files
                    ) {
                Person person = new Person(file);
                build(person);
                personList.add(person);
            }
            buildExcel(personList);
        }
    }

    public void build(Person person){
        try{
            Template template = freemakerTemplate.getTemplate("moban2.ftlx");
            Map<String,Object> map = new HashMap<>();
            map = Bean2Map.object2Map(person);
            String content = FreeMarkerTemplateUtils.processTemplateIntoString(template,map);
            String filename = "./word/"+person.getXingMing()+UUID.randomUUID().toString()+".doc";
            File sendfile = new File(filename);
            FileUtils.writeStringToFile(sendfile,content,"UTF-8");
            log.info("生成word完成");
        }catch (IOException e){
            e.printStackTrace();
        }catch (TemplateException e){
            e.printStackTrace();
        }
    }

    public void buildExcel(List personList){
        try{
            Template template = freemakerTemplate.getTemplate("excel.ftlx");
            Map<String,Object> map = new HashMap<>();
            map.put("personList",personList);
            String content = FreeMarkerTemplateUtils.processTemplateIntoString(template,map);
            String filename = "./excel/"+UUID.randomUUID().toString()+".xls";
            File sendfile = new File(filename);
            FileUtils.writeStringToFile(sendfile,content,"UTF-8");
            log.info("生成excel完成");
        }catch (IOException e){
            e.printStackTrace();
        }catch (TemplateException e){
            e.printStackTrace();
        }
    }
}