fmd-last-seq-no-get.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package models_func
  2. import (
  3. "errors"
  4. "github.com/dabory/abango-rest"
  5. e "github.com/dabory/abango-rest/etc"
  6. )
  7. // 이것 안쓰는 펑션이지만 다른것에 사용 가능하므로 일단 내비둠.
  8. // func LastSeqNoGet(y *abango.Controller, table string, idName string, hdId int) (int, error) {
  9. // seqNo := 0
  10. // sql := " select MAX(seq_no) as last_seq_no from " + locals.Tpf + table
  11. // sql += " where " + idName + " = " + e.NumToStr(hdId)
  12. // sql += " group by " + idName
  13. // if rs, err := y.Db.Query(sql); err == nil { //레코드가 없으면 넣어놓은 seqNo 는 0으로 유지 된다.
  14. // for _, val := range rs {
  15. // for _, val := range val {
  16. // seqNo, _ = strconv.Atoi(string(val))
  17. // // fmt.Println(val, string(val))
  18. // }
  19. // }
  20. // e.FuncRun("wqerfvdfgherh", e.CurrFuncName())
  21. // return seqNo + 1, nil //마지막번호에서 1을 더한다.
  22. // } else {
  23. // return 0, errors.New(e.FuncRunErr("owqrkmpofae", e.CurrFuncName()+err.Error()))
  24. // }
  25. // }
  26. type SeqNoUpDownReq struct {
  27. BdTableName string
  28. HdIdName string
  29. HdId int
  30. CurrId int
  31. Move string
  32. QueryCnt int64
  33. }
  34. type SeqNoUpDownFuncSet struct {
  35. SeqNoUpDownReq
  36. CurrSeqNo int
  37. Page []SeqNoUpDownSingle
  38. }
  39. type SeqNoUpDownSingle struct {
  40. Id int
  41. SeqNo int
  42. }
  43. func (t *SeqNoUpDownFuncSet) SeqNoUpDown(y *abango.Controller) error {
  44. //NextVersion Deprecates
  45. // tblName := t.BdTableName
  46. // if strings.Contains(t.BdTableName, "main_") || strings.Contains(t.BdTableName, "pro_") {
  47. // tblName = t.BdTableName
  48. // } else {
  49. // tblName = "dbr_" + t.BdTableName
  50. // }
  51. qry := *y.Db.
  52. Table(t.BdTableName).
  53. Select("id, seq_no").
  54. Where(t.HdIdName + "=" + e.NumToStr(t.HdId)).
  55. Asc("seq_no")
  56. if err := qry.Find(&t.Page); err == nil {
  57. // if cnt, err := qry.FindAndCount(&t.Page); err == nil {
  58. // t.QueryCnt = cnt
  59. e.FuncRun("wpkjcapqjd", e.CurrFuncName())
  60. // return nil //마지막번호에서 1을 더한다.
  61. } else {
  62. return errors.New(e.PageQryErr("dfgdresgfsf", e.CurrFuncName()+err.Error()))
  63. }
  64. currKey := 0
  65. currSeqNo := 0
  66. for key, r := range t.Page {
  67. if r.Id == t.CurrId {
  68. currKey = key
  69. currSeqNo = r.SeqNo
  70. break
  71. }
  72. }
  73. if currSeqNo == 0 {
  74. return errors.New(e.PageQryErr("otjnsaefvq", " SeqNo does NOT exist. Check HdId/CurrID !"))
  75. }
  76. targetId := 0
  77. targetSeqNo := 0
  78. if t.Move == "up" {
  79. if currKey == 0 {
  80. return errors.New(e.PageQryErr("otjnsaefvq", "Current SeqNo is the first one "))
  81. } else {
  82. targetId = t.Page[currKey-1].Id
  83. targetSeqNo = t.Page[currKey-1].SeqNo
  84. }
  85. } else if t.Move == "down" {
  86. if currKey == len(t.Page)-1 {
  87. return errors.New(e.PageQryErr("iueytbwerfas", "Current SeqNo is the last one "))
  88. } else {
  89. targetId = t.Page[currKey+1].Id
  90. targetSeqNo = t.Page[currKey+1].SeqNo
  91. }
  92. } else {
  93. return errors.New(e.PageQryErr("klkfefasfwcv", "Value of Move should be \"up\" or \"down\" "))
  94. }
  95. sess := y.Db.NewSession()
  96. if err := sess.Begin(); err == nil {
  97. defer sess.Close()
  98. } else {
  99. return errors.New(e.PageQryErr("otyhjnsefㄹㅇ", "Transaction Action Failed "))
  100. }
  101. strSql := "update " + t.BdTableName + " set seq_no = ? where id = ? "
  102. if _, err := y.Db.Exec(strSql, currSeqNo, targetId); err != nil {
  103. return errors.New(e.FuncRunErr("nbhserfvaew", e.CurrFuncName()+err.Error()))
  104. }
  105. if _, err := y.Db.Exec(strSql, targetSeqNo, t.CurrId); err != nil {
  106. return errors.New(e.FuncRunErr("oyujxerfsew", e.CurrFuncName()+err.Error()))
  107. }
  108. if err := sess.Commit(); err == nil {
  109. t.CurrSeqNo = currSeqNo
  110. e.FuncRun("ptervafhhf", e.CurrFuncName())
  111. return nil
  112. } else {
  113. return errors.New(e.FuncRunErr("oyujxerfsew", "Transaction Commit Failed"))
  114. }
  115. }