forked from golang/hotime
316 lines
7.5 KiB
Go
316 lines
7.5 KiB
Go
|
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
|
|||
|
}
|