package common

import (
	"encoding/json"
	"fmt"
	"git.listensoft.net/tool/jspkit/logger"
	"github.com/go-rod/rod/lib/utils"
	"github.com/tidwall/gjson"
	"os"
	"path/filepath"
	"strings"
	"time"
)

// GetCollectPDFPath 生成一个采集用的pdf路径
func GetCollectPDFPath(district, taxNo, taxTape string) (string, error) {
	var err error
	var path string
	path = "./data/collection/" + district + "/"
	if !PathExists(path) {
		err = os.MkdirAll(path, os.ModePerm)
	}
	path = path + taxNo + "_" + taxTape + ".pdf"
	return path, err
}

// name pdf文件名
// path 文件路径
func PostBankPdf(name, suffix, path string) string {
	var params = map[string]string{
		"path": "/mnt/bank/banks/",
		"name": name + "." + suffix,
	}
	str, _ := PostFile("https://public.listensoft.net/api/uploadFile", map[string]string{
		"upFile": path,
	}, params)
	return gjson.GetBytes(str, "data.url").String()
}
func GetBankPDFPath(bank, taxNo, taxTape string) (string, error) {
	var err error
	var path string
	path = "./data/bank/" + bank + "/" + taxNo + "/"
	if !PathExists(path) {
		err = os.MkdirAll(path, os.ModePerm)
	}
	path = path + "_" + taxTape + ".pdf"
	return path, err
}

// PDFtoStrArrayPython pdf转二维数组 python服务
func PDFtoStrArrayPython(path string) ([][]string, error) {
	var code [][]string
	files := map[string]string{
		"file": path,
	}
	var bys []byte
	var err error
	pdfServers := []string{
		`http://47.104.75.113:6000/pdfToText`,
		`http://47.104.75.113:6000/pdfToText`,
	}

	for _, uri := range pdfServers {
		bys, err = PostFile(uri, files, map[string]string{})
		if err != nil {
			utils.Sleep(10)
			continue
		}
		break
	}
	if err != nil {
		return code, err
	}
	json.Unmarshal(bys, &code)
	return code, nil
}

// UploadPdfToStrArray java PDF转数组
func UploadPdfToStrArray(path string) ([]string, error) {
	f := 0
begin:
	url := "http://47.104.75.113:8080/api/uploadPdfToText" //灵信服务器
	files := map[string]string{
		"file": path,
	}
	resp, err := PostFile(url, files, map[string]string{})
	arr := []string{}
	if err != nil {
		if f < 3 {
			f++
			utils.Sleep(10)
			goto begin
		}
		return arr, err
	}
	type resut struct {
		Text string `json:"text"`
	}
	var Result resut
	if err := json.Unmarshal(resp, &Result); err != nil {
		return arr, err
	}
	arr = strings.Split(Result.Text, "\n")
	return arr, nil
}

// 生成验证码图片的路径
func GenerateFilePath(name, suffix string) string {
	os.MkdirAll("captcha", os.ModePerm)
	n := fmt.Sprintf("%s_%s.%s", name, GetTimestamp(13), suffix)
	return filepath.Join("captcha", n)
}

func GetCollectZipPath(district, taxNo, taxTape string) (string, error) {
	var err error
	var path string
	path = "./data/collection/" + district + "/"
	if !PathExists(path) {
		err = os.MkdirAll(path, os.ModePerm)
	}
	path = path + taxNo + "_" + taxTape + ".zip"
	return path, err
}

// 保存时长文件  地区 任务 开始/结束
func SaveDurationToFile(area string, task string, msg string) {
	path := "./data/duration/"
	if !PathExists(path) {
		os.MkdirAll(path, os.ModePerm)
	}
	path = path + "file.txt"
	data := area + "----" + task + "----" + time.Now().Format("2006-01-02 15:04:05") + "---用时---" + msg + "秒"

	// 创建或打开文件，文件名为test.txt，使用读写权限打开
	file, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
	if err != nil {
		logger.Info("打开文件失败")
	}
	defer file.Close()

	// 写入数据
	//writeData := []byte(data)
	_, err = file.WriteString("\r" + data)
	if err != nil {
		logger.Info("写入文件失败")
	}
}
