business.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/admin/main/reply/model"
  5. "go-common/library/database/sql"
  6. )
  7. const (
  8. _inBussinessSQL = "INSERT INTO business (type, name, appkey, remark, alias) VALUES (?,?,?,?,?)"
  9. _upBussinessSQL = "UPDATE business SET name=?, appkey=?, remark=?, alias=? WHERE type=?"
  10. _upBussinessSteteSQL = "UPDATE business SET state=? WHERE type=?"
  11. _selBussinessSQL = "SELECT type, name, appkey, remark, alias FROM business WHERE state=?"
  12. _selOneBusinessSQL = "SELECT type, name, appkey, remark, alias FROM business WHERE type=?"
  13. )
  14. // InBusiness insert a business record
  15. func (dao *Dao) InBusiness(c context.Context, tp int32, name, appkey, remark, alias string) (id int64, err error) {
  16. res, err := dao.db.Exec(c, _inBussinessSQL, tp, name, appkey, remark, alias)
  17. if err != nil {
  18. return
  19. }
  20. return res.LastInsertId()
  21. }
  22. // UpBusiness update business by type
  23. func (dao *Dao) UpBusiness(c context.Context, name, appkey, remark, alias string, tp int32) (id int64, err error) {
  24. res, err := dao.db.Exec(c, _upBussinessSQL, name, appkey, remark, alias, tp)
  25. if err != nil {
  26. return
  27. }
  28. return res.RowsAffected()
  29. }
  30. // UpBusinessState logical delete a business record by set state to StateDelete
  31. func (dao *Dao) UpBusinessState(c context.Context, state, tp int32) (id int64, err error) {
  32. res, err := dao.db.Exec(c, _upBussinessSteteSQL, state, tp)
  33. if err != nil {
  34. return
  35. }
  36. return res.RowsAffected()
  37. }
  38. // Business return one business instance by type
  39. func (dao *Dao) Business(c context.Context, tp int32) (business *model.Business, err error) {
  40. row := dao.db.QueryRow(c, _selOneBusinessSQL, tp)
  41. business = new(model.Business)
  42. err = row.Scan(&business.Type, &business.Name, &business.Appkey, &business.Remark, &business.Alias)
  43. if err != nil {
  44. if err == sql.ErrNoRows {
  45. err = nil
  46. business = nil
  47. }
  48. }
  49. return
  50. }
  51. // ListBusiness Gets gets all business records
  52. func (dao *Dao) ListBusiness(c context.Context, state int32) (business []*model.Business, err error) {
  53. rows, err := dao.db.Query(c, _selBussinessSQL, state)
  54. if err != nil {
  55. return
  56. }
  57. defer rows.Close()
  58. business = make([]*model.Business, 0)
  59. for rows.Next() {
  60. b := new(model.Business)
  61. if err = rows.Scan(&b.Type, &b.Name, &b.Appkey, &b.Remark, &b.Alias); err != nil {
  62. return
  63. }
  64. business = append(business, b)
  65. }
  66. err = rows.Err()
  67. return
  68. }