table.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. package gen_service
  2. import (
  3. "gfast/app/model/admin/gen_table"
  4. "gfast/app/model/admin/gen_table_column"
  5. "github.com/gogf/gf/database/gdb"
  6. "github.com/gogf/gf/encoding/gjson"
  7. "github.com/gogf/gf/errors/gerror"
  8. "github.com/gogf/gf/frame/g"
  9. "github.com/gogf/gf/os/gtime"
  10. "github.com/gogf/gf/util/gconv"
  11. "strings"
  12. )
  13. //根据条件分页查询数据
  14. func SelectDbTableList(param *gen_table.SelectPageReq) (total int, list []*gen_table.Entity, err error) {
  15. return gen_table.SelectDbTableList(param)
  16. }
  17. //根据条件分页查询数据
  18. func SelectListByPage(param *gen_table.SelectPageReq) (total int, list []*gen_table.Entity, err error) {
  19. return gen_table.SelectListByPage(param)
  20. }
  21. //查询据库列表
  22. func SelectDbTableListByNames(tableNames []string) ([]*gen_table.Entity, error) {
  23. return gen_table.SelectDbTableListByNames(tableNames)
  24. }
  25. //导入表结构
  26. func ImportGenTable(tableList []*gen_table.Entity, operName string) error {
  27. if tableList != nil && operName != "" {
  28. tx, err := g.DB().Begin()
  29. if err != nil {
  30. return err
  31. }
  32. for _, table := range tableList {
  33. tableName := table.TableName
  34. InitTable(table, operName)
  35. result, err := tx.Table(gen_table.Table).Insert(table)
  36. if err != nil {
  37. return err
  38. }
  39. tmpid, err := result.LastInsertId()
  40. if err != nil || tmpid <= 0 {
  41. tx.Rollback()
  42. return gerror.New("保存数据失败")
  43. }
  44. table.TableId = tmpid
  45. // 保存列信息
  46. genTableColumns, err := gen_table_column.SelectDbTableColumnsByName(tableName)
  47. if err != nil || len(genTableColumns) <= 0 {
  48. tx.Rollback()
  49. return gerror.New("获取列数据失败")
  50. }
  51. for _, column := range genTableColumns {
  52. InitColumnField(column, table)
  53. _, err = tx.Table("gen_table_column").Insert(column)
  54. if err != nil {
  55. tx.Rollback()
  56. return gerror.New("保存列数据失败")
  57. }
  58. }
  59. }
  60. return tx.Commit()
  61. } else {
  62. return gerror.New("参数错误")
  63. }
  64. }
  65. //获取数据库类型字段
  66. func GetDbType(columnType string) string {
  67. if strings.Index(columnType, "(") > 0 {
  68. return columnType[0:strings.Index(columnType, "(")]
  69. } else {
  70. return columnType
  71. }
  72. }
  73. //将下划线大写方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。 例如:HELLO_WORLD->HelloWorld
  74. func ConvertToCamelCase(name string) string {
  75. if name == "" {
  76. return ""
  77. } else if !strings.Contains(name, "_") {
  78. // 不含下划线,仅将首字母大写
  79. return strings.ToUpper(name[0:1]) + name[1:len(name)]
  80. }
  81. var result string = ""
  82. camels := strings.Split(name, "_")
  83. for index := range camels {
  84. if camels[index] == "" {
  85. continue
  86. }
  87. camel := camels[index]
  88. result = result + strings.ToUpper(camel[0:1]) + strings.ToLower(camel[1:len(camel)])
  89. }
  90. return result
  91. }
  92. ////将下划线大写方式命名的字符串转换为驼峰式,首字母小写。如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。 例如:HELLO_WORLD->helloWorld
  93. func ConvertToCamelCase1(name string) string {
  94. if name == "" {
  95. return ""
  96. } else if !strings.Contains(name, "_") {
  97. // 不含下划线,原值返回
  98. return name
  99. }
  100. var result string = ""
  101. camels := strings.Split(name, "_")
  102. for index := range camels {
  103. if camels[index] == "" {
  104. continue
  105. }
  106. camel := camels[index]
  107. if result == "" {
  108. result = strings.ToLower(camel[0:1]) + strings.ToLower(camel[1:len(camel)])
  109. } else {
  110. result = result + strings.ToUpper(camel[0:1]) + strings.ToLower(camel[1:len(camel)])
  111. }
  112. }
  113. return result
  114. }
  115. //获取字段长度
  116. func GetColumnLength(columnType string) int {
  117. start := strings.Index(columnType, "(")
  118. end := strings.Index(columnType, ")")
  119. result := ""
  120. if start >= 0 && end >= 0 {
  121. result = columnType[start+1 : end-1]
  122. }
  123. return gconv.Int(result)
  124. }
  125. //初始化列属性字段
  126. func InitColumnField(column *gen_table_column.Entity, table *gen_table.Entity) {
  127. dataType := GetDbType(column.ColumnType)
  128. columnName := column.ColumnName
  129. column.TableId = table.TableId
  130. column.CreateBy = table.CreateBy
  131. column.CreateTime = gtime.Now()
  132. column.UpdateTime = column.CreateTime
  133. //设置字段名
  134. column.GoField = ConvertToCamelCase(columnName)
  135. column.HtmlField = ConvertToCamelCase1(columnName)
  136. if gen_table_column.IsStringObject(dataType) {
  137. //字段为字符串类型
  138. column.GoType = "string"
  139. columnLength := GetColumnLength(column.ColumnType)
  140. if columnLength >= 500 {
  141. column.HtmlType = "textarea"
  142. } else {
  143. column.HtmlType = "input"
  144. }
  145. } else if gen_table_column.IsTimeObject(dataType) {
  146. //字段为时间类型
  147. column.GoType = "Time"
  148. column.HtmlType = "datatime"
  149. } else if gen_table_column.IsNumberObject(dataType) {
  150. //字段为数字类型
  151. column.HtmlType = "input"
  152. // 如果是浮点型
  153. tmp := column.ColumnType
  154. start := strings.Index(tmp, "(")
  155. end := strings.Index(tmp, ")")
  156. result := "0"
  157. if start > 0 && end > 0 {
  158. result = tmp[start+1 : end]
  159. }
  160. arr := strings.Split(result, ",")
  161. if len(arr) == 2 && gconv.Int(arr[1]) > 0 {
  162. column.GoType = "float64"
  163. } else if len(arr) == 1 && gconv.Int(arr[0]) <= 10 {
  164. column.GoType = "int"
  165. } else {
  166. column.GoType = "int64"
  167. }
  168. }
  169. //新增字段
  170. if columnName == "create_by" || columnName == "create_time" || columnName == "update_by" || columnName == "update_time" {
  171. column.IsRequired = "0"
  172. column.IsInsert = "0"
  173. } else {
  174. column.IsRequired = "0"
  175. column.IsInsert = "1"
  176. if strings.Index(columnName, "name") >= 0 || strings.Index(columnName, "status") >= 0 {
  177. column.IsRequired = "1"
  178. }
  179. }
  180. // 编辑字段
  181. if gen_table_column.IsNotEdit(columnName) {
  182. if column.IsPk == "1" {
  183. column.IsEdit = "0"
  184. } else {
  185. column.IsEdit = "1"
  186. }
  187. } else {
  188. column.IsEdit = "0"
  189. }
  190. // 列表字段
  191. if gen_table_column.IsNotList(columnName) {
  192. column.IsList = "1"
  193. } else {
  194. column.IsList = "0"
  195. }
  196. // 查询字段
  197. if gen_table_column.IsNotQuery(columnName) {
  198. column.IsQuery = "1"
  199. } else {
  200. column.IsQuery = "0"
  201. }
  202. // 查询字段类型
  203. if CheckNameColumn(columnName) {
  204. column.QueryType = "LIKE"
  205. } else {
  206. column.QueryType = "EQ"
  207. }
  208. // 状态字段设置单选框
  209. if CheckStatusColumn(columnName) {
  210. column.HtmlType = "radio"
  211. } else if CheckTypeColumn(columnName) || CheckSexColumn(columnName) {
  212. // 类型&性别字段设置下拉框
  213. column.HtmlType = "select"
  214. }
  215. }
  216. //检查字段名后3位是否是sex
  217. func CheckSexColumn(columnName string) bool {
  218. if len(columnName) >= 3 {
  219. end := len(columnName)
  220. start := end - 3
  221. if start <= 0 {
  222. start = 0
  223. }
  224. if columnName[start:end] == "sex" {
  225. return true
  226. }
  227. }
  228. return false
  229. }
  230. //检查字段名后4位是否是type
  231. func CheckTypeColumn(columnName string) bool {
  232. if len(columnName) >= 4 {
  233. end := len(columnName)
  234. start := end - 4
  235. if start <= 0 {
  236. start = 0
  237. }
  238. if columnName[start:end] == "type" {
  239. return true
  240. }
  241. }
  242. return false
  243. }
  244. //检查字段名后6位是否是status
  245. func CheckStatusColumn(columnName string) bool {
  246. if len(columnName) >= 6 {
  247. end := len(columnName)
  248. start := end - 6
  249. if start <= 0 {
  250. start = 0
  251. }
  252. tmp := columnName[start:end]
  253. if tmp == "status" {
  254. return true
  255. }
  256. }
  257. return false
  258. }
  259. //检查字段名后4位是否是name
  260. func CheckNameColumn(columnName string) bool {
  261. if len(columnName) >= 4 {
  262. end := len(columnName)
  263. start := end - 4
  264. if start <= 0 {
  265. start = 0
  266. }
  267. tmp := columnName[start:end]
  268. if tmp == "name" {
  269. return true
  270. }
  271. }
  272. return false
  273. }
  274. //初始化表信息
  275. func InitTable(table *gen_table.Entity, operName string) {
  276. table.ClassName = ConvertClassName(table.TableName)
  277. table.PackageName = g.Cfg().GetString("gen.packageName")
  278. table.ModuleName = g.Cfg().GetString("gen.moduleName")
  279. table.BusinessName = GetBusinessName(table.TableName)
  280. table.FunctionName = strings.ReplaceAll(table.TableComment, "表", "")
  281. table.FunctionAuthor = g.Cfg().GetString("gen.author")
  282. table.CreateBy = operName
  283. table.TplCategory = "crud"
  284. table.CreateTime = gtime.Now()
  285. table.UpdateTime = table.CreateTime
  286. }
  287. //表名转换成类名
  288. func ConvertClassName(tableName string) string {
  289. autoRemovePre := g.Cfg().GetBool("gen.autoRemovePre")
  290. tablePrefix := g.Cfg().GetString("gen.tablePrefix")
  291. if autoRemovePre && tablePrefix != "" {
  292. searchList := strings.Split(tablePrefix, ",")
  293. for _, str := range searchList {
  294. tableName = strings.ReplaceAll(tableName, str, "")
  295. }
  296. }
  297. return tableName
  298. }
  299. //获取业务名
  300. func GetBusinessName(tableName string) string {
  301. lastIndex := strings.LastIndex(tableName, "_")
  302. nameLength := len(tableName)
  303. businessName := tableName[lastIndex+1 : nameLength]
  304. return businessName
  305. }
  306. //根据table_id查询表列数据
  307. func SelectGenTableColumnListByTableId(tableId int64) ([]*gen_table_column.Entity, error) {
  308. return gen_table_column.SelectGenTableColumnListByTableId(tableId)
  309. }
  310. func GetTableInfoByTableId(tableId int64) (info *gen_table.Entity, err error) {
  311. return gen_table.GetInfoById(tableId)
  312. }
  313. //修改表和列信息
  314. func SaveEdit(req *gen_table.EditReq) (err error) {
  315. if req == nil {
  316. err = gerror.New("参数错误")
  317. return
  318. }
  319. table, err := gen_table.FindOne("table_id=?", req.TableId)
  320. if err != nil || table == nil {
  321. err = gerror.New("数据不存在")
  322. return
  323. }
  324. if req.TableName != "" {
  325. table.TableName = req.TableName
  326. }
  327. if req.TableComment != "" {
  328. table.TableComment = req.TableComment
  329. }
  330. if req.BusinessName != "" {
  331. table.BusinessName = req.BusinessName
  332. }
  333. if req.ClassName != "" {
  334. table.ClassName = req.ClassName
  335. }
  336. if req.FunctionAuthor != "" {
  337. table.FunctionAuthor = req.FunctionAuthor
  338. }
  339. if req.FunctionName != "" {
  340. table.FunctionName = req.FunctionName
  341. }
  342. if req.ModuleName != "" {
  343. table.ModuleName = req.ModuleName
  344. }
  345. if req.PackageName != "" {
  346. table.PackageName = req.PackageName
  347. }
  348. if req.Remark != "" {
  349. table.Remark = req.Remark
  350. }
  351. if req.TplCategory != "" {
  352. table.TplCategory = req.TplCategory
  353. }
  354. if req.Params != "" {
  355. table.Options = req.Params
  356. }
  357. table.UpdateTime = gtime.Now()
  358. table.UpdateBy = req.UserName
  359. if req.TplCategory == "tree" {
  360. //树表设置options
  361. options := g.Map{
  362. "tree_code": req.TreeCode,
  363. "tree_parent_code": req.TreeParentCode,
  364. "tree_name": req.TreeName,
  365. }
  366. table.Options = gconv.String(options)
  367. } else {
  368. table.Options = ""
  369. }
  370. var tx *gdb.TX
  371. tx, err = g.DB().Begin()
  372. if err != nil {
  373. return
  374. }
  375. _, err = tx.Table(gen_table.Table).Save(table)
  376. if err != nil {
  377. tx.Rollback()
  378. return err
  379. }
  380. //保存列数据
  381. if req.Columns != "" {
  382. var j *gjson.Json
  383. if j, err = gjson.DecodeToJson([]byte(req.Columns)); err != nil {
  384. tx.Rollback()
  385. return
  386. } else {
  387. var columnList []gen_table_column.Entity
  388. err = j.ToStructs(&columnList)
  389. if err == nil && columnList != nil && len(columnList) > 0 {
  390. for _, column := range columnList {
  391. if column.ColumnId > 0 {
  392. tmp, _ := gen_table_column.FindOne("column_id=?", column.ColumnId)
  393. if tmp != nil {
  394. tmp.ColumnComment = column.ColumnComment
  395. tmp.GoType = column.GoType
  396. tmp.HtmlType = column.HtmlType
  397. tmp.QueryType = column.QueryType
  398. tmp.GoField = column.GoField
  399. tmp.DictType = column.DictType
  400. tmp.IsInsert = column.IsInsert
  401. tmp.IsEdit = column.IsEdit
  402. tmp.IsList = column.IsList
  403. tmp.IsQuery = column.IsQuery
  404. _, err = tx.Table(gen_table_column.Table).Save(tmp)
  405. if err != nil {
  406. tx.Rollback()
  407. return
  408. }
  409. }
  410. }
  411. }
  412. }
  413. }
  414. }
  415. tx.Commit()
  416. return
  417. }
  418. //删除表格
  419. func Delete(ids []int) error {
  420. tx, err := g.DB().Begin()
  421. if err != nil {
  422. g.Log().Error(err)
  423. return gerror.New("开启删除事务出错")
  424. }
  425. _, err = tx.Table(gen_table.Table).Where(gen_table.Columns.TableId+" in(?)", ids).Delete()
  426. if err != nil {
  427. g.Log().Error(err)
  428. tx.Rollback()
  429. return gerror.New("删除表格数据失败")
  430. }
  431. _, err = tx.Table(gen_table_column.Table).Where(gen_table_column.Columns.TableId+" in(?)", ids).Delete()
  432. if err != nil {
  433. g.Log().Error(err)
  434. tx.Rollback()
  435. return gerror.New("删除表格字段数据失败")
  436. }
  437. tx.Commit()
  438. return nil
  439. }
  440. func SelectRecordById(tableId int64) (entity *gen_table.EntityExtend, err error) {
  441. entity, err = gen_table.SelectRecordById(tableId)
  442. return
  443. }
  444. //设置主键列信息
  445. func SetPkColumn(table *gen_table.EntityExtend, columns []*gen_table_column.Entity) {
  446. for _, column := range columns {
  447. if column.IsPk == "1" {
  448. table.PkColumn = column
  449. break
  450. }
  451. }
  452. if table.PkColumn == nil {
  453. table.PkColumn = columns[0]
  454. }
  455. }