package main

import (
	"fmt"
	log "github.com/donnie4w/go-logger/logger"
	"github.com/tealeg/xlsx"
	"os"
	qu "qfw/util"
)

var sheet1, sheet2, sheet3 *xlsx.Sheet
var row1, row2, row3 *xlsx.Row
var index2, index3 = 1, 1

//父子级结构写表格-超链接
func testInvestInfo() {
	os.Remove("1.xlsx")
	f := xlsx.NewFile()
	sheet1, _ = f.AddSheet("Sheet1")
	sheet2, _ = f.AddSheet("Sheet2")
	sheet3, _ = f.AddSheet("Sheet3")
	row1 = sheet1.AddRow()
	row1.AddCell().Value = "企业名称"
	row1.AddCell().Value = "父级企业名称"
	row2 = sheet2.AddRow()
	row2.AddCell().Value = "企业名称"
	row2.AddCell().Value = "父级企业名称"
	row3 = sheet3.AddRow()
	row3.AddCell().Value = "企业名称"
	row3.AddCell().Value = "父级企业名称"
	testGetInvests("158308314d4cfdefe6f5e943d4dcaf4e", "中国核工业集团有限公司", 3)
	err := f.Save("1.xlsx")
	if err != nil {
		log.Debug("保存xlsx失败:", err)
	} else {
		log.Debug("保存xlsx成功:", err)
	}
}
func testGetInvests(tmpid string, pre_name string, level int) bool {
	q := map[string]interface{}{"stock_name_id": tmpid}
	info, _ := Spi_Mgo.Find("company_partner", q, nil, nil)
	var infos []map[string]interface{}
	temp := map[string]string{}
	for _, v := range info {
		company_id := qu.ObjToString(v["company_id"])
		if temp[company_id] == "" && qu.ObjToString(v["company_name"]) != "" && qu.IntAll(v["use_flag"]) == 0 {
			infos = append(infos, v)
			temp[company_id] = company_id
		}
	}
	if level <= 1 {
		for _, v := range infos {
			infos = append(infos, v)
			row3 = sheet3.AddRow()
			row3.AddCell().Value = qu.ObjToString(v["company_name"])
			row3.AddCell().Value = pre_name
			index3++
		}
		if len(infos) > 0 {
			return true
		}
		return false
	}
	for _, invest := range infos {
		b := testGetInvests(qu.ObjToString(invest["company_id"]), qu.ObjToString(invest["company_name"]), level-1)
		if level == 2 {
			row2 = sheet2.AddRow()
			index2++
			row2.AddCell().Value = qu.ObjToString(invest["company_name"])
			row2.AddCell().Value = pre_name
			if b {
				cell := row2.AddCell()
				link := "=HYPERLINK(" + "\"#Sheet3!A" + fmt.Sprintf("%d", index3) + "\",\"" + "下一级链接" + "\")"
				cell.SetFormula(link)
				style := cell.GetStyle()
				style.Font.Underline = true
				style.Font.Color = "FF0000FF"
			}
		}
		if level == 3 {
			row1 = sheet1.AddRow()
			row1.AddCell().Value = qu.ObjToString(invest["company_name"])
			row1.AddCell().Value = pre_name
			if b {
				cell := row1.AddCell()
				link := "=HYPERLINK(" + "\"#Sheet2!A" + fmt.Sprintf("%d", index2) + "\",\"" + "下一级链接" + "\")"
				cell.SetFormula(link)
				style := cell.GetStyle()
				style.Font.Underline = true
				style.Font.Color = "FF0000FF"
			}
		}
	}
	if len(infos) > 0 {
		return true
	}
	return false
}
