From a95b2fccd74111af1ed8b4a6c7386cc7a0a76c0b Mon Sep 17 00:00:00 2001 From: hoteas Date: Mon, 17 Jan 2022 04:47:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=A0=94=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code.go | 20 ++- code/makecode.go | 30 +++- dri/aliyun/company.go | 58 +++++++ dri/baidu/map.go | 56 ++++++ example/admin/companyinout.go | 315 ++++++++++++++++++++++++++++++++++ example/config/config.json | 2 +- example/main.go | 142 +++++++++++++++ 7 files changed, 605 insertions(+), 18 deletions(-) create mode 100644 dri/aliyun/company.go create mode 100644 dri/baidu/map.go create mode 100644 example/admin/companyinout.go diff --git a/code.go b/code.go index 0cb2cd8..8157679 100644 --- a/code.go +++ b/code.go @@ -75,16 +75,20 @@ var TptProject = Proj{ } //索引管理,便于检索以及权限 - if inData.Get("parent_id") != nil && inData.GetString("index") != "" { - Index := that.Db.Get(that.RouterString[1], "`index`", Map{"id": that.RouterString[2]}) - parentIndex := that.Db.Get(that.RouterString[1], "`index`", Map{"id": inData.Get("parent_id")}) - inData["index"] = parentIndex.GetString("index") + that.RouterString[2] + "," + if inData.GetString("index") != "" { + if inData.Get("parent_id") != nil { + Index := that.Db.Get(that.RouterString[1], "`index`", Map{"id": that.RouterString[2]}) + parentIndex := that.Db.Get(that.RouterString[1], "`index`", Map{"id": inData.Get("parent_id")}) + inData["index"] = parentIndex.GetString("index") + that.RouterString[2] + "," - childNodes := that.Db.Select(that.RouterString[1], "id,`index``", Map{"index[~]": "," + that.RouterString[2] + ","}) + childNodes := that.Db.Select(that.RouterString[1], "id,`index``", Map{"index[~]": "," + that.RouterString[2] + ","}) - for _, v := range childNodes { - v["index"] = strings.Replace(v.GetString("index"), Index.GetString("index"), inData.GetString("index"), -1) - that.Db.Update(that.RouterString[1], Map{"index": v["index"]}, Map{"id": v.GetCeilInt("id")}) + for _, v := range childNodes { + v["index"] = strings.Replace(v.GetString("index"), Index.GetString("index"), inData.GetString("index"), -1) + that.Db.Update(that.RouterString[1], Map{"index": v["index"]}, Map{"id": v.GetCeilInt("id")}) + } + } else { + delete(inData, "index") } } diff --git a/code/makecode.go b/code/makecode.go index 71ca91b..3e2f302 100644 --- a/code/makecode.go +++ b/code/makecode.go @@ -171,8 +171,8 @@ func (that *MakeCode) Db2JSON(name string, path string, db *db.HoTimeDB, makeCod } idSlice = append(idSlice, tableInfo) - for _, info := range tableInfo { - + for _, info1 := range tableInfo { + info := DeepCopyMap(info1).(Map) if info.GetString("label") == "" { info["label"] = info.GetString("name") } @@ -193,11 +193,17 @@ func (that *MakeCode) Db2JSON(name string, path string, db *db.HoTimeDB, makeCod //"add": false, "info": false, "edit": false, "list": true, //"must": false, } + //备注以空格隔开,空格后的是其他备注 - indexNum := strings.Index(info.GetString("label"), ":") - if indexNum > 0 { + indexNum := strings.Index(info.GetString("label"), " ") + if indexNum > -1 { coloum["label"] = info.GetString("label")[:indexNum] } + //去除数据信息,是用:号分割的 + indexNum = strings.Index(coloum.GetString("label"), ":") + if indexNum > -1 { + coloum["label"] = coloum.GetString("label")[:indexNum] + } for _, ColumnName := range ColumnNameType { if (ColumnName.Strict && coloum.GetString("name") == ColumnName.Name) || @@ -311,7 +317,6 @@ func (that *MakeCode) Db2JSON(name string, path string, db *db.HoTimeDB, makeCod //生成id,判断数据库是否有改变,以保证数据库和配置文件匹配唯一 id := Md5(ObjToStr(idSlice)) - if id == that.Config.GetString("id") { if isMake { //有生成包文件 fmt.Println("有新的业务代码生成,请重新运行") @@ -326,7 +331,7 @@ func (that *MakeCode) Db2JSON(name string, path string, db *db.HoTimeDB, makeCod isMenusGet := false //判断是否被目录收录 for indexKey, _ := range that.IndexMenus { indexCode := strings.Index(indexKey, fk) - if indexCode == 0 { + if indexCode == 0 || indexCode == 4 { isMenusGet = false continue } @@ -352,7 +357,7 @@ func (that *MakeCode) Db2JSON(name string, path string, db *db.HoTimeDB, makeCod //if tablePrefixCode != -1 { for ck, _ := range that.TableColumns { //判断不止一个前缀相同 - if strings.Index(ck, prefixName) == 0 && ck != fk { + if (strings.Index(ck, prefixName) == 0 || strings.Index(prefixName, ck) == 4) && ck != fk { isNewPrefix = true break } @@ -372,8 +377,15 @@ func (that *MakeCode) Db2JSON(name string, path string, db *db.HoTimeDB, makeCod } //没有新前缀 if that.IndexMenus[prefixName] != nil { - that.IndexMenus.GetMap(prefixName)["menus"] = append(that.IndexMenus.GetMap(prefixName).GetSlice("menus"), menuIns) - that.IndexMenus[prefixName+"/"+fk] = menuIns + if that.IndexMenus[prefixName+"/"+fk] == nil { + that.IndexMenus.GetMap(prefixName)["menus"] = append(that.IndexMenus.GetMap(prefixName).GetSlice("menus"), menuIns) + that.IndexMenus[prefixName+"/"+fk] = menuIns + } else { + for k, v := range menuIns { + that.IndexMenus.GetMap(prefixName + "/" + fk)[k] = v + } + } + } else { that.Config["menus"] = append(that.Config.GetSlice("menus"), mMenu) //注入配置 diff --git a/dri/aliyun/company.go b/dri/aliyun/company.go new file mode 100644 index 0000000..f9123f2 --- /dev/null +++ b/dri/aliyun/company.go @@ -0,0 +1,58 @@ +package aliyun + +import ( + "fmt" + "io/ioutil" + "net/http" + //"fmt" +) + +type Company struct { + ApiCode string + Url string +} + +func (this *Company) Init(apiCode string) { + //"06c6a07e89dd45c88de040ee1489eef7" + this.ApiCode = apiCode + this.Url = "https://api.81api.com" +} + +// GetCompanyBaseInfo 获取企业基础信息 +func (this *Company) GetCompanyBaseInfo(name string) (string, error) { + url := "/getCompanyBaseInfo/" + + client := &http.Client{} + + reqest, err := http.NewRequest("GET", this.Url+url+name+"/?isRaiseErrorCode=1", nil) + + if err != nil { + fmt.Println("Fatal error ", err.Error()) + return "", err + } + + reqest.Header.Add("Authorization", "APPCODE "+this.ApiCode) + response, err := client.Do(reqest) + defer response.Body.Close() + + if err != nil { + fmt.Println("Fatal error ", err.Error()) + return "", err + } + + body, err := ioutil.ReadAll(response.Body) + if err != nil { + return "", err + } + + fmt.Println(string(body)) + + return string(body), err + +} + +var DefaultCompany Company + +func init() { + DefaultCompany = Company{} +} diff --git a/dri/baidu/map.go b/dri/baidu/map.go new file mode 100644 index 0000000..cdd682b --- /dev/null +++ b/dri/baidu/map.go @@ -0,0 +1,56 @@ +package baidu + +import ( + "fmt" + "io/ioutil" + "net/http" + "net/url" +) + +type BaiduMap struct { + Ak string + Url string +} + +func (this *BaiduMap) Init(Ak string) { + //"ak=ZeT902EZvVgIoGVWEFK3osUm" + this.Ak = Ak + this.Url = "https://api.map.baidu.com/place/v2/suggestion?output=json®ion=" + url.PathEscape("全国") + "&ak=" + Ak + //query +} + +// GetPosition 获取定位列表 +func (this *BaiduMap) GetPosition(name string) (string, error) { + + client := &http.Client{} + + reqest, err := http.NewRequest("GET", this.Url+"&query="+url.PathEscape(name), nil) + + if err != nil { + fmt.Println("Fatal error ", err.Error()) + return "", err + } + response, err := client.Do(reqest) + defer response.Body.Close() + + if err != nil { + fmt.Println("Fatal error ", err.Error()) + return "", err + } + + body, err := ioutil.ReadAll(response.Body) + if err != nil { + return "", err + } + + //fmt.Println(string(body)) + + return string(body), err + +} + +var DefaultBaiDuMap BaiduMap + +func init() { + DefaultBaiDuMap = BaiduMap{} +} diff --git a/example/admin/companyinout.go b/example/admin/companyinout.go new file mode 100644 index 0000000..227d6bd --- /dev/null +++ b/example/admin/companyinout.go @@ -0,0 +1,315 @@ +package admin + +import ( + . "../../../hotime" + . "../../common" + "../../dri/baidu" + "errors" + "fmt" + "github.com/chain-zhang/pinyin" + "github.com/xuri/excelize" + "io" + "os" + "strings" + "time" +) + +var CompanyInOutCtr = Ctr{ + + "upload": func(this *Context) { + + //读取网络文件 + fi, fheader, err := this.Req.FormFile("file") + if err != nil { + this.Display(3, err) + return + + } + filePath := this.Config.GetString("filePath") + if filePath == "" { + filePath = "excel/2006/01/02/" + } + + path := time.Now().Format(filePath) + e := os.MkdirAll(this.Config.GetString("tpt")+"/"+path, os.ModeDir) + + if e != nil { + this.Display(3, e) + return + } + + filePath = path + Md5(ObjToStr(RandX(100000, 9999999))) + fheader.Filename[strings.LastIndex(fheader.Filename, "."):] + newFile, e := os.Create(this.Config.GetString("tpt") + "/" + filePath) + + if e != nil { + this.Display(3, e) + return + } + + _, e = io.Copy(newFile, fi) + + if e != nil { + this.Display(3, e) + return + } + + //this.Display(0, filePath) + + //读取excel + data := excel(this.Config.GetString("tpt") + "/" + filePath) + if len(data) != 1 { + this.Display(3, "表格不标准,请重新提交") + return + } + + err = decodeData2Sql(data[0], this) + if err != nil { + this.Display(4, err) + return + } + + this.Display(0, "上传成功") + + }, +} + +func decodeData2Sql(table [][]string, this *Context) error { + tags := []Map{} //报错所有的tag记录 + tagCtgs := []Map{} + for k, v := range table { + + //第一排是指标分类,去除掉第一个参数,其存在合并所以,如果是合并的则删除 + if k == 0 { + + for ck, cv := range v { + cv = strings.Replace(cv, " ", "", -1) + cv = strings.Replace(cv, "\r\t", "", -1) + cv = strings.Replace(cv, "\r", "", -1) + v[ck] = cv + + if ck == 0 { + continue + } + if cv == "" { + v[ck] = v[ck-1] + cv = v[ck] + } + + tagCtg := this.Db.Get("tag_ctg", "*", Map{"name": cv}) + if tagCtg != nil { + tagCtgs = append(tagCtgs, tagCtg) + continue + } + tagCtg = Map{ + "name": cv, + "admin_id": this.Session("admin_id").ToInt(), + "create_time": time.Now().Unix(), + "modify_time": time.Now().Unix(), + } + id := this.Db.Insert("tag_ctg", tagCtg) + + if id != 0 { + tagCtg["id"] = id + this.Db.Update("tag_ctg", Map{ + "index": "," + ObjToStr(id) + ",", + }, Map{"id": id}) + + tagCtgs = append(tagCtgs, tagCtg) + } + + } + + } + + //第二排是指标项,去除掉第一个参数, + //最重要的是参数tag生成,通过中文转拼音获取首字母实现,遇到同名的则最后加一位数字 + if k == 1 { + + for ck, cv := range v { + cv = strings.Replace(cv, " ", "", -1) + cv = strings.Replace(cv, "\r\t", "", -1) + cv = strings.Replace(cv, "\r", "", -1) + v[ck] = cv + if ck == 0 { + continue + } + //补充分类 + if ck >= len(table[0]) { + tagCtgs = append(tagCtgs, tagCtgs[len(tagCtgs)-1]) + } + //补充说明 + if ck >= len(table[3]) { + table[3] = append(table[3], table[3][len(table[3])-1]) + } + //补充单位 + if ck >= len(table[2]) { + table[2] = append(table[2], "") + } + + tag := this.Db.Get("tag", "*", Map{"name": cv}) + + if tag != nil { + tags = append(tags, tag) + continue + } + sn, err := pinyin.New(cv).Split(" ").Mode(pinyin.InitialsInCapitals).Convert() + if err != nil { + fmt.Println(err) + sn = cv + } else { + + sns := strings.Split(sn, " ") + + sn = "IEDC" + for _, v1 := range sns { + if v1 != "" { + sn = sn + string([]rune(v1)[:1]) + fmt.Println(sn, v1) + } + } + } + //sn=strings.Replace(sn,"\ufffd","",-1) + + tagsn := this.Db.Get("tag", "id", Map{"sn": sn}) + if tagsn != nil { + for i := 0; i < 100; i++ { + tagsn = this.Db.Get("tag", "id", Map{"sn": sn + ObjToStr(i)}) + if tagsn == nil { + sn = sn + ObjToStr(i) + break + } + } + } + + tag = Map{ + "name": cv, + "description": table[3][ck], //第三行对应位置为描述 + "unit": table[2][ck], + "admin_id": this.Session("admin_id").ToInt(), + "type": 1, + "sn": sn, + "tag_ctg_id": tagCtgs[ck-1].GetCeilInt("id"), + "create_time": time.Now().Unix(), + "modify_time": time.Now().Unix(), + } + id := this.Db.Insert("tag", tag) + tag["id"] = id + tags = append(tags, tag) + } + + } + + //第四排以后是数据项,去除掉第一个参数, + //根据统一社会信用代码识别是否存在,同时将校验部分数字 + if k > 4 { + + //整行数据 + rowData := Map{} + for ck, cv := range v { + if ck == 0 { + continue + } + cv = strings.Replace(cv, " ", "", -1) + cv = strings.Replace(cv, "\r\t", "", -1) + cv = strings.Replace(cv, "\r", "", -1) + v[ck] = cv + if cv == "" { + return errors.New("第" + ObjToStr(k+1) + "行," + ObjToStr(ck+1) + "列,数据为空请注意检查") + } + + rowData[tags[ck-1].GetString("sn")] = cv + } + + companyData := Map{ + "name": rowData["IEDCQYMC"], + "sn": rowData["IEDCTYSHXYDM"], + "address": rowData["IEDCQYDZ"], + "unit": rowData["IEDCSDMC"], + "zdmj": rowData["IEDCYDMJ"], + "yysr": rowData["IEDC2NYYSR"], + "lrze": rowData["IEDC2NLRZE"], + "yjsj": rowData["IEDCYNSJ"], + "yfjf": rowData["IEDCNYFFY"], + "zgrs": rowData["IEDCSBRS"], + "zywrwpfdl": rowData["IEDCZYWRWPFDL"], + "zhnh": rowData["IEDCZHNH"], + "admin_id": this.Session("admin_id").ToInt(), + "upload_data": rowData.ToJsonString(), + //"create_time": time.Now().Unix(), + "modify_time": time.Now().Unix(), + //"lat": lat, + //"lng": lng, + } + + baiduDataStr, _ := baidu.DefaultBaiDuMap.GetPosition(companyData.GetString("address")) + baiduData := ObjToMap(baiduDataStr) + + city := Map{} + if baiduData != nil && baiduData.GetCeilInt("status") == 0 { + address := baiduData.GetSlice("result") + if len(address) != 0 { + companyData["lat"] = address.GetMap(0).GetMap("location").GetFloat64("lat") + companyData["lng"] = address.GetMap(0).GetMap("location").GetFloat64("lng") + + city = this.Db.Get("city", "id", Map{"name[~]": address.GetMap(0).GetString("district"), "ORDER": "`level` DESC"}) + if city == nil { + city = this.Db.Get("city", "id", Map{"name[~]": address.GetMap(0).GetString("city"), "ORDER": "`level` DESC"}) + } + } + } + + if city != nil { + companyData["city_id"] = city.GetCeilInt("id") + } + + //行业ID + category := this.Db.Get("category", "id", Map{"code": rowData["IEDCSSXYXLDM"]}) + if category != nil { + companyData["category_id"] = category.GetCeilInt("id") + } + + //此处需要优化 + companyData["org_id"] = 3 + + company := this.Db.Get("company", "id", Map{"sn": companyData["sn"]}) + //没有则创建 + if company == nil { + //更新两张表 + companyData["create_time"] = time.Now().Unix() + id := this.Db.Insert("company", companyData) + companyData["company_id"] = id + } else { + //有则更新 + this.Db.Update("company", companyData, Map{"sn": companyData["sn"]}) + companyData["company_id"] = company.GetCeilInt("id") + companyData["create_time"] = time.Now().Unix() + } + + this.Db.Insert("company_history", companyData) + + } + + } + return nil + +} + +func excel(filePath string) [][][]string { + xlsx, err := excelize.OpenFile(filePath) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + list := xlsx.GetSheetList() + var data [][][]string + for _, v := range list { + rows, e := xlsx.GetRows(v) + if e != nil { + fmt.Println(e) + continue + } + data = append(data, rows) + } + + return data +} diff --git a/example/config/config.json b/example/config/config.json index 55396a3..37cf56e 100644 --- a/example/config/config.json +++ b/example/config/config.json @@ -24,7 +24,7 @@ "db": { "mysql": { "host": "192.168.6.253", - "name": "myhs", + "name": "iedc_dev", "password": "dasda8454456", "port": "3306", "prefix": "", diff --git a/example/main.go b/example/main.go index 63de6f0..996a77d 100644 --- a/example/main.go +++ b/example/main.go @@ -2,15 +2,25 @@ package main import ( "../../hotime" + "../common" + "io/ioutil" + + //"../dri/aliyun" + "../dri/baidu" "../dri/ddsms" + "./admin" "./app" "fmt" + "github.com/xuri/excelize" + "os" "time" ) func main() { date, _ := time.Parse("2006-01-02 15:04", time.Now().Format("2006-01-02")+" 14:00") fmt.Println(date, date.Unix()) + + baidu.DefaultBaiDuMap.Init("ZeT902EZvVgIoGVWEFK3osUm") //fmt.Println("0123456"[1:7]) appIns := hotime.Init("config/config.json") //RESTfull接口适配 @@ -18,6 +28,86 @@ func main() { //支撑权限设置 return true }) + appIns.Router["admin"]["company_inout"] = admin.CompanyInOutCtr + appIns.Router["admin"]["test"] = hotime.Ctr{ + + "test": func(this *hotime.Context) { + data := excel() + this.Display(0, data) + }, + + "api": func(this *hotime.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, common.ObjToMap(data1)) + + }, + "temp": func(this *hotime.Context) { + count := 0 + for true { + + data := this.Db.Get("area_copy", "id,parent_id,`index`,parent_code", common.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`", common.Map{"area_code": data.GetString("parent_code")}) + if parentData == nil { + this.Db.Update("area_copy", common.Map{"index": data.GetString("id") + ","}, common.Map{"id": data.GetString("id")}) + } else { + this.Db.Update("area_copy", common.Map{"index": parentData.GetString("index") + data.GetString("id") + ",", "parent_id": parentData["id"]}, common.Map{"id": data.GetString("id")}) + } + + } + this.Display(0, count) + + }, + "tempctg": func(this *hotime.Context) { + dataBytes, e := ioutil.ReadFile("2017.json") + if e != nil { + this.Display(4, e) + return + } + data := common.ObjToSlice(string(dataBytes)) + + for k, _ := range data { + ins := data.GetMap(k) + ins["level"] = 1 + id := this.Db.Insert("category", common.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," + common.ObjToStr(id) + "," + this.Db.Update("category", common.Map{"index": ins["index"]}, common.Map{"id": ins["id"]}) + + run(ins, this) + + } + + }, + } //makeCode := code.MakeCode{} //fmt.Println(common.ObjToStr(makeCode.Db2JSON("admin","test",appIns.Db))) @@ -29,3 +119,55 @@ func main() { "app": app.Project, }) } + +func excel() common.Slice { + xlsx, err := excelize.OpenFile("tpt/piduqu.xlsx") + if err != nil { + fmt.Println(err) + os.Exit(1) + } + list := xlsx.GetSheetList() + data := common.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 common.Map, this *hotime.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", common.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") + common.ObjToStr(id) + "," + this.Db.Update("category", common.Map{"index": cins["index"]}, common.Map{"id": cins["id"]}) + + run(cins, this) + + } +}