resouce.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. package dao
  2. import (
  3. "context"
  4. xsql "database/sql"
  5. "fmt"
  6. "strconv"
  7. "strings"
  8. "go-common/app/admin/main/vip/model"
  9. "go-common/library/database/sql"
  10. "github.com/pkg/errors"
  11. )
  12. // SelBatchCodeCount .
  13. func (d *Dao) SelBatchCodeCount(c context.Context, arg *model.ArgBatchCode) (n int64, err error) {
  14. autoSQLStr := d.batchCodeAutoArg(arg)
  15. row := d.db.QueryRow(c, _selBatchCodeCountSQL+autoSQLStr)
  16. if err = row.Scan(&n); err != nil {
  17. if err == sql.ErrNoRows {
  18. n = 0
  19. err = nil
  20. return
  21. }
  22. err = errors.WithStack(err)
  23. return
  24. }
  25. return
  26. }
  27. func (d *Dao) selBatchCodeIDs(c context.Context, poolID, businessID, batchID int64) (ids []int64, err error) {
  28. var rows *sql.Rows
  29. autoSQL := d.batchCodeAutoArg(&model.ArgBatchCode{
  30. BusinessID: businessID,
  31. PoolID: poolID,
  32. ID: batchID,
  33. })
  34. if rows, err = d.db.Query(c, _selBatchCodeIDSQL+autoSQL); err != nil {
  35. err = errors.WithStack(err)
  36. d.errProm.Incr("db_query")
  37. return
  38. }
  39. for rows.Next() {
  40. var r int64
  41. if err = rows.Scan(&r); err != nil {
  42. err = errors.WithStack(err)
  43. ids = nil
  44. d.errProm.Incr("db_scan")
  45. return
  46. }
  47. ids = append(ids, r)
  48. }
  49. return
  50. }
  51. func (d *Dao) codeAutoArgSQL(arg *model.ArgCode) string {
  52. autoSQLStr := ""
  53. if len(arg.BatchCodeIDs) > 0 {
  54. idStr := ""
  55. for _, v := range arg.BatchCodeIDs {
  56. idStr += "," + strconv.Itoa(int(v))
  57. }
  58. autoSQLStr += fmt.Sprintf(" AND batch_code_id in(%s) ", idStr[1:])
  59. }
  60. if arg.Status != 0 {
  61. autoSQLStr += fmt.Sprintf(" AND status = %v ", arg.Status)
  62. }
  63. if arg.UseStartTime > 0 && arg.UseEndTime > 0 {
  64. autoSQLStr += fmt.Sprintf(" AND use_time >='%v' AND use_time <= '%v'", arg.UseStartTime.Time().Format("2006-01-02 15:04:05"), arg.UseEndTime.Time().Format("2006-01-02 15:04:05"))
  65. }
  66. if len(arg.Code) > 0 {
  67. autoSQLStr += fmt.Sprintf(" AND code = '%v' ", arg.Code)
  68. }
  69. if arg.Mid != 0 {
  70. autoSQLStr += fmt.Sprintf(" AND mid = %v ", arg.Mid)
  71. }
  72. return autoSQLStr
  73. }
  74. // SelCode .
  75. func (d *Dao) SelCode(c context.Context, arg *model.ArgCode, cursor int64, ps int) (res []*model.ResourceCode, err error) {
  76. var batchIDs []int64
  77. if batchIDs, err = d.selBatchCodeIDs(c, arg.PoolID, arg.BusinessID, arg.BatchCodeID); err != nil {
  78. err = errors.WithStack(err)
  79. return
  80. }
  81. arg.BatchCodeIDs = batchIDs
  82. argSQL := d.codeAutoArgSQL(arg)
  83. if ps <= 0 || ps > 2000 {
  84. ps = _defps
  85. }
  86. var rows *sql.Rows
  87. argSQL += fmt.Sprintf(" AND id>%v LIMIT %v", cursor, ps)
  88. if rows, err = d.db.Query(c, _selCodeSQL+argSQL); err != nil {
  89. err = errors.WithStack(err)
  90. return
  91. }
  92. for rows.Next() {
  93. r := new(model.ResourceCode)
  94. if err = rows.Scan(&r.ID, &r.BatchCodeID, &r.Status, &r.Code, &r.Mid, &r.UseTime, &r.Ctime); err != nil {
  95. err = errors.WithStack(err)
  96. res = nil
  97. return
  98. }
  99. res = append(res, r)
  100. }
  101. return
  102. }
  103. // batchCodeAutoArg .
  104. func (d *Dao) batchCodeAutoArg(arg *model.ArgBatchCode) string {
  105. autoSQLStr := ""
  106. if arg.BusinessID > 0 {
  107. autoSQLStr += fmt.Sprintf(" AND business_id=%v ", arg.BusinessID)
  108. }
  109. if arg.PoolID > 0 {
  110. autoSQLStr += fmt.Sprintf(" AND pool_id = %v ", arg.PoolID)
  111. }
  112. if arg.ID > 0 {
  113. autoSQLStr += fmt.Sprintf(" AND id = %v ", arg.ID)
  114. }
  115. if len(arg.Name) > 0 {
  116. autoSQLStr += " AND batch_name like '%" + arg.Name + "%'"
  117. }
  118. if arg.Status > 0 {
  119. autoSQLStr += fmt.Sprintf(" AND status = %v", arg.Status)
  120. }
  121. if arg.StartTime > 0 && arg.EndTime > 0 {
  122. autoSQLStr += fmt.Sprintf(" AND ctime >='%v' AND ctime <= '%v'", arg.StartTime.Time().Format("2006-01-02 15:04:05"), arg.EndTime.Time().Format("2006-01-02 15:04:05"))
  123. }
  124. return autoSQLStr
  125. }
  126. // SelBatchCodes .
  127. func (d *Dao) SelBatchCodes(c context.Context, batchIds []int64) (res []*model.BatchCode, err error) {
  128. var (
  129. rows *sql.Rows
  130. ids []string
  131. )
  132. if len(batchIds) <= 0 {
  133. return
  134. }
  135. for _, v := range batchIds {
  136. ids = append(ids, fmt.Sprintf("%v", v))
  137. }
  138. if rows, err = d.db.Query(c, fmt.Sprintf(_selBatchCodesSQL, strings.Join(ids, ","))); err != nil {
  139. err = errors.WithStack(err)
  140. return
  141. }
  142. for rows.Next() {
  143. r := new(model.BatchCode)
  144. if err = rows.Scan(&r.ID, &r.BusinessID, &r.PoolID, &r.Status, &r.Type, &r.LimitDay, &r.MaxCount, &r.BatchName, &r.Reason, &r.Unit, &r.Count, &r.SurplusCount, &r.Price, &r.StartTime, &r.EndTime, &r.Contacts, &r.ContactsNumber, &r.Ctime); err != nil {
  145. err = errors.WithStack(err)
  146. res = nil
  147. return
  148. }
  149. res = append(res, r)
  150. }
  151. return
  152. }
  153. // SelBatchCode .
  154. func (d *Dao) SelBatchCode(c context.Context, arg *model.ArgBatchCode, pn, ps int) (res []*model.BatchCode, err error) {
  155. var rows *sql.Rows
  156. autoSQLStr := d.batchCodeAutoArg(arg)
  157. if pn <= 0 {
  158. pn = _defpn
  159. }
  160. if ps <= 0 || ps > _maxps {
  161. ps = _defps
  162. }
  163. autoSQLStr += fmt.Sprintf(" LIMIT %v,%v", (pn-1)*ps, ps)
  164. if rows, err = d.db.Query(c, _selBatchCodeSQL+autoSQLStr); err != nil {
  165. err = errors.WithStack(err)
  166. return
  167. }
  168. defer rows.Close()
  169. for rows.Next() {
  170. r := new(model.BatchCode)
  171. if err = rows.Scan(&r.ID, &r.BusinessID, &r.PoolID, &r.Status, &r.Type, &r.LimitDay, &r.MaxCount, &r.BatchName, &r.Reason, &r.Unit, &r.Count, &r.SurplusCount, &r.Price, &r.StartTime, &r.EndTime, &r.Contacts, &r.ContactsNumber, &r.Ctime); err != nil {
  172. err = errors.WithStack(err)
  173. res = nil
  174. return
  175. }
  176. res = append(res, r)
  177. }
  178. return
  179. }
  180. // SelBatchCodeName .
  181. func (d *Dao) SelBatchCodeName(c context.Context, name string) (r *model.BatchCode, err error) {
  182. row := d.db.QueryRow(c, _selBatchCodeByNameSQL, name)
  183. r = new(model.BatchCode)
  184. if err = row.Scan(&r.ID, &r.BusinessID, &r.PoolID, &r.Status, &r.Type, &r.LimitDay, &r.MaxCount, &r.BatchName, &r.Reason, &r.Unit, &r.Count, &r.SurplusCount, &r.Price, &r.StartTime, &r.EndTime, &r.Contacts, &r.ContactsNumber, &r.Ctime); err != nil {
  185. if err == sql.ErrNoRows {
  186. r = nil
  187. err = nil
  188. fmt.Printf("this is %+v", err)
  189. return
  190. }
  191. err = errors.WithStack(err)
  192. return
  193. }
  194. return
  195. }
  196. // SelBatchCodeID .
  197. func (d *Dao) SelBatchCodeID(c context.Context, batchCodeID int64) (r *model.BatchCode, err error) {
  198. row := d.db.QueryRow(c, _selBatchCodeByIDSQL, batchCodeID)
  199. r = new(model.BatchCode)
  200. if err = row.Scan(&r.ID, &r.BusinessID, &r.PoolID, &r.Status, &r.Type, &r.LimitDay, &r.MaxCount, &r.BatchName, &r.Reason, &r.Unit, &r.Count, &r.SurplusCount, &r.Price, &r.StartTime, &r.EndTime, &r.Contacts, &r.ContactsNumber, &r.Ctime); err != nil {
  201. if err == sql.ErrNoRows {
  202. r = nil
  203. err = nil
  204. return
  205. }
  206. err = errors.WithStack(err)
  207. return
  208. }
  209. return
  210. }
  211. // SelCodeID .
  212. func (d *Dao) SelCodeID(c context.Context, codeID int64) (r *model.ResourceCode, err error) {
  213. row := d.db.QueryRow(c, _selCodeByIDSQL, codeID)
  214. r = new(model.ResourceCode)
  215. if err = row.Scan(&r.ID, &r.BatchCodeID, &r.Status, &r.Code, &r.Mid, &r.UseTime, &r.Ctime); err != nil {
  216. if err == sql.ErrNoRows {
  217. r = nil
  218. err = nil
  219. }
  220. err = errors.WithStack(err)
  221. return
  222. }
  223. return
  224. }
  225. // TxAddBatchCode .
  226. func (d *Dao) TxAddBatchCode(tx *sql.Tx, bc *model.BatchCode) (ID int64, err error) {
  227. var result xsql.Result
  228. if result, err = tx.Exec(_addBatchCodeSQL, bc.BusinessID, bc.PoolID, bc.Status, bc.BatchName, bc.Reason, bc.Unit, bc.Count, bc.SurplusCount, bc.Price, bc.StartTime, bc.EndTime, bc.Contacts, bc.ContactsNumber, bc.Type, bc.LimitDay, bc.MaxCount, bc.Operator); err != nil {
  229. err = errors.WithStack(err)
  230. return
  231. }
  232. if ID, err = result.LastInsertId(); err != nil {
  233. err = errors.WithStack(err)
  234. return
  235. }
  236. return
  237. }
  238. // UpdateBatchCode .
  239. func (d *Dao) UpdateBatchCode(c context.Context, bc *model.BatchCode) (eff int64, err error) {
  240. var result xsql.Result
  241. if result, err = d.db.Exec(c, _updateBatchCodeSQL, &bc.Status, &bc.BatchName, &bc.Reason, &bc.Price, &bc.Contacts, &bc.ContactsNumber, &bc.Type, &bc.LimitDay, &bc.MaxCount, &bc.Operator, &bc.ID); err != nil {
  242. err = errors.WithStack(err)
  243. return
  244. }
  245. if eff, err = result.RowsAffected(); err != nil {
  246. err = errors.WithStack(err)
  247. return
  248. }
  249. return
  250. }
  251. // UpdateCode .
  252. func (d *Dao) UpdateCode(c context.Context, codeID int64, status int8) (eff int64, err error) {
  253. var result xsql.Result
  254. if result, err = d.db.Exec(c, _updateCodeSQL, status, codeID); err != nil {
  255. err = errors.WithStack(err)
  256. return
  257. }
  258. if eff, err = result.RowsAffected(); err != nil {
  259. err = errors.WithStack(err)
  260. return
  261. }
  262. return
  263. }
  264. // BatchAddCode .
  265. func (d *Dao) BatchAddCode(tx *sql.Tx, codes []*model.ResourceCode) (err error) {
  266. values := make([]string, 0)
  267. for _, v := range codes {
  268. s := fmt.Sprintf("('%v','%v','%v')", v.BatchCodeID, v.Status, v.Code)
  269. values = append(values, s)
  270. }
  271. valuesStr := strings.Join(values, ",")
  272. if _, err = tx.Exec(_batchAddCodeSQL + valuesStr); err != nil {
  273. err = errors.WithStack(err)
  274. return
  275. }
  276. return
  277. }