2021-12-11 09:59:02 +00:00
package app
import (
. "../../../hotime"
. "../../../hotime/common"
"strings"
"time"
)
var materialCtr = Ctr {
"info" : func ( that * Context ) {
data := that . Db . Get ( "admin" , "*" , Map { "id" : that . Session ( "admin_id" ) . ToCeilInt ( ) } )
str , inData := that . MakeCode . Info ( that . RouterString [ 1 ] , data , that . Db )
where := Map { "id" : that . RouterString [ 2 ] }
if len ( inData ) == 1 {
inData [ "id" ] = where [ "id" ]
where = Map { "AND" : inData }
} else if len ( inData ) > 1 {
where [ "OR" ] = inData
where = Map { "AND" : where }
}
re := that . Db . Get ( that . RouterString [ 1 ] , str , where )
if re == nil {
that . Display ( 4 , "找不到对应信息" )
return
}
for k , v := range re {
column := that . MakeCode . TableColumns [ that . RouterString [ 1 ] ] [ k ]
if column == nil {
continue
}
if ( column [ "list" ] == nil || column . GetBool ( "list" ) ) && column . GetString ( "link" ) != "" {
re [ column . GetString ( "link" ) ] = that . Db . Get ( column . GetString ( "link" ) , "id," + column . GetString ( "value" ) , Map { "id" : v } )
}
}
that . Display ( 0 , re )
} ,
"add" : func ( that * Context ) {
adminID := that . Session ( "id" ) . ToInt ( )
if adminID == 0 {
that . Display ( 2 , "登录失效,请重新登录" )
return
}
name := that . Req . FormValue ( "name" )
img := that . Req . FormValue ( "img" )
2021-12-14 21:56:31 +00:00
validity := ObjToInt ( that . Req . FormValue ( "validity" ) )
num := ObjToInt ( that . Req . FormValue ( "num" ) )
2021-12-11 09:59:02 +00:00
rule := that . Req . FormValue ( "rule" )
2021-12-14 21:56:31 +00:00
content := that . Req . FormValue ( "content" )
description := that . Req . FormValue ( "description" )
2021-12-11 09:59:02 +00:00
if name == "" || rule == "" {
that . Display ( 3 , "参数不足,请补充参数" )
return
}
data := Map {
"name" : name ,
"img" : img ,
"rule" : rule ,
"admin_id" : adminID ,
"count" : 0 ,
"used" : 0 ,
"saved" : 0 ,
2021-12-14 21:56:31 +00:00
"num" : num ,
"validity" : validity ,
"description" : description ,
"content" : content ,
2021-12-11 09:59:02 +00:00
"create_time" : time . Now ( ) . Unix ( ) ,
"modify_time" : time . Now ( ) . Unix ( ) ,
}
id := that . Db . Insert ( "material" , data )
if id == 0 {
that . Display ( 4 , "添加材料失败,请重新添加" )
return
}
data [ "id" ] = id
that . Display ( 0 , data )
} ,
"update" : func ( that * Context ) {
inData := that . MakeCode . Edit ( that . RouterString [ 1 ] , that . Req )
if inData == nil {
that . Display ( 3 , "没有找到要更新的数据" )
return
}
//索引管理,便于检索以及权限
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 ] + ","
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" ) } )
}
}
re := that . Db . Update ( that . RouterString [ 1 ] , inData , Map { "id" : that . RouterString [ 2 ] } )
if re == 0 {
that . Display ( 4 , "更新数据失败" )
return
}
that . Display ( 0 , re )
} ,
2021-12-14 21:56:31 +00:00
"inout" : func ( that * Context ) {
adminID := that . Session ( "id" ) . ToInt ( )
if adminID == 0 {
that . Display ( 2 , "登录失效,请重新登录" )
return
}
data := ObjToMap ( that . Req . FormValue ( "data" ) )
2021-12-11 09:59:02 +00:00
2021-12-14 21:56:31 +00:00
texts := data . GetSlice ( "text" )
textData := [ ] Map { }
for k , _ := range texts {
v := texts . GetString ( k )
if len ( v ) < 4 {
continue
}
vs := that . Db . Select ( "material" , "name,id,content,rule,num" , Map { "content[~]" : v [ : len ( v ) / 2 ] } )
for _ , v1 := range vs {
if len ( textData ) == 0 {
textData = append ( textData , v1 )
}
for _ , vt := range textData {
if v1 . GetString ( "id" ) != vt . GetString ( "id" ) {
add := true
for _ , vt1 := range textData {
if vt1 . GetCeilInt ( "id" ) == v1 . GetCeilInt ( "id" ) {
add = false
break
}
}
if add {
v1 [ "count" ] = 1
textData = append ( textData , v1 )
}
} else {
vt [ "count" ] = vt . GetCeilInt ( "count" ) + 1
}
}
}
}
qrcode := data . GetSlice ( "qrcode" )
for k , _ := range qrcode {
v := texts . GetString ( k )
if len ( v ) < 4 {
continue
}
vs := that . Db . Select ( "material" , "name,id,content,rule,num" , Map { "content[~]" : v [ : len ( v ) / 2 ] } )
for _ , v1 := range vs {
2021-12-15 01:24:41 +00:00
if len ( textData ) == 0 {
textData = append ( textData , v1 )
}
2021-12-14 21:56:31 +00:00
for _ , vt := range textData {
if v1 . GetString ( "id" ) != vt . GetString ( "id" ) {
v1 [ "count" ] = 1
textData = append ( textData , v1 )
} else {
vt [ "count" ] = vt . GetCeilInt ( "count" ) + 1
}
}
}
}
that . Display ( 0 , textData )
} ,
2021-12-11 09:59:02 +00:00
"search" : func ( that * Context ) {
adminID := that . Session ( "id" ) . ToInt ( )
if adminID == 0 {
that . Display ( 2 , "登录失效,请重新登录" )
return
}
page := ObjToInt ( that . Req . FormValue ( "page" ) )
pageSize := ObjToInt ( that . Req . FormValue ( "pageSize" ) )
if page < 1 {
page = 1
}
if pageSize <= 0 {
2021-12-11 20:22:30 +00:00
pageSize = 10
2021-12-11 09:59:02 +00:00
}
leftJoin := Map { "[><]admin" : "material.admin_id=admin.id" }
2021-12-11 20:22:30 +00:00
columnStr := "material.id,material.sn,material.name,material.img,material.count,material.used,material.saved,material.admin_id,admin.name AS admin_name,material.modify_time,material.state"
where := Map { "ORDER" : "modify_time DESC" }
count := that . Db . Count ( "material" , where )
2021-12-11 09:59:02 +00:00
reData := that . Db . Page ( page , pageSize ) .
PageSelect ( "material" , leftJoin , columnStr , where )
that . Display ( 0 , Map { "count" : count , "data" : reData } )
} ,
}