repair_order.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 TRepairOrder struct {
  13. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  14. ClassId int64 `gorm:"column:class_id" json:"class_id"`
  15. ApplyPeople string `gorm:"column:apply_people" json:"apply_people"`
  16. ApplyPeoplePhone string `gorm:"column:apply_people_phone" json:"apply_people_phone"`
  17. Appointment time.Time `gorm:"column:appointment" json:"appointment"`
  18. ApplyContent string `gorm:"column:apply_content" json:"apply_content"`
  19. ApplyPic string `gorm:"column:apply_pic" json:"apply_pic"`
  20. BuildingId int64 `gorm:"column:building_id" json:"building_id"`
  21. UnitId int64 `gorm:"column:unit_id" json:"unit_id"`
  22. HouseId int64 `gorm:"column:house_id" json:"house_id"`
  23. HouseName string `gorm:"column:house_name" json:"house_name"`
  24. LastUid int64 `gorm:"column:last_uid" json:"last_uid"`
  25. RepairPic string `gorm:"column:repair_pic" json:"repair_pic"`
  26. ReturnVisitContent string `gorm:"column:return_visit_content" json:"return_visit_content"`
  27. ReturnVisitLevel int32 `gorm:"column:return_visit_level" json:"return_visit_level"`
  28. Feedback string `gorm:"column:feedback" json:"feedback"`
  29. Status int32 `gorm:"column:status" json:"status"`
  30. Pipeline string `gorm:"column:pipeline" json:"pipeline"`
  31. CurrentUid int64 `gorm:"column:current_uid" json:"current_uid"`
  32. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  33. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  34. RepairTime time.Time `gorm:"column:repair_time" json:"repair_time"`
  35. LastUidIsCompany bool `gorm:"column:last_uid_is_company" json:"last_uid_is_company"`
  36. HouseholdUid int64 `form:"household_uid" json:"household_uid"`
  37. //Area int32 `form:"area" json:"area"`
  38. table string
  39. }
  40. func (p *TRepairOrder) TableName() string {
  41. return p.table
  42. }
  43. func NewRepairOrder(database string) *TRepairOrder {
  44. return &TRepairOrder{table: fmt.Sprintf("%s.%s", database, "t_repair_order")}
  45. }
  46. func (p *TRepairOrder) SetTable(database string) {
  47. p.table = fmt.Sprintf("%s.%s", database, "t_repair_order")
  48. }
  49. func (p *TRepairOrder) CreateTable(db *gorm.DB) error {
  50. sql :="CREATE TABLE IF NOT EXISTS "+p.TableName()+"("+
  51. " `id` int(10) NOT NULL AUTO_INCREMENT,"+
  52. " `class_id` int(10) NOT NULL COMMENT '分类id',"+
  53. "`apply_people` varchar(128) COLLATE utf8mb4_bin NOT NULL COMMENT '报修人',"+
  54. "`apply_people_phone` varchar(32) COLLATE utf8mb4_bin NOT NULL COMMENT '报修人电话',"+
  55. "`appointment` datetime NOT NULL COMMENT '预约时间',"+
  56. "`apply_content` text COLLATE utf8mb4_bin NOT NULL COMMENT '报修内容',"+
  57. "`apply_pic` text COLLATE utf8mb4_bin NOT NULL COMMENT '报修图片',"+
  58. "`building_id` int(11) NOT NULL COMMENT '楼栋id',"+
  59. "`unit_id` int(11) NOT NULL COMMENT '单元id',"+
  60. "`house_id` int(11) NOT NULL COMMENT '房屋id',"+
  61. "`house_name` varchar(255) COLLATE utf8mb4_bin NOT NULL COMMENT '房屋全称',"+
  62. "`last_uid` int(10) NOT NULL COMMENT '上级处理人uid',"+
  63. "`repair_pic` text COLLATE utf8mb4_bin NOT NULL COMMENT '维修图片',"+
  64. "`return_visit_content` text COLLATE utf8mb4_bin NOT NULL COMMENT '回访内容',"+
  65. "`return_visit_level` tinyint(1) NOT NULL COMMENT '回访满意度',"+
  66. "`feedback` text COLLATE utf8mb4_bin NOT NULL COMMENT '处理意见',"+
  67. "`status` tinyint(1) NOT NULL COMMENT '1 未派单 2 已派单 3 已完结 4 退单',"+
  68. "`pipeline` text COLLATE utf8mb4_bin NOT NULL COMMENT '工单流水线详情',"+
  69. "`household_uid` bigint(11) NOT NULL COMMENT '业主id,业主小程序添加任务时不为空',"+
  70. "`current_uid` int(10) NOT NULL COMMENT '当前处理人',"+
  71. "`last_uid_is_company` tinyint(1) NOT NULL COMMENT '上级处理人是否是公司账户',"+
  72. "`repair_time` datetime NOT NULL COMMENT '维修时间',"+
  73. "`created_at` datetime NOT NULL,"+
  74. " `updated_at` datetime NOT NULL,"+
  75. // " `area` tinyint(1) NOT NULL COMMENT '1 公共区域 2 户内',"+
  76. " PRIMARY KEY (`id`),"+
  77. "KEY `class_id` (`class_id`) USING BTREE,"+
  78. "KEY `apply_people` (`apply_people`) USING BTREE,"+
  79. " KEY `household_uid` (`household_uid`) USING BTREE,"+
  80. "KEY `apply_people_phone` (`apply_people_phone`) USING BTREE"+
  81. ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;"
  82. err := db.Exec(sql).Error
  83. if err != nil {
  84. logger.Error("mysql",
  85. zap.String("sql", "create table "+p.TableName()),
  86. zap.String("fields", ""),
  87. zap.String("error", err.Error()))
  88. }
  89. return err
  90. }
  91. func (p *TRepairOrder) Find(db *gorm.DB, where map[string]interface{}) error {
  92. err := db.Table(p.TableName()).Where(where).Find(p).Error
  93. if err != nil {
  94. fields, _ := util.MarshalToString(where)
  95. logger.Error("mysql",
  96. zap.String("sql", "select from "+p.TableName()),
  97. zap.String("fields", fields),
  98. zap.String("error", err.Error()))
  99. }
  100. return err
  101. }
  102. func (p *TRepairOrder) Last(db *gorm.DB) error {
  103. err := db.Table(p.TableName()).Last(p).Error
  104. if err != nil {
  105. logger.Error("mysql",
  106. zap.String("sql", "select last from "+p.TableName()),
  107. zap.String("fields", ""),
  108. zap.String("error", err.Error()))
  109. }
  110. return err
  111. }
  112. // Insert 插入一条记录
  113. func (p *TRepairOrder) Insert(db *gorm.DB) error {
  114. err := db.Table(p.TableName()).Create(p).Error
  115. if err != nil {
  116. fields, _ := util.MarshalToString(*p)
  117. logger.Error("mysql",
  118. zap.String("sql", "insert into "+p.TableName()),
  119. zap.String("fields", fields),
  120. zap.String("error", err.Error()))
  121. }
  122. return err
  123. }
  124. // Insert 插入多条记录
  125. func (p *TRepairOrder) InsertMulti(db *gorm.DB, values interface{}) error {
  126. err := db.Table(p.TableName()).Create(values).Error
  127. if err != nil {
  128. fields, _ := util.MarshalToString(*p)
  129. logger.Error("mysql",
  130. zap.String("sql", "insert into "+p.TableName()),
  131. zap.String("fields", fields),
  132. zap.String("error", err.Error()))
  133. }
  134. return err
  135. }
  136. func (p *TRepairOrder) Delete(db *gorm.DB, filter map[string]interface{}) error {
  137. cond, val, err := whereBuild(filter)
  138. if err != nil {
  139. return err
  140. }
  141. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  142. }
  143. func (p *TRepairOrder) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  144. cond, val, err := whereBuild(where)
  145. if err != nil {
  146. if err != nil {
  147. fields, _ := util.MarshalToString(values)
  148. logger.Error("mysql",
  149. zap.String("sql", "update "+p.TableName()),
  150. zap.String("fields", fields),
  151. zap.String("error", err.Error()))
  152. }
  153. return err
  154. }
  155. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  156. }
  157. func (p *TRepairOrder) UpdateByModel(db *gorm.DB) error {
  158. err := db.Table(p.TableName()).Model(p).Updates(p).Error
  159. if err != nil {
  160. fields, _ := util.MarshalToString(*p)
  161. logger.Error("mysql",
  162. zap.String("sql", "update "+p.TableName()),
  163. zap.String("fields", fields),
  164. zap.String("error", err.Error()))
  165. }
  166. return err
  167. }
  168. func (p *TRepairOrder) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  169. cond, val, err := whereBuildAndOr(where, or)
  170. if err != nil {
  171. return 0, err
  172. }
  173. ret := int64(0)
  174. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  175. if err != nil {
  176. fields, _ := util.MarshalToString(where)
  177. logger.Error("mysql",
  178. zap.String("sql", "select count "+p.TableName()),
  179. zap.String("fields", fields),
  180. zap.String("error", err.Error()))
  181. }
  182. return ret, err
  183. }
  184. func (p *TRepairOrder) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TRepairOrder, err error) {
  185. cond, val, err := whereBuildAndOr(where, or)
  186. if err != nil {
  187. return list, err
  188. }
  189. if pageSize < 0 {
  190. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Find(&list)
  191. if result.Error != nil {
  192. wherefields, _ := util.MarshalToString(where)
  193. logger.Error("mysql",
  194. zap.String("sql", "select * from "+p.TableName()),
  195. zap.String("where", wherefields),
  196. zap.String("error", result.Error.Error()))
  197. }
  198. return list, result.Error
  199. }
  200. offset := (page - 1) * pageSize
  201. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Order("created_at desc").Find(&list)
  202. if result.Error != nil {
  203. wherefields, _ := util.MarshalToString(where)
  204. logger.Error("mysql",
  205. zap.String("sql", "select * from "+p.TableName()),
  206. zap.String("where", wherefields),
  207. zap.String("error", result.Error.Error()))
  208. }
  209. return list, result.Error
  210. }