auth.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. package dao
  2. import (
  3. "demo/internal/setting"
  4. "demo/internal/util"
  5. "fmt"
  6. "github.com/patrickmn/go-cache"
  7. "github.com/pkg/errors"
  8. "github.com/siddontang/go/log"
  9. "math"
  10. "strconv"
  11. "strings"
  12. "unsafe"
  13. )
  14. var (
  15. Apisql_cache *cache.Cache
  16. )
  17. func (d *Dao) CheckUser(username, passwd string) (bool, error) {
  18. return d.db.Table("user").Where("username = ? ", username).And(" password = ? ", util.EncodeMD5(passwd)).Exist()
  19. }
  20. func (d *Dao) GetSqlByNameDBT(sqlname string) (string, string, error) {
  21. apiList, err := d.db.Table("apisql").Where("sqlname = ?", sqlname).QueryString()
  22. if err != nil {
  23. err = errors.Wrap(err, "GetSqlByNameDBT-db-error")
  24. log.Error(err)
  25. return "", "", err
  26. }
  27. sqlstr := ""
  28. p := ""
  29. for _, api := range apiList {
  30. sqlstr = api["sqlstr"]
  31. p = api["params"]
  32. }
  33. return sqlstr, p, nil
  34. }
  35. func (d *Dao) QueryByListT(sqlstr string, offset, count int, params []interface{}) (interface{}, error) {
  36. if strings.Contains(sqlstr, ";") {
  37. return nil, errors.New("can not have ;")
  38. }
  39. if count == 0 {
  40. count = 1000
  41. }
  42. recordcount := int64(0)
  43. if count > 1 {
  44. tt, err := d.db.SQL("SELECT COUNT(*) as count FROM ("+sqlstr+") a", params...).QueryInterface()
  45. if err != nil {
  46. return nil, err
  47. }
  48. switch tt[0]["count"].(type) {
  49. case int64:
  50. recordcount = tt[0]["count"].(int64)
  51. case string:
  52. temp, _ := strconv.Atoi(tt[0]["count"].(string))
  53. recordcount = int64(temp)
  54. }
  55. }
  56. queryData := make([]map[string]interface{}, 1)
  57. var err error
  58. if count == 0 {
  59. queryData, err = d.db.SQL(sqlstr, params...).QueryInterface()
  60. } else {
  61. if count > 0 && offset > 0 {
  62. offset = offset * count
  63. }
  64. queryData, err = d.db.SQL(sqlstr, params...).Limit(count, offset).QueryInterface()
  65. }
  66. if err != nil {
  67. return nil, err
  68. }
  69. columnsname := make([]string, 0)
  70. listdata := make(map[string][]interface{})
  71. if len(queryData) > 0 {
  72. for i, _ := range queryData {
  73. if i == 0 {
  74. for i, _ := range queryData[0] {
  75. columnsname = append(columnsname, i)
  76. }
  77. }
  78. for _, v := range columnsname {
  79. listdata[v] = append(listdata[v], queryData[i][v])
  80. }
  81. }
  82. recordcount = int64(len(queryData))
  83. }
  84. data := make(map[string]interface{})
  85. data["lists"] = listdata
  86. //data["columnsname"] = columnsname
  87. data["total"] = recordcount
  88. data["code"] = 0
  89. data["message"] = "ok"
  90. if count > 0 {
  91. data["pages"] = math.Ceil(float64(recordcount) / float64(count))
  92. data["pageNum"] = math.Ceil(float64(offset+1) / float64(count))
  93. }
  94. data["pageSize"] = count
  95. return data, nil
  96. }
  97. func (d *Dao) QueryByListListT(sqlstr string, offset, count int, params []interface{}) (interface{}, error) {
  98. if strings.Contains(sqlstr, ";") {
  99. return nil, errors.New("can not have ;")
  100. }
  101. if count == 0 {
  102. count = 1000
  103. }
  104. recordcount := int64(0)
  105. if count > 1 {
  106. tt, err := d.db.SQL("SELECT COUNT(*) as count FROM ("+sqlstr+") a", params...).QueryInterface()
  107. if err != nil {
  108. return nil, err
  109. }
  110. switch tt[0]["count"].(type) {
  111. case int64:
  112. recordcount = tt[0]["count"].(int64)
  113. case string:
  114. temp, _ := strconv.Atoi(tt[0]["count"].(string))
  115. recordcount = int64(temp)
  116. }
  117. }
  118. queryData := make([]map[string]interface{}, 1)
  119. var err error
  120. if count == 0 {
  121. queryData, err = d.db.SQL(sqlstr, params...).QueryInterface()
  122. } else {
  123. if count > 0 && offset > 0 {
  124. offset = offset * count
  125. }
  126. queryData, err = d.db.SQL(sqlstr, params...).Limit(count, offset).QueryInterface()
  127. }
  128. if err != nil {
  129. return nil, err
  130. }
  131. columnsname := make([]string, 0)
  132. listdata := make([]interface{}, 0)
  133. if len(queryData) > 0 {
  134. for i, _ := range queryData {
  135. if i == 0 {
  136. for i, _ := range queryData[0] {
  137. columnsname = append(columnsname, i)
  138. }
  139. }
  140. listdatarow := make([]interface{}, 0)
  141. for _, v := range columnsname {
  142. listdatarow = append(listdatarow, queryData[i][v])
  143. }
  144. listdata = append(listdata, listdatarow)
  145. }
  146. recordcount = int64(len(queryData))
  147. }
  148. data := make(map[string]interface{})
  149. data["list"] = listdata
  150. data["columnsname"] = columnsname
  151. data["total"] = recordcount
  152. data["code"] = 0
  153. data["message"] = "ok"
  154. if count > 0 {
  155. data["pages"] = math.Ceil(float64(recordcount) / float64(count))
  156. data["pageNum"] = math.Ceil(float64(offset+1) / float64(count))
  157. }
  158. data["pageSize"] = count
  159. return data, nil
  160. }
  161. func (d *Dao) QueryByMapT(sqlstr string, offset, count int, params []interface{}) (interface{}, error) {
  162. if strings.Contains(sqlstr, ";") {
  163. return nil, errors.New("can not have ;")
  164. }
  165. recordcount := int64(0)
  166. if count > 1 {
  167. tt, err := d.db.SQL("SELECT COUNT(*) as count FROM ("+sqlstr+") a", params...).Count()
  168. if err != nil {
  169. fmt.Println("s1", err)
  170. return nil, err
  171. }
  172. recordcount = tt
  173. //if tt == nil {
  174. // recordcount = 0
  175. //} else {
  176. // switch tt[0]["count"].(type) {
  177. // case int64:
  178. // recordcount = tt[0]["count"].(int64)
  179. // case string:
  180. // temp, _ := strconv.Atoi(tt[0]["count"].(string))
  181. // recordcount = int64(temp)
  182. //
  183. // }
  184. //
  185. //}
  186. }
  187. queryData := make([]map[string]string, 0)
  188. var err error
  189. if count == 0 {
  190. queryData, err = d.db.SQL(sqlstr, params...).QueryString()
  191. } else {
  192. offset := (offset - 1) * count
  193. queryData, err = d.db.SQL(fmt.Sprintf("%s limit %d,%d", sqlstr, offset, count), params...).QueryString()
  194. }
  195. //fmt.Println("count, offset==================",count, offset)
  196. if err != nil {
  197. return nil, err
  198. }
  199. if recordcount == 0 {
  200. recordcount = int64(len(queryData))
  201. }
  202. data := make(map[string]interface{})
  203. data["list"] = queryData
  204. data["total"] = recordcount
  205. data["code"] = 0
  206. data["message"] = "ok"
  207. if count > 0 {
  208. data["pages"] = math.Ceil(float64(recordcount) / float64(count))
  209. data["pageNum"] = math.Ceil(float64(offset+1) / float64(count))
  210. }
  211. data["pageSize"] = count
  212. return data, nil
  213. }
  214. func (d *Dao) QueryByMap(sqlstr string, offset, count int, params []interface{}) (interface{}, error) {
  215. recordcount := int64(0)
  216. if count > 1 {
  217. tt, err := d.db.SQL("SELECT COUNT(*) as count FROM ("+sqlstr+") a", params...).QueryInterface()
  218. if err != nil {
  219. fmt.Println("s1", err)
  220. return nil, err
  221. }
  222. if tt == nil {
  223. recordcount = 0
  224. } else {
  225. recordcount = tt[0]["count"].(int64)
  226. }
  227. }
  228. queryData := make([]map[string]string, 1)
  229. var err error
  230. if count == 0 {
  231. queryData, err = d.db.SQL(sqlstr, params...).QueryString()
  232. } else {
  233. if count > 0 && offset > 0 {
  234. offset = offset * count
  235. }
  236. queryData, err = d.db.SQL(sqlstr, params...).Limit(count, offset).QueryString()
  237. }
  238. //fmt.Println("count, offset==================",count, offset)
  239. if err != nil {
  240. return nil, err
  241. }
  242. if recordcount == 0 {
  243. recordcount = int64(len(queryData))
  244. }
  245. data := make(map[string]interface{})
  246. data["list"] = queryData
  247. data["total"] = recordcount
  248. data["code"] = 0
  249. data["message"] = "ok"
  250. if count > 0 {
  251. data["pages"] = math.Ceil(float64(recordcount) / float64(count))
  252. data["pageNum"] = math.Ceil(float64(offset+1) / float64(count))
  253. }
  254. data["pageSize"] = count
  255. return data, nil
  256. }
  257. func (d *Dao) ExecQueryT(sqlstr string, params []interface{}) (interface{}, error) {
  258. //if err := sqlCheckParam(sqlstr); err != nil {
  259. // return 0, err
  260. //}
  261. var args []interface{}
  262. args = append(args, sqlstr)
  263. args = append(args, params...)
  264. rows, err := d.db.Exec(args...)
  265. if err != nil {
  266. log.Error(err)
  267. return nil, err
  268. }
  269. LastInsertId, err := rows.LastInsertId()
  270. idPointer := (*int)(unsafe.Pointer(&LastInsertId))
  271. idd16 := *idPointer
  272. data := make(map[string]interface{})
  273. RowsAffected, _ := rows.RowsAffected()
  274. _idPointer := (*int)(unsafe.Pointer(&RowsAffected))
  275. _idd16 := *_idPointer
  276. data["LastInsertId"] = strconv.Itoa(idd16)
  277. data["LastInsertIdStr"] = idd16
  278. data["RowsAffected"] = _idd16
  279. data["result"] = rows
  280. return data, nil
  281. }
  282. func (d *Dao) GetUserInfo(username string) map[string]interface{} {
  283. valuesMap, err := d.db.SQL("SELECT * FROM `user` WHERE `username` = ? and `enable` >0 ", username).QueryString()
  284. if err != nil {
  285. return nil
  286. }
  287. resp := make(map[string]interface{})
  288. if len(valuesMap) > 0 {
  289. valuesMap2, err := d.db.SQL("SELECT r.`name`,r.`id` FROM `user` u JOIN `role` r ON r.id=u.`roleid` WHERE r.`enable`>0 AND u.username=?", username).QueryString()
  290. if err == nil {
  291. resp["role"] = valuesMap2
  292. }
  293. for k, v := range valuesMap[0] {
  294. resp[k] = v
  295. }
  296. return resp
  297. }
  298. return nil
  299. }
  300. func (d *Dao) GetSqlByNameDB(name string) (string, string) {
  301. sqls := ""
  302. param := ""
  303. //Apisql_cache.Get(name)
  304. //fmt.Println(setting.DatabaseSetting.CacheApiSql)
  305. //if sql, ok := Apisql_cache.Get(name); ok && setting.DatabaseSetting.CacheApiSql {
  306. // tempstrb := strings.IndexAny(sql.(string), "|")
  307. // sqls = sql.(string)[0:tempstrb]
  308. // param = sql.(string)[tempstrb+1:]
  309. //} else {
  310. valuesMap, err := d.db.SQL("SELECT sqlstr,params FROM apisql WHERE sqlname = ? AND ENABLE>0 ", name).QueryString()
  311. if err == nil && len(valuesMap) > 0 {
  312. sqls = valuesMap[0]["sqlstr"]
  313. param = valuesMap[0]["params"]
  314. //Apisql_cache.Set(name, (sqls + "|" + param), cache.DefaultExpiration)
  315. }
  316. //}
  317. if setting.DatabaseSetting.ShowGetSqllog {
  318. fmt.Println("==============")
  319. fmt.Println("apisql:" + sqls)
  320. fmt.Println("--------------")
  321. fmt.Println("param:" + param)
  322. fmt.Println("==============")
  323. }
  324. return sqls, param
  325. }
  326. func (d *Dao) QueryByMapMenu(sqlstr string, offset, count int, params []interface{}) ([]map[string]interface{}, error) {
  327. if offset < 0 {
  328. offset = 0
  329. }
  330. if count <= 0 {
  331. count = 0
  332. }
  333. if count > 0 {
  334. sqlstr += " limit " + strconv.Itoa(offset) + "," + strconv.Itoa(count)
  335. }
  336. rows, err := d.db.SQL(sqlstr, params...).QueryString()
  337. if err != nil {
  338. return nil, err
  339. }
  340. //defer rows.Close()
  341. //columnsType, _ := rows.ColumnTypes()
  342. //columnsLen := len(columnsType)
  343. //columns, _ := rows.Columns()
  344. //queryData := make([]map[string]interface{}, 0)
  345. //values := make([]interface{}, columnsLen)
  346. //valuePtrs := make([]interface{}, columnsLen)
  347. //
  348. //for rows.Next() {
  349. // for a := 0; a < columnsLen; a++ {
  350. // valuePtrs[a] = &values[a]
  351. // }
  352. // rows.Scan(valuePtrs...)
  353. // entry := make(map[string]interface{})
  354. // for i, col := range columns {
  355. // entry[col] = getValue(values[i], columnsType[i].DatabaseTypeName())
  356. // }
  357. // queryData = append(queryData, entry)
  358. //}
  359. dataList := make([]map[string]interface{}, 0)
  360. for _, row := range rows {
  361. data := make(map[string]interface{}, 0)
  362. for k, v := range row {
  363. data[k] = v
  364. }
  365. dataList = append(dataList, data)
  366. }
  367. //data := rows
  368. return dataList, nil
  369. //return Engine.SQL(sqlstr, params...).Query().List()
  370. }