123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- package frontm
- import (
- "github.com/astaxie/beego/orm"
- "wuyebaoxiuapi/models"
- )
- // 报修
- type Repair struct {
- Id int
- School string
- Position string
- ApplicantName string
- ApplicantPhone string
- ApplicantUserId int
- CreateTime int
- RepairUserId int
- Status int
- RepairTime int
- ApplicantContent string
- Address string
- //RepairUserPicture *RepairUserPicture `orm:"rel(fk)"`
- }
- type RepairCount struct {
- Count int64
- }
- type OutReRepair struct {
- Id int
- School string
- Position string
- ApplicantName string
- ApplicantPhone string
- ApplicantUserId int
- CreateTime int
- RepairUserId int
- Status int
- RepairTime int
- ApplicantContent string
- Address string
- RepairImages []string // 报修图片
- ServiceImages []string // 维修图片
- }
- func (a *Repair) TableName() string {
- return models.TableName("repair")
- }
- func init() {
- orm.RegisterModel(new(Repair))
- }
- func NewRepair() *Repair {
- return new(Repair)
- }
- func RepairAdd(a *Repair)(int64, error) {
- return orm.NewOrm().Insert(a)
- }
- // 报修用户报修列表
- func (a *Repair)FindRepairUserRepairs(userid int,page int , pageSize int)([]*Repair,int64) {
- return a.FindRepairsByUserId(userid,page,pageSize)
- }
- func (a *Repair)FindRepairsByUserId(userid int,page int , pageSize int) ([]*Repair,int64) {
- list := make([]*Repair, 0)
- qBuilder := a.UserRepairQBuilder("*")
- qBuilder.And("applicant_user_id = ?")
- qBuilder.And("status > 0")
- offset := (page - 1) * pageSize
- qBuilder.OrderBy("id").
- Desc().
- Limit(pageSize).
- Offset(offset)
- sql := qBuilder.String()
- o := orm.NewOrm()
- total, err := o.Raw(sql, userid).QueryRows(&list)
- if err != nil {
- return nil,0
- }
- return list,total
- }
- // 报修用户报修数量
- func (a *Repair)FindRepairUserRepairCount(userid int)(int64) {
- return a.findRepairCountByStatus(userid,2,"status > 0")
- }
- // --------------维修用户维修管理----------------------
- // 查找维修用户待维修数量
- func (a *Repair)FindServiceUserWRepairCount(userid int)(int64) {
- return a.findRepairCountByStatus(userid,2,"status = 2")
- }
- // 查找维修用户已维修数量
- func (a *Repair)FindServiceUserHRepairCount(userid int)(int64) {
- return a.findRepairCountByStatus(userid,2,"status > 2")
- }
- // 查找维修用户待维修列表
- func (a *Repair)FindServiceUserWRepairs(userid int,page int , pageSize int)([]*Repair,int64) {
- list := make([]*Repair, 0)
- qBuilder := a.UserRepairQBuilder("*")
- qBuilder.And("applicant_user_id = ?")
- qBuilder.And("status = 2")
- offset := (page - 1) * pageSize
- qBuilder.OrderBy("id").
- Desc().
- Limit(pageSize).
- Offset(offset)
- sql := qBuilder.String()
- o := orm.NewOrm()
- total, err := o.Raw(sql, userid).QueryRows(&list)
- if err != nil {
- return nil,0
- }
- return list,total
- }
- // 查找维修用户已维修列表
- func (a *Repair)FindServiceUserHRepairs(userid int,page int , pageSize int)([]*Repair,int64) {
- list := make([]*Repair, 0)
- qBuilder := a.UserRepairQBuilder("*")
- qBuilder.And("repair_user_id = ?")
- qBuilder.And("status > 2")
- offset := (page - 1) * pageSize
- qBuilder.OrderBy("id").
- Desc().
- Limit(pageSize).
- Offset(offset)
- sql := qBuilder.String()
- o := orm.NewOrm()
- total, err := o.Raw(sql, userid).QueryRows(&list)
- if err != nil {
- return nil,0
- }
- return list,total
- }
- // 确认维修
- func (a *Repair)ConfirmRepair()(int,error){
- o := orm.NewOrm()
- _, err := o.Raw("UPDATE pp_repair SET status = 3 , repair_time = ? WHERE id = ?" +
- " and status = 2 and repair_user_id=?", a.RepairTime,a.Id, a.RepairUserId).Exec()
- if err != nil {
- return a.Id,nil
- }
- return 0,nil
- }
- // --------------管理员维修管理----------------------
- // 查找待维修数量
- func (a *Repair)FindAdminWRepairCount(userid int)(int64) {
- return a.findRepairCountByStatus(userid,0,"status > 0 and status < 3")
- }
- // 查找已维修数量
- func (a *Repair)FindAdminHRepairCount(userid int)(int64) {
- return a.findRepairCountByStatus(userid,0,"status >= 3")
- }
- // 查找待维修列表
- func (a *Repair)FindAdminWRepairs(page int , pageSize int)([]*Repair,int64) {
- list := make([]*Repair, 0)
- qBuilder := a.UserRepairQBuilder("*")
- qBuilder.And("status > 0 and status < 3")
- offset := (page - 1) * pageSize
- qBuilder.OrderBy("id").
- Desc().
- Limit(pageSize).
- Offset(offset)
- sql := qBuilder.String()
- o := orm.NewOrm()
- total, err := o.Raw(sql).QueryRows(&list)
- if err != nil {
- return nil,0
- }
- return list,total
- }
- // 查找已维修列表
- func (a *Repair)FindAdminHRepairs(page int , pageSize int)([]*Repair,int64) {
- list := make([]*Repair, 0)
- qBuilder := a.UserRepairQBuilder("*")
- qBuilder.And("status >= 3")
- offset := (page - 1) * pageSize
- qBuilder.OrderBy("id").
- Desc().
- Limit(pageSize).
- Offset(offset)
- sql := qBuilder.String()
- o := orm.NewOrm()
- total, err := o.Raw(sql).QueryRows(&list)
- if err != nil {
- return nil,0
- }
- return list,total
- }
- // 查找维修详情
- func (a *Repair)FindRepairById(id int)(*Repair,error) {
- err := orm.NewOrm().QueryTable(models.TableName("repair")).Filter("id", id).One(a)
- if err != nil {
- return nil, err
- }
- return a, nil
- }
- // 指派维修任务
- func (a *Repair)AssignRepairTask(id int,repairUserId int ) (int64,error) {
- o := orm.NewOrm()
- result, err := o.Raw("UPDATE pp_repair SET status = 2 , repair_user_id = ? WHERE id = ?" +
- " and status = 1 ", repairUserId,id).Exec()
- if err != nil {
- return result.RowsAffected()
- }
- return 0,nil
- }
- // 用户报修SQL构造器
- func (a *Repair)UserRepairQBuilder(field string )(orm.QueryBuilder) {
- qb, _ := orm.NewQueryBuilder("mysql")
- qBuilder := qb.Select(field).From("pp_repair ")
- qBuilder.Where("id > 0")
- //if userid > 0{
- // qBuilder.Where("userid = ?")
- //}
- return qBuilder
- }
- // 查找维修数量
- func (a *Repair)findRepairCountByStatus(userid int,types int, value string)(int64) {
- o := orm.NewOrm()
- repairCount := new(RepairCount)
- where := " id > 0"
- if types == 1 {
- where += " and repair_user_id = ?"
- }else if types == 2{
- where += " and applicant_user_id = ?"
- }
- if value != "" {
- where += " and "+value
- }
- var err error
- if types == 0 {
- err = o.Raw("SELECT count(*)" +
- " as count FROM pp_repair WHERE"+where).QueryRow(&repairCount)
- }else{
- err = o.Raw("SELECT count(*)" +
- " as count FROM pp_repair WHERE"+where,userid).QueryRow(&repairCount)
- }
- if err != nil {
- return 0
- }
- return repairCount.Count
- }
- // 更新
- func (a *Repair) Update(fields ...string) error {
- if _, err := orm.NewOrm().Update(a, fields...); err != nil {
- return err
- }
- return nil
- }
|