package main

import (
	"fmt"
	"github.com/go-xweb/log"
	"github.com/tealeg/xlsx"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"os"
	qu "qfw/util"
	"strings"
)

var (
	f_6 = xlsx.NewFile()
	f_7 = xlsx.NewFile()
	f_8 = xlsx.NewFile()
	f_9 = xlsx.NewFile()
	f_10 = xlsx.NewFile()
	f_11 = xlsx.NewFile()
	sheet_6 ,sheet_7,sheet_8,sheet_9,sheet_10,sheet_11 	*xlsx.Sheet
)

func initQydata()  {
	os.Remove("公司资质.xlsx")
	os.Remove("注册人员.xlsx")
	os.Remove("黑名单.xlsx")
	os.Remove("不良名单.xlsx")
	os.Remove("诚信惩戒.xlsx")
	os.Remove("围标串标.xlsx")

	sheet_6, _= f_6.AddSheet("公司资质")
	sheet_7, _= f_7.AddSheet("注册人员")
	sheet_8, _= f_8.AddSheet("黑名单")
	sheet_9, _= f_9.AddSheet("不良名单")
	sheet_10, _= f_10.AddSheet("诚信惩戒")
	sheet_11, _= f_11.AddSheet("围标串标")




	row_6:= sheet_6.AddRow()
	row_7:= sheet_7.AddRow()
	row_8:= sheet_8.AddRow()
	row_9:= sheet_9.AddRow()
	row_10:= sheet_10.AddRow()
	row_11:= sheet_11.AddRow()


	for _,v := range qy_tagText[0]{
		row_6.AddCell().Value = v
	}
	for _,v := range qy_tagText[1]{
		row_7.AddCell().Value = v
	}
	for _,v := range qy_tagText[2]{
		row_8.AddCell().Value = v
	}
	for _,v := range qy_tagText[3]{
		row_9.AddCell().Value = v
	}
	for _,v := range qy_tagText[4]{
		row_10.AddCell().Value = v
	}
	for _,v := range qy_tagText[5]{
		row_11.AddCell().Value = v
	}

}

//导出企业信息表
func exportQydataOne() {
	log.Println("导出~企业信息表...")
	sess := py_mgo.GetMgoConn()
	defer py_mgo.DestoryMongoConn(sess)
	q,total := map[string]interface{}{},0
	num_6,num_7,num_8,num_9,num_10:=0,0,0,0,0
	it := sess.DB(py_mgo.DbName).C(py_coll_one).Find(&q).Iter()
	for tmp := make(map[string]interface{}); it.Next(&tmp);total++ {
		if total%1000 == 0 {
			log.Println("curent index ",total,num_6,num_7,num_8,num_9,num_10)
			f_6.Save("公司资质.xlsx")
			f_7.Save("注册人员.xlsx")
			f_8.Save("黑名单.xlsx")
			f_9.Save("不良名单.xlsx")
			f_10.Save("诚信惩戒.xlsx")
		}

		//公共字段数据数据
		num_6 = dealWithGSZZ(tmp,num_6)
		num_7 = dealWithZCRY(tmp,num_7)
		num_8 = dealWithHMD(tmp,num_8)
		num_9 = dealWithBLMD(tmp,num_9)
		num_10 = dealWithCXCJ(tmp,num_10)

		tmp = make(map[string]interface{})
	}

	if total > 0 {
		f_6.Save("公司资质.xlsx")
		f_7.Save("注册人员.xlsx")
		f_8.Save("黑名单.xlsx")
		f_9.Save("不良名单.xlsx")
		f_10.Save("诚信惩戒.xlsx")
	}

	log.Println("is over one ",total,num_6,num_7,num_8,num_9,num_10)
}
//导出围标串标表
func exportQydataTwo()  {
	log.Println("导出~围标串标表...")
	sess := py_mgo.GetMgoConn()
	defer py_mgo.DestoryMongoConn(sess)
	q,total := map[string]interface{}{},0
	num_11 := 0
	it := sess.DB(py_mgo.DbName).C(py_coll_two).Find(&q).Iter()
	for tmp := make(map[string]interface{}); it.Next(&tmp);total++ {
		if total%1000 == 0 {
			log.Println("curent index ",total,num_11)
			f_11.Save("围标串标.xlsx")
		}
		num_11 = dealWithWBCB(tmp,num_11)
		tmp = make(map[string]interface{})
	}
	if total > 0 {
		f_11.Save("围标串标.xlsx")
	}
	log.Println("is over two ",total,num_11)
}


//公司资质
func dealWithGSZZ(tmp map[string]interface{},index int)int{
	company_quality := []map[string]interface{}{}
	if info , ok := tmp["company_quality"].(primitive.A); ok {
		company_quality = qu.ObjArrToMapArr(info)
	}else {
		if info, ok := tmp["company_quality"].([]interface{}); ok {
			company_quality = qu.ObjArrToMapArr(info)
		}
	}
	//公共字段
	credit_no := qu.ObjToString(tmp["credit_no"])
	legal_person := qu.ObjToString(tmp["legal_person"])
	company_type := qu.ObjToString(tmp["company_type"])
	company_name := qu.ObjToString(tmp["company_name"])
	address := qu.ObjToString(tmp["address"])
	business_address := qu.ObjToString(tmp["business_address"])
	industry := qu.ObjToString(tmp["industry"])
	register_date := qu.ObjToString(tmp["register_date"])
	tel_phone := qu.ObjToString(tmp["tel_phone"])
	area,city,district := analyseACD(address)
	for _,v := range company_quality {
		index++
		dataArr := []string{}
		dataArr = append(dataArr,fmt.Sprintf("%d",index))
		dataArr = append(dataArr,"全国建筑市场监管公共服务平台")
		dataArr = append(dataArr,company_name)//企业名称
		dataArr = append(dataArr,credit_no)
		dataArr = append(dataArr,legal_person)
		dataArr = append(dataArr,qu.ObjToString(company_type))
		dataArr = append(dataArr,industry)//所属行业
		dataArr = append(dataArr,area)
		dataArr = append(dataArr,city)
		dataArr = append(dataArr,district)
		dataArr = append(dataArr,business_address) //企业地址

		dataArr = append(dataArr,qu.ObjToString(v["quality_type"])) //企业资质类别
		dataArr = append(dataArr,qu.ObjToString(v["quality_no"]))
		dataArr = append(dataArr,qu.ObjToString(v["fzrq"]))//发证日期
		dataArr = append(dataArr,qu.ObjToString(v["fzyxq"]))
		dataArr = append(dataArr,qu.ObjToString(v["fzjg"]))
		dataArr = append(dataArr,qu.ObjToString(v["quality_name"]))

		dataArr = append(dataArr,register_date)//注册时间
		dataArr = append(dataArr,tel_phone)
		dataArr = append(dataArr,"")
		dataArr = append(dataArr,"")
		dataArr = append(dataArr,"")//失信标记

		row_6:= sheet_6.AddRow()
		for _,v_1 := range dataArr{
			row_6.AddCell().Value = v_1
		}
	}
	return index
}
//注册人员
func dealWithZCRY(tmp map[string]interface{},index int) int {
	company_staff := []map[string]interface{}{}
	if info , ok := tmp["company_staff"].(primitive.A); ok {
		company_staff = qu.ObjArrToMapArr(info)
	}else {
		if info, ok := tmp["company_staff"].([]interface{}); ok {
			company_staff = qu.ObjArrToMapArr(info)
		}
	}
	company_name := qu.ObjToString(tmp["company_name"])
	register_date := qu.ObjToString(tmp["register_date"])

	dict := map[string]string{}

	for _,v := range company_staff {
		id_no := qu.ObjToString(v["id_no"])
		reg_type := qu.ObjToString(v["reg_type"])
		reg_no := qu.ObjToString(v["reg_no"])
		reg_major := qu.ObjToString(v["reg_major"])
		key := fmt.Sprintf("%s_%s_%s_%s_%s",company_name,id_no,reg_type,reg_no,reg_major)
		if dict[key]=="" {
			index++
			dict[key] = key
		}else {
			continue
		}
		dataArr := []string{}
		dataArr = append(dataArr,fmt.Sprintf("%d",index))
		dataArr = append(dataArr,"全国建筑市场监管公共服务平台")
		dataArr = append(dataArr,qu.ObjToString(v["name"]))
		dataArr = append(dataArr,company_name)//企业名称
		dataArr = append(dataArr,qu.ObjToString(v["id_no"]))
		dataArr = append(dataArr,qu.ObjToString(v["reg_type"]))
		dataArr = append(dataArr,qu.ObjToString(v["reg_no"]))
		dataArr = append(dataArr,qu.ObjToString(v["reg_major"]))
		dataArr = append(dataArr,register_date)//注册时间

		row_7:= sheet_7.AddRow()
		for _,v_1 := range dataArr{
			row_7.AddCell().Value = v_1
		}
	}
	return index
}
//黑名单
func dealWithHMD(tmp map[string]interface{},index int) int {
	black_list := []map[string]interface{}{}
	if info , ok := tmp["black_list"].(primitive.A); ok {
		black_list = qu.ObjArrToMapArr(info)
	}else {
		if info, ok := tmp["black_list"].([]interface{}); ok {
			black_list = qu.ObjArrToMapArr(info)
		}
	}
	company_name := qu.ObjToString(tmp["company_name"])
	for _,v := range black_list {
		index++
		dataArr := []string{}
		dataArr = append(dataArr, fmt.Sprintf("%d", index))
		dataArr = append(dataArr, "全国建筑市场监管公共服务平台")
		dataArr = append(dataArr, company_name)//企业名称
		dataArr = append(dataArr, qu.ObjToString(v["black_list_no"]))
		dataArr = append(dataArr, qu.ObjToString(v["black_list_rdyj"]))
		dataArr = append(dataArr, qu.ObjToString(v["rdbm"]))

		dataArr = append(dataArr, "") //文号
		dataArr = append(dataArr, "")
		dataArr = append(dataArr, "")

		row_8:= sheet_8.AddRow()
		for _,v_1 := range dataArr{
			row_8.AddCell().Value = v_1
		}
	}
	return index
}
//不良名单
func dealWithBLMD(tmp map[string]interface{},index int) int {
	bad_behavior := []map[string]interface{}{}
	if info , ok := tmp["bad_behavior"].(primitive.A); ok {
		bad_behavior = qu.ObjArrToMapArr(info)
	}else {
		if info, ok := tmp["bad_behavior"].([]interface{}); ok {
			bad_behavior = qu.ObjArrToMapArr(info)
		}
	}
	company_name := qu.ObjToString(tmp["company_name"])
	for _,v := range bad_behavior {
		index++
		dataArr := []string{}
		dataArr = append(dataArr, fmt.Sprintf("%d", index))
		dataArr = append(dataArr, "全国建筑市场监管公共服务平台")
		dataArr = append(dataArr, company_name)//企业名称

		dataArr = append(dataArr, qu.ObjToString(v["integrity_no"]))
		dataArr = append(dataArr, qu.ObjToString(v["decide_content"]))
		dataArr = append(dataArr, qu.ObjToString(v["ssbm"]))

		dataArr = append(dataArr, "") //文号
		dataArr = append(dataArr, "")
		dataArr = append(dataArr, "")

		row_9:= sheet_9.AddRow()
		for _,v_1 := range dataArr{
			row_9.AddCell().Value = v_1
		}
	}

	return index
}
//诚信惩戒
func dealWithCXCJ(tmp map[string]interface{},index int) int {
	punish := []map[string]interface{}{}
	if info , ok := tmp["punish"].(primitive.A); ok {
		punish = qu.ObjArrToMapArr(info)
	}else {
		if info, ok := tmp["punish"].([]interface{}); ok {
			punish = qu.ObjArrToMapArr(info)
		}
	}
	company_name := qu.ObjToString(tmp["company_name"])
	for _,v := range punish {
		index++
		dataArr := []string{}
		dataArr = append(dataArr, fmt.Sprintf("%d", index))
		dataArr = append(dataArr, "全国建筑市场监管公共服务平台")
		dataArr = append(dataArr, company_name)//企业名称
		dataArr = append(dataArr, qu.ObjToString(v["punish_no"]))
		dataArr = append(dataArr, qu.ObjToString(v["legal_person"]))
		dataArr = append(dataArr, qu.ObjToString(v["reason"]))
		dataArr = append(dataArr, qu.ObjToString(v["rdbm"]))

		dataArr = append(dataArr, "") //文号
		dataArr = append(dataArr, qu.ObjToString(v["join_date"]))
		dataArr = append(dataArr, "")

		row_10:= sheet_10.AddRow()
		for _,v_1 := range dataArr{
			row_10.AddCell().Value = v_1
		}
	}

	return index
}



//围标串标
func dealWithWBCB(tmp map[string]interface{},index int) int {
	info := []map[string]interface{}{}
	if info_data , ok := tmp["info"].(primitive.A); ok {
		info = qu.ObjArrToMapArr(info_data)
	}else {
		if info_data, ok := tmp["info"].([]interface{}); ok {
			info = qu.ObjArrToMapArr(info_data)
		}
	}
	company_name := qu.ObjToString(tmp["company"])
	key_words := qu.ObjToString(tmp["key_words"])
	if strings.Contains(key_words,"串") {
		key_words = "串标"
	}else if strings.Contains(key_words,"围") {
		key_words = "围标"
	}else {
		key_words = "其他"
	}
	for _,v := range info {
		index++
		dataArr := []string{}
		dataArr = append(dataArr, fmt.Sprintf("%d", index))
		dataArr = append(dataArr, qu.ObjToString(v["source"]))
		dataArr = append(dataArr, company_name)
		dataArr = append(dataArr, qu.ObjToString(v["time"]))
		dataArr = append(dataArr, key_words)
		dataArr = append(dataArr, qu.ObjToString(v["url"]))

		row_11:= sheet_11.AddRow()
		for _,v_1 := range dataArr{
			row_11.AddCell().Value = v_1
		}
	}


	return index
}






func analyseACD(str string) (string,string,string) {
	area,city,district := "","",""
	arr := strings.Split(str,"-")
	if len(arr)==1 {
		area = arr[0]
	}else if len(arr)==2 {
		area = arr[0]
		city = arr[1]
	}else if len(arr)>=3 {
		area = arr[0]
		city = arr[1]
		district = arr[2]
	}else {

	}
	return area,city,district
}