function-models-table.go 6.1 KB

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