iedc-go/example/admin/companyinout.go

536 lines
15 KiB
Go
Raw Normal View History

2022-01-16 20:47:39 +00:00
package admin
import (
2022-09-27 01:22:49 +00:00
. "../../../iedc-go"
2022-01-16 20:47:39 +00:00
. "../../common"
"fmt"
"github.com/chain-zhang/pinyin"
"github.com/xuri/excelize"
"io"
"os"
"strings"
"time"
2022-02-23 22:26:36 +00:00
"unicode"
2022-01-16 20:47:39 +00:00
)
var CompanyInOutCtr = Ctr{
"upload": func(this *Context) {
2022-02-23 22:26:36 +00:00
orgId := ObjToInt(this.Req.FormValue("org_id"))
if orgId == 0 {
this.Display(3, "参数错误")
return
}
//orgId := 1
2022-01-16 20:47:39 +00:00
//读取网络文件
fi, fheader, err := this.Req.FormFile("file")
if err != nil {
2022-07-12 03:45:41 +00:00
this.Display(3, err.Error())
2022-01-16 20:47:39 +00:00
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 {
2022-07-12 03:45:41 +00:00
this.Display(3, e.Error())
2022-01-16 20:47:39 +00:00
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 {
2022-07-12 03:45:41 +00:00
this.Display(3, e.Error())
2022-01-16 20:47:39 +00:00
return
}
_, e = io.Copy(newFile, fi)
if e != nil {
2022-07-12 03:45:41 +00:00
this.Display(3, e.Error())
2022-01-16 20:47:39 +00:00
return
}
//this.Display(0, filePath)
//读取excel
2022-02-23 22:26:36 +00:00
//fmt.Println(Org[orgId],OrgId)
2022-01-16 20:47:39 +00:00
data := excel(this.Config.GetString("tpt") + "/" + filePath)
if len(data) != 1 {
this.Display(3, "表格不标准,请重新提交")
return
}
2022-01-26 03:45:17 +00:00
err = decodeData2Sql(data[0], this, orgId)
2022-01-16 20:47:39 +00:00
if err != nil {
2022-07-12 03:45:41 +00:00
this.Display(4, err.Error())
2022-01-16 20:47:39 +00:00
return
}
this.Display(0, "上传成功")
},
2022-07-12 03:45:41 +00:00
//"analyse": func(this *Context) {
// orgId := ObjToInt(this.Req.FormValue("org_id"))
// if orgId == 0 {
// this.Display(3, "参数错误")
// return
// }
// companys := this.Db.Select("company", "*", Map{"org_id": orgId})
//
// for _, v := range companys {
//
// data := Org[orgId].analyse(v.GetMap("upload_data"))
// for k, _ := range data {
// data[k] = data.GetFloat64(k)
// }
// v["analyse"] = data
// }
//
// cs := Org[orgId].analyseSort(companys)
// for _, v := range cs {
// as := v.GetMap("analyse")
// //v["analyse"] = as.ToJsonString()
// switch as.GetString("PJ") {
// case "A":
// v["level"] = 0
// case "B":
// v["level"] = 1
// case "C":
// v["level"] = 2
// case "D":
// v["level"] = 3
// }
// v["score"] = as.GetFloat64("ZDF")
//
// this.Db.Update("company", Map{"analyse": as.ToJsonString(), "level": v["level"], "score": v["score"]}, Map{"id": v.GetCeilInt("id")})
// this.Db.Update("company_history", Map{"analyse": as.ToJsonString(), "level": v["level"], "score": v["score"]}, Map{"id": v.GetCeilInt("company_history_id")})
// }
//
// this.Display(0, len(companys))
//
//},
2022-02-16 20:10:53 +00:00
2022-07-19 05:42:59 +00:00
"total_analyse": func(this *Context) {
orgId := ObjToInt(this.Req.FormValue("org_id"))
if orgId == 0 {
this.Display(3, "参数错误")
return
}
companys := this.Db.Select("company", "*", Map{"AND": Map{"org_id": orgId}})
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)
data := Map{
"name": org.GetString("name"),
"home_data": home_data.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"] = time.Now().Format("2006-01-02 15:04") + "创建"
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)
},
2022-02-16 20:10:53 +00:00
//
//"category_analyse": func(this *Context) {
//
// orgId := 1
// org := this.Db.Get("org", "*", Map{"id": orgId})
// categoryIDS := []int{
// 1369, //电子行业
// 799, //航空航天
// 481, //生物医药
// 990, //绿色能源
// 972, //机械制造
// 1021, //建材制造
// 717, //化工类
// 170, //轻工类
// 260, //纺织类
// 1088, //商贸类
// 1424, //投资管理
// 903, //配套类
// 959, //其他
// }
// totalAnalyse := this.Db.Get("org_analyse", "*", Map{"org_id": orgId})
//
// for _, id := range categoryIDS {
//
// companys := this.Db.Select("company", Map{"[><]category": "company.category_id=category.id"},
// "company.id,company.gs,company.level,company.score,company.upload_data,company.analyse,company.category_id", Map{"category.index[~]": "," + ObjToStr(id) + ","})
// ctgAn := ctgAnalyse(companys, totalAnalyse)
// data := Map{
// "name": org.GetString("name"),
// "modify_time": time.Now().Unix(),
// "data": ctgAn.ToJsonString(),
// }
// ctgAnalyse := this.Db.Get("category_analyse", "id", Map{"AND": Map{"org_id": orgId, "category_id": id}})
// if ctgAnalyse == nil {
// data["name"] = time.Now().Format("2006-01-02 15:04") + "创建"
// data["name"] = org.GetString("name")
// data["org_id"] = orgId
// data["category_id"] = id
// data["create_time"] = time.Now().Unix()
// id := this.Db.Insert("category_analyse", data)
// data["id"] = id
// } else {
// this.Db.Update("category_analyse", data, Map{"AND": Map{"org_id": orgId, "category_id": id}})
// }
//
// }
// this.Display(0, categoryIDS)
//},
//
2022-02-23 22:26:36 +00:00
"industry_analyse": func(this *Context) {
orgId := ObjToInt(this.Req.FormValue("org_id"))
if orgId == 0 {
this.Display(3, "参数错误")
return
2022-02-16 20:10:53 +00:00
}
2022-02-23 22:26:36 +00:00
industrys := this.Db.Select("industry_analyse", "id,name,industry_id", Map{"org_id": orgId})
totalAnalyse := this.Db.Get("org_analyse", "*", Map{"org_id": orgId})
2022-02-16 20:10:53 +00:00
2022-02-23 22:26:36 +00:00
for _, industry := range industrys {
id := industry.GetCeilInt("industry_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{"AND": Map{"industry.id": id, "company.org_id": orgId}})
ctgAn := ctgAnalyse(companys, totalAnalyse)
data := Map{
"modify_time": time.Now().Unix(),
"data": ctgAn.ToJsonString(),
2022-02-16 20:10:53 +00:00
}
2022-02-23 22:26:36 +00:00
this.Db.Update("industry_analyse", data, Map{"AND": Map{"org_id": orgId, "industry_id": id}})
2022-02-16 20:10:53 +00:00
}
2022-02-23 22:26:36 +00:00
this.Display(0, industrys)
},
2022-01-16 20:47:39 +00:00
}
2022-01-26 03:45:17 +00:00
func decodeData2Sql(table [][]string, this *Context, orgId int) error {
2022-01-16 20:47:39 +00:00
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]
}
2022-02-23 22:26:36 +00:00
tagCtg := this.Db.Get("tag_ctg", "*", Map{"AND": Map{"name": cv, "org_id": orgId}})
2022-01-16 20:47:39 +00:00
if tagCtg != nil {
tagCtgs = append(tagCtgs, tagCtg)
continue
}
tagCtg = Map{
"name": cv,
"admin_id": this.Session("admin_id").ToInt(),
2022-02-14 11:10:58 +00:00
"parent_id": 1, //上传分类为1
2022-02-23 22:26:36 +00:00
"org_id": orgId,
2022-01-16 20:47:39 +00:00
"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{
2022-02-14 11:10:58 +00:00
"index": ",1," + ObjToStr(id) + ",",
2022-01-16 20:47:39 +00:00
}, 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], "")
}
2022-02-23 22:26:36 +00:00
tag := this.Db.Get("tag", "*", Map{"AND": Map{"name": cv, "type": "0", "org_id": orgId}})
2022-01-16 20:47:39 +00:00
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 {
2022-02-14 11:10:58 +00:00
if v1 != "" && !unicode.IsPunct([]rune(v1)[0]) {
2022-01-16 20:47:39 +00:00
sn = sn + string([]rune(v1)[:1])
fmt.Println(sn, v1)
}
}
}
//sn=strings.Replace(sn,"\ufffd","",-1)
2022-02-23 22:26:36 +00:00
tagsn := this.Db.Get("tag", "id", Map{"AND": Map{"sn": sn, "type": "0", "org_id": orgId}})
2022-01-16 20:47:39 +00:00
if tagsn != nil {
for i := 0; i < 100; i++ {
2022-02-23 22:26:36 +00:00
tagsn = this.Db.Get("tag", "id", Map{"AND": Map{"sn": sn + ObjToStr(i), "type": "0", "org_id": orgId}})
2022-01-16 20:47:39 +00:00
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(),
2022-02-23 22:26:36 +00:00
"org_id": orgId,
2022-02-14 11:10:58 +00:00
"type": 0,
2022-01-16 20:47:39 +00:00
"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)
}
}
//第四排以后是数据项,去除掉第一个参数,
//根据统一社会信用代码识别是否存在,同时将校验部分数字
2022-01-19 23:15:14 +00:00
if k > 3 {
2022-01-16 20:47:39 +00:00
//整行数据
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)
2022-02-14 11:10:58 +00:00
2022-01-16 20:47:39 +00:00
if cv == "" {
2022-02-14 11:10:58 +00:00
cv = "暂无"
//return errors.New("第" + ObjToStr(k+1) + "行," + ObjToStr(ck+1) + "列,数据为空请注意检查")
2022-01-16 20:47:39 +00:00
}
2022-02-14 11:10:58 +00:00
v[ck] = cv
2022-02-16 20:10:53 +00:00
if ck-1 >= len(tags) {
fmt.Println(cv, "表格错误")
continue
}
2022-01-16 20:47:39 +00:00
rowData[tags[ck-1].GetString("sn")] = cv
}
2022-02-23 22:26:36 +00:00
if rowData["IEDCQYMC"] == nil {
fmt.Println("数据错误")
}
2022-01-16 20:47:39 +00:00
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(),
2022-01-26 03:45:17 +00:00
"org_id": orgId,
2022-01-16 20:47:39 +00:00
//"create_time": time.Now().Unix(),
"modify_time": time.Now().Unix(),
//"lat": lat,
//"lng": lng,
}
2022-01-26 03:45:17 +00:00
//行业ID
2022-02-14 11:10:58 +00:00
//org := this.Db.Get("org", "id,name", Map{"id": orgId})
//regin := "全国"
//if org != nil {
// regin = org.GetString("name")
//}
//baiduDataStr, _ := baidu.DefaultBaiDuMap.GetPosition(companyData.GetString("address"), regin)
//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")
//}
2022-01-16 20:47:39 +00:00
//行业ID
category := this.Db.Get("category", "id", Map{"code": rowData["IEDCSSXYXLDM"]})
if category != nil {
companyData["category_id"] = category.GetCeilInt("id")
}
2022-01-26 03:45:17 +00:00
////此处需要优化
//companyData["org_id"] = 3
//产业ID
industry := this.Db.Get("industry", "id", Map{"name": rowData["IEDCCYLB"]})
if industry == nil {
industry = Map{
"admin_id": this.Session("admin_id").ToInt(),
"create_time": time.Now().Unix(),
"modify_time": time.Now().Unix(),
"name": rowData["IEDCCYLB"],
}
id := this.Db.Insert("industry", industry)
industry["id"] = id
}
if industry.GetCeilInt("id") != 0 {
//产业ID
industryAnalyse := this.Db.Get("industry_analyse", "id", Map{"AND": Map{"industry_id": industry.GetCeilInt("id"), "org_id": orgId}})
if industryAnalyse == nil {
industryAnalyse = Map{
"name": rowData["IEDCCYLB"],
"create_time": time.Now().Unix(),
"modify_time": time.Now().Unix(),
"org_id": orgId,
"industry_id": industry.GetCeilInt("id"),
}
2022-02-14 11:10:58 +00:00
//if city != nil {
// industryAnalyse["city_id"] = city.GetCeilInt("id")
//}
2022-01-26 03:45:17 +00:00
this.Db.Insert("industry_analyse", industryAnalyse)
}
companyData["industry_id"] = industry.GetCeilInt("id")
}
2022-01-16 20:47:39 +00:00
2022-03-03 16:06:04 +00:00
company := this.Db.Get("company", "id", Map{"name": companyData["name"]})
2022-02-28 11:08:48 +00:00
//没有则创建
2022-03-03 16:06:04 +00:00
if company == nil && companyData.GetString("sn") != "暂无" {
company = this.Db.Get("company", "id", Map{"sn": companyData["sn"]})
2022-02-28 11:08:48 +00:00
}
2022-01-16 20:47:39 +00:00
//没有则创建
if company == nil {
//更新两张表
companyData["create_time"] = time.Now().Unix()
id := this.Db.Insert("company", companyData)
companyData["company_id"] = id
2022-02-14 11:10:58 +00:00
company = Map{"id": id}
2022-01-16 20:47:39 +00:00
} else {
//有则更新
2022-02-28 11:08:48 +00:00
this.Db.Update("company", companyData, Map{"id": company.GetCeilInt("id")})
2022-01-16 20:47:39 +00:00
companyData["company_id"] = company.GetCeilInt("id")
companyData["create_time"] = time.Now().Unix()
}
2022-01-19 23:15:14 +00:00
id := this.Db.Insert("company_history", companyData)
//更新最新的企业信息
this.Db.Update("company", Map{"company_history_id": id}, Map{"id": company.GetCeilInt("id")})
2022-01-16 20:47:39 +00:00
}
}
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
}