eta_business_contract.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package eta_business
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "strings"
  6. "time"
  7. )
  8. // EtaBusinessContract ETA商家合同表
  9. type EtaBusinessContract struct {
  10. EtaBusinessContractId int `orm:"column(eta_business_contract_id);pk"`
  11. EtaBusinessId int `description:"ETA商家ID"`
  12. SigningTime time.Time `description:"签约时间"`
  13. ExpiredTime time.Time `description:"到期时间"`
  14. IsFirst int `description:"是否为首份签约"`
  15. CreateTime time.Time `description:"创建时间"`
  16. ModifyTime time.Time `description:"更新时间"`
  17. }
  18. func (m *EtaBusinessContract) TableName() string {
  19. return "eta_business_contract"
  20. }
  21. func (m *EtaBusinessContract) PrimaryId() string {
  22. return EtaBusinessContractColumns.EtaBusinessContractId
  23. }
  24. var EtaBusinessContractColumns = struct {
  25. EtaBusinessContractId string
  26. EtaBusinessId string
  27. SigningTime string
  28. ExpiredTime string
  29. IsFirst string
  30. CreateTime string
  31. ModifyTime string
  32. }{
  33. EtaBusinessContractId: "eta_business_contract_id",
  34. EtaBusinessId: "eta_business_id",
  35. SigningTime: "signing_time",
  36. ExpiredTime: "expired_time",
  37. IsFirst: "is_first",
  38. CreateTime: "create_time",
  39. ModifyTime: "modify_time",
  40. }
  41. func (m *EtaBusinessContract) Create() (err error) {
  42. o := orm.NewOrm()
  43. id, err := o.Insert(m)
  44. if err != nil {
  45. return
  46. }
  47. m.EtaBusinessContractId = int(id)
  48. return
  49. }
  50. func (m *EtaBusinessContract) CreateMulti(items []*EtaBusinessContract) (err error) {
  51. if len(items) == 0 {
  52. return
  53. }
  54. o := orm.NewOrm()
  55. _, err = o.InsertMulti(len(items), items)
  56. return
  57. }
  58. func (m *EtaBusinessContract) Update(cols []string) (err error) {
  59. o := orm.NewOrm()
  60. _, err = o.Update(m, cols...)
  61. return
  62. }
  63. func (m *EtaBusinessContract) Del() (err error) {
  64. o := orm.NewOrm()
  65. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  66. _, err = o.Raw(sql, m.EtaBusinessContractId).Exec()
  67. return
  68. }
  69. func (m *EtaBusinessContract) GetItemById(id int) (item *EtaBusinessContract, err error) {
  70. o := orm.NewOrm()
  71. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  72. err = o.Raw(sql, id).QueryRow(&item)
  73. return
  74. }
  75. func (m *EtaBusinessContract) GetItemByCondition(condition string, pars []interface{}) (err error) {
  76. o := orm.NewOrm()
  77. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
  78. err = o.Raw(sql, pars).QueryRow(&m)
  79. return
  80. }
  81. func (m *EtaBusinessContract) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  82. o := orm.NewOrm()
  83. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  84. err = o.Raw(sql, pars).QueryRow(&count)
  85. return
  86. }
  87. func (m *EtaBusinessContract) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EtaBusinessContract, err error) {
  88. o := orm.NewOrm()
  89. fields := strings.Join(fieldArr, ",")
  90. if len(fieldArr) == 0 {
  91. fields = `*`
  92. }
  93. order := `ORDER BY create_time DESC`
  94. if orderRule != "" {
  95. order = ` ORDER BY ` + orderRule
  96. }
  97. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  98. _, err = o.Raw(sql, pars).QueryRows(&items)
  99. return
  100. }
  101. // CreateMaybeUpdateFirst 新增签约合同, 或同时更新首次签约合同
  102. func (m *EtaBusinessContract) CreateMaybeUpdateFirst(item *EtaBusinessContract, changeFirst bool) (err error) {
  103. // 直接新增
  104. o := orm.NewOrm()
  105. if !changeFirst {
  106. id, e := o.Insert(item)
  107. if e != nil {
  108. return e
  109. }
  110. item.EtaBusinessContractId = int(id)
  111. return
  112. }
  113. // 更新签约合同
  114. tx, e := o.Begin()
  115. if e != nil {
  116. return e
  117. }
  118. defer func() {
  119. if err != nil {
  120. _ = tx.Rollback()
  121. return
  122. }
  123. _ = tx.Commit()
  124. }()
  125. id, e := tx.Insert(item)
  126. if e != nil {
  127. err = fmt.Errorf("contract insert err: %s", e.Error())
  128. return
  129. }
  130. item.EtaBusinessContractId = int(id)
  131. sql := fmt.Sprintf(`UPDATE %s SET %s = 0 WHERE %s = ? AND %s <> ?`, m.TableName(), EtaBusinessContractColumns.IsFirst, EtaBusinessContractColumns.EtaBusinessId, EtaBusinessContractColumns.EtaBusinessContractId)
  132. _, e = tx.Raw(sql, item.EtaBusinessId, item.EtaBusinessContractId).Exec()
  133. if e != nil {
  134. err = fmt.Errorf("update first contract err: %s", e.Error())
  135. return
  136. }
  137. return
  138. }
  139. // UpdateMaybeUpdateFirst 编辑签约合同, 或同时更新首次签约合同
  140. func (m *EtaBusinessContract) UpdateMaybeUpdateFirst(item *EtaBusinessContract, changeFirst bool, updateCols []string) (err error) {
  141. // 无需修改首次签约则直接更新
  142. o := orm.NewOrm()
  143. if !changeFirst {
  144. e := item.Update(updateCols)
  145. if e != nil {
  146. return e
  147. }
  148. return
  149. }
  150. // 更新首次签约合同
  151. tx, e := o.Begin()
  152. if e != nil {
  153. return e
  154. }
  155. defer func() {
  156. if err != nil {
  157. _ = tx.Rollback()
  158. return
  159. }
  160. _ = tx.Commit()
  161. }()
  162. _, e = tx.Update(item, updateCols...)
  163. if e != nil {
  164. err = fmt.Errorf("contract update err: %s", e.Error())
  165. return
  166. }
  167. sql := fmt.Sprintf(`UPDATE %s SET %s = 0 WHERE %s = ? AND %s <> ?`, m.TableName(), EtaBusinessContractColumns.IsFirst, EtaBusinessContractColumns.EtaBusinessId, EtaBusinessContractColumns.EtaBusinessContractId)
  168. _, e = tx.Raw(sql, item.EtaBusinessId, item.EtaBusinessContractId).Exec()
  169. if e != nil {
  170. err = fmt.Errorf("update first contract err: %s", e.Error())
  171. return
  172. }
  173. return
  174. }
  175. // EtaBusinessContractItem 商家签约信息
  176. type EtaBusinessContractItem struct {
  177. EtaBusinessContractId int
  178. EtaBusinessId int `description:"ETA商家ID"`
  179. SigningTime string `description:"签约时间"`
  180. ExpiredTime string `description:"到期时间"`
  181. ExpireDay string `description:"到期天数"`
  182. Using bool `description:"是否当前合约"`
  183. }