function-models-table.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package models_table
  2. import (
  3. "fmt"
  4. "reflect"
  5. "strings"
  6. "time"
  7. "github.com/dabory/abango-rest"
  8. e "github.com/dabory/abango-rest/etc"
  9. "github.com/go-xorm/xorm"
  10. "github.com/google/uuid"
  11. )
  12. func ComAddaRowDefault(y *abango.Controller, table interface{}) {
  13. fe := reflect.ValueOf(table).Elem()
  14. fieldNum := fe.NumField()
  15. for i := 0; i < fieldNum; i++ {
  16. tag := string(fe.Type().Field(i).Tag)
  17. name := fe.Type().Field(i).Name
  18. value := fe.Field(i)
  19. if strings.Contains(tag, "DECIMAL") && value.String() == "" {
  20. fe.Field(i).SetString("0.0000")
  21. } else if name == "CreatedOn" {
  22. fe.Field(i).SetInt(e.GetNowUnix())
  23. } else if name == "OfficialDate" {
  24. fe.Field(i).SetString((time.Now().Format("20060102")))
  25. } else if name == "OfficialTime" {
  26. fe.Field(i).SetString((time.Now().Format("15:04:05")))
  27. } else if name == "IsUnused" && value.String() == "" {
  28. fe.Field(i).SetString("0")
  29. } else if name == "Status" && value.String() == "" {
  30. fe.Field(i).SetString("0")
  31. } else if name == "Sort" && value.String() == "" {
  32. fe.Field(i).SetString("0")
  33. } else if name == "Ip" {
  34. fe.Field(i).SetString(y.Gtb.RemoteIp)
  35. } else if name == "UserId" && y.Gtb.UserId != 0 {
  36. fe.Field(i).SetInt(int64(y.Gtb.UserId))
  37. } else if name == "MemberId" && y.Gtb.MemberId != 0 {
  38. fe.Field(i).SetInt(int64(y.Gtb.MemberId))
  39. } else if name == "StorageId" {
  40. fe.Field(i).SetInt(int64(y.Gtb.StorageId))
  41. } else if name == "BranchId" {
  42. fe.Field(i).SetInt(int64(y.Gtb.BranchId))
  43. } else if name == "MemberBuyerId" && y.Gtb.MemberBuyerId != 0 {
  44. fe.Field(i).SetInt(int64(y.Gtb.MemberBuyerId))
  45. } else if name == "BuyerId" && value.String() == "" && y.Gtb.MemberBuyerId != 0 { //Pro에서 MemberBuyerId 지정되어 있으면
  46. fe.Field(i).SetInt(int64(y.Gtb.MemberBuyerId))
  47. } else if name == "ItemCode" && value.String() == "" { //UniqKey가 지정되어 있으면
  48. fe.Field(i).SetString(e.RandString(16))
  49. //ForeignId에 1 을 넣는 조건
  50. } else if name == "Duid" && value.String() == "" { //UniqKey가 지정되어 있으면
  51. uid := uuid.New()
  52. fe.Field(i).SetString(uid.String())
  53. }
  54. //redundunt but to make sure to have no 0 value in Ids
  55. if (strings.Contains(name, "MediaId") || name == "MemberId" || name == "UserId" || name == "BuyerId" || name == "SupplierId" || name == "IgroupId" || name == "CgroupId" || name == "BranchId" || name == "StorageId" || name == "AgroupId") && value.Int() == 0 {
  56. fe.Field(i).SetInt(int64(1))
  57. }
  58. if name == "FromBuyerId" && value.Int() == 0 {
  59. fe.Field(i).SetInt(int64(1))
  60. }
  61. if name == "FromBuyerId" && value.Int() == 0 {
  62. fmt.Println("FromBuyerId:", value.Int())
  63. }
  64. }
  65. }
  66. func EmptyTableColumn(table interface{}) { // 정리해서 연결할 것.
  67. fmt.Println("aaaa")
  68. fe := reflect.ValueOf(table).Elem()
  69. fmt.Println("bbbb")
  70. fieldNum := fe.NumField()
  71. fmt.Println("cccc")
  72. for i := 0; i < fieldNum; i++ {
  73. name := fe.Type().Field(i).Name
  74. if name == "Password" || name == "ActivateCode" || name == "ResetCode" {
  75. fe.Field(i).SetString("")
  76. }
  77. }
  78. }
  79. func ComEditaRowDefault(y *abango.Controller, table interface{}) {
  80. fe := reflect.ValueOf(table).Elem()
  81. fieldNum := fe.NumField()
  82. for i := 0; i < fieldNum; i++ {
  83. tag := string(fe.Type().Field(i).Tag)
  84. name := fe.Type().Field(i).Name
  85. value := fe.Field(i)
  86. if strings.Contains(tag, "DECIMAL") && value.String() == "" {
  87. fe.Field(i).SetString("0.0000")
  88. } else if name == "UpdatedOn" {
  89. fe.Field(i).SetInt(e.GetNowUnix())
  90. } else if name == "OfficialDate" && value.String() == "" { //직접수정시에는 변경가능
  91. fe.Field(i).SetString((time.Now().Format("20060102")))
  92. } else if name == "OfficialTime" && value.String() == "" { //직접수정시에는 변경가능
  93. fe.Field(i).SetString((time.Now().Format("15:04:05")))
  94. } else if name == "UserId" && value.String() == "" { // 수정한 관리자 입력
  95. fe.Field(i).SetInt(int64(y.Gtb.UserId))
  96. } else if name == "MemberId" && value.String() == "" { // 수정한 회원 입력
  97. fe.Field(i).SetInt(int64(y.Gtb.MemberId))
  98. } else if name == "MemberBuyerId" && value.String() == "" { // 수정한 회원 입력
  99. fe.Field(i).SetInt(int64(y.Gtb.MemberBuyerId))
  100. } else if name == "IsUnused" && value.String() == "" {
  101. fe.Field(i).SetString("0")
  102. } else if name == "Status" && value.String() == "" {
  103. fe.Field(i).SetString("0")
  104. } else if name == "Sort" && value.String() == "" {
  105. fe.Field(i).SetString("0")
  106. } else if name == "Ip" { //최종업데이트한 Ip가 들어간다.
  107. fe.Field(i).SetString(y.Gtb.RemoteIp)
  108. }
  109. if (strings.Contains(name, "MediaId") || name == "MemberId" || name == "UserId" || name == "BuyerId" || name == "SupplierId" || name == "IgroupId" || name == "CgroupId" || name == "BranchId" || name == "StorageId" || name == "AgroupId") && value.Int() == 0 {
  110. fe.Field(i).SetInt(int64(1))
  111. }
  112. }
  113. }
  114. func ComUpdateQry(y *abango.Controller, id int) *xorm.Session {
  115. qry := y.Db.Id(id)
  116. if y.UpdateFieldList != "" {
  117. slc := strings.Split(y.UpdateFieldList, ",")
  118. for _, str := range slc {
  119. qry = qry.Cols(str)
  120. }
  121. } else { //UpdateFieldList가 비었으면 전체 컬럼선텍
  122. qry = qry.AllCols()
  123. }
  124. return qry
  125. }