zhangqian
2023-11-01 530fed8ec225453572d57b15c200ab062c335457
model/serviceOrder.go
@@ -5,62 +5,75 @@
   "aps_crm/pkg/mysqlx"
   "errors"
   "fmt"
   "github.com/shopspring/decimal"
   "gorm.io/gorm"
   "gorm.io/gorm/clause"
)
type (
   // ServiceOrder 服务单
   ServiceOrder struct {
      Id              int           `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
      ServiceNumber   string        `gorm:"service_number" json:"service_number"` // 服务单编号
      ClientId        int           `gorm:"client_id" json:"client_id"`           // 客户id
      Client          Client        `gorm:"foreignKey:ClientId"`
      ContractId      int           `gorm:"contract_id" json:"contract_id"` // 合同id
      Contract        Contract      `gorm:"foreignKey:ContractId"`
      OrderId         int           `gorm:"order_id" json:"order_id"` // 销售订单id
      Order           OrderManage   `gorm:"foreignKey:OrderId"`
      Subject         string        `gorm:"subject" json:"subject"`       // 主题
      ProductId       int           `gorm:"product_id" json:"product_id"` // 产品id
      Product         Product       `gorm:"foreignKey:ProductId"`
      ServiceTypeId   int           `gorm:"service_type_id" json:"service_type_id"` // 服务方式
      ServiceType     ServiceType   `gorm:"foreignKey:ServiceTypeId"`
      ServiceManId    int           `gorm:"service_man_id" json:"service_man_id"`       // 服务人员
      LinkmanId       int           `gorm:"linkman_id" json:"linkman_id"`               // 联系人id
      Address         string        `gorm:"address" json:"address"`                     // 上门地址
      PriorityLevelId int           `gorm:"priority_level_id" json:"priority_level_id"` // 优先级别
      PriorityLevel   PriorityLevel `gorm:"foreignKey:PriorityLevelId"`
      AppointmentTime string        `gorm:"appointment_time" json:"appointment_time"` // 预约上门时间
      SaleLeads       int           `gorm:"sale_leads" json:"sale_leads"`             // 销售机会id
      SeverityId      int           `gorm:"severity_id" json:"severity"`              // 严重程度
      Severity        Severity      `gorm:"foreignKey:SeverityId"`
      Status          int           `gorm:"status" json:"status"`               // 处理状态
      ExpectTime      string        `gorm:"expect_time" json:"expect_time"`     // 希望处理时间
      RealTime        string        `gorm:"real_time" json:"real_time"`         // 实际处理时间
      CarFare         float64       `gorm:"car_fare" json:"car_fare"`           // 交通费
      ChargeAmount    float64       `gorm:"charge_amount" json:"charge_amount"` // 收费金额
      TimeSpentId     int           `gorm:"time_spent_id" json:"time_spent_id"` // 花费时间
      FaqId           int           `gorm:"problem_id" json:"problem_id"`       // 常见问题id
      Faq             Faq           `gorm:"foreignKey:FaqId"`
      ProblemDesc     string        `gorm:"problem_desc" json:"problem_desc"`       // 问题描述
      Solution        string        `gorm:"solution" json:"solution"`               // 解决方法
      SolutionRemark  string        `gorm:"solution_remark" json:"solution_remark"` // 内部备注
      Remark          string        `gorm:"remark" json:"remark"`                   // 备注
      Id                   int                `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
      ServiceNumber        string             `gorm:"column:service_number;type:varchar(255);not null;default:'';comment:服务单编号" json:"serviceNumber"` // 服务单编号
      ClientId             int                `gorm:"column:client_id;type:int;not null;default:0;comment:客户id" json:"clientId"`                      // 客户id
      Client               Client             `gorm:"foreignKey:ClientId"`
      ServiceContractId    int                `gorm:"column:contract_id;type:int;not null;default:0;comment:服务合同id" json:"serviceContractId"` // 服务合同id
      ServiceContract      ServiceContract    `gorm:"foreignKey:ServiceContractId"`
      SalesDetailsId       int                `gorm:"column:order_id;type:int;not null;default:0;comment:销售订单id" json:"salesDetailsId"` // 销售明细id
      SalesDetails         SalesDetails       `gorm:"foreignKey:SalesDetailsId"`
      Subject              string             `gorm:"column:subject;type:varchar(255);not null;default:'';comment:主题" json:"subject"`                       // 主题
      ProductTypeName      string             `gorm:"column:product_type_name;type:varchar(255);not null;default:'';comment:产品分类名称" json:"productTypeName"` // 产品分类名称
      ProductName          string             `gorm:"column:product_name;type:varchar(255);not null;default:'';comment:产品名称" json:"productName"`            // 产品名称
      ServiceTypeId        int                `gorm:"column:service_type_id;type:int;not null;default:0;comment:服务方式id" json:"serviceTypeId"`               // 服务方式id
      ServiceType          ServiceType        `gorm:"foreignKey:ServiceTypeId"`
      ServiceManId         int                `gorm:"column:service_man_id;type:int;not null;default:0;comment:服务人员" json:"serviceManId"` // 服务人员
      ServiceMan           User               `gorm:"foreignKey:ServiceManId" json:"serviceMan"`                                          // 服务人员
      ContactId            int                `gorm:"linkman_id" json:"contactId"`                                                        // 联系人id
      Contact              Contact            `gorm:"foreignKey:ContactId"`
      Address              string             `gorm:"column:address;type:varchar(255);not null;default:'';comment:上门地址" json:"address"`           // 上门地址
      PriorityLevelId      int                `gorm:"column:priority_level_id;type:int;not null;default:0;comment:优先级别id" json:"priorityLevelId"` // 优先级别id
      PriorityLevel        PriorityLevel      `gorm:"foreignKey:PriorityLevelId"`
      AppointmentTime      string             `gorm:"appointment_time" json:"appointmentTime"`                                              // 预约上门时间
      SaleChanceId         int                `gorm:"column:sale_chance_id;type:int;not null;default:0;comment:销售机会id" json:"saleChanceId"` // 销售机会id
      SaleChance           SaleChance         `gorm:"foreignKey:SaleChanceId"`
      FaultTypeId          int                `gorm:"column:severity_id;type:int;not null;default:0;comment:故障类别id" json:"faultTypeId"` // 故障类别id
      FaultType            FaultType          `gorm:"foreignKey:FaultTypeId"`
      SeverityId           int                `gorm:"column:severity_order_id;type:int;not null;default:0;comment:严重程度id" json:"severityId"` // 严重程度id
      Severity             Severity           `gorm:"foreignKey:SeverityId"`
      ServiceOrderStatusId int                `gorm:"column:service_order_status_id;type:int;not null;default:0;comment:服务单状态id" json:"serviceOrderStatusId"` // 处理状态
      ServiceOrderStatus   ServiceOrderStatus `gorm:"foreignKey:ServiceOrderStatusId"`
      ExpectTime           string             `gorm:"column:expect_time;type:varchar(255);not null;default:'';comment:希望处理时间" json:"expectTime"`          // 希望处理时间
      RealTime             string             `gorm:"column:real_time;type:varchar(255);not null;default:'';comment:实际处理时间" json:"realTime"`              // 实际处理时间
      CarFare              decimal.Decimal    `gorm:"column:car_fare;type:decimal(10,2);not null;default:'0.00';comment:希望处理时间" json:"carFare"`           //交通费                                                                 // 交通费
      ChargeAmount         decimal.Decimal    `gorm:"column:charge_amount;type:decimal(10,2);not null;default:'0.00';comment:希望处理时间" json:"chargeAmount"` //收费金额                                                       // 收费金额
      TimeSpentId          int                `gorm:"column:time_spent_id;type:int;not null;default:0;comment:花费时间" json:"timeSpentId"`                   // 花费时间
      TimeSpent            TimeSpent          `gorm:"foreignKey:TimeSpentId"`
      FaqId                int                `gorm:"column:faq_id;type:int;not null;default:0;comment:常见问题id" json:"faqId"` // 常见问题id
      Faq                  Faq                `gorm:"foreignKey:FaqId"`
      ProblemDesc          string             `gorm:"column:problem_desc;type:varchar(255);not null;default:'';comment:主题" json:"problemDesc"`         // 问题描述
      Solution             string             `gorm:"column:solution;type:varchar(255);not null;default:'';comment:解决方法" json:"solution"`              // 解决方法
      SolutionRemark       string             `gorm:"column:solution_remark;type:varchar(255);not null;default:'';comment:内部备注" json:"solutionRemark"` // 内部备注
      Remark               string             `gorm:"column:remark;type:varchar(255);not null;default:'';comment:备注" json:"remark"`                    // 备注
      CodeStandID          string             `json:"codeStandID" gorm:"column:code_stand_id;type:varchar(255);comment:编码id"`
      CrmModel
   }
   // ServiceOrderSearch 服务单搜索条件
   ServiceOrderSearch struct {
      ServiceOrder
      Orm         *gorm.DB
      QueryClass  constvar.ServiceOrderQueryClass
      KeywordType constvar.ServiceOrderKeywordType
      Keyword     string
      PageNum     int
      PageSize    int
      Preload     bool
      Orm           *gorm.DB
      QueryClass    constvar.ServiceOrderQueryClass
      KeywordType   constvar.ServiceOrderKeywordType
      Keyword       string
      PageNum       int
      PageSize      int
      Preload       bool
      ServiceManIds []int
      Order         string
   }
)
func (ServiceOrder) TableName() string {
func (slf *ServiceOrder) TableName() string {
   return "service_order"
}
@@ -75,16 +88,54 @@
   if slf.Id != 0 {
      db = db.Where("id = ?", slf.Id)
   }
   if slf.ServiceContractId != 0 {
      db = db.Where("contract_id = ?", slf.ServiceContractId)
   }
   if slf.SalesDetailsId != 0 {
      db = db.Where("order_id = ?", slf.SalesDetailsId)
   }
   if slf.Order != "" {
      db = db.Order(slf.Order)
   }
   if slf.Preload {
      db = db.Model(&ServiceOrder{}).
      db = db.
         Preload("Client").
         Preload("Contract").
         Preload("OrderManage").
         Preload("Product").
         Preload("ServiceContract").
         Preload("SalesDetails").
         Preload("ServiceType").
         Preload("Contact").
         Preload("ServiceMan").
         Preload("PriorityLevel").
         Preload("SaleChance").
         Preload("FaultType").
         Preload("Severity").
         Preload("ServiceOrderStatus").
         Preload("TimeSpent").
         Preload("Faq")
   }
   switch slf.KeywordType {
   case constvar.ServiceOrderKeywordClientName:
      db = db.Joins("Client", clause.LeftJoin).Where("Client.name like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
   case constvar.ServiceOrderKeywordOrderNumber:
      db = db.Where("service_number = ?", fmt.Sprintf("%%%s%%", slf.Keyword))
   case constvar.ServiceOrderKeywordSubject:
      db = db.Where("subject = ?", fmt.Sprintf("%%%s%%", slf.Keyword))
   case constvar.ServiceOrderKeywordRealProcessTime:
      db = db.Where("real_time = ?", fmt.Sprintf("%%%s%%", slf.Keyword))
   case constvar.ServiceOrderKeywordServiceMan:
      db = db.Joins("left join user on user.id = service_order.service_man_id").Where("user.username like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
   case constvar.ServiceOrderKeywordServiceType:
      db = db.Joins("ServiceType", clause.LeftJoin).Where("ServiceType.name like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
   case constvar.ServiceOrderKeywordFaultType:
      db = db.Joins("FaultType", clause.LeftJoin).Where("FaultType.name like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
   }
   if len(slf.ServiceManIds) > 0 {
      db = db.Where("service_order.service_man_id in ?", slf.ServiceManIds)
   }
   if slf.ServiceNumber != "" {
      db = db.Where("service_number = ?", slf.ServiceNumber)
   }
   return db
@@ -97,6 +148,12 @@
func (slf *ServiceOrderSearch) Delete() error {
   var db = slf.build()
   return db.Delete(&ServiceOrder{}).Error
}
func (slf *ServiceOrderSearch) DeleteByIds(ids []int) error {
   var db = slf.build()
   db = db.Where("id in ?", ids)
   return db.Delete(&ServiceOrder{}).Error
}
@@ -117,8 +174,53 @@
   return slf
}
func (slf *ServiceOrderSearch) SetServiceContractId(id int) *ServiceOrderSearch {
   slf.ServiceContractId = id
   return slf
}
func (slf *ServiceOrderSearch) SetSalesDetailsId(id int) *ServiceOrderSearch {
   slf.SalesDetailsId = id
   return slf
}
func (slf *ServiceOrderSearch) SetPage(page, size int) *ServiceOrderSearch {
   slf.PageNum, slf.PageSize = page, size
   return slf
}
func (slf *ServiceOrderSearch) SetKeywordType(keyword constvar.ServiceOrderKeywordType) *ServiceOrderSearch {
   slf.KeywordType = keyword
   return slf
}
func (slf *ServiceOrderSearch) SetQueryClass(queryClass constvar.ServiceOrderQueryClass) *ServiceOrderSearch {
   slf.QueryClass = queryClass
   return slf
}
func (slf *ServiceOrderSearch) SetKeyword(keyword string) *ServiceOrderSearch {
   slf.Keyword = keyword
   return slf
}
func (slf *ServiceOrderSearch) SetMemberIds(memberIds []int) *ServiceOrderSearch {
   slf.ServiceManIds = memberIds
   return slf
}
func (slf *ServiceOrderSearch) SetPreload(preload bool) *ServiceOrderSearch {
   slf.Preload = preload
   return slf
}
func (slf *ServiceOrderSearch) SetOrder(order string) *ServiceOrderSearch {
   slf.Order = order
   return slf
}
func (slf *ServiceOrderSearch) SetNumber(number string) *ServiceOrderSearch {
   slf.ServiceNumber = number
   return slf
}
@@ -161,3 +263,32 @@
   err := db.Find(&records).Error
   return records, total, err
}
func (slf *ServiceOrderSearch) UpdateByMap(data map[string]interface{}) error {
   var db = slf.build()
   return db.Updates(data).Error
}
func (slf *ServiceOrderSearch) Count() (int64, error) {
   var db = slf.build()
   var total int64
   err := db.Count(&total).Error
   return total, err
}
func (slf *ServiceOrderSearch) MaxAutoIncr() (int, error) {
   type Result struct {
      Max int
   }
   var (
      result Result
      db     = slf.build()
   )
   err := db.Select("MAX(id) as max").Scan(&result).Error
   if err != nil {
      return result.Max, fmt.Errorf("max err: %v", err)
   }
   return result.Max, nil
}