load_optionset_logic.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package logic
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "git.i2edu.net/i2/i2-bill-erp/internal/svc"
  7. "git.i2edu.net/i2/i2-bill-erp/transform"
  8. "git.i2edu.net/i2/go-zero/core/logx"
  9. )
  10. type Options struct {
  11. Value interface{} `json:"value"`
  12. Text string `json:"text"`
  13. }
  14. type LoadOptionsetLogic struct {
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. logx.Logger
  18. }
  19. func NewLoadOptionsetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoadOptionsetLogic {
  20. return &LoadOptionsetLogic{
  21. ctx: ctx,
  22. svcCtx: svcCtx,
  23. Logger: logx.WithContext(ctx),
  24. }
  25. }
  26. func (l *LoadOptionsetLogic) LoadOptionset(in *transform.OptionsetReq) (*transform.OptionsetRes, error) {
  27. values := []*Options{}
  28. var options struct {
  29. Value string `db:"value"`
  30. }
  31. l.svcCtx.SqlConn.QueryRowPartial(&options, "select value from sys_optionset where code=?", in.Code)
  32. json.Unmarshal([]byte(options.Value), &values)
  33. optionset := make(map[string]string)
  34. for _, opt := range values {
  35. value := fmt.Sprintf("%v", opt.Value)
  36. optionset[value] = opt.Text
  37. }
  38. return &transform.OptionsetRes{MapList: optionset}, nil
  39. }