system_service.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package service
  2. import (
  3. "errors"
  4. "fmt"
  5. "time"
  6. "gopkg.in/guregu/null.v3"
  7. "xiaoniaokuaiyan.com/xiaoniao/entity"
  8. "xiaoniaokuaiyan.com/xiaoniao/util"
  9. )
  10. type SystemService struct{}
  11. func (srv *SystemService) GetFirstPagePicList() (interface{}, error) {
  12. db := util.GetSqlDB()
  13. strSql := "select title, value from t_dictionaries where class = ? and flag = 'Y';"
  14. var rList = []struct {
  15. Title string `db:"title" json:"title"`
  16. Value string `db:"value", json:"value"`
  17. }{}
  18. err := db.Select(&rList, strSql, "indexpic")
  19. return rList, err
  20. }
  21. func (srv *SystemService) GetWorkdays(n int) (interface{}, error) {
  22. if n <= 0 {
  23. n = 7
  24. }
  25. today := time.Now().Format("2006-01-02")
  26. strSql := "select curr_date from t_workdays where isworkday = 'Y' and curr_date > ? order by curr_date asc limit ?;"
  27. workDays := []string{}
  28. db := util.GetWriteSqlDB()
  29. err := db.Select(&workDays, strSql, today, n)
  30. return workDays, err
  31. }
  32. func (srv *SystemService) GetQuestion(qid int) (interface{}, error) {
  33. if qid == 0 {
  34. qid = 5
  35. }
  36. db := util.GetSqlDB()
  37. strSql := "select title from t_activity_title where id = ?;"
  38. var qTitle string
  39. err := db.Get(&qTitle, strSql, qid)
  40. if err != nil {
  41. return nil, err
  42. }
  43. var qlist = []struct {
  44. Id int `json:"id" db:"id"`
  45. AtId int `json:"at_id" db:"at_iq"`
  46. Title string `json:"title" db:"title"`
  47. Type string `json:"type" db:"type"`
  48. IsShow string `json:"is_show" db:"is_show"`
  49. IsMust string `json:"is_must" db:"is_must"`
  50. VerifyRule string `json:"verify_rule" db:"verify_rule"`
  51. LogicAllAqId string `json:"logic_all_aq_id" db:"logic_all_aq_id"`
  52. DefaultValue string `json:"default_value" db:"default_value"`
  53. Order string `json:"order" db:"order"`
  54. OptionList []interface{} `json:"option_list" db:"-"`
  55. }{}
  56. strSql = "select * from t_activity_question where at_iq = ? order by `order`;"
  57. err = db.Select(&qlist, strSql, qid)
  58. if err != nil {
  59. return nil, err
  60. }
  61. if len(qlist) == 0 {
  62. return nil, errors.New("数据错误")
  63. }
  64. var (
  65. strAqIds string
  66. qlistIdxMap = map[int]int{}
  67. )
  68. for idx, qitem := range qlist {
  69. strAqIds += fmt.Sprintf("%d,", qitem.Id)
  70. qlistIdxMap[qitem.Id] = idx
  71. }
  72. strAqIds = strAqIds[0 : len(strAqIds)-1]
  73. strSql = "select * from t_activity_option where aq_id in(" + strAqIds + ") order by aq_id,`order`;"
  74. var optionList = []struct {
  75. Id int `json:"id" db:"id"`
  76. AqId int `json:"aq_id" db:"aq_id"`
  77. Name string `json:"name" db:"name"`
  78. LogicAqId null.String `json:"logic_aq_id" db:"logic_aq_id"`
  79. Order string `json:"order" db:"order"`
  80. DetectProducts []interface{} `json:"detect_products" db:"-"`
  81. }{}
  82. err = db.Select(&optionList, strSql)
  83. if err != nil {
  84. return nil, err
  85. }
  86. if len(optionList) == 0 {
  87. return nil, errors.New("数据错误1")
  88. }
  89. var (
  90. strOpids string
  91. olistIdxMap = map[int]int{}
  92. )
  93. for idx, oitem := range optionList {
  94. strOpids += fmt.Sprintf("%d,", oitem.Id)
  95. olistIdxMap[oitem.Id] = idx
  96. }
  97. strOpids = strOpids[0 : len(strOpids)-1]
  98. strSql = "select t1.*, t2.price from t_activity_detect t1 left join t_detect_product t2 on t1.detect_product_id=t2.id where ap_id in(" + strOpids + ") or ap_id is NULL order by ap_id;"
  99. var dpList = []struct {
  100. Id int `json:"id" db:"id"`
  101. ApId null.Int `json:"ap_id" db:"ap_id"`
  102. DetectProductId int `json:"detect_product_id" db:"detect_product_id"`
  103. DetectProductName string `json:"detect_product_name" db:"detect_product_name"`
  104. Price int `json:"price" db:"price"`
  105. IsBase string `json:"is_base" db:"is_base"`
  106. }{}
  107. err = db.Select(&dpList, strSql)
  108. if err != nil {
  109. return nil, err
  110. }
  111. if len(dpList) == 0 {
  112. return nil, errors.New("数据错误3")
  113. }
  114. var result = struct {
  115. BaseDetectProducts []interface{} `json:"base_detect_products"`
  116. Title string `json:"title"`
  117. QuestionList interface{} `json:"question_list"`
  118. }{
  119. Title: qTitle,
  120. }
  121. for _, dpItem := range dpList {
  122. if idx, ok := olistIdxMap[int(dpItem.ApId.Int64)]; ok {
  123. optionList[idx].DetectProducts = append(optionList[idx].DetectProducts, dpItem)
  124. } else {
  125. result.BaseDetectProducts = append(result.BaseDetectProducts, dpItem)
  126. }
  127. }
  128. for _, oitem := range optionList {
  129. if idx, ok := qlistIdxMap[oitem.AqId]; ok {
  130. qlist[idx].OptionList = append(qlist[idx].OptionList, oitem)
  131. }
  132. }
  133. result.QuestionList = qlist
  134. return result, nil
  135. }
  136. func (srv *SystemService) GetDictList(className, ordernum string) (interface{}, error) {
  137. var dictList = []entity.DictItem{}
  138. db := util.GetSqlDB()
  139. if len(ordernum) == 0 {
  140. strSql := "select * from t_dictionaries where class = ? and flag = 'Y';"
  141. err := db.Select(&dictList, strSql, className)
  142. return dictList, err
  143. } else {
  144. strSql := "select * from t_dictionaries where class = ? and ordernum = ? and flag='Y';"
  145. err := db.Select(&dictList, strSql, className, ordernum)
  146. return dictList, err
  147. }
  148. }