package com.ruoyi.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.sql.SqlUtil;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.ICompanyService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Decoder;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.ruoyi.common.utils.ImgUtils.convertFileToBase64;

/**
 * 企业Service业务层处理
 *
 * @author xu
 * @date 2019-08-22
 */
@Service
public class CompanyServiceImpl implements ICompanyService {
    @Autowired
    private InfoMapper infoMapper;
    @Autowired
    private QyInfoMapper qyInfoMapper;
    @Autowired
    private GtgsywMapper gtgsywMapper;
    @Autowired
    private QyCzrxxMapper qyCzrxxMapper;

    @Autowired
    private QyPersonMapper qyPersonMapper;
    /**
     * 查询企业待审核
     * @param flg 状态(0,1)
     * @return
     */
    @Override
    public List<QyInfo> selectFlgInfo(String flg,QyInfo info) {
        info.setFlg(flg);
        startPage();
        if(!info.getHangye2().equals("")) {
            String Seime = info.getHangye1() + " 00:00:00";
            String Eeime = info.getHangye2() + " 23:59:59";
            info.setHangye1(Seime);
            info.setHangye2(Eeime);
        }
        return infoMapper.selectFlgInfo(info);
    }

    /**
     * 只查看自己(管理员)审核中的企业
     * @param flg 状态
     * @param userId 登录管理员的Id
     * @return
     */
    @Override
    public List<QyInfo> selectFlgInfoI(String flg, String userId,QyInfo info) {
        //状态
        info.setFlg(flg);
        //审核人id
        info.setShenhenameid(userId);
        return infoMapper.selectFlgInfoI(info);
    }

    /**
     * id查询info(企业审核表)详细信息
     */
    @Override
    public QyInfo selectIdAll(String id) {
        return infoMapper.selectIdAll(id);
    }

    /**
     * 根据id修改企业为审核中状态(1)
     * @param id
     * @return
     */
    @Override
    public int alteraudit(String id,Long userId) {

        String userName=gtgsywMapper.selectUserName(userId);
        //如果当前状态为0(待审核)就修改成1(审核中)
        if ("0" .equals(infoMapper.selectIdAll(id).getFlg())) {
            return infoMapper.updateFlg(id,userName,userId.toString());
        } else {
            return 0;
        }
    }


    /**
     * 查询企业申请来源字典
     * @return
     */
    @Override
    public List<String> selectQyApply() {
        return infoMapper.selectQyApply();
    }

    /**
     * 保存不予受理
     * @param id 不予受理id
     * @param userId 当前登录管理员id
     * @param returnReason 不予受理理由
     * @return
     */
	   @Override
    public int updateSend(String id, Long userId, String returnReason) {
        //把退回id改成退回状态(3)
        QyInfo info=new QyInfo();
        //退回id
        info.setId(Long.valueOf(id));
        //审核管理员id
        info.setShenhenameid(userId.toString());
        //审核管理员名字
        String userName=gtgsywMapper.selectUserName(userId);
        info.setShenhename(userName);
        //退回原因
        info.setShenhenote(returnReason);
        //审核时间
        info.setShenhedate(new Date());
       int i= infoMapper.updateSend(info);
        return i;
    }


    @Override
    public List<String> selectqyeasy() {
        return infoMapper.selectqyeasy();
    }

    /**
     * 添加企业退回便捷语
     * @param add
     * @return
     */
    @Override
    public int insertQyeasy(String add) {
        //查询企业退回便捷语在sys_dict_type表中的dict_type值(字典类型)
        String value=infoMapper.selectQyvalue();
        //插入企业退回便捷语
        int i=infoMapper.insertQyeasy(add,value);
        return i;
    }

    /**
     * 查企业退回表
     * @return
     */
    @Override
    public List<QySend> selectQySend(QyInfo info) {
        startPage();
        return infoMapper.selectQySend(info);
    }

    /**
     * 保存审核通过
     * @param id 审核id
     * @param xycode 信用代码
     * @param book 书文号
     * @param reg 注册号
     * @param regdate 注册日期
     * @param QRcode 二维码
     * @param userId 登录管理员id
     * @return
     */
    @Override
    public int updateAuditPass(String id, String xycode, String book, String reg, String regdate, String QRcode, Long userId, HttpServletRequest request) {
        //当前登录管理员username
        String userName=gtgsywMapper.selectUserName(userId);
        QyInfo info=new QyInfo();
        //审核通过id
        info.setId(Long.valueOf(id));
        //信用代码
        info.setXinyongcode(xycode);
        //书文号
        info.setShuwenhao(book);
        //注册号
        info.setZhucehao(reg);
        //二维码
        if(QRcode!=null){
            info.setErweimabase64(QRcode);
        }
        info.setFlg("2");
        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        DateFormat format2= new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        Date date = null;
        String sdate= request.getParameter("regdate");
        try {
            date = format1.parse(sdate);
            System.err.println(date);
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }

        //注册日期
        info.setQyclrq(date);
        //审核人
        info.setShenhename(userName);
        //审核人id
        info.setShenhenameid(userId.toString());
        //审核时间
        info.setShenhedate(new Date());
        //更改数据 更改状态该为审核通过(2)
        int i=qyInfoMapper.updateQyInfo(info);
        return i;
    }

    /**
     * 修改保存企业基本信息
     * @param info
     * @return
     */
    @Override
    public int updateQymessage(QyInfo info) {
        String[] hangye=info.getHangye().split(",");
        if(hangye.length>=4){
            info.setHangye(hangye[0]);
            info.setHangye1(hangye[1]);
            info.setHangye2(hangye[2]);
            info.setHangye3(hangye[3]);
        }
        return infoMapper.updateQymessage(info);
    }

    /**
     * 根据企业的bsid查询党建信息
     * @param bsId
     * @return
     */
    @Override
    public QyDjxx selectQyjdxx(String bsId) {
        return infoMapper.selectQyjdxx(bsId);
    }
    /**
     * 法定代表人、董事、监事、经理表单
     */
    @Override
    public List<QyPerson> selectidPerson(String bsid) {
        startPage();
        return infoMapper.selectidPerson(bsid);
    }

    /**
     * 股东出资人表单
     * @param bsId
     * @return
     */
    @Override
    public List<QyCzrxx> selectCzrxx(String bsId) {

        return infoMapper.selectCzrxx(bsId);
    }

    /**
     *   修改有党建组织党建信息
     */
    @Override
    public int updateQyjdzzAll(QyDjxx djxx) {
        return infoMapper.updateQyjdzzAll(djxx);
    }

    /**
     * 修改没有党建组织党建信息
     * @param djxx
     * @return
     */
    @Override
    public int updateQyjdzz(QyDjxx djxx) {
        return infoMapper.updateQyjdzz(djxx);
    }

    /**
     * 查询财务负责人
     * @param bsid
     * @return
     */
    @Override
    public QyPerson selectFinance(String bsid) {
        return infoMapper.selectFinance(bsid);
    }

    /**
     * 修改保存财务负责人
     * @param person
     * @return
     */
    @Override
    public int updateFinance(QyPerson person) {
        return infoMapper.updateFinance(person);
    }

    /**
     * 查看股东人详细信息
     * @param id
     * @return
     */
    @Override
    public QyCzrxx selectCzrxxById(String id) {
        return infoMapper.selectCzrxxById(id);
    }

    /**
     * 保存更新股东详细信息
     * @param czrxx
     * @return
     */
    @Override
    public int updateQyCzrxx(QyCzrxx czrxx) {
        return infoMapper.updateQyCzrxx(czrxx);
    }

    /**
     * 法定法定代表人、董事、监事、经理详细信息
     * @param id
     * @return
     */
    @Override
    public QyPerson selectQyPersonById(String id) {
        return infoMapper.selectQyPersonById(id);
    }

    /**
     * 修改保存法定法定代表人、董事、监事、经理详细信息
     * @param person
     * @return
     */
    @Override
    public int updateQyPerson(QyPerson person) {
        return infoMapper.updateQyPerson(person);
    }


	/**
     * base64字符串转换成图片
     *
     * @param imgStr      base64字符串
     * @param imgFilePath 图片存放路径
     * @return
     * @author ZHANGJL
     * @dateTime 2018-02-23 14:42:17
     */
    public static boolean Base64ToImage(String imgStr, String imgFilePath) { // 对字节数组字符串进行Base64解码并生成图片
        if (StringUtil.isEmpty(imgStr)) // 图像数据为空
            return false;

        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Base64解码

            byte[] b = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
                }
            }
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }

    }


    /**
     * 下载doc文档
     * @param id
     * @param down
     * @return
     * @throws FileNotFoundException
     */
    @Override
    public File createDoc(String id, String down) throws FileNotFoundException {
        // 创建数据
        Map<String, Object> dataMap = new HashMap<String, Object>();
        QyInfo qy = infoMapper.selectIdAll(id);
        String city = Global.getCityName();
        String scity = Global.getSuperCity();
        String yzbm= Global.getConfig("ruoyi.cityPostcode");
        String sttr = "iVBORw0KGgoAAAANSUhEUgAAAKMAAABKCAYAAADXJ+TKAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAADRSURBVHhe7dIxAQAADMOg+Tfd2cgBGrhBhIxkyEiGjGTISIaMZMhIhoxkyEiGjGTISIaMZMhIhoxkyEiGjGTISIaMZMhIhoxkyEiGjGTISIaMZMhIhoxkyEiGjGTISIaMZMhIhoxkyEiGjGTISIaMZMhIhoxkyEiGjGTISIaMZMhIhoxkyEiGjGTISIaMZMhIhoxkyEiGjGTISIaMZMhIhoxkyEiGjGTISIaMZMhIhoxkyEiGjGTISIaMZMhIhoxkyEiGjGTISIaMZMhIhoxEbA8iDcZ+yy2tAwAAAABJRU5ErkJggg==";
        Date date = qy.getShenhedate();
        if(date == null){
            date = new Date();
        }
        String dateStr = new SimpleDateFormat("yyyy年MM月dd日").format(date);
        String nowDate = new SimpleDateFormat("yyyy年MM月dd日").format(date);
        QyCzrxx czrxx1=new QyCzrxx();
        czrxx1.setBsid(qy.getBsid());
        List<QyCzrxx> qyc = qyCzrxxMapper.selectQyCzrxxList(czrxx1);
        if(qy.getFrname() == null){
            qy.setFrname("-");
        }
        String template = "";
        //下载个人独资企业章程
        if (down.equals("1")) {

            if(qyc.size()!=0){
                //出资方式
                dataMap.put("czfs",qyc.get(0).getCzfs());
                //出资时间
                if(qyc.get(0).getShijiaoqxtime()==null){
                    qyc.get(0).setShijiaoqxtime(new Date());
                }
                dataMap.put("czsj",new SimpleDateFormat("yyyy年MM月dd日").format(qyc.get(0).getShijiaoqxtime()));
                //出资人
                dataMap.put("czrname",qyc.get(0).getCzrname());
                //出资金额
                dataMap.put("czrzj",qyc.get(0).getCzrzj());

                String qianming = qyc.get(0).getCzrqm();
                String frqm="";
                if(qianming!=null) {
//                    String[] str = qianming.split(",");
                    frqm=convertFileToBase64(
                            Global.getProfile()+String.valueOf(qianming)
                                    .replace("/profile",""));

                }
               // Base64ToImage(str[1], "D:\\" + qyc.get(0).getCzrname() + "签名.jpg");
                dataMap.put("frnameqm",frqm);
            }
            // 企业名称
            dataMap.put("qyname", qy.getQyname());
            //企业地址
            dataMap.put("qyyoujiaddress", qy.getQyjycs());
            //企业经营范围
            dataMap.put("qyjyfw", qy.getQyjyfw());
            //企业注册资金
            dataMap.put("qyzczj", qy.getQyzczj());
            //法人姓名
            dataMap.put("frname", qy.getFrname());
            //企业经营期限
            dataMap.put("qyjyqx", qy.getQyjyqx());
            dataMap.put("qynx", qy.getQynx());
            //文档数量


            List<?> eList = new ArrayList<>();

            dataMap.put("date", dateStr);
            dataMap.put("eList", eList);
            // 获取模板
            template = "/templates/grqy.ftl";
        } else if (down.equals("2")) {
            //下载个人独资决定参考文本
            List<?> eList = new ArrayList<>();

            if(qyc.size()!=0){
                System.err.println(qyc.get(0).toString());

                //出资方式
                dataMap.put("czfs",qyc.get(0).getCzfs());
                //出资时间
                if(qyc.get(0).getShijiaoqxtime()==null){
                    qyc.get(0).setShijiaoqxtime(new Date());
                }
                dataMap.put("czsj",new SimpleDateFormat("yyyy年MM月dd日").format(qyc.get(0).getShijiaoqxtime()));
                //出资人
                dataMap.put("czrname",qyc.get(0).getCzrname());
                //出资金额
                dataMap.put("czrzj",qyc.get(0).getCzrzj());

                //签名
                String qianming = qyc.get(0).getCzrqm();
                String frqm="";
                if(qianming!=null) {
                    frqm=convertFileToBase64(Global.getProfile()+qianming.replace("/profile",""));
                }
//                Base64ToImage(str[1], "D:\\" + qyc.get(0).getCzrname() + "签名.jpg");
                dataMap.put("frnameqm",frqm);
            }
            // 企业名称
            dataMap.put("qyname", qy.getQyname());
            //法人姓名
            dataMap.put("frname", qy.getFrname());
            //企业注册资金
            dataMap.put("qyzczj", qy.getQyzczj());
            //经营范围
            dataMap.put("qyjyfw", qy.getQyjyfw());

            if(qy.getQyjyqx().equals("长期")) {
                dataMap.put("qyjyqx", qy.getQyjyqx());
            }else{
                dataMap.put("qyjyqx", qy.getQynx());
            }
            List<QyPerson> qyPeople= qyPersonMapper.selectQyPersonByBsid(qy.getBsid());
            String jianshi="";
            String dailiren="";
            for(QyPerson person:qyPeople){
                if(person.getRentype().equals("监事")){
                    jianshi=person.getRenname();
                }
                if(person.getRentype().equals("委托代理人")){
                    dailiren=person.getRenname();
                }
            }
            dataMap.put("jianshi",jianshi);
            dataMap.put("dailiren",dailiren);

            dataMap.put("qylx",qy.getQylx());
            //企业地址
            dataMap.put("qyyoujiaddress", qy.getQyjycs());
            dataMap.put("date", dateStr);
            dataMap.put("eList", eList);
            // 获取模板
            template = "/templates/grqy2.ftl";
        }
        else if (down.equals("3")) {
            dataMap.put("qynx", qy.getQynx());
            int heji = 0;
            List<String> eList = new ArrayList<String>();
            List<QyCzrxx> gdList;
            gdList = qyc;
            //未查找到boss
            if(qyc.size() == 0){
                gdList.add(new QyCzrxx());
            }
            List<QyPerson> qyp = infoMapper.selectidPerson(qy.getBsid());
            if (qyp.size()!=0){
                for (int i = 0;i<qyp.size();i++){
                    if(qyp.get(i).getIsfr().equals("1")){
                        String qianming = qyp.get(i).getRenqianming();
                        String frqm="";
                        if(qianming!=null) {
                            frqm=convertFileToBase64(Global.getProfile()+qianming.replace("/profile",""));
                        }
//                        Base64ToImage(str[1], "D:\\" + qyp.get(i).getRenname() + "签名.jpg");
                        dataMap.put("frqm",frqm);
                    }
                }
            }
            for (QyCzrxx czr:qyc){
                //签名
                System.err.println(czr.toString());
                String qianming = czr.getCzrqm();
                String czrqm="";
                if(qianming!=null) {
                    czrqm=convertFileToBase64(Global.getProfile()+qianming.replace("/profile",""));
                }
//                Base64ToImage(str[1], "D:\\" + czr.getCzrname() + "签名.jpg");
                eList.add(czrqm);
                heji+=Integer.parseInt(czr.getCzrzj());
            }
            dataMap.put("heji",heji);

            dataMap.put("frname",qy.getFrname());
            //企业名
            dataMap.put("qyname",qy.getQyname());
            //企业地址
            dataMap.put("qyyoujiaddress",qy.getQyjycs());
            //经营范围
            dataMap.put("qyjyfw",qy.getQyjyfw());
            //注册资金
            dataMap.put("qyzczj",qy.getQyzczj());
            //经营期限
            dataMap.put("qyjyqx",qy.getQyjyqx());
            dataMap.put("date",dateStr);
            //文档数量
            dataMap.put("num",qyc.size()+2);

            // 获取模板
//            /*template = "/templates/sqa.ftl";  //模板文件的地址*/
            template = "/templates/jituan.ftl";  //模板文件的地址
            dataMap.put("eList", eList);
            dataMap.put("gdList",gdList);
        }
        else if (down.equals("4")) {
            dataMap.put("qynx", qy.getQynx());
            //未查找到boss
            int heji = 0;
            List<String> eList = new ArrayList<String>();
            List<QyCzrxx> gdList = new ArrayList<QyCzrxx>();
            gdList = qyc;
            if(qyc.size() == 0){
                gdList.add(new QyCzrxx());
            }

            for (QyCzrxx czr:qyc){
                //签名
                String qianming = czr.getCzrqm();
                String czrqm="";
                if(qianming!=null) {
                    czrqm=convertFileToBase64(Global.getProfile()+qianming.replace("/profile",""));
                }
//                String[] str = qianming.split(",");
//                Base64ToImage(str[1], "D:\\" + czr.getCzrname() + "签名.jpg");
                eList.add(czrqm);
                heji+=Integer.parseInt(czr.getCzrzj());
            }

            dataMap.put("heji",heji);
            //法人
            dataMap.put("frname",qy.getFrname());
            //企业名称
            dataMap.put("qyname",qy.getQyname());
            //地址
            dataMap.put("qyyoujiaddress",qy.getQyjycs());
            //经营范围
            dataMap.put("qyjyfw",qy.getQyjyfw());
            //注册资金
            dataMap.put("qyzczj",qy.getQyzczj());
            //经营期限
            dataMap.put("qyyyqz",qy.getQyjyqx());
            dataMap.put("date",dateStr);
            //会议时间
            dataMap.put("hydate",dateStr);
            //文档数量
            dataMap.put("num",qyc.size());
            //出资最多的
            Integer czrmax=0;
            Integer ii=0;
            czrmax=Integer.valueOf(gdList.get(0).getCzrzj());
            dataMap.put("czrmax",gdList.get(0).getCzrname());

            for (int  i=1;i<gdList.size();i++){
                if (czrmax==Integer.valueOf(gdList.get(i).getCzrzj())){
                    ii++;
                }
            }
            if(ii==gdList.size()-1){
                dataMap.put("czr",0);
            }else {
                dataMap.put("czr",1);
            }
            List<QyPerson> qyp = infoMapper.selectidPerson(qy.getBsid());
            for (QyPerson person : qyp) {
                if (person.getRentype().equals("董事")) {
                    dataMap.put("dsname", person.getRenname());
                }
                else if (person.getRentype().equals("监事")) {
                    dataMap.put("jsname", person.getRenname());
                }else if (person.getRentype().equals("经理")) {
                    dataMap.put("jlname", person.getRenname());
                }else if (person.getRentype().equals("委托代理人")) {
                    dataMap.put("wtname", person.getRenname());
                }
            }
            // 获取模板
//            /*template = "/templates/sqa.ftl";  //模板文件的地址*/
            template = "/templates/jituancankao.ftl";  //模板文件的地址
            dataMap.put("eList", eList);
            dataMap.put("gdList",gdList);

        }
        else if (down.equals("6")) {
            //企业申请书
            int heji = 0;
            List<String> eList = new ArrayList<String>();
            List<QyCzrxx> gdList = qyc;
            List<QyPerson> dsList = new ArrayList<QyPerson>();

            //获取企业人员
            List<QyPerson> qyp = infoMapper.selectidPerson(qy.getBsid());
            for (QyCzrxx gd : gdList){
                gd.setCzrsfz(convertFileToBase64(Global.getProfile()+gd.getCzrsfz().replace("/profile","")));
                gd.setCzrsfz1(convertFileToBase64(Global.getProfile()+gd.getCzrsfz1().replace("/profile","")));

            }
            for (QyPerson person : qyp){
                String czrsfz = null;
                String czrsfz2 = null;
                //判断是否是 董事 经理 监事 并把身份证照片格式化

                if(person.getRentype().equals("法定代表人")||person.getIsfr().equals("1")){
                    //法定代表人
                    czrsfz=person.getCzrsfz();
                    czrsfz2=person.getCzrsfz1();
                    dataMap.put("frsfz",convertFileToBase64(Global.getProfile()+czrsfz.replace("/profile","")));
                    dataMap.put("frsfz2",convertFileToBase64(Global.getProfile()+czrsfz2.replace("/profile","")));
                    //法人签名
                    String frqm="";
                    if(person.getRenqianming()!=null) {
                        frqm=convertFileToBase64(Global.getProfile()+person.getRenqianming().replace("/profile",""));
                    }
                    dataMap.put("frqm",frqm);
                    //法人身份证
                    dataMap.put("frcardno",person.getRencardno());
                    //法人邮箱
                    dataMap.put("fremail",person.getRenemail());
                    //法人移动电话
                    dataMap.put("frmoblie",person.getRenmoblie());
                    //法人固定电话
                    dataMap.put("frphone",person.getRenphone());
                    //法人姓名
                    dataMap.put("frname",person.getRenname());
                    //法人住址
                    dataMap.put("fraddress",person.getRenaddress());
                    //产生方式
                    dataMap.put("frcsfs",person.getCsfs());
                }
                if(person.getRentype().equals("董事") || person.getRentype().equals("监事") || person.getRentype().equals("经理")){

                    czrsfz=person.getCzrsfz();
                    czrsfz2=person.getCzrsfz1();
                    person.setCzrsfz(convertFileToBase64(Global.getProfile()+czrsfz.replace("/profile","")));
                    person.setCzrsfz1(convertFileToBase64(Global.getProfile()+czrsfz2.replace("/profile","")));
                    dsList.add(person);
                }else if(person.getRentype().equals("联络员")){
                    //联络员
                    czrsfz=person.getCzrsfz();
                    czrsfz2=person.getCzrsfz1();
                    dataMap.put("lianluosfz",convertFileToBase64(Global.getProfile()+czrsfz.replace("/profile","")));
                    dataMap.put("lianluosfz2",convertFileToBase64(Global.getProfile()+czrsfz2.replace("/profile","")));
                    //联络员身份证
                    dataMap.put("lianluocardno",person.getRencardno());
                    //联络员邮箱
                    dataMap.put("lianluoemail",person.getRenemail());
                    //联络员移动电话
                    dataMap.put("lianluorenmoblie",person.getRenmoblie());
                    //联络员固定电话
                    dataMap.put("lianluophone",person.getRenphone());
                    //联络员姓名
                    dataMap.put("lianluoname",person.getRenname());
                }else if(person.getRentype().equals("委托代理人")){
                    //委托代理人
                    czrsfz=person.getCzrsfz();
                    czrsfz2=person.getCzrsfz1();
                    dataMap.put("dlrsfz",convertFileToBase64(Global.getProfile()+czrsfz.replace("/profile","")));
                    dataMap.put("dlrsfz2",convertFileToBase64(Global.getProfile()+czrsfz2.replace("/profile","")));
                    //代理人签名
                    String dlrqm="";
                    if(person.getRenqianming()!=null) {
                        dlrqm=convertFileToBase64(Global.getProfile()+person.getRenqianming().replace("/profile",""));
                    }
                    dataMap.put("dlrqm",dlrqm);
                    //代理人身份证
                    dataMap.put("dlcardno",person.getRencardno());
                    //代理人邮箱
                    dataMap.put("dlemail",person.getRenemail());
                    //代理人移动电话
                    dataMap.put("dlmoblie",person.getRenmoblie());
                    //代理人固定电话
                    dataMap.put("dlphone",person.getRenphone());
                    //代理人姓名
                    dataMap.put("dlname",person.getRenname());
                }
            }
            //查询财务负责人
            QyPerson cw = infoMapper.selectFinance(qy.getBsid());
            dataMap.put("cwsfz",convertFileToBase64(Global.getProfile()+cw.getCzrsfz().replace("/profile","")));
            dataMap.put("cwsfz2",convertFileToBase64(Global.getProfile()+cw.getCzrsfz1().replace("/profile","")));
            //财务负责人身份证
            dataMap.put("cwcardno",cw.getRencardno());
            //财务负责人邮箱
            dataMap.put("cwemail",cw.getRenemail());
            //财务负责人移动电话
            dataMap.put("cwmoblie",cw.getRenmoblie());
            //财务负责人固定电话
            dataMap.put("cwphone",cw.getRenphone());
            //财务负责人姓名
            dataMap.put("cwname",cw.getRenname());
            //如果没有查找到出资人
            if(qyc.size() == 0){
                gdList.add(new QyCzrxx());
            }
            //法人
            dataMap.put("frname",qy.getFrname());
            //企业名称
            dataMap.put("qyname",qy.getQyname());
            //地址
            dataMap.put("qyyoujiaddress",qy.getQyjycs());
            //经营范围
            dataMap.put("qyjyfw",qy.getQyjyfw());
            //注册资金
            dataMap.put("qyzczj",qy.getQyzczj());
            //经营期限
            dataMap.put("qyyyqz",qy.getQyjyqx());
            dataMap.put("date",dateStr);
            //企业从业人数
            dataMap.put("cyrs",qy.getQycyrs());
            //信用代码
            dataMap.put("xinyongcode",qy.getXinyongcode());

            //企业联系电话
            dataMap.put("qylxdh",qy.getQylxdh());

            //邮编

            dataMap.put("yzbm",yzbm);
            if(qy.getLqrqianming()!=null){
                //领取人签名
                dataMap.put("dlrqm",convertFileToBase64(Global.getProfile()+qy.getLqrqianming().replace("/profile","")));
            }
            //领取时间
            Date lqdate=null;

            if(qy.getLqdate()==null&&qy.getShenhedate()!=null){
                lqdate=qy.getShenhedate();
            }else {
                lqdate=qy.getLqdate();
            }
            dataMap.put("lqdate",lqdate );


            //登记机关
            dataMap.put("laiyuan",qy.getLaiyuan());
            //文档数量
            dataMap.put("num",qyc.size()+2);

            if(qy.getQyjyqx().equals("长期")) {
                dataMap.put("qyjyqx", qy.getQyjyqx());
            }else{
                dataMap.put("qyjyqx", qy.getQynx().toString());
            }


            for (QyCzrxx czr:qyc){
                //获取出资人签名
                String qianming = czr.getCzrqm();
                if(qianming!=null) {
                    qianming=convertFileToBase64(Global.getProfile()+qianming.replace("/profile",""));
                    eList.add(qianming);
                }
                if(czr.getCzrzj()==null){
                    heji+=Integer.parseInt(czr.getRenjiaoje());
                }else {
                    heji += Integer.parseInt(czr.getCzrzj());
                }
            }
            dataMap.put("heji",heji);
            dataMap.put("city",city);
            //签名
           /* for (QyCzrxx czr:qyc){
                //获取出资人签名
                String qianming = czr.getCzrqm();
                  qianming=convertFileToBase64(Global.getProfile()+qianming.replace("/profile",""));
                eList.add(qianming);
                //获取出资人签名
            }*/
            // 获取模板
            template = "/templates/dengji1.ftl";  //模板文件的地址
            dataMap.put("eList", eList);
            dataMap.put("gdList",gdList);
            dataMap.put("dsList",dsList);
            dataMap.put("qyjycs",qy.getQyjycs());
        } else if (down.equals("7")) {
            int heji = 0;
            List<QyPerson> qyp = infoMapper.selectidPerson(qy.getBsid());
            List<String> czrlist = new ArrayList<String>();
            //住所（经营场所）信息申报表
            //商户名称
            dataMap.put("sbmcname", qy.getQyname());
            //经营场所
            dataMap.put("sbmanagesit", qy.getQyjycs());
            //房屋规划用途
            dataMap.put("fwghyt", qy.getJycsxz());
            //是否军产
            String Isjc = qy.getIsjc();
            if (Isjc.equals("0")){
                dataMap.put("isjs", "否");
            } else {
                dataMap.put("isjs", "是");
            }
            //签名
            for (QyCzrxx czr:qyc){
                //获取出资人签名
                String qianming = czr.getCzrqm();
                if(qianming!=null) {
                    qianming=convertFileToBase64(Global.getProfile()+qianming.replace("/profile",""));
                    czrlist.add(qianming);
                }
                //获取出资人签名
            }
            //法人签名
            for (QyPerson person : qyp){
                if(person.getRentype().equals("法定代表人")||person.getIsfr().equals("1")) {
                    if(person.getRenqianming()!=null) {
                        dataMap.put("sbqianming", convertFileToBase64(Global.getProfile()+person.getRenqianming().replace("/profile","")));
                    }else {
                        dataMap.put("sbqianming", "");
                    }
                }
            }
            //日期
            Date date1 = qy.getShenhedate();
            dataMap.put("sbdate", new Date());
            dataMap.put("images1", czrlist);
            dataMap.put("scity", scity);
            template = "/templates/qy申报.ftl";  //模板文件的地址
        }else if (down.equals("8")){
            //个人独资企业

            List<QyPerson> qyp = qyPersonMapper.selectQyPersonByBsid(qy.getBsid());
            for (QyCzrxx czr:qyc) {
                dataMap.put("czrname",czr.getCzrname());
                dataMap.put("czrxb",czr.getCzrxb());
                dataMap.put("czrday",czr.getCzrcardno().substring(6,14));
                dataMap.put("mz",czr.getCzrmz());
                dataMap.put("zzmm",czr.getCzrzzmm());
                dataMap.put("czrmoble",czr.getCzrphone());

                dataMap.put("sfzcard",czr.getCzrcardno());
                dataMap.put("czrzs",czr.getCzrzz());
                dataMap.put("czrqm",convertFileToBase64(Global.getProfile()+czr.getCzrqm().replace("/profile","")));
                dataMap.put("czrsfz1",convertFileToBase64(Global.getProfile()+czr.getCzrsfz().replace("/profile","")));
                dataMap.put("czrsfz2",convertFileToBase64(Global.getProfile()+czr.getCzrsfz1().replace("/profile","")));
            }
            for(QyPerson qys:qyp){
                if(qys.getRentype().equals("联络员")){
                    dataMap.put("llyname",qys.getRenname());
                    dataMap.put("llyphone",qys.getRenphone());
                    dataMap.put("llymoble",qys.getRenmoblie());
                    dataMap.put("llyemail",qys.getRenemail());
                    dataMap.put("llycard",qys.getRencardno());
                    dataMap.put("llysfz1",convertFileToBase64(Global.getProfile()+qys.getCzrsfz().replace("/profile","")));
                    dataMap.put("llysfz2",convertFileToBase64(Global.getProfile()+qys.getCzrsfz1().replace("/profile","")));
                }else if(qys.getRentype().equals("财务负责人")){
                    dataMap.put("cwname",qys.getRenname());
                    dataMap.put("cwphone",qys.getRenphone());
                    dataMap.put("cwmoble",qys.getRenmoblie());
                    dataMap.put("cwemail",qys.getRenemail());
                    dataMap.put("cwcard",qys.getRencardno());
                    dataMap.put("cwsfz1",convertFileToBase64(Global.getProfile()+qys.getCzrsfz().replace("/profile","")));
                    dataMap.put("cwsfz2",convertFileToBase64(Global.getProfile()+qys.getCzrsfz1().replace("/profile","")));
                }else if(qys.getRentype().equals("委托代理人")){
                    dataMap.put("wtname",qys.getRenname());
                    dataMap.put("wtphone",qys.getRenphone());
                    dataMap.put("wtqianming",convertFileToBase64(Global.getProfile()+qys.getRenqianming().replace("/profile","")));
                    dataMap.put("wtmoble",qys.getRenmoblie());
                    dataMap.put("wtemail",qys.getRenemail());
                    dataMap.put("wtcard",qys.getRencardno());
                    dataMap.put("wtsfz1",convertFileToBase64(Global.getProfile()+qys.getCzrsfz().replace("/profile","")));
                    dataMap.put("wtsfz2",convertFileToBase64(Global.getProfile()+qys.getCzrsfz1().replace("/profile","")));
                }
            }
            dataMap.put("date",nowDate);
            dataMap.put("qymc",qy.getQyname());
            dataMap.put("hzh",qy.getQyhezhun());
            dataMap.put("zch",qy.getZhucehao());
            dataMap.put("xinyongcode",qy.getXinyongcode());
            dataMap.put("city",Global.getCityName());
            dataMap.put("scity",Global.getSuperCity());
            dataMap.put("moble",qy.getQylxdh());
            dataMap.put("yzbm",yzbm);
            dataMap.put("cze",qy.getQyzczj());
            dataMap.put("cyrs",qy.getQycyrs());
            dataMap.put("jyfw",qy.getQyjyfw());
            dataMap.put("jycsxz",qy.getJycsxz());
            dataMap.put("jycs",qy.getQyjycs());
            dataMap.put("kongbai",sttr);

            template = "/templates/个人独资企业设立.ftl";  //模板文件的地址
//            template = "/templates/个人独资企业申请书1.ftl";  //模板文件的地址
        }else if(down.equals("9")){
            for (QyCzrxx czr:qyc) {
                dataMap.put("czrname",czr.getCzrname());
                dataMap.put("czrzs",czr.getCzrzz());
                dataMap.put("czrzj",czr.getCzrzj());
            }
            dataMap.put("qymc",qy.getQyname());
            dataMap.put("date",nowDate);
            dataMap.put("swh",qy.getShuwenhao());
            dataMap.put("jyfw",qy.getQyjyfw());
            dataMap.put("qyzs",qy.getQyjycs());
            dataMap.put("zch",qy.getZhucehao());
            if(qy.getLqrqianming()==null){
                throw new NullPointerException("暂无执照领取人，请领取执照后方可下载该文件");
            }
            dataMap.put("lqrqm",convertFileToBase64(Global.getProfile()+qy.getLqrqianming().replace("/profile","")));
            if(qy.getPrintzdate()!=null){
                dataMap.put("printTime", DateUtils.parseDateToStr("yyyy年MM月dd日",qy.getPrintzdate()));
            }else {
                dataMap.put("printTime", DateUtils.datePaths());
            }
            template = "/templates/grdz设立.ftl";  //模板文件的地址
        }else if(down.equals("10")){
            if(qy.getPrintzdate()!=null){
                dataMap.put("date", qy.getPrintzdate());
            }else {
                dataMap.put("date", DateUtils.datePaths());
            }
            dataMap.put("qymc",qy.getQyname());
            dataMap.put("qyjycs",qy.getQyjycs());
            template = "/templates/使用证明.ftl";  //模板文件的地址
        }else if(down.equals("11")){
            List<QyPerson> qyp = qyPersonMapper.selectQyPersonByBsid(qy.getBsid());

            for(QyPerson qys:qyp){
                if(qys.getRentype().equals("委托代理人")) {
                    dataMap.put("qianming",convertFileToBase64(Global.getProfile()+qys.getRenqianming().replace("/profile","")));
                }
                }
                    dataMap.put("city",Global.getCityName());
                    dataMap.put("scity",scity);
                    dataMap.put("qymc",qy.getQyname());
                    dataMap.put("date",qy.getShenhedate());
                    template = "/templates/zs承诺书.ftl";  //模板文件的地址
        }

        String templatePath = template.substring(0, template.lastIndexOf("/"));
        String templateName = template.substring(template.lastIndexOf("/") + 1);
        System.err.println("templateName:" + templateName);
        Configuration configuration = null;
        if (configuration == null) {
            configuration = new Configuration(Configuration.VERSION_2_3_23);
            configuration.setDefaultEncoding("utf-8");
            configuration.setClassForTemplateLoading(this.getClass(), templatePath);
        }
        Template t = null;
        /* String name = "temp"+(int)(Math.random()*1000)+".doc";*/
        String name = templateName;
        System.err.println("name:" + name);
        File file = new File(name);
        try {
            t = configuration.getTemplate(templateName);
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(name), StandardCharsets.UTF_8));
            t.process(dataMap, out);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
        return file;
    }


    /**
     * 设置请求分页数据
     */
    protected void startPage()
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
        {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            PageHelper.startPage(pageNum, pageSize, orderBy);
        }
    }
    /**
     * 返回数组第二个
     */
    private String str2(String img)
    {
      if(img.split(",")[1]!=null){
          return img.split(",")[1];
      }
      return  img;
    }
}

