repair.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package frontm
  2. import (
  3. "github.com/astaxie/beego/orm"
  4. "wuyebaoxiuapi/models"
  5. )
  6. // 报修
  7. type Repair struct {
  8. Id int
  9. School string
  10. Position string
  11. ApplicantName string
  12. ApplicantPhone string
  13. ApplicantUserId int
  14. CreateTime int
  15. RepairUserId int
  16. Status int
  17. RepairTime int
  18. ApplicantContent string
  19. Address string
  20. //RepairUserPicture *RepairUserPicture `orm:"rel(fk)"`
  21. }
  22. type RepairCount struct {
  23. Count int64
  24. }
  25. type OutReRepair struct {
  26. Id int
  27. School string
  28. Position string
  29. ApplicantName string
  30. ApplicantPhone string
  31. ApplicantUserId int
  32. CreateTime int
  33. RepairUserId int
  34. Status int
  35. RepairTime int
  36. ApplicantContent string
  37. Address string
  38. RepairImages []string // 报修图片
  39. ServiceImages []string // 维修图片
  40. }
  41. func (a *Repair) TableName() string {
  42. return models.TableName("repair")
  43. }
  44. func init() {
  45. orm.RegisterModel(new(Repair))
  46. }
  47. func NewRepair() *Repair {
  48. return new(Repair)
  49. }
  50. func RepairAdd(a *Repair)(int64, error) {
  51. return orm.NewOrm().Insert(a)
  52. }
  53. // 报修用户报修列表
  54. func (a *Repair)FindRepairUserRepairs(userid int,page int , pageSize int)([]*Repair,int64) {
  55. return a.FindRepairsByUserId(userid,page,pageSize)
  56. }
  57. func (a *Repair)FindRepairsByUserId(userid int,page int , pageSize int) ([]*Repair,int64) {
  58. list := make([]*Repair, 0)
  59. qBuilder := a.UserRepairQBuilder("*")
  60. qBuilder.And("applicant_user_id = ?")
  61. qBuilder.And("status > 0")
  62. offset := (page - 1) * pageSize
  63. qBuilder.OrderBy("id").
  64. Desc().
  65. Limit(pageSize).
  66. Offset(offset)
  67. sql := qBuilder.String()
  68. o := orm.NewOrm()
  69. total, err := o.Raw(sql, userid).QueryRows(&list)
  70. if err != nil {
  71. return nil,0
  72. }
  73. return list,total
  74. }
  75. // 报修用户报修数量
  76. func (a *Repair)FindRepairUserRepairCount(userid int)(int64) {
  77. return a.findRepairCountByStatus(userid,2,"status > 0")
  78. }
  79. // --------------维修用户维修管理----------------------
  80. // 查找维修用户待维修数量
  81. func (a *Repair)FindServiceUserWRepairCount(userid int)(int64) {
  82. return a.findRepairCountByStatus(userid,2,"status = 2")
  83. }
  84. // 查找维修用户已维修数量
  85. func (a *Repair)FindServiceUserHRepairCount(userid int)(int64) {
  86. return a.findRepairCountByStatus(userid,2,"status > 2")
  87. }
  88. // 查找维修用户待维修列表
  89. func (a *Repair)FindServiceUserWRepairs(userid int,page int , pageSize int)([]*Repair,int64) {
  90. list := make([]*Repair, 0)
  91. qBuilder := a.UserRepairQBuilder("*")
  92. qBuilder.And("applicant_user_id = ?")
  93. qBuilder.And("status = 2")
  94. offset := (page - 1) * pageSize
  95. qBuilder.OrderBy("id").
  96. Desc().
  97. Limit(pageSize).
  98. Offset(offset)
  99. sql := qBuilder.String()
  100. o := orm.NewOrm()
  101. total, err := o.Raw(sql, userid).QueryRows(&list)
  102. if err != nil {
  103. return nil,0
  104. }
  105. return list,total
  106. }
  107. // 查找维修用户已维修列表
  108. func (a *Repair)FindServiceUserHRepairs(userid int,page int , pageSize int)([]*Repair,int64) {
  109. list := make([]*Repair, 0)
  110. qBuilder := a.UserRepairQBuilder("*")
  111. qBuilder.And("repair_user_id = ?")
  112. qBuilder.And("status > 2")
  113. offset := (page - 1) * pageSize
  114. qBuilder.OrderBy("id").
  115. Desc().
  116. Limit(pageSize).
  117. Offset(offset)
  118. sql := qBuilder.String()
  119. o := orm.NewOrm()
  120. total, err := o.Raw(sql, userid).QueryRows(&list)
  121. if err != nil {
  122. return nil,0
  123. }
  124. return list,total
  125. }
  126. // 确认维修
  127. func (a *Repair)ConfirmRepair()(int,error){
  128. o := orm.NewOrm()
  129. _, err := o.Raw("UPDATE pp_repair SET status = 3 , repair_time = ? WHERE id = ?" +
  130. " and status = 2 and repair_user_id=?", a.RepairTime,a.Id, a.RepairUserId).Exec()
  131. if err != nil {
  132. return a.Id,nil
  133. }
  134. return 0,nil
  135. }
  136. // --------------管理员维修管理----------------------
  137. // 查找待维修数量
  138. func (a *Repair)FindAdminWRepairCount(userid int)(int64) {
  139. return a.findRepairCountByStatus(userid,0,"status > 0 and status < 3")
  140. }
  141. // 查找已维修数量
  142. func (a *Repair)FindAdminHRepairCount(userid int)(int64) {
  143. return a.findRepairCountByStatus(userid,0,"status >= 3")
  144. }
  145. // 查找待维修列表
  146. func (a *Repair)FindAdminWRepairs(page int , pageSize int)([]*Repair,int64) {
  147. list := make([]*Repair, 0)
  148. qBuilder := a.UserRepairQBuilder("*")
  149. qBuilder.And("status > 0 and status < 3")
  150. offset := (page - 1) * pageSize
  151. qBuilder.OrderBy("id").
  152. Desc().
  153. Limit(pageSize).
  154. Offset(offset)
  155. sql := qBuilder.String()
  156. o := orm.NewOrm()
  157. total, err := o.Raw(sql).QueryRows(&list)
  158. if err != nil {
  159. return nil,0
  160. }
  161. return list,total
  162. }
  163. // 查找已维修列表
  164. func (a *Repair)FindAdminHRepairs(page int , pageSize int)([]*Repair,int64) {
  165. list := make([]*Repair, 0)
  166. qBuilder := a.UserRepairQBuilder("*")
  167. qBuilder.And("status >= 3")
  168. offset := (page - 1) * pageSize
  169. qBuilder.OrderBy("id").
  170. Desc().
  171. Limit(pageSize).
  172. Offset(offset)
  173. sql := qBuilder.String()
  174. o := orm.NewOrm()
  175. total, err := o.Raw(sql).QueryRows(&list)
  176. if err != nil {
  177. return nil,0
  178. }
  179. return list,total
  180. }
  181. // 查找维修详情
  182. func (a *Repair)FindRepairById(id int)(*Repair,error) {
  183. err := orm.NewOrm().QueryTable(models.TableName("repair")).Filter("id", id).One(a)
  184. if err != nil {
  185. return nil, err
  186. }
  187. return a, nil
  188. }
  189. // 指派维修任务
  190. func (a *Repair)AssignRepairTask(id int,repairUserId int ) (int64,error) {
  191. o := orm.NewOrm()
  192. result, err := o.Raw("UPDATE pp_repair SET status = 2 , repair_user_id = ? WHERE id = ?" +
  193. " and status = 1 ", repairUserId,id).Exec()
  194. if err != nil {
  195. return result.RowsAffected()
  196. }
  197. return 0,nil
  198. }
  199. // 用户报修SQL构造器
  200. func (a *Repair)UserRepairQBuilder(field string )(orm.QueryBuilder) {
  201. qb, _ := orm.NewQueryBuilder("mysql")
  202. qBuilder := qb.Select(field).From("pp_repair ")
  203. qBuilder.Where("id > 0")
  204. //if userid > 0{
  205. // qBuilder.Where("userid = ?")
  206. //}
  207. return qBuilder
  208. }
  209. // 查找维修数量
  210. func (a *Repair)findRepairCountByStatus(userid int,types int, value string)(int64) {
  211. o := orm.NewOrm()
  212. repairCount := new(RepairCount)
  213. where := " id > 0"
  214. if types == 1 {
  215. where += " and repair_user_id = ?"
  216. }else if types == 2{
  217. where += " and applicant_user_id = ?"
  218. }
  219. if value != "" {
  220. where += " and "+value
  221. }
  222. var err error
  223. if types == 0 {
  224. err = o.Raw("SELECT count(*)" +
  225. " as count FROM pp_repair WHERE"+where).QueryRow(&repairCount)
  226. }else{
  227. err = o.Raw("SELECT count(*)" +
  228. " as count FROM pp_repair WHERE"+where,userid).QueryRow(&repairCount)
  229. }
  230. if err != nil {
  231. return 0
  232. }
  233. return repairCount.Count
  234. }
  235. // 更新
  236. func (a *Repair) Update(fields ...string) error {
  237. if _, err := orm.NewOrm().Update(a, fields...); err != nil {
  238. return err
  239. }
  240. return nil
  241. }