lib.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. package lib
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/nats-io/nats.go"
  6. "math/rand"
  7. "os"
  8. "path/filepath"
  9. "runtime"
  10. "sync"
  11. "sort"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. var Run_My_Server = false // 运行当期服务
  17. // var DeviceRealSnMap map[string]int /*创建集合 */
  18. var DeviceRealSnMap *sync.Map /*创建集合 */
  19. var Nats *nats.Conn
  20. func init() {
  21. //DeviceRealSnMap = make(map[string]int)
  22. DeviceRealSnMap = new(sync.Map)
  23. }
  24. type JSONS struct {
  25. //必须的大写开头
  26. Code int16
  27. Msg string
  28. Data interface{} // 泛型
  29. }
  30. type R_JSONS_List struct {
  31. //必须的大写开头
  32. Data []interface{}
  33. Num int64
  34. Page int
  35. Page_size int
  36. }
  37. type R_JSONS struct {
  38. //必须的大写开头
  39. Data interface{}
  40. Num int64
  41. Page int
  42. Page_size int
  43. }
  44. type R1_JSONS struct {
  45. //必须的大写开头
  46. List interface{}
  47. Num int
  48. Page int
  49. Page_size int
  50. Pages []Page_T
  51. }
  52. // 登录验证
  53. //func Verification(GetCookie string, GetString string) (bool, Account.Admin) {
  54. // Run_My_Server = true // 运行当期服务
  55. // // 自适应 参数
  56. // User_tokey := GetCookie
  57. // if len(User_tokey) == 0 {
  58. // User_tokey = GetString
  59. // }
  60. // if len(User_tokey) == 0 {
  61. // return false, Account.Admin{}
  62. // }
  63. // tokey, is := Account.Redis_Tokey_Get(User_tokey)
  64. // T_uuid := strings.Split(tokey, "|")[0]
  65. // if !is {
  66. // return false, Account.Admin{}
  67. // }
  68. // var err error
  69. // admin_r, err := Account.Read_Admin_ByUuid(T_uuid)
  70. // if err != nil {
  71. // return false, Account.Admin{}
  72. // }
  73. // Admin_r = &admin_r
  74. // log.Println("登录 Admin_name 为:", Admin_r.T_name)
  75. // return true, admin_r
  76. //}
  77. // func_page 分页 [{3 1} {4 2} {4 3} {4 4} {4 5} {4 6} {4 7} {4 8} {4 9} {5 2}]-
  78. type Page_T struct {
  79. A int
  80. V int64
  81. }
  82. func Func_page(Page int64, Page_size int64) (page_t_list []Page_T) {
  83. if Page > 1 {
  84. page_t_list = append(page_t_list, Page_T{A: 1, V: Page - 1})
  85. }
  86. i := int64(0)
  87. for aa := int64(1); aa < 5; aa++ {
  88. if Page-aa <= 0 {
  89. break
  90. }
  91. page_t_list = append(page_t_list, Page_T{A: 2, V: Page - aa})
  92. i++
  93. }
  94. page_t_list = append(page_t_list, Page_T{A: 3, V: Page})
  95. for aa := int64(1); aa < 10-i; aa++ {
  96. if Page_size < Page+aa {
  97. break
  98. }
  99. page_t_list = append(page_t_list, Page_T{A: 4, V: Page + aa})
  100. }
  101. sort.Slice(page_t_list, func(i, j int) bool {
  102. if page_t_list[i].V < page_t_list[j].V {
  103. return true
  104. }
  105. return false
  106. })
  107. sort.Slice(page_t_list, func(i, j int) bool {
  108. if page_t_list[i].A < page_t_list[j].A {
  109. return true
  110. }
  111. return false
  112. })
  113. if Page < Page_size {
  114. page_t_list = append(page_t_list, Page_T{A: 5, V: Page + 1})
  115. }
  116. return page_t_list
  117. }
  118. func Strval(value interface{}) string {
  119. var key string
  120. if value == nil {
  121. return key
  122. }
  123. switch value.(type) {
  124. case float64:
  125. ft := value.(float64)
  126. key = strconv.FormatFloat(ft, 'f', -1, 64)
  127. case float32:
  128. ft := value.(float32)
  129. key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
  130. case int:
  131. it := value.(int)
  132. key = strconv.Itoa(it)
  133. case uint:
  134. it := value.(uint)
  135. key = strconv.Itoa(int(it))
  136. case int8:
  137. it := value.(int8)
  138. key = strconv.Itoa(int(it))
  139. case uint8:
  140. it := value.(uint8)
  141. key = strconv.Itoa(int(it))
  142. case int16:
  143. it := value.(int16)
  144. key = strconv.Itoa(int(it))
  145. case uint16:
  146. it := value.(uint16)
  147. key = strconv.Itoa(int(it))
  148. case int32:
  149. it := value.(int32)
  150. key = strconv.Itoa(int(it))
  151. case uint32:
  152. it := value.(uint32)
  153. key = strconv.Itoa(int(it))
  154. case int64:
  155. it := value.(int64)
  156. key = strconv.FormatInt(it, 10)
  157. case uint64:
  158. it := value.(uint64)
  159. key = strconv.FormatUint(it, 10)
  160. case string:
  161. key = value.(string)
  162. case []byte:
  163. key = string(value.([]byte))
  164. default:
  165. newValue, _ := json.Marshal(value)
  166. key = string(newValue)
  167. }
  168. return key
  169. }
  170. func To_int(value interface{}) int {
  171. var key int
  172. if value == nil {
  173. return key
  174. }
  175. switch value.(type) {
  176. case float64:
  177. key = int(value.(float64))
  178. case float32:
  179. key = int(value.(float32))
  180. case int:
  181. key = int(value.(int))
  182. case uint:
  183. key = int(value.(uint))
  184. case int8:
  185. key = int(value.(int8))
  186. case uint8:
  187. key = int(value.(uint8))
  188. case int16:
  189. key = int(value.(int16))
  190. case uint16:
  191. key = int(value.(uint16))
  192. case int32:
  193. key = int(value.(int32))
  194. case uint32:
  195. key = int(value.(uint32))
  196. case int64:
  197. key = int(value.(int64))
  198. case uint64:
  199. key = int(value.(uint64))
  200. case string:
  201. key, _ = strconv.Atoi(value.(string))
  202. case []byte:
  203. key, _ = strconv.Atoi(string(value.([]byte)))
  204. default:
  205. newValue, _ := json.Marshal(value)
  206. key, _ = strconv.Atoi(string(newValue))
  207. }
  208. return key
  209. }
  210. func To_float32(value interface{}) float32 {
  211. var key float32
  212. if value == nil {
  213. return key
  214. }
  215. switch value.(type) {
  216. case float64:
  217. key = float32(value.(float64))
  218. case float32:
  219. key = float32(value.(float32))
  220. case int:
  221. key = float32(value.(int))
  222. case uint:
  223. key = float32(value.(uint))
  224. case int8:
  225. key = float32(value.(int8))
  226. case uint8:
  227. key = float32(value.(uint8))
  228. case int16:
  229. key = float32(value.(int16))
  230. case uint16:
  231. key = float32(value.(uint16))
  232. case int32:
  233. key = float32(value.(int32))
  234. case uint32:
  235. key = float32(value.(uint32))
  236. case int64:
  237. key = float32(value.(int64))
  238. case uint64:
  239. key = float32(value.(uint64))
  240. case string:
  241. key_float64, _ := strconv.ParseFloat(value.(string), 32/64)
  242. key = float32(key_float64)
  243. case []byte:
  244. key_float64, _ := strconv.ParseFloat(string(value.([]byte)), 32/64)
  245. key = float32(key_float64)
  246. default:
  247. newValue, _ := json.Marshal(value)
  248. key_float64, _ := strconv.ParseFloat(string(newValue), 32/64)
  249. key = float32(key_float64)
  250. }
  251. key_float64, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", key), 32/64)
  252. key = float32(key_float64)
  253. return key
  254. }
  255. func To_string(value interface{}) string {
  256. var key string
  257. if value == nil {
  258. return key
  259. }
  260. switch value.(type) {
  261. case float64:
  262. ft := value.(float64)
  263. key = strconv.FormatFloat(ft, 'f', -1, 64)
  264. case float32:
  265. ft := value.(float32)
  266. key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
  267. case int:
  268. it := value.(int)
  269. key = strconv.Itoa(it)
  270. case uint:
  271. it := value.(uint)
  272. key = strconv.Itoa(int(it))
  273. case int8:
  274. it := value.(int8)
  275. key = strconv.Itoa(int(it))
  276. case uint8:
  277. it := value.(uint8)
  278. key = strconv.Itoa(int(it))
  279. case int16:
  280. it := value.(int16)
  281. key = strconv.Itoa(int(it))
  282. case uint16:
  283. it := value.(uint16)
  284. key = strconv.Itoa(int(it))
  285. case int32:
  286. it := value.(int32)
  287. key = strconv.Itoa(int(it))
  288. case uint32:
  289. it := value.(uint32)
  290. key = strconv.Itoa(int(it))
  291. case int64:
  292. it := value.(int64)
  293. key = strconv.FormatInt(it, 10)
  294. case uint64:
  295. it := value.(uint64)
  296. key = strconv.FormatUint(it, 10)
  297. case string:
  298. key = value.(string)
  299. case []byte:
  300. key = string(value.([]byte))
  301. default:
  302. newValue, _ := json.Marshal(value)
  303. key = string(newValue)
  304. }
  305. return key
  306. }
  307. func Float32_to_string(value *float32) string {
  308. var key string
  309. if value == nil {
  310. return key
  311. }
  312. return fmt.Sprintf("%.1f", *value)
  313. }
  314. func Random(min, max int) int {
  315. rand.Seed(time.Now().Unix()) //Seed生成的随机数
  316. return rand.Intn(max-min) + min
  317. }
  318. // 取文本(字符串)中间
  319. func GetBetweenStr(str, start, end string) string {
  320. n := strings.Index(str, start)
  321. if n == -1 {
  322. n = 0
  323. } else {
  324. n = n + len(start) // 增加了else,不加的会把start带上
  325. }
  326. str = string([]byte(str)[n:])
  327. m := strings.Index(str, end)
  328. if m == -1 {
  329. m = len(str)
  330. }
  331. str = string([]byte(str)[:m])
  332. return str
  333. }
  334. // getYearMonthToDay 查询指定年份指定月份有多少天
  335. // @params year int 指定年份
  336. // @params month int 指定月份
  337. func GetYearMonthToDay(year int, month int) int {
  338. // 有31天的月份
  339. day31 := map[int]bool{
  340. 1: true,
  341. 3: true,
  342. 5: true,
  343. 7: true,
  344. 8: true,
  345. 10: true,
  346. 12: true,
  347. }
  348. if day31[month] == true {
  349. return 31
  350. }
  351. // 有30天的月份
  352. day30 := map[int]bool{
  353. 4: true,
  354. 6: true,
  355. 9: true,
  356. 11: true,
  357. }
  358. if day30[month] == true {
  359. return 30
  360. }
  361. // 计算是平年还是闰年
  362. if (year%4 == 0 && year%100 != 0) || year%400 == 0 {
  363. // 得出2月的天数
  364. return 29
  365. }
  366. // 得出2月的天数
  367. return 28
  368. }
  369. func Decimal(value float64) float64 {
  370. value, _ = strconv.ParseFloat(fmt.Sprintf("%.1f", value), 64)
  371. return value
  372. }
  373. func Strconv_Atoi(string string) int {
  374. int, _ := strconv.Atoi(string)
  375. return int
  376. }
  377. // golang获取程序运行路径
  378. func GetCurrentDirectory() string {
  379. dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
  380. return strings.Replace(dir, "\\", "/", -1)
  381. }
  382. // 获取正在运行的函数名
  383. func FuncName() string {
  384. pc := make([]uintptr, 1)
  385. runtime.Callers(2, pc)
  386. f := runtime.FuncForPC(pc[0])
  387. return f.Name()
  388. }
  389. // 获取两个时间相差的天数,0表同一天,正数表t1>t2,负数表t1<t2
  390. func GetDiffDays(t1, t2 time.Time) int {
  391. t1 = time.Date(t1.Year(), t1.Month(), t1.Day(), 0, 0, 0, 0, time.Local)
  392. t2 = time.Date(t2.Year(), t2.Month(), t2.Day(), 0, 0, 0, 0, time.Local)
  393. return int(t1.Sub(t2).Hours() / 24)
  394. }
  395. func FindStrListMissingData(data1, data2 []string) []string {
  396. set1 := make(map[string]struct{})
  397. set2 := make(map[string]struct{})
  398. for _, item := range data1 {
  399. set1[item] = struct{}{}
  400. }
  401. for _, item := range data2 {
  402. set2[item] = struct{}{}
  403. }
  404. var missing []string
  405. for item := range set1 {
  406. if _, found := set2[item]; !found {
  407. missing = append(missing, item)
  408. }
  409. }
  410. return missing
  411. }
  412. func FindStrListExtraData(data1, data2 []string) []string {
  413. set1 := make(map[string]struct{})
  414. set2 := make(map[string]struct{})
  415. for _, item := range data1 {
  416. set1[item] = struct{}{}
  417. }
  418. for _, item := range data2 {
  419. set2[item] = struct{}{}
  420. }
  421. var extra []string
  422. for item := range set2 {
  423. if _, found := set1[item]; !found {
  424. extra = append(extra, item)
  425. }
  426. }
  427. return extra
  428. }
  429. func StrListEqual(slice1, slice2 []string) bool {
  430. if len(slice1) != len(slice2) {
  431. return false
  432. }
  433. for i := range slice1 {
  434. if slice1[i] != slice2[i] {
  435. return false
  436. }
  437. }
  438. return true
  439. }