table.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. //设置字段名
  132. column.GoField = ConvertToCamelCase(columnName)
  133. column.HtmlField = ConvertToCamelCase1(columnName)
  134. if gen_table_column.IsStringObject(dataType) {
  135. //字段为字符串类型
  136. column.GoType = "string"
  137. columnLength := GetColumnLength(column.ColumnType)
  138. if columnLength >= 500 {
  139. column.HtmlType = "textarea"
  140. } else {
  141. column.HtmlType = "input"
  142. }
  143. } else if gen_table_column.IsTimeObject(dataType) {
  144. //字段为时间类型
  145. column.GoType = "Time"
  146. column.HtmlType = "datatime"
  147. } else if gen_table_column.IsNumberObject(dataType) {
  148. //字段为数字类型
  149. column.HtmlType = "input"
  150. // 如果是浮点型
  151. tmp := column.ColumnType
  152. start := strings.Index(tmp, "(")
  153. end := strings.Index(tmp, ")")
  154. result := "0"
  155. if start > 0 && end > 0 {
  156. result = tmp[start+1 : end]
  157. }
  158. arr := strings.Split(result, ",")
  159. if len(arr) == 2 && gconv.Int(arr[1]) > 0 {
  160. column.GoType = "float64"
  161. } else if len(arr) == 1 && gconv.Int(arr[0]) <= 10 {
  162. column.GoType = "int"
  163. } else {
  164. column.GoType = "int64"
  165. }
  166. }
  167. //新增字段
  168. if columnName == "create_by" || columnName == "create_time" || columnName == "update_by" || columnName == "update_time" {
  169. column.IsRequired = "0"
  170. column.IsInsert = "0"
  171. } else {
  172. column.IsRequired = "0"
  173. column.IsInsert = "1"
  174. if strings.Index(columnName, "name") >= 0 || strings.Index(columnName, "status") >= 0 {
  175. column.IsRequired = "1"
  176. }
  177. }
  178. // 编辑字段
  179. if gen_table_column.IsNotEdit(columnName) {
  180. if column.IsPk == "1" {
  181. column.IsEdit = "0"
  182. } else {
  183. column.IsEdit = "1"
  184. }
  185. } else {
  186. column.IsEdit = "0"
  187. }
  188. // 列表字段
  189. if gen_table_column.IsNotList(columnName) {
  190. column.IsList = "1"
  191. } else {
  192. column.IsList = "0"
  193. }
  194. // 查询字段
  195. if gen_table_column.IsNotQuery(columnName) {
  196. column.IsQuery = "1"
  197. } else {
  198. column.IsQuery = "0"
  199. }
  200. // 查询字段类型
  201. if CheckNameColumn(columnName) {
  202. column.QueryType = "LIKE"
  203. } else {
  204. column.QueryType = "EQ"
  205. }
  206. // 状态字段设置单选框
  207. if CheckStatusColumn(columnName) {
  208. column.HtmlType = "radio"
  209. } else if CheckTypeColumn(columnName) || CheckSexColumn(columnName) {
  210. // 类型&性别字段设置下拉框
  211. column.HtmlType = "select"
  212. }
  213. }
  214. //检查字段名后3位是否是sex
  215. func CheckSexColumn(columnName string) bool {
  216. if len(columnName) >= 3 {
  217. end := len(columnName)
  218. start := end - 3
  219. if start <= 0 {
  220. start = 0
  221. }
  222. if columnName[start:end] == "sex" {
  223. return true
  224. }
  225. }
  226. return false
  227. }
  228. //检查字段名后4位是否是type
  229. func CheckTypeColumn(columnName string) bool {
  230. if len(columnName) >= 4 {
  231. end := len(columnName)
  232. start := end - 4
  233. if start <= 0 {
  234. start = 0
  235. }
  236. if columnName[start:end] == "type" {
  237. return true
  238. }
  239. }
  240. return false
  241. }
  242. //检查字段名后6位是否是status
  243. func CheckStatusColumn(columnName string) bool {
  244. if len(columnName) >= 6 {
  245. end := len(columnName)
  246. start := end - 6
  247. if start <= 0 {
  248. start = 0
  249. }
  250. tmp := columnName[start:end]
  251. if tmp == "status" {
  252. return true
  253. }
  254. }
  255. return false
  256. }
  257. //检查字段名后4位是否是name
  258. func CheckNameColumn(columnName string) bool {
  259. if len(columnName) >= 4 {
  260. end := len(columnName)
  261. start := end - 4
  262. if start <= 0 {
  263. start = 0
  264. }
  265. tmp := columnName[start:end]
  266. if tmp == "name" {
  267. return true
  268. }
  269. }
  270. return false
  271. }
  272. //初始化表信息
  273. func InitTable(table *gen_table.Entity, operName string) {
  274. table.ClassName = ConvertClassName(table.TableName)
  275. table.PackageName = g.Cfg().GetString("gen.packageName")
  276. table.ModuleName = g.Cfg().GetString("gen.moduleName")
  277. table.BusinessName = GetBusinessName(table.TableName)
  278. table.FunctionName = strings.ReplaceAll(table.TableComment, "表", "")
  279. table.FunctionAuthor = g.Cfg().GetString("gen.author")
  280. table.CreateBy = operName
  281. table.TplCategory = "crud"
  282. table.CreateTime = gtime.Now()
  283. }
  284. //表名转换成类名
  285. func ConvertClassName(tableName string) string {
  286. autoRemovePre := g.Cfg().GetBool("gen.autoRemovePre")
  287. tablePrefix := g.Cfg().GetString("gen.tablePrefix")
  288. if autoRemovePre && tablePrefix != "" {
  289. searchList := strings.Split(tablePrefix, ",")
  290. for _, str := range searchList {
  291. tableName = strings.ReplaceAll(tableName, str, "")
  292. }
  293. }
  294. return tableName
  295. }
  296. //获取业务名
  297. func GetBusinessName(tableName string) string {
  298. lastIndex := strings.LastIndex(tableName, "_")
  299. nameLength := len(tableName)
  300. businessName := tableName[lastIndex+1 : nameLength]
  301. return businessName
  302. }
  303. //根据table_id查询表列数据
  304. func SelectGenTableColumnListByTableId(tableId int64) ([]*gen_table_column.Entity, error) {
  305. return gen_table_column.SelectGenTableColumnListByTableId(tableId)
  306. }
  307. func GetTableInfoByTableId(tableId int64) (info *gen_table.Entity, err error) {
  308. return gen_table.GetInfoById(tableId)
  309. }
  310. //修改表和列信息
  311. func SaveEdit(req *gen_table.EditReq) (err error) {
  312. if req == nil {
  313. err = gerror.New("参数错误")
  314. return
  315. }
  316. table, err := gen_table.FindOne("table_id=?", req.TableId)
  317. if err != nil || table == nil {
  318. err = gerror.New("数据不存在")
  319. return
  320. }
  321. if req.TableName != "" {
  322. table.TableName = req.TableName
  323. }
  324. if req.TableComment != "" {
  325. table.TableComment = req.TableComment
  326. }
  327. if req.BusinessName != "" {
  328. table.BusinessName = req.BusinessName
  329. }
  330. if req.ClassName != "" {
  331. table.ClassName = req.ClassName
  332. }
  333. if req.FunctionAuthor != "" {
  334. table.FunctionAuthor = req.FunctionAuthor
  335. }
  336. if req.FunctionName != "" {
  337. table.FunctionName = req.FunctionName
  338. }
  339. if req.ModuleName != "" {
  340. table.ModuleName = req.ModuleName
  341. }
  342. if req.PackageName != "" {
  343. table.PackageName = req.PackageName
  344. }
  345. if req.Remark != "" {
  346. table.Remark = req.Remark
  347. }
  348. if req.TplCategory != "" {
  349. table.TplCategory = req.TplCategory
  350. }
  351. if req.Params != "" {
  352. table.Options = req.Params
  353. }
  354. table.UpdateTime = gtime.Now()
  355. table.UpdateBy = req.UserName
  356. if req.TplCategory == "tree" {
  357. //树表设置options
  358. options := g.Map{
  359. "tree_code": req.TreeCode,
  360. "tree_parent_code": req.TreeParentCode,
  361. "tree_name": req.TreeName,
  362. }
  363. table.Options = gconv.String(options)
  364. } else {
  365. table.Options = ""
  366. }
  367. var tx *gdb.TX
  368. tx, err = g.DB().Begin()
  369. if err != nil {
  370. return
  371. }
  372. _, err = tx.Table(gen_table.Table).Save(table)
  373. if err != nil {
  374. tx.Rollback()
  375. return err
  376. }
  377. //保存列数据
  378. if req.Columns != "" {
  379. var j *gjson.Json
  380. if j, err = gjson.DecodeToJson([]byte(req.Columns)); err != nil {
  381. tx.Rollback()
  382. return
  383. } else {
  384. var columnList []gen_table_column.Entity
  385. err = j.ToStructs(&columnList)
  386. if err == nil && columnList != nil && len(columnList) > 0 {
  387. for _, column := range columnList {
  388. if column.ColumnId > 0 {
  389. tmp, _ := gen_table_column.FindOne("column_id=?", column.ColumnId)
  390. if tmp != nil {
  391. tmp.ColumnComment = column.ColumnComment
  392. tmp.GoType = column.GoType
  393. tmp.HtmlType = column.HtmlType
  394. tmp.QueryType = column.QueryType
  395. tmp.GoField = column.GoField
  396. tmp.DictType = column.DictType
  397. tmp.IsInsert = column.IsInsert
  398. tmp.IsEdit = column.IsEdit
  399. tmp.IsList = column.IsList
  400. tmp.IsQuery = column.IsQuery
  401. _, err = tx.Table(gen_table_column.Table).Save(tmp)
  402. if err != nil {
  403. tx.Rollback()
  404. return
  405. }
  406. }
  407. }
  408. }
  409. }
  410. }
  411. }
  412. tx.Commit()
  413. return
  414. }
  415. //删除表格
  416. func Delete(ids []int) error {
  417. tx, err := g.DB().Begin()
  418. if err != nil {
  419. g.Log().Error(err)
  420. return gerror.New("开启删除事务出错")
  421. }
  422. _, err = tx.Table(gen_table.Table).Where(gen_table.Columns.TableId+" in(?)", ids).Delete()
  423. if err != nil {
  424. g.Log().Error(err)
  425. tx.Rollback()
  426. return gerror.New("删除表格数据失败")
  427. }
  428. _, err = tx.Table(gen_table_column.Table).Where(gen_table_column.Columns.TableId+" in(?)", ids).Delete()
  429. if err != nil {
  430. g.Log().Error(err)
  431. tx.Rollback()
  432. return gerror.New("删除表格字段数据失败")
  433. }
  434. tx.Commit()
  435. return nil
  436. }
  437. func SelectRecordById(tableId int64) (entity *gen_table.EntityExtend, err error) {
  438. entity, err = gen_table.SelectRecordById(tableId)
  439. return
  440. }
  441. //设置主键列信息
  442. func SetPkColumn(table *gen_table.EntityExtend, columns []*gen_table_column.Entity) {
  443. for _, column := range columns {
  444. if column.IsPk == "1" {
  445. table.PkColumn = column
  446. break
  447. }
  448. }
  449. if table.PkColumn == nil {
  450. table.PkColumn = columns[0]
  451. }
  452. }