package main

import (
	"fmt"
	log "github.com/donnie4w/go-logger/logger"
	qu "qfw/util"
	"regexp"
	"strings"
)

var (
	sysconfig                                                  map[string]interface{} //配置文件
	save_mgo, qy_mgo, spi_mgo                                  *MongodbSim            //mongodb操作对象
	MysqlTool                                                  *Mysql
	s_area_coll, s_product_coll, s_company_coll, s_qybase_coll string
	o_area_coll, o_class_coll, o_company_coll, o_product_coll  string
	s_class_xlsx_name, s_area_xlsx_name                        string
	o_company_err_coll                                         string
	s_bidding_coll, o_bidding_coll, s_company_history_coll     string
	o_hospital_coll, o_company_history_coll                    string
)

var timeReg = regexp.MustCompile(`^(\d{4}-[0-1][0-9]-[0-3][0-9])$`)
var timeReg1 = regexp.MustCompile(`^(\d{4}-\d{1,2}-\d{1,2})$`)
var Url = "https://www.jianyu360.com/article/content/%s.html"

var bidclass_dict = map[string]string{}      //v表-招标类别
var buyerclass_dict = map[string]string{}    //v表-采购类别
var industry_dict = map[string]string{}      //v表-行业类别
var area_dict = map[string]string{}          //v表-行政区划
var proclass_name_dict = map[string]string{} //v表-医疗分类-名称对应-代码
var proclass_code_dict = map[string]string{} //v表-医疗分类-代码对应-名称
var timeLayout = "2006-01-02 15:04:05"

func init() {
	qu.ReadConfig(&sysconfig)
	initConfig()
	log.Debug("初始化完毕...")
}

func main() {
	//initMySqlMgoData()
	//saveBiddingPurMysql()
	//exportAreaMysql()
	//saveHospitalMysql()

	saveYlqxclassMysql()

	//医械分类数据
	/*
		repairYiXieChaData() 		//医械查
		dealWithClassData()			//AB 匹配
		repairEqRegNoClassData()	//注册号-截取匹配
		repairEqClassData()			//同分类-补充-0123
	*/
	repairEqRegNoClassData() //注册号-截取匹配
	return

	arr := *MysqlTool.Find("f_bidproduct", nil, "product_name,product_brand,bid_company_name,bid_company_ysname", "", -1, -1)
	data_arr := map[string][]string{}
	for _, v := range arr {
		product_name := qu.ObjToString(v["product_name"])
		product_brand := qu.ObjToString(v["product_brand"])
		bid_company_ysname := qu.ObjToString(v["bid_company_ysname"])
		if product_name == "" || product_brand == "" {
			continue
		}
		if bid_company_ysname != "" {
			arr_name := strings.Split(bid_company_ysname, ",")
			for _, value := range arr_name {
				if data_arr[value] == nil {
					data_arr[value] = []string{product_name + "~" + product_brand}
				} else { //拼接
					tmp_arr := data_arr[value]
					tmp_arr = append(tmp_arr, product_name+"~"+product_brand)
					data_arr[value] = tmp_arr
				}
			}
		}
	}
	for k, v := range data_arr {
		str := ""
		for _, v1 := range v {
			if str == "" {
				str = v1
				continue
			}
			if !strings.Contains(str, v1) {
				str += "," + v1
			}
		}
		insertMysqlData("f_brandname_ys", map[string]interface{}{
			"company_name": k,
			"brands":       str,
		}, "")
	}
	log.Debug("结束~", len(data_arr))
}

//插入数据
func insertMysqlData(name string, data map[string]interface{}, mark string) {
	inb := MysqlTool.Insert(name, data)
	if inb == -1 {
		log.Debug("插入数据异常...", name, "~", mark)
	}
}

type Info struct {
	id               int
	product_name     string
	project_brand    string
	bid_company_name string
	bid_company_id   string
}

func test() {
	sqlStr := "select id,product_name,project_brand,bid_company_name,bid_company_id, from f_bidproduct where id > ?"
	rows, err := MysqlTool.DB.Query(sqlStr, 0)
	if err != nil {
		fmt.Printf("query failed, err:%v\n", err)
		return
	}
	defer rows.Close()
	// 循环读取结果集中的数据
	for rows.Next() {
		var info Info
		err := rows.Scan(
			&info.id,
			&info.product_name,
			&info.project_brand,
			&info.bid_company_name,
			&info.bid_company_id,
		)
		if err != nil {
			fmt.Printf("scan failed, err:%v\n", err)
			return
		}
		log.Debug(info)
	}
}
