package com.jeeplus.modules.esignature.web;

import com.alibaba.fastjson.JSONObject;
import com.jeeplus.common.utils.FileUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.modules.esignature.kinggridserver.KingGridServer;
import com.jeeplus.modules.esignature.service.signer.SignerService;
import com.jeeplus.modules.esignature.utils.HttpRequest;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;


/**
 * 电子签章Controller
 * @author fgy
 * @version 2018-04-10
 */
@SuppressWarnings("all")
@Controller
@RequestMapping(value = "${adminPath}/esignature/esignature")
public class EsignatureController extends BaseController{

    @RequiresPermissions("esignature:esignature:demoView")
    @RequestMapping("demoView")
    public String demoView(HttpServletRequest request, Model model, RedirectAttributes redirectAttributes){

        return "modules/workcontractinfo/demoView";
    }

    /**
     * 创建个人用户
     * @return
     */
    @RequestMapping("createSigner")
    @ResponseBody
    public String createSigner(HttpServletRequest request, Model model){
        String result = "";
        JSONObject jsonObject = new JSONObject();
        String signer_code = request.getParameter("signer_code");
        String signer_name = request.getParameter("signer_name");
        String signer_idcode = request.getParameter("singer_idcode");
        try {
            jsonObject = SignerService.createSigner(signer_code, signer_name, signer_idcode);
            result = jsonObject.toJSONString();
        }catch (IOException e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 创建企业用户
     */
    @RequestMapping("createCompanySigner")
    public JSONObject createCompanySigner(HttpServletRequest request, Model model){
        JSONObject jsonObject = new JSONObject();

        return jsonObject;
    }

    /**
     * 签章
     */
    @RequestMapping("sign")
    @ResponseBody
    public String sign(HttpServletRequest req, Model model, MultipartFile doc_file){
        String result = "";
        String contractId = req.getParameter("contractId");
        String contractName = req.getParameter("contractName");
        String bizId = req.getParameter("bizId");
        String signerCode = req.getParameter("signerCode");
        if(doc_file!=null && !doc_file.isEmpty() &&doc_file.getSize()>0){
            HttpRequest request = KingGridServer.createHttpRequest("/api/cnt/sign");
            InputStream is = null;
            String realPath = req.getSession().getServletContext().getRealPath("esignature_upload/"+System.currentTimeMillis());
            try{
                new File(realPath).mkdirs();
                String fullName = realPath + "/" +doc_file.getOriginalFilename();
                File f = new File(fullName);
                FileUtils.copyInputStreamToFile(doc_file.getInputStream(),f);

                is = new FileInputStream(new File(fullName));
                request.addInputStream("docx_file", fullName, is);
                request.addParam("contract_name", contractName);
                request.addParam("contract_id", contractId);
                request.addParam("biz_id", StringUtils.isBlank(bizId)?UUID.randomUUID().toString().replaceAll("-", ""):bizId);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                //设置业务时间参数
                request.addParam("biz_time", sdf.format(new Date()));
                //设置签署人的编码
                request.addParam("signer_info", "[{code:\""+signerCode+"\",seal_code:\"\" , fontsize:50,offsetX:200,y:400,x:200}]" );


                request.send();

                result = request.resultToString();
                System.out.println(result);
            }catch (IOException e){
                e.printStackTrace();
            }
            finally{
                if(is!=null){
                    try {
                        is.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                boolean b = FileUtils.deleteDirectory(realPath);
            }
        }

        return result;
    }




}





































