|
|
package Test
|
|
|
|
|
|
import (
|
|
|
"dsBaseRpc/Utils/CommonUtil"
|
|
|
"dsBaseRpc/Utils/ConfigUtil"
|
|
|
"dsBaseRpc/Utils/DbUtil"
|
|
|
"dsBaseRpc/models"
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
"strings"
|
|
|
"testing"
|
|
|
)
|
|
|
|
|
|
var db = DbUtil.Engine
|
|
|
|
|
|
/**
|
|
|
功能:导入EXCEL的字段描述JSON对应的结构体
|
|
|
*/
|
|
|
type ImportExcelColumnStruct struct {
|
|
|
Required bool `json:"required"`
|
|
|
Memo string `json:"memo"`
|
|
|
LengthLimit string `json:"length_limit"`
|
|
|
IdCardLimit bool `json:"id_card_limit"`
|
|
|
TelLimit bool `json:"tel_limit"`
|
|
|
EmailLimit bool `json:"email_limit"`
|
|
|
Width int `json:"width"`
|
|
|
MapSql string `json:"map_sql"`
|
|
|
ColName string `json:"col_name"`
|
|
|
ParentColName string `json:"parent_col_name"`
|
|
|
Map map[string]string `json:"map"`
|
|
|
ChildMap map[string]map[string]string `json:"child_map"`
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
功能:导出的表格级的结构体
|
|
|
作者:黄海
|
|
|
时间:2020-06-15
|
|
|
*/
|
|
|
type ImportExcelTableStruct struct {
|
|
|
TitleName string `json:"title_name"`
|
|
|
SheetName string `json:"sheet_name"`
|
|
|
FileName string `json:"file_name"`
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
功能:检查研发人员定义的导入导出表格的合法性
|
|
|
作者:黄海
|
|
|
时间:2020-06-15
|
|
|
*/
|
|
|
func CheckImportTable(tableName string, bureauName string) (bool, []ImportExcelColumnStruct, ImportExcelTableStruct) {
|
|
|
//表的注释
|
|
|
//{"title_name":"{{.bureau_name}}教工信息表","sheet_name":"教工信息表","file_name":"{{.bureau_name}}教工信息表"}
|
|
|
sql := `select t1.table_comment from information_schema.tables as t1 where t1.table_schema=? and t1.table_name=?`
|
|
|
list, err := db.SQL(sql, ConfigUtil.MysqlDataBase, tableName).Query().List()
|
|
|
tableComment := list[0]["table_comment"].(string)
|
|
|
tableComment = strings.Replace(tableComment, "{{.bureau_name}}", bureauName, -1)
|
|
|
|
|
|
var tableStruct ImportExcelTableStruct
|
|
|
if err := json.Unmarshal([]byte(tableComment), &tableStruct); err != nil {
|
|
|
fmt.Println(err.Error())
|
|
|
return false, nil, tableStruct
|
|
|
}
|
|
|
//返回结果
|
|
|
var columnStructs = make([]ImportExcelColumnStruct, 0)
|
|
|
//1、读取有哪些字段
|
|
|
sql = "select column_name,column_comment from information_schema.columns where table_name =? and table_schema=?"
|
|
|
list, err = db.SQL(sql, tableName, ConfigUtil.MysqlDataBase).Query().List()
|
|
|
|
|
|
if err != nil {
|
|
|
fmt.Println("发生了严重SQL错误!")
|
|
|
return false, nil, tableStruct
|
|
|
}
|
|
|
//2、id和batch_id是保留字段
|
|
|
for i := range list {
|
|
|
//排除掉id和batch_id
|
|
|
if CommonUtil.IsContainString([]string{"id", "batch_id"}, list[i]["column_name"].(string)) {
|
|
|
continue
|
|
|
}
|
|
|
//读取每一列的JSON描述
|
|
|
columnName := list[i]["column_name"].(string)
|
|
|
columnJson := list[i]["column_comment"].(string)
|
|
|
//是不是能转化为json
|
|
|
var someOne ImportExcelColumnStruct
|
|
|
if err := json.Unmarshal([]byte(columnJson), &someOne); err == nil {
|
|
|
someOne.ColName = columnName
|
|
|
//扩展一级map
|
|
|
if someOne.MapSql != "" && someOne.ParentColName == "" {
|
|
|
l1, _ := db.SQL(someOne.MapSql).Query().List()
|
|
|
_map := make(map[string]string)
|
|
|
for j := range l1 {
|
|
|
if l1[j]["dict_code"] == nil {
|
|
|
fmt.Println("配置" + someOne.ColName + "中dict_code存在错误!")
|
|
|
return false, nil, tableStruct
|
|
|
}
|
|
|
if l1[j]["dict_value"] == nil {
|
|
|
fmt.Println("配置" + someOne.ColName + "中dict_value存在错误!")
|
|
|
return false, nil, tableStruct
|
|
|
}
|
|
|
_map[l1[j]["dict_value"].(string)] = l1[j]["dict_code"].(string)
|
|
|
}
|
|
|
someOne.Map = _map
|
|
|
}
|
|
|
//扩展二级map
|
|
|
if someOne.MapSql != "" && someOne.ParentColName != "" {
|
|
|
//二级map
|
|
|
var m2 = make(map[string]map[string]string, 0)
|
|
|
//(1)执行SQL,获取结果集
|
|
|
l3, _ := db.SQL(someOne.MapSql).Query().List()
|
|
|
//(2)遍历结果集,填充childMap,初次是由parentMap获取过来的空的。
|
|
|
for k := range l3 {
|
|
|
if l3[k]["parent_value"] == nil {
|
|
|
fmt.Println("配置" + someOne.ColName + "中parent_value存在错误!")
|
|
|
return false, nil, tableStruct
|
|
|
}
|
|
|
parentValue := l3[k]["parent_value"].(string) //Name参数
|
|
|
|
|
|
if l3[k]["dict_value"] == nil {
|
|
|
fmt.Println("配置" + someOne.ColName + "中dict_value存在错误!")
|
|
|
return false, nil, tableStruct
|
|
|
}
|
|
|
dictValue := l3[k]["dict_value"].(string) //Name参数
|
|
|
|
|
|
if l3[k]["dict_code"] == nil {
|
|
|
fmt.Println("配置" + someOne.ColName + "中dict_code存在错误!")
|
|
|
return false, nil, tableStruct
|
|
|
}
|
|
|
dictCode := l3[k]["dict_code"].(string) //Code参数
|
|
|
// map[string]map[string]string
|
|
|
// ["小学"]--->["语文","数学","外语"]
|
|
|
// ["初中"]--->["语文","数学","外语","物理","化学"]
|
|
|
// ["高中"]--->["语文","数学","外语","物理","化学","生物","历史"]
|
|
|
if _, exist := m2[parentValue]; exist {
|
|
|
m2[parentValue][dictValue] = dictCode
|
|
|
} else {
|
|
|
c := make(map[string]string)
|
|
|
c[dictValue] = dictCode
|
|
|
m2[parentValue] = c
|
|
|
}
|
|
|
}
|
|
|
//3、放入结构体
|
|
|
someOne.ChildMap = m2
|
|
|
}
|
|
|
columnStructs = append(columnStructs, someOne)
|
|
|
} else {
|
|
|
fmt.Println(err.Error())
|
|
|
return false, nil, tableStruct
|
|
|
}
|
|
|
}
|
|
|
return true, columnStructs, tableStruct
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
功能:测试导入导出的基准EXCEL表格(教师)
|
|
|
作者:黄海
|
|
|
时间:2020-06-05
|
|
|
*/
|
|
|
func TestImportTeacherTable(t *testing.T) {
|
|
|
//1、单位ID
|
|
|
bureauId := "011EBF44-7946-4056-AB35-23EF20BBD19B"
|
|
|
//2、获取单位信息
|
|
|
var m1 models.TBaseOrganization
|
|
|
db.ID(bureauId).Get(&m1)
|
|
|
//3、检查导入表的配置情况
|
|
|
success, columnStructs, tableStruct := CheckImportTable("t_base_teacher_import_excel", m1.OrgName)
|
|
|
if !success {
|
|
|
fmt.Println("存在不合法配置项目,请检查!")
|
|
|
return
|
|
|
}
|
|
|
for i := range columnStructs {
|
|
|
fmt.Println(columnStructs[i])
|
|
|
}
|
|
|
//4、生成导入导出模板
|
|
|
|
|
|
//5、生成现有人员数据,导出excel
|
|
|
fmt.Println(tableStruct)
|
|
|
}
|
|
|
|