hotime/example/admin/test.go
2022-02-14 19:10:58 +08:00

691 lines
18 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package admin
import (
. "../../../hotime"
. "../../common"
"../../dri/baidu"
"fmt"
"github.com/robertkrimen/otto"
"github.com/xuri/excelize"
"io/ioutil"
"os"
"sort"
"strings"
"time"
"unicode"
)
func analyse(companys []Map, tag Map) {
cs := append(NewCompanys{}, companys...)
for _, company := range companys {
vm := otto.New()
vm.Set("sortLevel", func(call otto.FunctionCall) otto.Value {
AnalyseSortType = call.Argument(0).String()
sort.Sort(cs)
for k, v := range cs {
if company.GetMap(AnalyseTypeName).GetFloat64(AnalyseSortType) > v.GetMap(AnalyseTypeName).GetFloat64(AnalyseSortType) {
val, _ := vm.ToValue(k + 1)
return val
}
}
val, _ := vm.ToValue(len(cs))
return val
})
vm.Set("count", func(call otto.FunctionCall) otto.Value {
//key:=call.Argument(0).String()
val, _ := vm.ToValue(len(cs))
return val
})
//上传数据
uploadData := company.GetMap("upload_data")
for k, _ := range uploadData {
strV := uploadData.GetString(k)
v := float64(0)
if strV == "是" {
v = 1
}
if strings.Index(strV, "%") == len(strV)-1 && len(strV) != 0 {
v = uploadData.GetFloat64(strings.Replace(strV, "%", "", -1)) / 100
}
flV := uploadData.GetFloat64(k)
if flV != 0 && v == 0 {
v = flV
}
vm.Set(k, v)
}
//api采集数据
collectData := company.GetMap("collect_data")
for k, cv := range collectData {
if cv == nil {
vm.Set(k, 0)
continue
}
strV := collectData.GetString(k)
if strings.Index(strV, "万人民币") != -1 {
strV = strings.Replace(strV, "", "万人民币", -1)
}
vm.Set(k, strV)
}
//api采集数据
analyseData := company.GetMap("analyse")
if analyseData == nil {
analyseData = Map{}
company["analyse"] = analyseData
}
for k, cv := range analyseData {
vm.Set(k, cv)
}
//res:=Map{}
//analyse
//for _,v:=range cmds{
vm.Set("res", nil)
_, e := vm.Run(tag.GetString("cmd"))
if e != nil {
fmt.Println(e)
analyseData[tag.GetString("name")] = 0
vm.Set(tag.GetString("name"), 0)
continue
}
result, e := vm.Get("res")
if e != nil {
fmt.Println(e)
analyseData[tag.GetString("name")] = 0
vm.Set(tag.GetString("name"), 0)
continue
}
if result.String() == ObjToStr(ObjToFloat64(result.String())) {
analyseData[tag.GetString("name")] = ObjToFloat64(result.String())
} else if result.String() == "NaN" {
analyseData[tag.GetString("name")] = 0
} else {
analyseData[tag.GetString("name")] = result.String()
}
vm.Set(tag.GetString("name"), analyseData[tag.GetString("name")])
}
}
type NewCompanys []Map
var AnalyseSortType = ""
var AnalyseTypeName = "analyse"
func (s NewCompanys) Len() int {
return len(s)
}
func (s NewCompanys) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s NewCompanys) Less(i, j int) bool {
AnalyseTypeName = "upload_data"
if s[i].GetMap(AnalyseTypeName)[AnalyseSortType] == nil {
AnalyseTypeName = "analyse"
}
return s[i].GetMap(AnalyseTypeName).GetFloat64(AnalyseSortType) > s[j].GetMap(AnalyseTypeName).GetFloat64(AnalyseSortType)
}
func totalAnalyse(companys []Map, tag Map, totalData Map) {
cs := append(NewCompanys{}, companys...)
for _, company := range companys {
vm := otto.New()
vm.Set("vcount", func(call otto.FunctionCall) otto.Value {
if call.Argument(0).IsUndefined() {
val, _ := vm.ToValue(0)
return val
}
key := call.Argument(0).String()
AnalyseTypeName := "upload_data"
if company.GetMap(AnalyseTypeName)[key] == nil {
AnalyseTypeName = "analyse"
}
num := float64(0)
for _, v := range cs {
if v.GetMap(AnalyseTypeName).GetFloat64(key) > 0 {
//num++
num = num + v.GetMap(AnalyseTypeName).GetFloat64(key)
}
}
val, _ := vm.ToValue(num)
return val
})
vm.Set("count", func(call otto.FunctionCall) otto.Value {
if call.Argument(0).IsUndefined() {
val, _ := vm.ToValue(len(cs))
return val
}
key := call.Argument(0).String()
AnalyseTypeName := "upload_data"
if company.GetMap(AnalyseTypeName)[key] == nil {
AnalyseTypeName = "analyse"
}
num := 0
for _, v := range cs {
if v.GetMap(AnalyseTypeName).GetFloat64(key) > 0 {
num++
}
}
val, _ := vm.ToValue(num)
return val
})
//上传数据
uploadData := company.GetMap("upload_data")
for k, _ := range uploadData {
strV := uploadData.GetString(k)
v := float64(0)
if strV == "是" {
v = 1
}
if strings.Index(strV, "%") == len(strV)-1 && len(strV) != 0 {
v = uploadData.GetFloat64(strings.Replace(strV, "%", "", -1)) / 100
}
flV := uploadData.GetFloat64(k)
if flV != 0 && v == 0 {
v = flV
}
vm.Set(k, v)
}
//api采集数据
collectData := company.GetMap("collect_data")
for k, cv := range collectData {
if cv == nil {
vm.Set(k, 0)
continue
}
strV := collectData.GetString(k)
if strings.Index(strV, "万人民币") != -1 {
strV = strings.Replace(strV, "", "万人民币", -1)
}
vm.Set(k, strV)
}
//api采集数据
analyseData := company.GetMap("analyse")
if analyseData == nil {
analyseData = Map{}
company["analyse"] = analyseData
}
for k, cv := range analyseData {
vm.Set(k, cv)
}
//totalData:=Map{}
//analyse
//for _,v:=range cmds{
vm.Set("res", nil)
_, e := vm.Run(tag.GetString("cmd"))
if e != nil {
fmt.Println(e)
totalData[tag.GetString("name")] = 0
vm.Set(tag.GetString("name"), 0)
continue
}
result, e := vm.Get("res")
if e != nil {
fmt.Println(e)
totalData[tag.GetString("name")] = 0
vm.Set(tag.GetString("name"), 0)
continue
}
if result.String() == ObjToStr(ObjToFloat64(result.String())) {
totalData[tag.GetString("name")] = ObjToFloat64(result.String())
} else if result.String() == "NaN" {
totalData[tag.GetString("name")] = 0
} else {
totalData[tag.GetString("name")] = result.String()
}
vm.Set(tag.GetString("name"), totalData[tag.GetString("name")])
}
}
func ctgAnalyse(companys []Map, tag Map, totalData, totalAnalyse Map) {
cs := append(NewCompanys{}, companys...)
for _, company := range companys {
vm := otto.New()
vm.Set("vcount", func(call otto.FunctionCall) otto.Value {
if call.Argument(0).IsUndefined() {
val, _ := vm.ToValue(0)
return val
}
key := call.Argument(0).String()
AnalyseTypeName := "upload_data"
if company.GetMap(AnalyseTypeName)[key] == nil {
AnalyseTypeName = "analyse"
}
num := float64(0)
for _, v := range cs {
if v.GetMap(AnalyseTypeName).GetFloat64(key) > 0 {
//num++
num = num + v.GetMap(AnalyseTypeName).GetFloat64(key)
}
}
val, _ := vm.ToValue(num)
return val
})
vm.Set("count", func(call otto.FunctionCall) otto.Value {
if call.Argument(0).IsUndefined() {
val, _ := vm.ToValue(len(cs))
return val
}
key := call.Argument(0).String()
AnalyseTypeName := "upload_data"
if company.GetMap(AnalyseTypeName)[key] == nil {
AnalyseTypeName = "analyse"
}
num := 0
for _, v := range cs {
if v.GetMap(AnalyseTypeName).GetFloat64(key) > 0 {
num++
}
}
val, _ := vm.ToValue(num)
return val
})
//上传数据
uploadData := company.GetMap("upload_data")
for k, _ := range uploadData {
strV := uploadData.GetString(k)
v := float64(0)
if strV == "是" {
v = 1
}
if strings.Index(strV, "%") == len(strV)-1 && len(strV) != 0 {
v = uploadData.GetFloat64(strings.Replace(strV, "%", "", -1)) / 100
}
flV := uploadData.GetFloat64(k)
if flV != 0 && v == 0 {
v = flV
}
vm.Set(k, v)
}
//api采集数据
collectData := company.GetMap("collect_data")
for k, cv := range collectData {
if cv == nil {
vm.Set(k, 0)
continue
}
strV := collectData.GetString(k)
if strings.Index(strV, "万人民币") != -1 {
strV = strings.Replace(strV, "", "万人民币", -1)
}
vm.Set(k, strV)
}
//api采集数据
analyseData := company.GetMap("analyse")
if analyseData == nil {
analyseData = Map{}
company["analyse"] = analyseData
}
for k, cv := range analyseData {
vm.Set(k, cv)
}
//totalData:=Map{}
//analyse
//for _,v:=range cmds{
vm.Set("res", nil)
_, e := vm.Run(tag.GetString("cmd"))
if e != nil {
fmt.Println(e)
totalData[tag.GetString("name")] = 0
vm.Set(tag.GetString("name"), 0)
continue
}
result, e := vm.Get("res")
if e != nil {
fmt.Println(e)
totalData[tag.GetString("name")] = 0
vm.Set(tag.GetString("name"), 0)
continue
}
if result.String() == ObjToStr(ObjToFloat64(result.String())) {
totalData[tag.GetString("name")] = ObjToFloat64(result.String())
if totalAnalyse.GetMap("home_data").GetCeilFloat64(tag.GetString("name")) == 0 {
totalData[tag.GetString("name")+"比值"] = 0
} else {
totalData[tag.GetString("name")+"比值"] = ObjToFloat64(result.String()) / totalAnalyse.GetMap("home_data").GetCeilFloat64(tag.GetString("name"))
}
} else if result.String() == "NaN" {
totalData[tag.GetString("name")] = 0
totalData[tag.GetString("name")+"比值"] = 0
} else {
totalData[tag.GetString("name")] = result.String()
//totalData[tag.GetString("name")+"比值"]=0
}
vm.Set(tag.GetString("name"), totalData[tag.GetString("name")])
}
}
var TestCtr = Ctr{
"test": func(this *Context) {
//data := excel1()
//this.Display(0, data)
fmt.Println(unicode.IsPunct(rune('')))
},
"industry_analyse": func(this *Context) {
orgId := 1
industrys := this.Db.Select("industry_analyse", "id,name", Map{"org_id": orgId})
totalAnalyse := this.Db.Get("org_analyse", "*", Map{"org_id": orgId})
tagCompany := this.Db.Select("tag", "*", Map{"type": 4, "ORDER": "`sort` ASC"})
for _, industry := range industrys {
id := industry.GetCeilInt("id")
companys := this.Db.Select("industry", Map{"[><]company": "company.industry_id=industry.id"},
"company.id,company.gs,company.level,company.score,company.upload_data,company.analyse,company.industry_id", Map{"industry.id": id})
for _, v := range companys {
//上传数据
v["upload_data"] = v.GetMap("upload_data")
v["collect_data"] = v.GetMap("collect_data")
v["analyse"] = v.GetMap("analyse")
}
//cs := append(NewCompanys{}, companys...)
ctgAn := Map{}
for _, tag := range tagCompany {
ctgAnalyse(companys, tag, ctgAn, totalAnalyse)
//fmt.Println(data)
}
//ctgAn := ctgAnalyse(companys, totalAnalyse)
data := Map{
"modify_time": time.Now().Unix(),
"data": ctgAn.ToJsonString(),
}
this.Db.Update("industry_analyse", data, Map{"AND": Map{"org_id": orgId, "industry_id": id}})
}
this.Display(0, industrys)
},
"total_Analyse": func(this *Context) {
orgId := 1
companys := this.Db.Select("company", "*")
for _, v := range companys {
//上传数据
v["upload_data"] = v.GetMap("upload_data")
v["collect_data"] = v.GetMap("collect_data")
v["analyse"] = v.GetMap("analyse")
}
tagCompany := this.Db.Select("tag", "*", Map{"type": 4, "ORDER": "`sort` ASC"})
//cs := append(NewCompanys{}, companys...)
totalData := Map{}
for _, tag := range tagCompany {
totalAnalyse(companys, tag, totalData)
//fmt.Println(data)
}
//home_data, six_item_data, three_item_data, n_item_data := totalAnalyse(companys)
org := this.Db.Get("org", "*", Map{"id": orgId})
//fmt.Println(six_item_data.ToJsonString())
data := Map{
"name": org.GetString("name"),
"home_data": totalData.ToJsonString(),
//"six_item_data": six_item_data.ToJsonString(),
//"three_item_data": three_item_data.ToJsonString(),
//"n_item_data": n_item_data.ToJsonString(),
"modify_time": time.Now().Unix(),
}
org_analyse := this.Db.Get("org_analyse", "id", Map{"org_id": orgId})
if org_analyse == nil {
data["name"] = org.GetString("name")
data["org_id"] = orgId
data["create_time"] = time.Now().Unix()
id := this.Db.Insert("org_analyse", data)
data["id"] = id
} else {
this.Db.Update("org_analyse", data, Map{"org_id": orgId})
}
this.Display(0, data)
},
"analyse": func(this *Context) {
companys := this.Db.Select("company", "*")
for _, v := range companys {
//上传数据
v["upload_data"] = v.GetMap("upload_data")
v["collect_data"] = v.GetMap("collect_data")
v["analyse"] = v.GetMap("analyse")
}
tagCompany := this.Db.Select("tag", "*", Map{"type": 2, "ORDER": "`sort` ASC"})
//cs := append(NewCompanys{}, companys...)
for _, tag := range tagCompany {
analyse(companys, tag)
//fmt.Println(data)
}
for _, c := range companys {
this.Db.Update("company", Map{"analyse": c.GetString("analyse")}, Map{"id": c.GetCeilInt("id")})
fmt.Println(c["analyse"])
}
this.Display(0, len(companys))
},
"jstest": func(this *Context) {
vm := otto.New()
vm.Run(`
console.log("845458454" ); // 4
abc = 2 + 2;
console.log("The value of abc is " + abc); // 4
`)
//if value, err := vm.Get("abc"); err == nil {
// if value_int, err := value.ToInteger(); err == nil {
// fmt.Printf("", value_int, err)
// }
//}
vm.Set("def", 11)
vm.Run(`
console.log("The value of def is " + def);
// The value of def is 11
`)
vm.Set("xyzzy", "Nothing happens.")
vm.Run(`
console.log(xyzzy.length); // 16
`)
value, _ := vm.Run("xyzzy.length")
//{
// // value is an int64 with a value of 16
// value, _ := value.ToInteger()
//}
fmt.Println(value.ToInteger())
_, err := vm.Run("abcdefghijlmnopqrstuvwxyz.length")
if err != nil {
// err = ReferenceError: abcdefghijlmnopqrstuvwxyz is not defined
// If there is an error, then value.IsUndefined() is true
//...
fmt.Println(err)
}
vm.Set("sayHello", func(call otto.FunctionCall) otto.Value {
fmt.Printf("Hello, %s.\n", call.Argument(0).String())
return otto.Value{}
})
result, _ := vm.Run(`
sayHello("Xyzzy"); // Hello, Xyzzy.
sayHello(); // Hello, undefined
result = twoPlus(2.0); // 4
`)
fmt.Println(result.String())
},
"tt": func(this *Context) {
tag_ctg := this.Db.Get("tag_ctg", "id", Map{"name": "企业数据分析"})
if tag_ctg == nil {
tag_ctg = Map{}
tag_ctg["id"] = this.Db.Insert("tag_ctg", Map{"name": "企业数据分析",
"admin_id": this.Session("admin_id").ToInt(),
"create_time": time.Now().Unix(),
"modify_time": time.Now().Unix()})
}
id := tag_ctg.GetCeilInt("id")
if id == 0 {
this.Display(3, "分类数据上传失败")
return
}
num := 0
for k, v := range ADataType {
tag := this.Db.Get("tag", "id", Map{"sn": k})
if tag == nil {
tag = Map{"sn": k, "name": v, "tag_ctg_id": id, "create_time": time.Now().Unix(),
"modify_time": time.Now().Unix()}
if strings.Index(k, "DF") == len(k)-2 {
tag["unit"] = "分"
}
tagId := this.Db.Insert("tag", tag)
if tagId != 0 {
num++
}
}
}
this.Display(0, num)
},
"api": func(this *Context) {
//curl -i --get --include 'http://api.81api.com/getCompanyBaseInfo/小米科技有限责任公司/' -H 'Authorization:APPCODE 你自己的AppCode'
//aliyun.DefaultCompany.Init("06c6a07e89dd45c88de040ee1489eef7")
//data,err:=aliyun.DefaultCompany.GetCompanyBaseInfo("小米科技有限责任公司")
//fmt.Println(data)
//if err!=nil{
// this.Display(4,err)
// return
//}
data1, _ := baidu.DefaultBaiDuMap.GetPosition("简阳市东溪镇奎星路28号", "")
this.Display(0, ObjToMap(data1))
},
"temp": func(this *Context) {
count := 0
for true {
data := this.Db.Get("area_copy", "id,parent_id,`index`,parent_code", Map{"index": nil, "ORDER": "`level` ASC"})
if data == nil {
break
}
count++
//if data.GetCeilInt("parent_id")==0{
// data["parent_id"]=3750
//}
parentData := this.Db.Get("area_copy", "id,parent_id,`index`", Map{"area_code": data.GetString("parent_code")})
if parentData == nil {
this.Db.Update("area_copy", Map{"index": data.GetString("id") + ","}, Map{"id": data.GetString("id")})
} else {
this.Db.Update("area_copy", Map{"index": parentData.GetString("index") + data.GetString("id") + ",", "parent_id": parentData["id"]}, Map{"id": data.GetString("id")})
}
}
this.Display(0, count)
},
"tempctg": func(this *Context) {
dataBytes, e := ioutil.ReadFile("2017.json")
if e != nil {
this.Display(4, e)
return
}
data := ObjToSlice(string(dataBytes))
for k, _ := range data {
ins := data.GetMap(k)
ins["level"] = 1
id := this.Db.Insert("category", Map{"name": ins.GetString("name"),
"code": ins.GetString("code"),
"level": ins["level"],
"parent_id": 1,
"admin_id": this.Session("admin_id").ToInt(),
"create_time": time.Now().Unix(),
"modify_time": time.Now().Unix(),
})
if id != 0 {
ins["id"] = id
}
ins["index"] = ",1," + ObjToStr(id) + ","
this.Db.Update("category", Map{"index": ins["index"]}, Map{"id": ins["id"]})
run(ins, this)
}
},
}
func excel1() Slice {
xlsx, err := excelize.OpenFile("tpt/piduqu.xlsx")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
list := xlsx.GetSheetList()
data := Slice{}
for _, v := range list {
rows, e := xlsx.GetRows(v)
fmt.Println(rows, e)
data = append(data, rows)
//for k1,v1:=range rows{
//
//}
}
return data
}
func run(ins Map, this *Context) {
cs := ins.GetSlice("children")
if cs == nil {
return
}
for ck, _ := range cs {
cins := cs.GetMap(ck)
cins["level"] = ins.GetCeilInt("level") + 1
id := this.Db.Insert("category", Map{"name": cins.GetString("name"),
"code": cins.GetString("code"),
"level": cins["level"],
"parent_id": ins["id"],
"admin_id": this.Session("admin_id").ToInt(),
"create_time": time.Now().Unix(),
"modify_time": time.Now().Unix(),
})
if id != 0 {
cins["id"] = id
}
cins["index"] = ins.GetString("index") + ObjToStr(id) + ","
this.Db.Update("category", Map{"index": cins["index"]}, Map{"id": cins["id"]})
run(cins, this)
}
}