package com.ruoyi.web.controller.Ytj;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.rmi.ConnectException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.domain.GtgsywPrintList;
import com.ruoyi.system.service.GtgsywPrintListService;
import com.ruoyi.system.service.IGtgsBgService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.GtgsBg;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import sun.misc.BASE64Encoder;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2020-05-13
 */
@Controller
@RequestMapping("/Ytj/bg")
public class GtgsBgController extends BaseController
{
    private String prefix = "Ytj/bg";
    @Autowired
    private IGtgsBgService gtgsBgService;


    @GetMapping()
    public String bg(ModelMap modelMap)
    {
        modelMap.put("city", Global.getCityName());
        return prefix + "/Read";
    }

    @GetMapping("/gtbg")
    public String gtbg(ModelMap modelMap)
    {
        modelMap.put("city", Global.getCityName());
        modelMap.put("score", Global.getConfig("ruoyi.rzdbsocre"));
        return prefix + "/gtbg";
    }

    @GetMapping("/shtg")
    public String shtg(String id)
    {

        return "ok";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GtgsBg gtgsBg)
    {
        String Uuid = UUID.randomUUID().toString().replace("-", "");
        gtgsBg.setId(Uuid);
        gtgsBg.setCreateTime(new Date());
        gtgsBg.setRegisterTime(gtgsBg.getRegisterTime());
        //判断信用代码是否为空或null
        if(gtgsBg.getUniformSocialCreditCode()!=null && !gtgsBg.getUniformSocialCreditCode().equals("")){
            System.err.println("xycode:"+gtgsBg.getUniformSocialCreditCode());
            /*String QRcode=GetImageStr("D:/二维码/licence.png");*/
            String url=Global.getConfig("ruoyi.gsdz")+":8005/gtdj/licence.do?method=toTowDimCode&appNo=&uniScid="+gtgsBg.getUniformSocialCreditCode()+"&certTypeId=41";
            String QRcode=  getImg(url);
            System.err.println("QRcode:"+QRcode);
            gtgsBg.setBusinessLicenseQrcode(QRcode);
        }
        return toAjax(gtgsBgService.insertGtgsBg(gtgsBg));
    }
    private static String strNetImageToBase64;
    public static String getImg(String urlgs){
        final ByteArrayOutputStream data = new ByteArrayOutputStream();
        try {
            // 创建URL
            URL url = new URL(urlgs);
            final byte[] by = new byte[1024];
            // 创建链接
            final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);
            try {
                InputStream is = conn.getInputStream();
                // 将内容读取内存中
                System.err.println(111);
                int len = -1;
                while ((len = is.read(by)) != -1) {
                    data.write(by, 0, len);
                }
                System.err.println(222);
                // 对字节数组Base64编码
                BASE64Encoder encoder = new BASE64Encoder();
                strNetImageToBase64 = encoder.encode(data.toByteArray());
                System.out.println("网络图片转换Base64:" + strNetImageToBase64);
                // 关闭流
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  strNetImageToBase64;
    }
    /**
     * 新增保存注销前台
     */
    @Log(title = "注销前台", businessType = BusinessType.INSERT)
    @PostMapping("/getInfo")
    @ResponseBody
    public AjaxResult getInfo(@RequestParam("XyCode") String XyCode,@RequestParam("regisCode")String regisCode) {
        String result ;
        if (XyCode != "") {
            result = HttpUtils.sendPost(Global.getConfig("ruoyi.pyip") + "/sel_xydm", "xydm=" + XyCode);
        } else {
            result = HttpUtils.sendPost(Global.getConfig("ruoyi.pyip") + "/sel_zch", "zch=" + regisCode);
        }
        if(result.equals("0")){
            return AjaxResult.warn("输入有误,请确认 (注册号) 或 (统一社会信用代码)！");
        }else if(result.equals("")){
            return AjaxResult.warn("获取超时,请重试 或 联系工作人员");
        }
        System.out.println(result);

        return AjaxResult.success("ok", result);
    }
}
