lib.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. package lib
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/signintech/gopdf"
  6. "math/rand"
  7. "os"
  8. "path/filepath"
  9. "sort"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. var Run_My_Server = false // 运行当期服务
  15. type Cl_ struct {
  16. Uuid_list map[string]string // 泛型
  17. }
  18. var CountrySnMap map[string]Cl_ /*创建集合 */
  19. var CountryRead_DeviceParameterSnMap map[string]string /*创建集合 */
  20. func init() {
  21. CountrySnMap = make(map[string]Cl_)
  22. CountryRead_DeviceParameterSnMap = make(map[string]string)
  23. }
  24. type JSONS struct {
  25. //必须的大写开头
  26. Code int16
  27. Msg string
  28. Data interface{} // 泛型
  29. }
  30. type R_JSONS struct {
  31. //必须的大写开头
  32. List interface{}
  33. Num int
  34. Page int
  35. Page_size int
  36. Pages []Page_T
  37. }
  38. type R_JSONS_s struct {
  39. //必须的大写开头
  40. List []interface{}
  41. Num int
  42. Page int
  43. Page_size int
  44. Pages []Page_T
  45. }
  46. type LOG_JSONS struct {
  47. //必须的大写开头
  48. Class_1 string
  49. Class_List interface{}
  50. List interface{}
  51. Num int
  52. Page int
  53. Page_size int
  54. Pages []Page_T
  55. }
  56. // func_page 分页 [{3 1} {4 2} {4 3} {4 4} {4 5} {4 6} {4 7} {4 8} {4 9} {5 2}]-
  57. type Page_T struct {
  58. A int
  59. V int64
  60. }
  61. func Func_page(Page int64, Page_size int64) (page_t_list []Page_T) {
  62. if Page > 1 {
  63. page_t_list = append(page_t_list, Page_T{A: 1, V: Page - 1})
  64. }
  65. i := int64(0)
  66. for aa := int64(1); aa < 5; aa++ {
  67. if Page-aa <= 0 {
  68. break
  69. }
  70. page_t_list = append(page_t_list, Page_T{A: 2, V: Page - aa})
  71. i++
  72. }
  73. page_t_list = append(page_t_list, Page_T{A: 3, V: Page})
  74. for aa := int64(1); aa < 10-i; aa++ {
  75. if Page_size < Page+aa {
  76. break
  77. }
  78. page_t_list = append(page_t_list, Page_T{A: 4, V: Page + aa})
  79. }
  80. sort.Slice(page_t_list, func(i, j int) bool {
  81. if page_t_list[i].V < page_t_list[j].V {
  82. return true
  83. }
  84. return false
  85. })
  86. sort.Slice(page_t_list, func(i, j int) bool {
  87. if page_t_list[i].A < page_t_list[j].A {
  88. return true
  89. }
  90. return false
  91. })
  92. if Page < Page_size {
  93. page_t_list = append(page_t_list, Page_T{A: 5, V: Page + 1})
  94. }
  95. return page_t_list
  96. }
  97. func Strval(value interface{}) string {
  98. var key string
  99. if value == nil {
  100. return key
  101. }
  102. switch value.(type) {
  103. case float64:
  104. ft := value.(float64)
  105. key = strconv.FormatFloat(ft, 'f', -1, 64)
  106. case float32:
  107. ft := value.(float32)
  108. key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
  109. case int:
  110. it := value.(int)
  111. key = strconv.Itoa(it)
  112. case uint:
  113. it := value.(uint)
  114. key = strconv.Itoa(int(it))
  115. case int8:
  116. it := value.(int8)
  117. key = strconv.Itoa(int(it))
  118. case uint8:
  119. it := value.(uint8)
  120. key = strconv.Itoa(int(it))
  121. case int16:
  122. it := value.(int16)
  123. key = strconv.Itoa(int(it))
  124. case uint16:
  125. it := value.(uint16)
  126. key = strconv.Itoa(int(it))
  127. case int32:
  128. it := value.(int32)
  129. key = strconv.Itoa(int(it))
  130. case uint32:
  131. it := value.(uint32)
  132. key = strconv.Itoa(int(it))
  133. case int64:
  134. it := value.(int64)
  135. key = strconv.FormatInt(it, 10)
  136. case uint64:
  137. it := value.(uint64)
  138. key = strconv.FormatUint(it, 10)
  139. case string:
  140. key = value.(string)
  141. case []byte:
  142. key = string(value.([]byte))
  143. default:
  144. newValue, _ := json.Marshal(value)
  145. key = string(newValue)
  146. }
  147. return key
  148. }
  149. func To_int(value interface{}) int {
  150. var key int
  151. if value == nil {
  152. return key
  153. }
  154. switch value.(type) {
  155. case float64:
  156. key = int(value.(float64))
  157. case float32:
  158. key = int(value.(float32))
  159. case int:
  160. key = int(value.(int))
  161. case uint:
  162. key = int(value.(uint))
  163. case int8:
  164. key = int(value.(int8))
  165. case uint8:
  166. key = int(value.(uint8))
  167. case int16:
  168. key = int(value.(int16))
  169. case uint16:
  170. key = int(value.(uint16))
  171. case int32:
  172. key = int(value.(int32))
  173. case uint32:
  174. key = int(value.(uint32))
  175. case int64:
  176. key = int(value.(int64))
  177. case uint64:
  178. key = int(value.(uint64))
  179. case string:
  180. key, _ = strconv.Atoi(value.(string))
  181. case []byte:
  182. key, _ = strconv.Atoi(string(value.([]byte)))
  183. default:
  184. newValue, _ := json.Marshal(value)
  185. key, _ = strconv.Atoi(string(newValue))
  186. }
  187. return key
  188. }
  189. func To_float32(value interface{}) float32 {
  190. var key float32
  191. if value == nil {
  192. return key
  193. }
  194. switch value.(type) {
  195. case float64:
  196. key = float32(value.(float64))
  197. case float32:
  198. key = float32(value.(float32))
  199. case int:
  200. key = float32(value.(int))
  201. case uint:
  202. key = float32(value.(uint))
  203. case int8:
  204. key = float32(value.(int8))
  205. case uint8:
  206. key = float32(value.(uint8))
  207. case int16:
  208. key = float32(value.(int16))
  209. case uint16:
  210. key = float32(value.(uint16))
  211. case int32:
  212. key = float32(value.(int32))
  213. case uint32:
  214. key = float32(value.(uint32))
  215. case int64:
  216. key = float32(value.(int64))
  217. case uint64:
  218. key = float32(value.(uint64))
  219. case string:
  220. key_float64, _ := strconv.ParseFloat(value.(string), 32/64)
  221. key = float32(key_float64)
  222. case []byte:
  223. key_float64, _ := strconv.ParseFloat(string(value.([]byte)), 32/64)
  224. key = float32(key_float64)
  225. default:
  226. newValue, _ := json.Marshal(value)
  227. key_float64, _ := strconv.ParseFloat(string(newValue), 32/64)
  228. key = float32(key_float64)
  229. }
  230. key_float64, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", key), 32/64)
  231. key = float32(key_float64)
  232. return key
  233. }
  234. func To_string(value interface{}) string {
  235. var key string
  236. if value == nil {
  237. return key
  238. }
  239. switch value.(type) {
  240. case float64:
  241. ft := value.(float64)
  242. key = strconv.FormatFloat(ft, 'f', -1, 64)
  243. case float32:
  244. ft := value.(float32)
  245. key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
  246. case int:
  247. it := value.(int)
  248. key = strconv.Itoa(it)
  249. case uint:
  250. it := value.(uint)
  251. key = strconv.Itoa(int(it))
  252. case int8:
  253. it := value.(int8)
  254. key = strconv.Itoa(int(it))
  255. case uint8:
  256. it := value.(uint8)
  257. key = strconv.Itoa(int(it))
  258. case int16:
  259. it := value.(int16)
  260. key = strconv.Itoa(int(it))
  261. case uint16:
  262. it := value.(uint16)
  263. key = strconv.Itoa(int(it))
  264. case int32:
  265. it := value.(int32)
  266. key = strconv.Itoa(int(it))
  267. case uint32:
  268. it := value.(uint32)
  269. key = strconv.Itoa(int(it))
  270. case int64:
  271. it := value.(int64)
  272. key = strconv.FormatInt(it, 10)
  273. case uint64:
  274. it := value.(uint64)
  275. key = strconv.FormatUint(it, 10)
  276. case string:
  277. key = value.(string)
  278. case []byte:
  279. key = string(value.([]byte))
  280. default:
  281. newValue, _ := json.Marshal(value)
  282. key = string(newValue)
  283. }
  284. return key
  285. }
  286. func Random(min, max int) int {
  287. rand.Seed(time.Now().Unix()) //Seed生成的随机数
  288. return rand.Intn(max-min) + min
  289. }
  290. //取文本(字符串)中间
  291. func GetBetweenStr(str, start, end string) string {
  292. n := strings.Index(str, start)
  293. if n == -1 {
  294. n = 0
  295. } else {
  296. n = n + len(start) // 增加了else,不加的会把start带上
  297. }
  298. str = string([]byte(str)[n:])
  299. m := strings.Index(str, end)
  300. if m == -1 {
  301. m = len(str)
  302. }
  303. str = string([]byte(str)[:m])
  304. return str
  305. }
  306. // getYearMonthToDay 查询指定年份指定月份有多少天
  307. // @params year int 指定年份
  308. // @params month int 指定月份
  309. func GetYearMonthToDay(year int, month int) int {
  310. // 有31天的月份
  311. day31 := map[int]bool{
  312. 1: true,
  313. 3: true,
  314. 5: true,
  315. 7: true,
  316. 8: true,
  317. 10: true,
  318. 12: true,
  319. }
  320. if day31[month] == true {
  321. return 31
  322. }
  323. // 有30天的月份
  324. day30 := map[int]bool{
  325. 4: true,
  326. 6: true,
  327. 9: true,
  328. 11: true,
  329. }
  330. if day30[month] == true {
  331. return 30
  332. }
  333. // 计算是平年还是闰年
  334. if (year%4 == 0 && year%100 != 0) || year%400 == 0 {
  335. // 得出2月的天数
  336. return 29
  337. }
  338. // 得出2月的天数
  339. return 28
  340. }
  341. const (
  342. AlignLeft = 4
  343. AlignCenter = 5
  344. AlignRight = 6
  345. )
  346. const (
  347. ValignTop = 1
  348. ValignMiddle = 2
  349. ValignBottom = 3
  350. )
  351. func RectFillColor(pdf *gopdf.GoPdf,
  352. text string,
  353. fontSize int,
  354. x, y, w, h float64,
  355. r, g, b uint8,
  356. align, valign int,
  357. ) {
  358. pdf.SetLineWidth(0.1)
  359. pdf.SetFillColor(r, g, b) //setup fill color
  360. pdf.SetLineType("") // 线条样式
  361. pdf.RectFromUpperLeftWithStyle(x, y, w, h, "FD")
  362. pdf.SetFillColor(0, 0, 0)
  363. if align == AlignCenter {
  364. textw, _ := pdf.MeasureTextWidth(text)
  365. x = x + (w / 2) - (textw / 2)
  366. } else if align == AlignRight {
  367. textw, _ := pdf.MeasureTextWidth(text)
  368. x = x + w - textw
  369. }
  370. pdf.SetX(x)
  371. if valign == ValignMiddle {
  372. y = y + (h / 2) - (float64(fontSize) / 2)
  373. } else if valign == ValignBottom {
  374. y = y + h - float64(fontSize)
  375. }
  376. pdf.SetY(y)
  377. pdf.Cell(nil, text)
  378. }
  379. // golang获取程序运行路径
  380. func GetCurrentDirectory() string {
  381. dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
  382. return strings.Replace(dir, "\\", "/", -1)
  383. }