batch_param.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package model
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "fmt"
  6. "time"
  7. "go-common/library/log"
  8. "go-common/library/xstr"
  9. )
  10. // ParamHandler fn
  11. type ParamHandler func(target *map[string]interface{}, args ...interface{})
  12. // BatchParam str
  13. type BatchParam struct {
  14. Params map[string]interface{}
  15. Handler ParamHandler
  16. }
  17. // NewBatchParam func
  18. func NewBatchParam(p map[string]interface{}, h ParamHandler) *BatchParam {
  19. if h == nil {
  20. h = BaseParamHandler
  21. }
  22. return &BatchParam{
  23. Params: p,
  24. Handler: h,
  25. }
  26. }
  27. var (
  28. // BaseParamHandler fn
  29. BaseParamHandler = func(target *map[string]interface{}, args ...interface{}) {}
  30. // PushParamHandler fn
  31. PushParamHandler = func(target *map[string]interface{}, args ...interface{}) {
  32. var (
  33. ok bool
  34. arc *Archive
  35. fans []int64
  36. )
  37. if target == nil || (*target)["archive"] == nil {
  38. log.Warn("PushParamHandler target(%+v)/target[archive] nil, args(%+v)", target, args)
  39. } else if arc, ok = (*target)["archive"].(*Archive); !ok || arc == nil {
  40. log.Warn("PushParamHandler target[archive]=%+v parse failed/nil, args(%+v), target(%+v)", (*target)["archive"], args, target)
  41. }
  42. if arc == nil {
  43. arc = &Archive{}
  44. }
  45. if len(args) != 1 || args[0] == nil {
  46. log.Warn("PushParamHandler args(%+v) less than 1 or nil, target(%+v) archive(%+v)", args, target, arc)
  47. } else if fans, ok = args[0].([]int64); !ok {
  48. log.Warn("PushParamHandler args(%+v) parse failed, target(%+v) archive(%+v)", args, target, arc)
  49. }
  50. b := md5.Sum([]byte(fmt.Sprintf("%d%s", arc.ID, xstr.JoinInts(fans))))
  51. (*target)["uuid"] = fmt.Sprintf("%s%d", hex.EncodeToString(b[:]), time.Now().UnixNano())
  52. }
  53. )