package model

import (
	"errors"
	"fmt"
	"strconv"
	"time"

	"app.yhyue.com/moapp/jybase/common"
	"app.yhyue.com/moapp/jybase/date"
	"app.yhyue.com/moapp/jybase/mongodb"
	"app.yhyue.com/moapp/jybase/mysql"
)

type Included struct {
	MysqlDb *mysql.Mysql
	Mgo     mongodb.MongodbSim
}

const (
	IncludedInfoDbName = "included_info" //招录信息表
)

type IncludedData struct {
	Year                    int64   `json:"Year"`                    //年
	Month                   int64   `json:"Month"`                   //月
	Day                     int64   `json:"Day"`                     //日
	Bid                     float64 `json:"Bid"`                     //招标信息的数值
	BidUnit                 string  `json:"BidUnit"`                 //招标信息的数值单位
	BidUnitAppend           string  `json:"BidUnitAppend"`           //招标信息的数值单位后面的加号
	Project                 float64 `json:"Project"`                 //招标采购项目的数值
	ProjectUnit             string  `json:"ProjectUnit"`             //招标采购项目的数值单位
	ProjectUnitAppend       string  `json:"ProjectUnitAppend"`       //招标采购项目的数值单位后面的加号
	Ent                     float64 `json:"Ent"`                     //企业数据库的数值
	EntUnit                 string  `json:"EntUnit"`                 //企业数据库的数值单位
	EntUnitAppend           string  `json:"EntUnitAppend"`           //企业数据库的数值单位后面的加号
	Buyer                   float64 `json:"Buyer"`                   //采购单位库的数值
	BuyerUnit               string  `json:"BuyerUnit"`               //采购单位库的数值单位
	BuyerUnitAppend         string  `json:"BuyerUnitAppend"`         //采购单位库的数值单位后面的加号
	BidDayUpdate            float64 `json:"BidDayUpdate"`            //每日更新招标信息的数值
	BidDayUpdateUnit        string  `json:"BidDayUpdateUnit"`        //每日更新招标信息的数值单位
	BidDayUpdateUnitAppend  string  `json:"BidDayUpdateUnitAppend"`  //每日更新招标信息的数值单位后面的加号
	BidField                float64 `json:"BidField"`                //招标信息数据字段的数值
	BidFieldUnit            string  `json:"BidFieldUnit"`            //招标信息数据字段的数值单位
	BidFieldUnitAppend      string  `json:"BidFieldUnitAppend"`      //招标信息数据字段数值单位后面的加号
	FieldAccuracy           float64 `json:"FieldAccuracy"`           //数据字段准确率的数值
	FieldAccuracyUnit       string  `json:"FieldAccuracyUnit"`       //数据字段准确率的数值单位
	FieldAccuracyUnitAppend string  `json:"FieldAccuracyUnitAppend"` //数据字段准确率的数值单位后面的加号
	Push                    float64 `json:"Push"`                    //推送招标信息的数值
	PushUnit                string  `json:"PushUnit"`                //推送招标信息的数值单位
	PushUnitAppend          string  `json:"PushUnitAppend"`          //推送招标信息的数值单位后面的加号
}

//获取收录情况的一堆数据
func (this *Included) GetIncludedData() (*IncludedData, error) {
	data := this.MysqlDb.SelectBySql(fmt.Sprintf(`select bid,project,ent,buyer,bid_day_update,bid_field,field_accuracy,create_time from %s order by create_time desc limit 1`, IncludedInfoDbName))
	if data == nil || len(*data) <= 0 {
		return nil, errors.New("暂无数据")
	}
	//
	info := (*data)[0]
	//招标信息的数值
	bid := common.Int64All(info["bid"])
	Bid, BidUnit := formdataNum(bid)
	//招标采购项目的数值
	project := common.Int64All(info["project"])
	Project, ProjectUnit := formdataNum(project)
	//企业数据库的数值
	ent := common.Int64All(info["ent"])
	Ent, EntUnit := formdataNum(ent)
	//采购单位库的数值
	buyer := common.Int64All(info["buyer"])
	Buyer, BuyerUnit := formdataNum(buyer)
	//每日更新招标信息的数值
	bid_day_update := common.Int64All(info["bid_day_update"])
	BidDayUpdate, BidDayUpdateUnit := formdataNum(bid_day_update)
	//每日更新招标信息的数值
	bid_field := common.Int64All(info["bid_field"])
	BidField, BidFieldUnit := formdataNum(bid_field)
	//每日更新招标信息的数值
	field_accuracy := common.Float64All(info["field_accuracy"])
	FieldAccuracy, FieldAccuracyUnit := field_accuracy, "%"
	mdata, ok := this.Mgo.Find("swordfish_index", map[string]interface{}{
		"i_push": map[string]interface{}{
			"$exists": true,
		},
	}, `{"_id":-1}`, `{"i_push":1}`, false, 0, 1)
	i_push := 0
	if mdata != nil && ok && len(*mdata) > 0 {
		swordData := (*mdata)[0]
		i_push = common.IntAll(swordData["i_push"])
	}
	Push, PushUnit := formdataNum(int64(i_push))
	createTime, _ := time.ParseInLocation(date.Date_Full_Layout, common.ObjToString(info["create_time"]), time.Local)
	includeData := &IncludedData{
		Year:              int64(createTime.Year()),
		Month:             int64(createTime.Month()),
		Day:               int64(createTime.Day()),
		Bid:               Bid,
		BidUnit:           BidUnit,
		Project:           Project,
		ProjectUnit:       ProjectUnit,
		Ent:               Ent,
		EntUnit:           EntUnit,
		Buyer:             Buyer,
		BuyerUnit:         BuyerUnit,
		BidDayUpdate:      BidDayUpdate,
		BidDayUpdateUnit:  BidDayUpdateUnit,
		BidField:          BidField,
		BidFieldUnit:      BidFieldUnit,
		FieldAccuracy:     FieldAccuracy,
		FieldAccuracyUnit: FieldAccuracyUnit,
		Push:              Push,
		PushUnit:          PushUnit,
	}
	return includeData, nil
}

//格式输出数据
//亿亿、万亿、亿、万 只有一位的时候保留1位小数点 两位及以上不保留  1.1亿  11亿
func formdataNum(num int64) (floatNum float64, unit string) {
	s_num := strconv.Itoa(int(num))
	len_m := len(s_num)
	m := ""
	indexArr := []int{17, 13, 9, 5}
	unitArr := []string{"亿亿", "万亿", "亿", "万"}

	for k, v := range indexArr {
		if len_m > v {
			if common.IntAll(s_num[len_m-(v-1):len_m-(v-2)]) >= 5 {
				if common.IntAll(s_num[0:len_m-(v-1)])+1 == 10 {
					//满10 进 1
					m1, _ := strconv.Atoi(s_num[0 : len_m-(v-1)])
					m = strconv.Itoa(m1 + 1)
				} else {
					//满 万 进1 单位
					if common.IntAll(s_num[0:len_m-(v-1)])+1 == 10000 {
						m = "1"
						unit = unitArr[k-1]
					} else {
						m = strconv.Itoa(common.IntAll(s_num[0:len_m-(v-1)]) + 1)
					}
				}
				// log.Println("m1:", m)
			} else {
				m = s_num[0 : len_m-(v-1)]
				// log.Println("m2:", m)
			}
		} else if len_m == v { //

			if common.IntAll(s_num[len_m-(v-2):len_m-(v-3)]) >= 5 {
				m = s_num[0 : len_m-(v-1)]
				//满10 进 1
				if common.IntAll(s_num[len_m-(v-1):len_m-(v-2)])+1 == 10 {
					m1, _ := strconv.Atoi(s_num[0 : len_m-(v-1)])
					m = strconv.Itoa(m1 + 1)
				} else {
					m += "." + strconv.Itoa(common.IntAll(s_num[len_m-(v-1):len_m-(v-2)])+1)
				}
				// log.Println("m3:", m)
			} else {
				m = s_num[0 : len_m-(v-1)]
				m += "." + s_num[len_m-(v-1):len_m-(v-2)]
				// log.Println("m4:", m)
			}
		}
		if m != "" {
			if unit == "" {
				unit = unitArr[k]
			}
			break
		}
	}
	if m == "" {
		m = s_num
	}
	//string 转float
	floatNum, _ = strconv.ParseFloat(m, 64)
	return floatNum, unit
}
