mirror of
https://gitee.com/xiangheng/x_admin.git
synced 2025-12-24 08:12:55 +08:00
更换为null_time,null_int
This commit is contained in:
@@ -32,7 +32,6 @@ func (c convertUtil) StructsToMaps(from interface{}) (data []map[string]interfac
|
||||
return nil
|
||||
}
|
||||
for _, v := range objList {
|
||||
// data = append(data, structs.Map(v))
|
||||
data = append(data, c.StructToMap(v))
|
||||
}
|
||||
return data
|
||||
@@ -44,10 +43,18 @@ func (c convertUtil) MapToStruct(from interface{}, to interface{}) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
// StructToStruct 将结构体from弱类型转换成结构体to,需要tag:json,mapstructure
|
||||
// StructToStruct 将结构体from弱类型转换成结构体to
|
||||
func (c convertUtil) StructToStruct(from interface{}, to interface{}) (err error) {
|
||||
m := c.StructToMap(from)
|
||||
err = c.MapToStruct(m, to)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (c convertUtil) Copy(toValue interface{}, fromValue interface{}) interface{} {
|
||||
if err := copier.Copy(toValue, fromValue); err != nil {
|
||||
core.Logger.Errorf("Copy err: err=[%+v]", err)
|
||||
panic("SystemError")
|
||||
}
|
||||
return toValue
|
||||
}
|
||||
|
||||
@@ -16,16 +16,16 @@ type Excel struct {
|
||||
type Col struct {
|
||||
Name string
|
||||
Key string
|
||||
Width int
|
||||
Replace map[string]any
|
||||
Encode func(value any) any //暂未使用
|
||||
Decode func(value any) any
|
||||
Width int // 宽度
|
||||
Replace map[string]any //实现值的替换
|
||||
Encode func(value any) any //暂未使用
|
||||
Decode func(value any) (any, error) //实现类型、值的替换
|
||||
}
|
||||
|
||||
// 下载
|
||||
func DownLoadExcel(fileName string, res http.ResponseWriter, file *excelize.File) {
|
||||
// 设置响应头
|
||||
res.Header().Set("Content-Type", "text/html; charset=UTF-8")
|
||||
// res.Header().Set("Content-Type", "text/html; charset=UTF-8")
|
||||
res.Header().Set("Content-Type", "application/octet-stream")
|
||||
res.Header().Set("Content-Disposition", "attachment; filename="+fileName+".xlsx")
|
||||
res.Header().Set("Access-Control-Expose-Headers", "Content-Disposition")
|
||||
|
||||
@@ -18,17 +18,17 @@ func GetExcelColumnName(columnNumber int) string {
|
||||
return columnName
|
||||
}
|
||||
|
||||
// NormalDynamicExport 导出excel
|
||||
// Export 导出excel
|
||||
// lists 要导出的数据
|
||||
// cols 列
|
||||
// sheet 文档
|
||||
// title 标题
|
||||
func NormalDynamicExport(lists any, cols []Col, sheet string, title string) (file *excelize.File, err error) {
|
||||
func Export(lists any, cols []Col, sheet string, title string) (file *excelize.File, err error) {
|
||||
e := ExcelInit()
|
||||
|
||||
listsAny := util.ConvertUtil.StructsToMaps(lists)
|
||||
data := util.ConvertUtil.StructsToMaps(lists)
|
||||
|
||||
err = ExportExcel(sheet, title, listsAny, cols, e)
|
||||
err = ExportExcel(sheet, title, data, cols, e)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -42,17 +42,17 @@ func ExportExcel(sheet, title string, lists []map[string]interface{}, cols []Col
|
||||
e.F.NewSheet(sheet)
|
||||
}
|
||||
// 构造表头
|
||||
endColName, startDataRow, err := normalBuildTitle(e, sheet, title, cols)
|
||||
endColName, startDataRow, err := buildTitle(e, sheet, title, cols)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
// 构造数据行
|
||||
err = normalBuildDataRow(e, sheet, endColName, startDataRow, lists, cols)
|
||||
err = buildDataRow(e, sheet, endColName, startDataRow, lists, cols)
|
||||
return
|
||||
}
|
||||
|
||||
// 构造表头(endColName 最后一列的列名 startDataRow 数据行开始的行号)
|
||||
func normalBuildTitle(e *Excel, sheet, title string, cols []Col) (endColName string, startDataRow int, err error) {
|
||||
func buildTitle(e *Excel, sheet, title string, cols []Col) (endColName string, startDataRow int, err error) {
|
||||
var titleRowData []interface{} // 列头行
|
||||
for i, colTitle := range cols {
|
||||
endColName := GetExcelColumnName(i + 1)
|
||||
@@ -87,7 +87,7 @@ func normalBuildTitle(e *Excel, sheet, title string, cols []Col) (endColName str
|
||||
}
|
||||
|
||||
// 构造数据行
|
||||
func normalBuildDataRow(e *Excel, sheet, endColName string, startDataRow int, lists []map[string]interface{}, cols []Col) (err error) {
|
||||
func buildDataRow(e *Excel, sheet, endColName string, startDataRow int, lists []map[string]interface{}, cols []Col) (err error) {
|
||||
//实时写入数据
|
||||
for i := 0; i < len(lists); i++ {
|
||||
startCol := fmt.Sprintf("A%d", startDataRow)
|
||||
@@ -102,6 +102,11 @@ func normalBuildDataRow(e *Excel, sheet, endColName string, startDataRow int, li
|
||||
|
||||
val := list[col.Key]
|
||||
|
||||
// 先编码
|
||||
if col.Encode != nil {
|
||||
val = col.Encode(val)
|
||||
}
|
||||
// 再替换
|
||||
for replaceKey, v := range replace {
|
||||
|
||||
if replaceKey == fmt.Sprintf("%v", val) {
|
||||
|
||||
@@ -79,16 +79,19 @@ func importData(f *excelize.File, dst interface{}, sheetName string, startRow in
|
||||
replace := col.Replace
|
||||
|
||||
val := rows[i][j]
|
||||
// 将val替换为key
|
||||
// 先替换,将val替换为key
|
||||
for replaceKey, v := range replace {
|
||||
if fmt.Sprintf("%v", v) == fmt.Sprintf("%v", val) {
|
||||
val = fmt.Sprintf("%v", replaceKey)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// 再解码
|
||||
if col.Decode != nil {
|
||||
rowMap[key] = col.Decode(val)
|
||||
v, e := col.Decode(val)
|
||||
if e == nil {
|
||||
rowMap[key] = v
|
||||
}
|
||||
} else {
|
||||
rowMap[key] = val
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ func TestExport(t *testing.T) {
|
||||
"备注备注备注备注com哈哈哈哈哈哈哈哈这里是最大行高测试哈哈哈哈哈哈哈哈这里是最大行高测试哈哈哈哈哈哈哈哈这里是最大行高测里是最大行高测试哈哈哈哈哈哈哈哈这里是最大行高测试"},
|
||||
}
|
||||
|
||||
f, err := NormalDynamicExport(testList, cols, "Sheet1", "用户信息")
|
||||
f, err := Export(testList, cols, "Sheet1", "用户信息")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
|
||||
108
server/util/null_time.go
Normal file
108
server/util/null_time.go
Normal file
@@ -0,0 +1,108 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"time"
|
||||
"x_admin/core"
|
||||
)
|
||||
|
||||
var NullTimeUtil = nullTimeUtil{
|
||||
DateFormat: "2006-01-02",
|
||||
TimeFormat: "2006-01-02 15:04:05",
|
||||
}
|
||||
|
||||
// arrayUtil 数组工具类
|
||||
type nullTimeUtil struct {
|
||||
DateFormat string
|
||||
TimeFormat string
|
||||
}
|
||||
|
||||
// DecodeTime 时间解码
|
||||
func (t nullTimeUtil) DecodeTime(value any) (any, error) {
|
||||
tt, e := t.Parse(value)
|
||||
return tt, e
|
||||
}
|
||||
|
||||
// EncodeTime 时间编码
|
||||
func (t nullTimeUtil) EncodeTime(value any) any {
|
||||
var str = ""
|
||||
switch v := value.(type) {
|
||||
case map[string]interface{}:
|
||||
tt := v["Time"]
|
||||
|
||||
ttt, _ := t.Parse(tt)
|
||||
str = ttt.String()
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
// ToUnix 时间戳转时间戳
|
||||
func (t nullTimeUtil) ToUnix(date any) int64 {
|
||||
switch v := date.(type) {
|
||||
case string:
|
||||
if v == "" {
|
||||
return 0
|
||||
}
|
||||
tt, e := time.Parse(t.TimeFormat, v)
|
||||
if e != nil {
|
||||
return 0
|
||||
}
|
||||
return time.Time(tt).Unix()
|
||||
case time.Time:
|
||||
return v.Unix()
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// Parse 时间戳转时间
|
||||
func (t nullTimeUtil) Parse(value interface{}) (core.NullTime, error) {
|
||||
switch v := value.(type) {
|
||||
case string:
|
||||
tt, e := t.ParseString(v)
|
||||
return tt, e
|
||||
case time.Time:
|
||||
tt := t.ParseTime(v)
|
||||
return tt, nil
|
||||
case core.NullTime:
|
||||
return v, nil
|
||||
default:
|
||||
return t.Null(), nil
|
||||
}
|
||||
}
|
||||
|
||||
// ParseTime 时间转时间戳
|
||||
func (t nullTimeUtil) ParseTime(date time.Time) core.NullTime {
|
||||
return core.NullTime{
|
||||
Time: &date,
|
||||
Valid: true,
|
||||
}
|
||||
}
|
||||
|
||||
// ParseString 时间字符串转时间戳
|
||||
func (t nullTimeUtil) ParseString(date string) (core.NullTime, error) {
|
||||
tt, e := time.Parse(t.TimeFormat, date)
|
||||
if e != nil {
|
||||
return t.Null(), e
|
||||
}
|
||||
return core.NullTime{
|
||||
Time: &tt,
|
||||
Valid: true,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// NowTime 当前时间
|
||||
func (t nullTimeUtil) Null() core.NullTime {
|
||||
return core.NullTime{
|
||||
Time: nil,
|
||||
Valid: true,
|
||||
}
|
||||
}
|
||||
|
||||
// NowTime 当前时间
|
||||
func (t nullTimeUtil) Now() core.NullTime {
|
||||
now := time.Now()
|
||||
return core.NullTime{
|
||||
Time: &now,
|
||||
Valid: true,
|
||||
}
|
||||
}
|
||||
76
server/util/time.go
Normal file
76
server/util/time.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
"x_admin/core"
|
||||
)
|
||||
|
||||
var TimeUtil = timeUtil{
|
||||
DateFormat: "2006-01-02",
|
||||
TimeFormat: "2006-01-02 15:04:05",
|
||||
}
|
||||
|
||||
// arrayUtil 数组工具类
|
||||
type timeUtil struct {
|
||||
DateFormat string
|
||||
TimeFormat string
|
||||
}
|
||||
|
||||
// ToUnix 时间戳转时间戳
|
||||
func (t timeUtil) ToUnix(date any) int64 {
|
||||
switch v := date.(type) {
|
||||
case string:
|
||||
if v == "" {
|
||||
return 0
|
||||
}
|
||||
tt, e := time.Parse(t.TimeFormat, v)
|
||||
if e != nil {
|
||||
return 0
|
||||
}
|
||||
return time.Time(tt).Unix()
|
||||
case time.Time:
|
||||
return v.Unix()
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// ParseTsTime 时间戳转时间
|
||||
func (t timeUtil) ParseTsTime(value any) (core.TsTime, error) {
|
||||
switch v := value.(type) {
|
||||
case string:
|
||||
tt, e := t.ParseString(v)
|
||||
return tt, e
|
||||
case time.Time:
|
||||
tt := t.ParseTime(v)
|
||||
return tt, nil
|
||||
default:
|
||||
return t.NowTime(), errors.New("not support type")
|
||||
}
|
||||
}
|
||||
|
||||
// ParseTime 时间转时间戳
|
||||
func (t timeUtil) ParseTime(date time.Time) core.TsTime {
|
||||
return core.TsTime(date)
|
||||
}
|
||||
|
||||
// ParseString 时间字符串转时间戳
|
||||
func (t timeUtil) ParseString(date string) (core.TsTime, error) {
|
||||
tt, e := time.Parse(t.TimeFormat, date)
|
||||
if e != nil {
|
||||
return t.NowTime(), e
|
||||
}
|
||||
return core.TsTime(tt), nil
|
||||
}
|
||||
|
||||
// NowTime 当前时间
|
||||
func (t timeUtil) NowTime() core.TsTime {
|
||||
return core.TsTime(time.Now())
|
||||
}
|
||||
|
||||
// DecodeTime 时间解码
|
||||
func (t timeUtil) DecodeTime(value any) (any, error) {
|
||||
tt, e := t.ParseTsTime(value)
|
||||
return tt, e
|
||||
}
|
||||
@@ -68,19 +68,7 @@ func (vu verifyUtil) VerifyJSON(c *gin.Context, obj any) (e error) {
|
||||
e = response.ParamsValidError.SetData(errs.Translate(trans))
|
||||
return
|
||||
}
|
||||
// response.Copy(obj, reqInfo)
|
||||
return
|
||||
|
||||
// if err := c.ShouldBindBodyWith(obj, binding.JSON); err != nil {
|
||||
// errs, ok := err.(validator.ValidationErrors)
|
||||
// if !ok {
|
||||
// e = response.ParamsValidError.SetData(err.Error())
|
||||
// return
|
||||
// }
|
||||
// e = response.ParamsValidError.SetData(errs.Translate(trans))
|
||||
// return
|
||||
// }
|
||||
// return
|
||||
}
|
||||
|
||||
func (vu verifyUtil) VerifyJSONArray(c *gin.Context, obj any) (e error) {
|
||||
|
||||
Reference in New Issue
Block a user