suggestion_order.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package model
  4. import (
  5. "fmt"
  6. "git.getensh.com/common/gopkgs/logger"
  7. "git.getensh.com/common/gopkgs/util"
  8. "go.uber.org/zap"
  9. "gorm.io/gorm"
  10. "time"
  11. )
  12. type TSuggestionOrder struct {
  13. ID int64 `gorm:"column:id" json:"id"`
  14. SuggestionType int32 `gorm:"column:suggestion_type" json:"suggestion_type"`
  15. ApplyPeople string `gorm:"column:apply_people" json:"apply_people"`
  16. ApplyPeoplePhone string `gorm:"column:apply_people_phone" json:"apply_people_phone"`
  17. ApplyContent string `gorm:"column:apply_content" json:"apply_content"`
  18. ApplyPic string `gorm:"column:apply_pic" json:"apply_pic"`
  19. LastUid int64 `gorm:"column:last_uid" json:"last_uid"`
  20. HandlePic string `gorm:"column:handle_pic" json:"handle_pic"`
  21. ReturnVisitContent string `gorm:"column:return_visit_content" json:"return_visit_content"`
  22. ReturnVisitLevel int32 `gorm:"column:return_visit_level" json:"return_visit_level"`
  23. Feedback string `gorm:"column:feedback" json:"feedback"`
  24. Status int32 `gorm:"column:status" json:"status"`
  25. Pipeline string `gorm:"column:pipeline" json:"pipeline"`
  26. CurrentUid int64 `gorm:"column:current_uid" json:"current_uid"`
  27. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  28. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  29. LastUidIsCompany bool `gorm:"column:last_uid_is_company" json:"last_uid_is_company"`
  30. HouseholdUid int64 `gorm:"column:household_uid" json:"household_uid"`
  31. table string
  32. }
  33. func (p *TSuggestionOrder) TableName() string {
  34. return p.table
  35. }
  36. func NewSuggestionOrder(database string) *TSuggestionOrder {
  37. return &TSuggestionOrder{table: fmt.Sprintf("%s.%s", database, "t_suggestion_order")}
  38. }
  39. func (p *TSuggestionOrder) SetTable(database string) {
  40. p.table = fmt.Sprintf("%s.%s", database, "t_suggestion_order")
  41. }
  42. func (p *TSuggestionOrder) CreateTable(db *gorm.DB) error {
  43. sql := "CREATE TABLE IF NOT EXISTS " + p.TableName() + "(" +
  44. "`id` int(10) NOT NULL AUTO_INCREMENT," +
  45. "`suggestion_type` int(10) NOT NULL COMMENT '1 投诉 2 建议'," +
  46. "`apply_people` varchar(128) COLLATE utf8mb4_bin NOT NULL COMMENT '报修人'," +
  47. "`apply_people_phone` varchar(32) COLLATE utf8mb4_bin NOT NULL COMMENT '报修人电话'," +
  48. "`apply_content` text COLLATE utf8mb4_bin NOT NULL COMMENT '投诉内容'," +
  49. "`apply_pic` text COLLATE utf8mb4_bin NOT NULL COMMENT '投诉图片'," +
  50. "`last_uid` int(10) NOT NULL COMMENT '上级处理人uid'," +
  51. "`handle_pic` text COLLATE utf8mb4_bin NOT NULL COMMENT '处理图片'," +
  52. "`return_visit_content` text COLLATE utf8mb4_bin NOT NULL COMMENT '回访内容'," +
  53. "`return_visit_level` tinyint(1) NOT NULL COMMENT '回访满意度'," +
  54. "`feedback` text COLLATE utf8mb4_bin NOT NULL COMMENT '处理意见'," +
  55. "`status` tinyint(1) NOT NULL COMMENT '1 未派单 2 已派单 3 已完结'," +
  56. "`pipeline` text COLLATE utf8mb4_bin NOT NULL COMMENT '工单流水线详情'," +
  57. "`current_uid` int(10) NOT NULL COMMENT '当前处理人uid'," +
  58. "`created_at` datetime NOT NULL," +
  59. " `updated_at` datetime NOT NULL," +
  60. " `last_uid_is_company` tinyint(1) NOT NULL COMMENT '上级处理人是否是公司账户'," +
  61. "`household_uid` bigint(11) NOT NULL COMMENT '业主id,业主小程序添加任务时不为空'," +
  62. "PRIMARY KEY (`id`)," +
  63. " KEY `apply_people` (`apply_people`) USING BTREE," +
  64. " KEY `apply_people_phone` (`apply_people_phone`) USING BTREE," +
  65. " KEY `household_uid` (`household_uid`) USING BTREE" +
  66. ") ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;"
  67. err := db.Exec(sql).Error
  68. if err != nil {
  69. logger.Error("mysql",
  70. zap.String("sql", "create table "+p.TableName()),
  71. zap.String("fields", ""),
  72. zap.String("error", err.Error()))
  73. }
  74. return err
  75. }
  76. func (p *TSuggestionOrder) Find(db *gorm.DB, where map[string]interface{}) error {
  77. err := db.Table(p.TableName()).Where(where).Find(p).Error
  78. if err != nil {
  79. fields, _ := util.MarshalToString(where)
  80. logger.Error("mysql",
  81. zap.String("sql", "select from "+p.TableName()),
  82. zap.String("fields", fields),
  83. zap.String("error", err.Error()))
  84. }
  85. return err
  86. }
  87. func (p *TSuggestionOrder) Last(db *gorm.DB) error {
  88. err := db.Table(p.TableName()).Last(p).Error
  89. if err != nil {
  90. logger.Error("mysql",
  91. zap.String("sql", "select last from "+p.TableName()),
  92. zap.String("fields", ""),
  93. zap.String("error", err.Error()))
  94. }
  95. return err
  96. }
  97. // Insert 插入一条记录
  98. func (p *TSuggestionOrder) Insert(db *gorm.DB) error {
  99. err := db.Table(p.TableName()).Create(p).Error
  100. if err != nil {
  101. fields, _ := util.MarshalToString(*p)
  102. logger.Error("mysql",
  103. zap.String("sql", "insert into "+p.TableName()),
  104. zap.String("fields", fields),
  105. zap.String("error", err.Error()))
  106. }
  107. return err
  108. }
  109. // Insert 插入多条记录
  110. func (p *TSuggestionOrder) InsertMulti(db *gorm.DB, values interface{}) error {
  111. err := db.Table(p.TableName()).Create(values).Error
  112. if err != nil {
  113. fields, _ := util.MarshalToString(*p)
  114. logger.Error("mysql",
  115. zap.String("sql", "insert into "+p.TableName()),
  116. zap.String("fields", fields),
  117. zap.String("error", err.Error()))
  118. }
  119. return err
  120. }
  121. func (p *TSuggestionOrder) Delete(db *gorm.DB, filter map[string]interface{}) error {
  122. cond, val, err := whereBuild(filter)
  123. if err != nil {
  124. return err
  125. }
  126. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  127. }
  128. func (p *TSuggestionOrder) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  129. cond, val, err := whereBuild(where)
  130. if err != nil {
  131. if err != nil {
  132. fields, _ := util.MarshalToString(values)
  133. logger.Error("mysql",
  134. zap.String("sql", "update "+p.TableName()),
  135. zap.String("fields", fields),
  136. zap.String("error", err.Error()))
  137. }
  138. return err
  139. }
  140. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  141. }
  142. func (p *TSuggestionOrder) UpdateByModel(db *gorm.DB) error {
  143. err := db.Table(p.TableName()).Model(p).Updates(p).Error
  144. if err != nil {
  145. fields, _ := util.MarshalToString(*p)
  146. logger.Error("mysql",
  147. zap.String("sql", "update "+p.TableName()),
  148. zap.String("fields", fields),
  149. zap.String("error", err.Error()))
  150. }
  151. return err
  152. }
  153. func (p *TSuggestionOrder) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  154. cond, val, err := whereBuildAndOr(where, or)
  155. if err != nil {
  156. return 0, err
  157. }
  158. ret := int64(0)
  159. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  160. if err != nil {
  161. fields, _ := util.MarshalToString(where)
  162. logger.Error("mysql",
  163. zap.String("sql", "select count "+p.TableName()),
  164. zap.String("fields", fields),
  165. zap.String("error", err.Error()))
  166. }
  167. return ret, err
  168. }
  169. func (p *TSuggestionOrder) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TSuggestionOrder, err error) {
  170. cond, val, err := whereBuildAndOr(where, or)
  171. if err != nil {
  172. return list, err
  173. }
  174. if pageSize < 0 {
  175. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Find(&list)
  176. if result.Error != nil {
  177. wherefields, _ := util.MarshalToString(where)
  178. logger.Error("mysql",
  179. zap.String("sql", "select * from "+p.TableName()),
  180. zap.String("where", wherefields),
  181. zap.String("error", result.Error.Error()))
  182. }
  183. return list, result.Error
  184. }
  185. offset := (page - 1) * pageSize
  186. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Order("created_at desc").Find(&list)
  187. if result.Error != nil {
  188. wherefields, _ := util.MarshalToString(where)
  189. logger.Error("mysql",
  190. zap.String("sql", "select * from "+p.TableName()),
  191. zap.String("where", wherefields),
  192. zap.String("error", result.Error.Error()))
  193. }
  194. return list, result.Error
  195. }