db_wechat.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package dao
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "go-common/app/admin/ep/saga/model"
  8. "go-common/library/log"
  9. "github.com/pkg/errors"
  10. pkgerr "github.com/pkg/errors"
  11. )
  12. const (
  13. _where = "WHERE"
  14. _and = "AND"
  15. _wildcards = "%"
  16. _contactRightJoinLogSQL = "SELECT m.id,m.name,ml.username,ml.operation_type,ml.operation_result,ml.ctime FROM machines AS m INNER JOIN machine_logs AS ml ON m.id = ml.machine_id"
  17. _contactRightCountSQL = "SELECT count(m.id) FROM machines AS m INNER JOIN machine_logs AS ml ON m.id = ml.machine_id"
  18. )
  19. //var (
  20. // regUserID = regexp.MustCompile(`^\d+$`)
  21. //)
  22. // QueryUserByUserName query user by user name
  23. func (d *Dao) QueryUserByUserName(userName string) (contactInfo *model.ContactInfo, err error) {
  24. contactInfo = &model.ContactInfo{}
  25. err = pkgerr.WithStack(d.db.Where(&model.ContactInfo{UserName: userName}).First(contactInfo).Error)
  26. return
  27. }
  28. // QueryUserByID query user by user ID
  29. func (d *Dao) QueryUserByID(userID string) (contactInfo *model.ContactInfo, err error) {
  30. contactInfo = &model.ContactInfo{}
  31. err = pkgerr.WithStack(d.db.Where(&model.ContactInfo{UserID: userID}).First(contactInfo).Error)
  32. return
  33. }
  34. // UserIds query user ids for the user names 从数据库表ContactInfo查询员工编号
  35. func (d *Dao) UserIds(userNames []string) (userIds string, err error) {
  36. var (
  37. userName string
  38. ids []string
  39. contactInfo *model.ContactInfo
  40. )
  41. if len(userNames) == 0 {
  42. err = errors.Errorf("UserIds: userNames is empty!")
  43. return
  44. }
  45. for _, userName = range userNames {
  46. if contactInfo, err = d.QueryUserByUserName(userName); err != nil {
  47. err = errors.Wrapf(err, "UserIds: no such user (%s) in db, err (%s)", userName, err.Error())
  48. return
  49. }
  50. log.Info("UserIds: username (%s), userid (%s)", userName, contactInfo.UserID)
  51. if contactInfo.UserID != "" {
  52. ids = append(ids, contactInfo.UserID)
  53. }
  54. }
  55. if len(ids) <= 0 {
  56. err = errors.Wrapf(err, "UserIds: failed to find all the users in db, what a pity!")
  57. return
  58. }
  59. userIds = strings.Join(ids, "|")
  60. return
  61. }
  62. // ContactInfos query all the records in contact_infos
  63. func (d *Dao) ContactInfos() (contactInfos []*model.ContactInfo, err error) {
  64. err = pkgerr.WithStack(d.db.Find(&contactInfos).Error)
  65. return
  66. }
  67. // FindContacts find application record by auditor.
  68. func (d *Dao) FindContacts(pn, ps int) (total int64, ars []*model.ContactInfo, err error) {
  69. cdb := d.db.Model(&model.ContactInfo{}).Order("ID desc").Offset((pn - 1) * ps).Limit(ps)
  70. if err = pkgerr.WithStack(cdb.Find(&ars).Error); err != nil {
  71. return
  72. }
  73. if err = pkgerr.WithStack(d.db.Model(&model.ContactInfo{}).Count(&total).Error); err != nil {
  74. return
  75. }
  76. return
  77. }
  78. // CreateContact create contact info record
  79. func (d *Dao) CreateContact(contact *model.ContactInfo) (err error) {
  80. return pkgerr.WithStack(d.db.Create(contact).Error)
  81. }
  82. // DelContact delete the contact info with the specified UserID
  83. func (d *Dao) DelContact(contact *model.ContactInfo) (err error) {
  84. return pkgerr.WithStack(d.db.Delete(contact).Error)
  85. }
  86. // UptContact update the contact information
  87. func (d *Dao) UptContact(contact *model.ContactInfo) (err error) {
  88. return pkgerr.WithStack(d.db.Model(&model.ContactInfo{}).Where(&model.ContactInfo{UserID: contact.UserID}).Updates(*contact).Error)
  89. }
  90. // InsertContactLog insert machine log.
  91. func (d *Dao) InsertContactLog(contactlog *model.ContactLog) (err error) {
  92. return pkgerr.WithStack(d.db.Create(contactlog).Error)
  93. }
  94. // FindMachineLogs Find Machine Logs.
  95. func (d *Dao) FindMachineLogs(queryRequest *model.QueryContactLogRequest) (total int64, machineLogs []*model.AboundContactLog, err error) {
  96. var (
  97. qSQL = _contactRightJoinLogSQL
  98. cSQL = _contactRightCountSQL
  99. rows *sql.Rows
  100. )
  101. if queryRequest.UserID > 0 || queryRequest.UserName != "" || queryRequest.OperateType != "" || queryRequest.OperateUser != "" {
  102. var (
  103. strSQL = ""
  104. logicalWord = _where
  105. )
  106. if queryRequest.UserID > 0 {
  107. strSQL = fmt.Sprintf("%s %s ml.machine_id = %s", strSQL, logicalWord, strconv.FormatInt(queryRequest.UserID, 10))
  108. logicalWord = _and
  109. }
  110. if queryRequest.UserName != "" {
  111. strSQL = fmt.Sprintf("%s %s m.name like '%s'", strSQL, logicalWord, _wildcards+queryRequest.UserName+_wildcards)
  112. logicalWord = _and
  113. }
  114. if queryRequest.OperateType != "" {
  115. strSQL = fmt.Sprintf("%s %s ml.operation_type like '%s'", strSQL, logicalWord, _wildcards+queryRequest.OperateType+_wildcards)
  116. logicalWord = _and
  117. }
  118. if queryRequest.OperateUser != "" {
  119. strSQL = fmt.Sprintf("%s %s ml.username like '%s'", strSQL, logicalWord, _wildcards+queryRequest.OperateUser+_wildcards)
  120. logicalWord = _and
  121. }
  122. qSQL = _contactRightJoinLogSQL + " " + strSQL
  123. cSQL = _contactRightCountSQL + " " + strSQL
  124. }
  125. cDB := d.db.Raw(cSQL)
  126. if err = pkgerr.WithStack(cDB.Count(&total).Error); err != nil {
  127. return
  128. }
  129. gDB := d.db.Raw(qSQL)
  130. if rows, err = gDB.Order("ml.ctime DESC").Offset((queryRequest.PageNum - 1) * queryRequest.PageSize).Limit(queryRequest.PageSize).Rows(); err != nil {
  131. return
  132. }
  133. defer rows.Close()
  134. for rows.Next() {
  135. ml := &model.AboundContactLog{}
  136. if err = rows.Scan(&ml.MachineID, &ml.Name, &ml.Username, &ml.OperateType, &ml.OperateResult, &ml.OperateTime); err != nil {
  137. return
  138. }
  139. machineLogs = append(machineLogs, ml)
  140. }
  141. return
  142. }
  143. // AddWechatCreateLog ...
  144. func (d *Dao) AddWechatCreateLog(req *model.WechatCreateLog) (err error) {
  145. return pkgerr.WithStack(d.db.Create(req).Error)
  146. }
  147. // QueryWechatCreateLog ...
  148. func (d *Dao) QueryWechatCreateLog(ifpage bool, req *model.Pagination, wechatCreateInfo *model.WechatCreateLog) (wechatCreateInfos []*model.WechatCreateLog, total int, err error) {
  149. gDB := d.db.Table("wechat_create_logs").Where(wechatCreateInfo).Model(&model.WechatCreateLog{})
  150. if err = errors.WithStack(gDB.Count(&total).Error); err != nil {
  151. return
  152. }
  153. if ifpage {
  154. gDB = gDB.Order("id DESC").Offset((req.PageNum - 1) * req.PageSize).Limit(req.PageSize).Find(&wechatCreateInfos)
  155. } else {
  156. gDB = gDB.Find(&wechatCreateInfos)
  157. }
  158. if gDB.Error != nil {
  159. if gDB.RecordNotFound() {
  160. err = nil
  161. } else {
  162. err = errors.WithStack(gDB.Error)
  163. }
  164. }
  165. return
  166. }
  167. // CreateChatLog ...
  168. func (d *Dao) CreateChatLog(req *model.WechatChatLog) (err error) {
  169. return pkgerr.WithStack(d.db.Create(req).Error)
  170. }
  171. // CreateMessageLog ...
  172. func (d *Dao) CreateMessageLog(req *model.WechatMessageLog) (err error) {
  173. return pkgerr.WithStack(d.db.Create(req).Error)
  174. }