289 lines
8.4 KiB
Go
289 lines
8.4 KiB
Go
package work
|
|
|
|
import (
|
|
model2 "ArmedPolice/app/common/model"
|
|
"ArmedPolice/app/controller/basic"
|
|
"ArmedPolice/app/model"
|
|
"ArmedPolice/app/service"
|
|
"ArmedPolice/serve/orm"
|
|
"ArmedPolice/utils"
|
|
"errors"
|
|
"gorm.io/gorm"
|
|
"time"
|
|
)
|
|
|
|
type Repair struct{ *service.Session }
|
|
|
|
type RepairHandle func(session *service.Session) *Repair
|
|
|
|
type (
|
|
// RepairInfo 基本信息
|
|
RepairInfo struct {
|
|
basic.CommonIDString
|
|
*model.WorkRepairInfo
|
|
}
|
|
// RepairDetailInfo 详细信息
|
|
RepairDetailInfo struct {
|
|
*RepairInfo
|
|
//Details []*model2.WorkRepairDetail `json:"details"`
|
|
Work *InstanceDetailInfo `json:"work"`
|
|
}
|
|
)
|
|
|
|
// List 列表信息
|
|
func (c *Repair) List(orderNo, equipmentCode, equipmentTitle string, page, pageSize int) (*basic.PageDataResponse, error) {
|
|
mWorkRepair := model.NewWorkRepair()
|
|
|
|
where := make([]*model2.ModelWhere, 0)
|
|
|
|
if orderNo != "" {
|
|
where = append(where, model2.NewWhereLike("r.order_no", orderNo))
|
|
}
|
|
if equipmentCode != "" {
|
|
where = append(where, model2.NewWhereLike("e.code", equipmentCode))
|
|
}
|
|
if equipmentTitle != "" {
|
|
where = append(where, model2.NewWhereLike("e.title", equipmentTitle))
|
|
}
|
|
var count int64
|
|
|
|
out, err := mWorkRepair.Repairs(page, pageSize, &count, where...)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
list := make([]*RepairInfo, 0)
|
|
|
|
mSysBreakdown := model.NewSysBreakdown()
|
|
|
|
for _, v := range out {
|
|
v.BreakdownTitle = mSysBreakdown.BreakdownTitle(v.Breakdown)
|
|
|
|
list = append(list, &RepairInfo{
|
|
CommonIDString: basic.CommonIDString{ID: v.GetEncodeID()},
|
|
WorkRepairInfo: v,
|
|
})
|
|
}
|
|
return &basic.PageDataResponse{Data: list, Count: count}, nil
|
|
}
|
|
|
|
// Detail 详细信息
|
|
func (c *Repair) Detail(id uint64) (*RepairDetailInfo, error) {
|
|
repair, err := model.NewWorkRepair().Detail(id)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
work := new(InstanceDetailInfo)
|
|
|
|
mWorkInstance := model.NewWorkInstance()
|
|
// 基本信息
|
|
if work.WorkInstanceInfo, err = mWorkInstance.Detail(repair.WorkID); err != nil {
|
|
return nil, err
|
|
}
|
|
mWorkInstance.SetID(work.WorkInstanceInfo.ID)
|
|
work.CommonIDString = basic.CommonIDString{
|
|
ID: mWorkInstance.GetEncodeID(),
|
|
}
|
|
work.EquipmentID = (&model2.Model{ID: work.WorkInstanceInfo.EquipmentID}).GetEncodeID()
|
|
work.ScheduleID = (&model2.Model{ID: work.WorkInstanceInfo.ScheduleID}).GetEncodeID()
|
|
// 位置信息
|
|
mWorkInstance.Distribution = work.WorkInstanceInfo.Distribution
|
|
|
|
work.Distribution = mWorkInstance.GetDistributionAttribute()
|
|
// 内修才有数据
|
|
if work.WorkInstanceInfo.Kind == model2.WorkInstanceKindForWithin {
|
|
// 器材信息
|
|
materials := make([]*model.WorkMaterialInfo, 0)
|
|
|
|
if materials, err = model.NewWorkMaterial().Materials(repair.WorkID); err != nil {
|
|
return nil, err
|
|
}
|
|
// 采购信息
|
|
purchases := make([]*model.WorkPurchaseInfo, 0)
|
|
|
|
if purchases, err = model.NewWorkPurchase().Purchases(id); err != nil {
|
|
return nil, err
|
|
}
|
|
work.Within = struct {
|
|
Material []*model.WorkMaterialInfo `json:"material"`
|
|
Purchase []*model.WorkPurchaseInfo `json:"purchase"`
|
|
}{Material: materials, Purchase: purchases}
|
|
} else if work.WorkInstanceInfo.Kind == model2.WorkInstanceKindForOutside {
|
|
work.Outside = struct {
|
|
SupplierName string `json:"supplier_name"`
|
|
}{SupplierName: work.WorkInstanceInfo.SupplierName}
|
|
}
|
|
return &RepairDetailInfo{
|
|
RepairInfo: &RepairInfo{
|
|
CommonIDString: basic.CommonIDString{ID: repair.GetEncodeID()},
|
|
WorkRepairInfo: repair,
|
|
},
|
|
Work: work,
|
|
//Details: details,
|
|
}, nil
|
|
}
|
|
|
|
// Begin 开始维修操作
|
|
func (c *Repair) Begin(id uint64, remark string) error {
|
|
mWorkRepair := model.NewWorkRepair()
|
|
mWorkRepair.ID = id
|
|
|
|
isExist, err := model2.FirstField(mWorkRepair.WorkRepair, []string{"id", "status"})
|
|
|
|
if err != nil {
|
|
return err
|
|
} else if !isExist {
|
|
return errors.New("操作错误,维修工单信息不存在或已被删除")
|
|
}
|
|
if mWorkRepair.Status != model2.WorkRepairStatusForNotBegin {
|
|
return errors.New("操作错误,维修工单状态异常,不可操作")
|
|
}
|
|
return orm.GetDB().Transaction(func(tx *gorm.DB) error {
|
|
if err = model2.Updates(mWorkRepair.WorkRepair, map[string]interface{}{
|
|
"status": model2.WorkRepairStatusForOngoing, "updated_at": time.Now(),
|
|
}, tx); err != nil {
|
|
return err
|
|
}
|
|
// 维修工单详情
|
|
mWorkRepairDetail := model.NewWorkRepairDetail()
|
|
mWorkRepairDetail.UID = c.UID
|
|
mWorkRepairDetail.RepairID = id
|
|
mWorkRepairDetail.Tage = model2.WorkRepairDetailTagsForBegin
|
|
mWorkRepairDetail.Remark = remark
|
|
|
|
if err = model2.Create(mWorkRepairDetail.WorkRepairDetail, tx); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// Finish 完成维修操作
|
|
func (c *Repair) Finish(id uint64, image, remark string) error {
|
|
mWorkRepair := model.NewWorkRepair()
|
|
mWorkRepair.ID = id
|
|
|
|
isExist, err := model2.FirstField(mWorkRepair.WorkRepair, []string{"id", "status"})
|
|
|
|
if err != nil {
|
|
return err
|
|
} else if !isExist {
|
|
return errors.New("操作错误,维修工单信息不存在或已被删除")
|
|
}
|
|
if mWorkRepair.Status != model2.WorkRepairStatusForOngoing {
|
|
return errors.New("操作错误,维修工单状态异常,不可操作")
|
|
}
|
|
return orm.GetDB().Transaction(func(tx *gorm.DB) error {
|
|
if err = model2.Updates(mWorkRepair.WorkRepair, map[string]interface{}{
|
|
"status": model2.WorkRepairStatusForFinished, "updated_at": time.Now(),
|
|
}, tx); err != nil {
|
|
return err
|
|
}
|
|
// 维修工单详情
|
|
mWorkRepairDetail := model.NewWorkRepairDetail()
|
|
mWorkRepairDetail.UID = c.UID
|
|
mWorkRepairDetail.RepairID = id
|
|
mWorkRepairDetail.Tage = model2.WorkRepairDetailTagsForFinish
|
|
mWorkRepairDetail.Remark = remark
|
|
mWorkRepairDetail.Other = utils.AnyToJSON(&model2.WorkRepairDetailForFinish{Image: image})
|
|
|
|
if err = model2.Create(mWorkRepairDetail.WorkRepairDetail, tx); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// Evaluate 评价
|
|
func (c *Repair) Evaluate(id uint64, score float64) error {
|
|
mWorkRepair := model.NewWorkRepair()
|
|
mWorkRepair.ID = id
|
|
|
|
isExist, err := model2.FirstField(mWorkRepair.WorkRepair, []string{"id", "work_id", "status", "is_evaluate"})
|
|
|
|
if err != nil {
|
|
return err
|
|
} else if !isExist {
|
|
return errors.New("操作错误,维修工单信息不存在或已被删除")
|
|
}
|
|
if mWorkRepair.Status != model2.WorkRepairStatusForFinished {
|
|
return errors.New("操作错误,维修工单状态异常,不可操作")
|
|
}
|
|
if mWorkRepair.IsEvaluate != model2.WorkRepairStatusEvaluateForNot {
|
|
return errors.New("操作错误,当前工单已评价,不可重复评价")
|
|
}
|
|
// 查询工单下使用的器材供应商信息
|
|
mWorkMaterial := model.NewWorkMaterial()
|
|
supplierIDs := make([]uint64, 0)
|
|
|
|
if err = model2.Pluck(mWorkMaterial.WorkMaterial, "material_supplier_id", &supplierIDs,
|
|
model2.NewWhere("work_id", mWorkRepair.WorkID)); err != nil {
|
|
return err
|
|
}
|
|
_supplierIDs := utils.ArrayUnique(supplierIDs)
|
|
|
|
return orm.GetDB().Transaction(func(tx *gorm.DB) error {
|
|
if err = model2.Updates(mWorkRepair.WorkRepair, map[string]interface{}{
|
|
"is_evaluate": model2.WorkRepairStatusEvaluateForYes, "evaluate_score": score, "updated_at": time.Now(),
|
|
}, tx); err != nil {
|
|
return err
|
|
}
|
|
updates := map[string]interface{}{
|
|
"updated_at": time.Now(),
|
|
}
|
|
if score == 1 {
|
|
updates["praise"] = gorm.Expr("praise + ?", 1)
|
|
} else if score == 2 {
|
|
updates["middle"] = gorm.Expr("middle + ?", 1)
|
|
} else {
|
|
updates["negative"] = gorm.Expr("negative + ?", 1)
|
|
}
|
|
mManageSupplierEvaluate := model.NewManageSupplierEvaluate()
|
|
|
|
supplierEvaluates := make([]*model2.ManageSupplierEvaluate, 0)
|
|
|
|
for _, v := range _supplierIDs {
|
|
// 查询是否存在
|
|
if isExist, err = model2.FirstField(mManageSupplierEvaluate.ManageSupplierEvaluate, []string{"id"},
|
|
model2.NewWhere("id", v)); err != nil {
|
|
return err
|
|
} else if isExist {
|
|
if err = model2.Updates(mManageSupplierEvaluate.ManageSupplierEvaluate, updates, tx); err != nil {
|
|
return err
|
|
}
|
|
continue
|
|
}
|
|
data := &model2.ManageSupplierEvaluate{SupplierID: v.(uint64)}
|
|
|
|
if score == 1 {
|
|
data.Praise = 1
|
|
} else if score == 2 {
|
|
data.Middle = 1
|
|
} else {
|
|
data.Negative = 1
|
|
}
|
|
supplierEvaluates = append(supplierEvaluates, data)
|
|
}
|
|
if len(supplierEvaluates) > 0 {
|
|
if err = model2.Creates(mManageSupplierEvaluate.ManageSupplierEvaluate, supplierEvaluates, tx); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// Delete 删除操作
|
|
func (c *Repair) Delete(id uint64) error {
|
|
mWorkRepair := model.NewWorkRepair()
|
|
mWorkRepair.ID = id
|
|
return model2.Delete(mWorkRepair.WorkRepair)
|
|
}
|
|
|
|
func NewRepair() RepairHandle {
|
|
return func(session *service.Session) *Repair {
|
|
return &Repair{session}
|
|
}
|
|
}
|