o2o公共业务代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.8 KiB

  1. package db
  2. import (
  3. zhios_o2o_business_logx "code.fnuoos.com/go_rely_warehouse/zyos_go_o2o_business.git/utils/logx"
  4. "database/sql"
  5. _ "github.com/go-sql-driver/mysql" //必须导入mysql驱动,否则会panic
  6. "xorm.io/xorm"
  7. )
  8. var Db *xorm.Engine
  9. /********************************************* 公用方法 *********************************************/
  10. // 数据批量插入
  11. func DbInsertBatch(Db *xorm.Engine, m ...interface{}) error {
  12. if len(m) == 0 {
  13. return nil
  14. }
  15. id, err := Db.Insert(m...)
  16. if id == 0 || err != nil {
  17. return zhios_o2o_business_logx.Warn("cannot insert data :", err)
  18. }
  19. return nil
  20. }
  21. // QueryNativeString 查询原生sql
  22. func QueryNativeString(Db *xorm.Engine, sql string, args ...interface{}) ([]map[string]string, error) {
  23. results, err := Db.SQL(sql, args...).QueryString()
  24. return results, err
  25. }
  26. // UpdateComm common update
  27. func UpdateComm(Db *xorm.Engine, id interface{}, model interface{}) (int64, error) {
  28. row, err := Db.ID(id).Update(model)
  29. return row, err
  30. }
  31. // InsertComm common insert
  32. func InsertComm(Db *xorm.Engine, model interface{}) (int64, error) {
  33. row, err := Db.InsertOne(model)
  34. return row, err
  35. }
  36. // GetComm
  37. // payload *model
  38. // return *model,has,err
  39. func GetComm(Db *xorm.Engine, model interface{}) (interface{}, bool, error) {
  40. has, err := Db.Get(model)
  41. if err != nil {
  42. _ = zhios_o2o_business_logx.Warn(err)
  43. return nil, false, err
  44. }
  45. return model, has, nil
  46. }
  47. // InsertCommWithSession common insert
  48. func InsertCommWithSession(session *xorm.Session, model interface{}) (int64, error) {
  49. row, err := session.InsertOne(model)
  50. return row, err
  51. }
  52. // ExecuteOriginalSql 执行原生sql
  53. func ExecuteOriginalSql(session *xorm.Session, sql string) (sql.Result, error) {
  54. result, err := session.Exec(sql)
  55. if err != nil {
  56. _ = zhios_o2o_business_logx.Warn(err)
  57. return nil, err
  58. }
  59. return result, nil
  60. }