helper.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package util
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/robfig/cron"
  6. "log"
  7. "math/rand"
  8. "reflect"
  9. "strconv"
  10. "strings"
  11. "sync"
  12. "time"
  13. "xiaoniaokuaiyan.com/xiaoniao/config"
  14. )
  15. func StructToMap(item interface{}) (map[string]string, error) {
  16. dv := reflect.ValueOf(item)
  17. if dv.Kind() != reflect.Ptr {
  18. return nil, errors.New("must pass a pointer")
  19. }
  20. if dv.IsNil() {
  21. return nil, errors.New("can not pass nil")
  22. }
  23. dv = dv.Elem()
  24. rt := dv.Type()
  25. fnum := rt.NumField()
  26. resultMap := map[string]string{}
  27. var fv reflect.Value
  28. for i := 0; i < fnum; i++ {
  29. fv = dv.Field(i)
  30. zero := reflect.Zero(fv.Type()).Interface()
  31. current := fv.Interface()
  32. if reflect.DeepEqual(zero, current) {
  33. continue
  34. }
  35. tag := rt.Field(i).Tag.Get("xml")
  36. tag = strings.Split(tag, ",")[0]
  37. if tag == "null" {
  38. continue
  39. }
  40. if tag == "" {
  41. tag = rt.Field(i).Name
  42. }
  43. resultMap[tag] = fmt.Sprintf("%v", current)
  44. }
  45. return resultMap, nil
  46. }
  47. func IntJoin(params []int, sep string) string {
  48. relStr := ""
  49. for _, param := range params {
  50. relStr += fmt.Sprintf("%d%s", param, sep)
  51. }
  52. return relStr[0:(len(relStr) - len(sep))]
  53. }
  54. var orderNoMutex = &sync.Mutex{}
  55. func GenerateOrderNo(prefix string) string {
  56. prefix = strings.ToUpper(prefix)
  57. timeStr := time.Now().Format("060102150405")
  58. orderNoMutex.Lock()
  59. defer orderNoMutex.Unlock()
  60. timestamp := time.Now().Unix()
  61. rand.Seed(timestamp + int64(time.Now().Nanosecond()))
  62. var rnum = rand.Intn(100000)
  63. strNum := fmt.Sprintf("0000%d", rnum)
  64. strNum = strNum[len(strNum)-5:]
  65. return fmt.Sprintf("%s%s%s", prefix, timeStr, strNum)
  66. }
  67. // qz add 生日转年龄
  68. func BirthDayToAge(birth string) (int, error) {
  69. //列举几种可能的日期格式,以后有需要再扩充
  70. const timeTemplate1 = "2006-01-02"
  71. const timeTemplate2 = "2006/01/02"
  72. const timeTemplate3 = "2006-01"
  73. const timeTemplate4 = "2006/01"
  74. var stamp time.Time
  75. i := strings.Count(birth, "-")
  76. if i == 2 {
  77. stamp, _ = time.ParseInLocation(timeTemplate1, birth, time.Local)
  78. //fmt.Println(stamp,"--")
  79. } else if i == 1 {
  80. stamp, _ = time.ParseInLocation(timeTemplate3, birth, time.Local)
  81. //fmt.Println(stamp,"-")
  82. } else {
  83. i = strings.Count(birth, "/")
  84. if i == 2 {
  85. stamp, _ = time.ParseInLocation(timeTemplate2, birth, time.Local)
  86. //fmt.Println(stamp,"//")
  87. } else if i == 1 {
  88. stamp, _ = time.ParseInLocation(timeTemplate4, birth, time.Local)
  89. //fmt.Println(stamp,"/")
  90. } else {
  91. return 0, errors.New("birthday error")
  92. }
  93. }
  94. year := time.Now().Year() - stamp.Year()
  95. if year > 150 || year < 0 {
  96. return 0, errors.New("too old or too young")
  97. }
  98. return year, nil
  99. }
  100. func StringToInt(s string) (int, bool) {
  101. if s == "" {
  102. return 0, false
  103. }
  104. result, err := strconv.Atoi(s)
  105. if err != nil {
  106. return 0, false
  107. }
  108. return result, true
  109. }
  110. func SourceCheck(source string) bool {
  111. //fs := flag.NewFlagSet("", flag.ExitOnError)
  112. //configFile := fs.String("f", "../config.ini", "app config file")
  113. //config.Reload(*configFile)
  114. strSource := config.IniConf.Section("server").Key("thirdsource").String()
  115. return strings.Contains(strSource, source)
  116. }
  117. func ProductStatusJumpTo10(productId int) bool {
  118. //fs := flag.NewFlagSet("", flag.ExitOnError)
  119. //configFile := fs.String("f", "../config.ini", "app config file")
  120. //config.Reload(*configFile)
  121. strSource := config.IniConf.Section("server").Key("productjump10").String()
  122. return strings.Contains(strSource, fmt.Sprintf("[%d]", productId))
  123. }
  124. // 20230329 问诊产品1566 20230413改名 ProductStatusJumpTo10->ProductWenZhen
  125. func ProductWenZhen(productId int) bool {
  126. //fs := flag.NewFlagSet("", flag.ExitOnError)
  127. //configFile := fs.String("f", "../config.ini", "app config file")
  128. //config.Reload(*configFile)
  129. strSource := config.IniConf.Section("server").Key("productwenzhen").String()
  130. return strings.Contains(strSource, fmt.Sprintf("[%d]", productId))
  131. }
  132. // 20221229 是否是抗原药物
  133. func ProductIsKangYuan(productId int) bool {
  134. //fs := flag.NewFlagSet("", flag.ExitOnError)
  135. //configFile := fs.String("f", "../config.ini", "app config file")
  136. //config.Reload(*configFile)
  137. strSource := config.IniConf.Section("server").Key("kangyuan").String()
  138. return strings.Contains(strSource, fmt.Sprintf("[%d]", productId))
  139. }
  140. // 20220525 远毅产品1338
  141. func ProductYuanYi(productId string) bool {
  142. strSource := config.IniConf.Section("server").Key("productyuanyi").String()
  143. return strings.Contains(strSource, fmt.Sprintf("[%s]", productId))
  144. }
  145. const NURSE_RUN = "0 0 1 * * *"
  146. // const NURSE_RUN="0 */1 * * * *"
  147. // 20220223 定时更新护士校验码
  148. func TimingTask() {
  149. c := cron.New()
  150. c.AddFunc(NURSE_RUN, func() {
  151. log.Println("nurse_code_run", SetNurseCode())
  152. })
  153. c.Start()
  154. }
  155. const NURSE_CODE_KEY = "NURSE:CODE"
  156. func SetNurseCode() string {
  157. client := GetRedis()
  158. client.Select(14)
  159. vcode := generateValidateCode(6)
  160. client.Set(NURSE_CODE_KEY, vcode)
  161. return vcode
  162. }
  163. const NURSE_CODE_VALUE = "667043"
  164. func GetNurseCode() string {
  165. client := GetRedis()
  166. client.Select(14)
  167. scmd := client.Get(NURSE_CODE_KEY)
  168. if err := scmd.Err(); err != nil || len(scmd.Val()) != 6 {
  169. client.Set(NURSE_CODE_KEY, NURSE_CODE_VALUE)
  170. return NURSE_CODE_VALUE
  171. }
  172. return scmd.Val()
  173. }
  174. func generateValidateCode(length int) string {
  175. var (
  176. chars = "0123456789"
  177. rnum int
  178. vcode string
  179. )
  180. for i := 0; i < length; i++ {
  181. rnum = rand.Intn(10)
  182. vcode += chars[rnum : rnum+1]
  183. }
  184. return vcode
  185. }
  186. func GetBirthById(id string) string {
  187. if len(id) < 18 {
  188. return ""
  189. }
  190. birth, err := time.Parse("20060102", id[6:14])
  191. if err != nil {
  192. return ""
  193. }
  194. return birth.Format("2006-01-02")
  195. }
  196. // 20230830 修改t_order ->t_order_history t_order_product -> t_order_product_history t_order_extra -> t_order_extra_history
  197. // 表名后面要有空格
  198. func ChangeOrderTableName(s string) string {
  199. return strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(s, "t_order ", "t_order_history "), "t_order_product ", "t_order_product_history "), "t_order_extra ", "t_order_extra_history ")
  200. }