123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- package util
- import (
- "errors"
- "fmt"
- "github.com/robfig/cron"
- "log"
- "math/rand"
- "reflect"
- "strconv"
- "strings"
- "sync"
- "time"
- "xiaoniaokuaiyan.com/xiaoniao/config"
- )
- func StructToMap(item interface{}) (map[string]string, error) {
- dv := reflect.ValueOf(item)
- if dv.Kind() != reflect.Ptr {
- return nil, errors.New("must pass a pointer")
- }
- if dv.IsNil() {
- return nil, errors.New("can not pass nil")
- }
- dv = dv.Elem()
- rt := dv.Type()
- fnum := rt.NumField()
- resultMap := map[string]string{}
- var fv reflect.Value
- for i := 0; i < fnum; i++ {
- fv = dv.Field(i)
- zero := reflect.Zero(fv.Type()).Interface()
- current := fv.Interface()
- if reflect.DeepEqual(zero, current) {
- continue
- }
- tag := rt.Field(i).Tag.Get("xml")
- tag = strings.Split(tag, ",")[0]
- if tag == "null" {
- continue
- }
- if tag == "" {
- tag = rt.Field(i).Name
- }
- resultMap[tag] = fmt.Sprintf("%v", current)
- }
- return resultMap, nil
- }
- func IntJoin(params []int, sep string) string {
- relStr := ""
- for _, param := range params {
- relStr += fmt.Sprintf("%d%s", param, sep)
- }
- return relStr[0:(len(relStr) - len(sep))]
- }
- var orderNoMutex = &sync.Mutex{}
- func GenerateOrderNo(prefix string) string {
- prefix = strings.ToUpper(prefix)
- timeStr := time.Now().Format("060102150405")
- orderNoMutex.Lock()
- defer orderNoMutex.Unlock()
- timestamp := time.Now().Unix()
- rand.Seed(timestamp + int64(time.Now().Nanosecond()))
- var rnum = rand.Intn(100000)
- strNum := fmt.Sprintf("0000%d", rnum)
- strNum = strNum[len(strNum)-5:]
- return fmt.Sprintf("%s%s%s", prefix, timeStr, strNum)
- }
- // qz add 生日转年龄
- func BirthDayToAge(birth string) (int, error) {
- //列举几种可能的日期格式,以后有需要再扩充
- const timeTemplate1 = "2006-01-02"
- const timeTemplate2 = "2006/01/02"
- const timeTemplate3 = "2006-01"
- const timeTemplate4 = "2006/01"
- var stamp time.Time
- i := strings.Count(birth, "-")
- if i == 2 {
- stamp, _ = time.ParseInLocation(timeTemplate1, birth, time.Local)
- //fmt.Println(stamp,"--")
- } else if i == 1 {
- stamp, _ = time.ParseInLocation(timeTemplate3, birth, time.Local)
- //fmt.Println(stamp,"-")
- } else {
- i = strings.Count(birth, "/")
- if i == 2 {
- stamp, _ = time.ParseInLocation(timeTemplate2, birth, time.Local)
- //fmt.Println(stamp,"//")
- } else if i == 1 {
- stamp, _ = time.ParseInLocation(timeTemplate4, birth, time.Local)
- //fmt.Println(stamp,"/")
- } else {
- return 0, errors.New("birthday error")
- }
- }
- year := time.Now().Year() - stamp.Year()
- if year > 150 || year < 0 {
- return 0, errors.New("too old or too young")
- }
- return year, nil
- }
- func StringToInt(s string) (int, bool) {
- if s == "" {
- return 0, false
- }
- result, err := strconv.Atoi(s)
- if err != nil {
- return 0, false
- }
- return result, true
- }
- func SourceCheck(source string) bool {
- //fs := flag.NewFlagSet("", flag.ExitOnError)
- //configFile := fs.String("f", "../config.ini", "app config file")
- //config.Reload(*configFile)
- strSource := config.IniConf.Section("server").Key("thirdsource").String()
- return strings.Contains(strSource, source)
- }
- func ProductStatusJumpTo10(productId int) bool {
- //fs := flag.NewFlagSet("", flag.ExitOnError)
- //configFile := fs.String("f", "../config.ini", "app config file")
- //config.Reload(*configFile)
- strSource := config.IniConf.Section("server").Key("productjump10").String()
- return strings.Contains(strSource, fmt.Sprintf("[%d]", productId))
- }
- // 20230329 问诊产品1566 20230413改名 ProductStatusJumpTo10->ProductWenZhen
- func ProductWenZhen(productId int) bool {
- //fs := flag.NewFlagSet("", flag.ExitOnError)
- //configFile := fs.String("f", "../config.ini", "app config file")
- //config.Reload(*configFile)
- strSource := config.IniConf.Section("server").Key("productwenzhen").String()
- return strings.Contains(strSource, fmt.Sprintf("[%d]", productId))
- }
- // 20221229 是否是抗原药物
- func ProductIsKangYuan(productId int) bool {
- //fs := flag.NewFlagSet("", flag.ExitOnError)
- //configFile := fs.String("f", "../config.ini", "app config file")
- //config.Reload(*configFile)
- strSource := config.IniConf.Section("server").Key("kangyuan").String()
- return strings.Contains(strSource, fmt.Sprintf("[%d]", productId))
- }
- // 20220525 远毅产品1338
- func ProductYuanYi(productId string) bool {
- strSource := config.IniConf.Section("server").Key("productyuanyi").String()
- return strings.Contains(strSource, fmt.Sprintf("[%s]", productId))
- }
- const NURSE_RUN = "0 0 1 * * *"
- // const NURSE_RUN="0 */1 * * * *"
- // 20220223 定时更新护士校验码
- func TimingTask() {
- c := cron.New()
- c.AddFunc(NURSE_RUN, func() {
- log.Println("nurse_code_run", SetNurseCode())
- })
- c.Start()
- }
- const NURSE_CODE_KEY = "NURSE:CODE"
- func SetNurseCode() string {
- client := GetRedis()
- client.Select(14)
- vcode := generateValidateCode(6)
- client.Set(NURSE_CODE_KEY, vcode)
- return vcode
- }
- const NURSE_CODE_VALUE = "667043"
- func GetNurseCode() string {
- client := GetRedis()
- client.Select(14)
- scmd := client.Get(NURSE_CODE_KEY)
- if err := scmd.Err(); err != nil || len(scmd.Val()) != 6 {
- client.Set(NURSE_CODE_KEY, NURSE_CODE_VALUE)
- return NURSE_CODE_VALUE
- }
- return scmd.Val()
- }
- func generateValidateCode(length int) string {
- var (
- chars = "0123456789"
- rnum int
- vcode string
- )
- for i := 0; i < length; i++ {
- rnum = rand.Intn(10)
- vcode += chars[rnum : rnum+1]
- }
- return vcode
- }
- func GetBirthById(id string) string {
- if len(id) < 18 {
- return ""
- }
- birth, err := time.Parse("20060102", id[6:14])
- if err != nil {
- return ""
- }
- return birth.Format("2006-01-02")
- }
- // 20230830 修改t_order ->t_order_history t_order_product -> t_order_product_history t_order_extra -> t_order_extra_history
- // 表名后面要有空格
- func ChangeOrderTableName(s string) string {
- 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 ")
- }
|