package com.ag.web.mobile;

import com.fasterxml.jackson.databind.util.JSONPObject;
import freemarker.template.Configuration;
import freemarker.template.ObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by wanglele on 2017/3/10.
 */
@Component
@RequestMapping("/mobile")
public class MobileWordController {
    @Autowired
    MobileWordService mobileWordService;

    /**
     * 保存金额，写入数据库，并生产word文档，实现发送邮件
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/saveMoneyJsonp",method = RequestMethod.POST)
    @ResponseBody
    public JSONPObject saveMoneyJsonp (HttpServletRequest request) throws Exception {
        String callback = request.getParameter("callback");
        String type = request.getParameter("type");
        Date date = new Date();
        String Id = String.valueOf(date.getTime());
        String email = "2769277248@qq.com";
        Map dataMap = new HashMap();
        if(type.equals("money")){
            String lowUsd = request.getParameter("lowUsd");
            String capUsd = request.getParameter("capUsd");
            mobileWordService.saveMoney(request,Id,lowUsd,capUsd);
            String ftl = "借支签收单USD.ftl";
            String wordPath = "D:\\借支签收单USD.doc";
            dataMap.put("lowUsd",lowUsd);
            dataMap.put("capUsd",capUsd);
            String filePath = createDoc(ftl,wordPath,dataMap);
            if(filePath !=null){
                try {
                    // mobileWordService.sendMobileEmail(email,filePath);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return new JSONPObject(callback,"1");
        }else{
            return new JSONPObject(callback,"0");
        }



    }

    /**
     *
     *根据发送的数据创建word文件
     * @param ftl
     * @param wordPath
     * @param dataMap
     * @return
     * @throws IOException
     */
    public String createDoc (String ftl,String wordPath,Map dataMap) throws IOException {
        Configuration configuration = new Configuration();
        configuration.setDefaultEncoding("UTF-8");

        configuration.setDirectoryForTemplateLoading(new File("D:/"));
        Template t = null;
        try {
            String dataparam = ftl;
            t = configuration.getTemplate(dataparam, "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        String filePath = wordPath;
        File outFile = new File(filePath);
        Writer out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        try {
            t.process(dataMap, out, ObjectWrapper.BEANS_WRAPPER);
            out.close();
        } catch (TemplateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return filePath;
    }
}
