package main

import (
	"fmt"
	"go.uber.org/zap"
	util "jygit.jydev.jianyu360.cn/data_processing/common_utils"
	"jygit.jydev.jianyu360.cn/data_processing/common_utils/log"
	"jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
	"net/http"
	"sort"
	"strconv"
	"strings"
)

//SendMail 发送邮件
func SendMail(title, content string) {
	url := fmt.Sprintf("%s?to=%s&title=%s&body=%s", GF.Email.Api, GF.Email.To, title, content)
	fmt.Println("url=>", url)
	res, err := http.Get(url)
	if err != nil {
		log.Info("SendMail", zap.Any("err", err))
	} else {
		log.Info("SendMail", zap.Any("res", res))
	}
}

//calculateFlag 根据数据，返回被标注的字段数字
func calculateFlag(marked map[string]interface{}, data []map[string]interface{}) int {
	var result int
	for _, item := range data {
		name, ok := item["name"].(string)
		if !ok {
			continue
		}

		sort, ok := item["sort"].(int)

		if !ok {
			continue
		}

		// 根据字段名称查找对应的标记值
		_, ok = marked[name]
		if !ok {
			continue
		}

		// 通过位运算将标记值放置到正确的位置
		result |= 1 << (sort - 1)
	}

	return result
}

//IsInStringArray 判断数组中是否存在字符串
func IsInStringArray(str string, arr []string) bool {
	// 先对字符串数组进行排序
	sort.Strings(arr)
	// 使用二分查找算法查找字符串
	pos := sort.SearchStrings(arr, str)
	// 如果找到了则返回 true，否则返回 false
	return pos < len(arr) && arr[pos] == str
}

//TagBidding 根据bidding数据打标签
func TagBidding(tmp map[string]interface{}) (res map[string]interface{}) {
	res = make(map[string]interface{}, 0)
	toptype := util.ObjToString(tmp["toptype"])
	subtype := util.ObjToString(tmp["subtype"])
	buyerclass := util.ObjToString(tmp["buyerclass"])
	title := util.ObjToString(tmp["title"])
	detail := util.ObjToString(tmp["detail"])
	spidercode := util.ObjToString(tmp["spidercode"])

	//剑鱼信息发布数据
	if spidercode == "a_jyxxfbpt_gg" {
		res["业主委托项目"] = 1
	}

	if _, ok := tmp["hot_data"]; ok {
		res["热门项目"] = 1
	}

	if toptype != "" && subtype != "" {
		column1Rule := []string{"招标", "邀标", "询价", "单一", "竞价", "变更"} //招标、邀标、询价、单一、竞价、变更
		if toptype == "招标" && IsInStringArray(subtype, column1Rule) {
			res["招标公告"] = 1
		}
		column2Rule := []string{"预告", "预审", "预审结果", "论证意见", "竞价", "变更"} //预告、预审、预审结果、论证意见、需求公示、其它
		if toptype == "预告" && IsInStringArray(subtype, column2Rule) {
			res["招标预告"] = 1
		}

		column3Rule := []string{"中标", "成交", "废标", "流标"} //中标、成交、废标、流标
		if toptype == "结果" && IsInStringArray(subtype, column3Rule) {
			res["招标结果"] = 1
		}

		column4Rule := []string{"合同", "验收", "违规"} //合同、验收、违规
		if toptype == "其它" && IsInStringArray(subtype, column4Rule) {
			res["招标信用"] = 1
		}

		if toptype == "采购意向" && subtype == "采购意向" {
			res["采购意向"] = 1
		}
	}

	if packages, ok := tmp["package"]; ok {
		packageMap, ok := packages.(map[string]interface{})
		if ok {
			if len(packageMap) >= 0 {
				for _, pack := range packageMap {
					pac, okk := pack.(map[string]interface{})
					if okk {
						_, okk1 := pac["winner"]
						_, okk2 := pac["bidamount"]
						_, okk3 := pac["budget"]
						if okk1 || okk2 || okk3 {
							res["项目分包"] = 1
							break
						}
					}
				}
			}
		}
	}

	//
	if buyerclass != "" {
		buyers1 := []string{"采矿业", "电信行业", "金融业", "建筑业", "能源化工", "农林牧渔", "批发零售", "信息技术", "运输物流", "制造业", "住宿餐饮"}
		if IsInStringArray(buyerclass, buyers1) {
			res["企业直采"] = 1
		}

		buyers2 := []string{"教育", "学校", "卫健委", "医疗", "交通", "工信", "农业", "住建", "城管", "市政", "人行",
			"出版广电", "检察院", "科技", "民政", "生态环境", "市场监管", "水利", "应急管理", "自然资源", "财政", "档案", "党委办",
			"组织", "发改", "宣传", "政府办", "政务中心", "人大", "政协", "法院", "公安", "国资委", "海关", "机关事务", "纪委",
			"军队", "人社", "商务", "审计", "税务", "司法", "体育", "统计", "统战", "文旅", "民宗", "银保监", "证监", "气象",
			"社会团体", "公共资源交易", "其它"}
		if IsInStringArray(buyerclass, buyers2) {
			res["政府采购"] = 1
		}
	}

	//
	keyWords1 := []string{"新能源车", "新能源电池", "太阳能", "风能", "氢能", "太阳能发电站", "风力发电站", "氢能燃料电池", "智能算法",
		"机器学习", "无人驾驶", "智慧城市", "5G基站建设", "5G网络覆盖", "5G应用研发", "无人驾驶", "远程医疗", "工业互联网", "基因编辑",
		"生物制药", "生物农业", "生物环保", "智能交通", "智能照明", "智能安防", "智能环保", "智慧校园", "智慧医院", "在线教育平台", "教育机器人",
		"个性化教学系统", "污水处理", "废气治理", "废弃物资源化利用", "污染物去除", "净化", "减排", "环保设施", "资源利用率", "废弃物回收",
		"低碳", "绿色生产", "生态恢复", "生物多样性", "生态系统稳定", "生态环境改善", "清洁能源", "可再生能源", "新能源", "低碳能源"}

	for _, v := range keyWords1 {
		if strings.Contains(title, v) || strings.Contains(detail, v) {
			res["新兴项目"] = 1
			break
		}
	}

	funds := util.ObjToString(tmp["funds"])
	fundsWors1 := []string{"国家", "财政性资金"}
	for _, v := range fundsWors1 {
		if strings.Contains(funds, v) {
			res["国家级项目"] = 1
			break
		}
	}

	fundsWors2 := []string{"省份自筹"}
	for _, v := range fundsWors2 {
		if strings.Contains(funds, v) {
			res["省级项目"] = 1
			break
		}
	}

	return
}

//TagProject 给拟建项目表 打标签
func TagProject(tmp map[string]interface{}) (res map[string]interface{}) {
	res = make(map[string]interface{}, 0)
	id := mongodb.BsonIdToSId(tmp["_id"])

	comb, _ := MgoP.FindById("projectset_comb", id, nil)
	if len(*comb) > 0 {
		res["拟在建项目"] = 1
	}

	project_stage_code := util.ObjToString(tmp["project_stage_code"])
	stage_code, _ := strconv.Atoi(project_stage_code)
	if stage_code != 0 && stage_code != 6 && stage_code != 7 {
		res["审批项目"] = 1
	}

	if stage_code > 6 {
		res["推荐项目"] = 1
	}

	return
}
