Files
2021-11-09 18:38:57 +08:00

328 lines
9.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package work
import (
model2 "ArmedPolice/app/common/model"
"ArmedPolice/app/controller/basic"
"ArmedPolice/app/handle"
"ArmedPolice/app/model"
"ArmedPolice/app/service"
"ArmedPolice/serve/orm"
"ArmedPolice/utils"
"errors"
"gorm.io/gorm"
"time"
)
type Instance struct{ *service.Session }
type InstanceHandle func(session *service.Session) *Instance
type (
// InstanceInfo 基本信息
InstanceInfo struct {
basic.CommonIDString
*model.WorkInstanceInfo
}
// InstanceDetailInfo 详细信息
InstanceDetailInfo struct {
basic.CommonIDString
*model.WorkInstanceInfo
}
// InstanceLaunchParams 发起参数信息
InstanceLaunchParams struct {
ID uint64
Kind int // 类型
Title string
EquipmentID uint64 // 装备ID
Breakdowns []uint64 // 故障类型
PlateNumber string // 车牌号
Priority, IsAssist int // 优先级,是否需要上级审批
Remark string // 备注信息
Material []*InstanceLaunchParamsForMaterial
Distribution *InstanceLaunchParamsForDistribution
}
// InstanceLaunchParamsForMaterial 配件信息
InstanceLaunchParamsForMaterial struct {
}
// InstanceLaunchParamsForDistribution 配送信息
InstanceLaunchParamsForDistribution struct {
Name, Mobile, Address string
}
)
func (c *Instance) publish(reviewer []string) {
utils.TryCatch(func() {
for _, v := range reviewer {
// Socket通知
service.HubMessage.EmitHandle(&service.HubEmit{
ID: v,
Msg: handle.NewWorkNotice("你有一条待办事项"),
})
}
})
}
// List 列表信息
func (c *Instance) List(materialID uint64, kind, page, pageSize int) (*basic.PageDataResponse, error) {
mWorkInstance := model.NewWorkInstance()
where := make([]*model2.ModelWhere, 0)
if materialID > 0 {
where = append(where, model2.NewWhere("w.material_id", materialID))
}
if kind > 0 {
where = append(where, model2.NewWhere("w.kind", kind))
}
var count int64
out, err := mWorkInstance.Instances(page, pageSize, &count, where...)
if err != nil {
return nil, err
}
list := make([]*InstanceInfo, 0)
for _, v := range out {
mWorkInstance.ID = v.ID
list = append(list, &InstanceInfo{
CommonIDString: basic.CommonIDString{ID: mWorkInstance.GetEncodeID()}, WorkInstanceInfo: v,
})
}
return &basic.PageDataResponse{Data: list, Count: count}, nil
}
// Person 个人提交的审核信息
func (c *Instance) Person(materialID uint64, kind, page, pageSize int) (*basic.PageDataResponse, error) {
mWorkInstance := model.NewWorkInstance()
where := make([]*model2.ModelWhere, 0)
if materialID > 0 {
where = append(where, model2.NewWhere("w.material_id", materialID))
}
if kind > 0 {
where = append(where, model2.NewWhere("w.kind", kind))
}
var count int64
out, err := mWorkInstance.Persons(c.UID, model2.WorkScheduleKindForRepair, page, pageSize, &count, where...)
if err != nil {
return nil, err
}
list := make([]*InstanceInfo, 0)
for _, v := range out {
mWorkInstance.ID = v.ID
list = append(list, &InstanceInfo{
CommonIDString: basic.CommonIDString{ID: mWorkInstance.GetEncodeID()}, WorkInstanceInfo: v,
})
}
return &basic.PageDataResponse{Data: list, Count: count}, nil
}
// Workbench 工作台
func (c *Instance) Workbench(materialID uint64, kind, page, pageSize int) (*basic.PageDataResponse, error) {
// 查询用户角色信息
mSysUserRole := model.NewSysUserRole()
roleIDs := make([]string, 0)
err := model2.Pluck(mSysUserRole.SysUserRole, "id", &roleIDs, model2.NewWhere("uid", c.UID))
if err != nil {
return nil, err
}
return nil, nil
}
func (c *Instance) Detail(id uint64) (interface{}, error) {
mWorkInstance := model.NewWorkInstance()
mWorkInstance.ID = id
return nil, nil
}
// Launch 发起工单申请
func (c *Instance) Launch(params *InstanceLaunchParams) error {
// 查询产品信息是否存在
mManageEquipment := model.NewManageEquipment()
var count int64
err := model2.Count(mManageEquipment.ManageEquipment, &count, model2.NewWhere("id", params.EquipmentID))
if err != nil {
return err
} else if count <= 0 {
return errors.New("操作错误,该装备信息不存在或已被删除")
}
// 工单信息
mWorkInstance := model.NewWorkInstance()
mWorkInstance.Kind = model2.WorkInstanceKind(params.Kind)
mWorkInstance.Title = params.Title
mWorkInstance.EquipmentID = params.EquipmentID
mWorkInstance.PlateNumber = params.PlateNumber
mWorkInstance.Priority = model2.WorkInstancePriority(params.Priority)
mWorkInstance.IsAssist = model2.WorkInstanceAssist(params.IsAssist)
mWorkInstance.SetBreakdownAttribute(params.Breakdowns)
mWorkInstance.SetDistributionAttribute(&model2.WorkInstanceDistribution{
Name: params.Distribution.Name, Mobile: params.Distribution.Mobile, Address: params.Distribution.Address,
})
mWorkInstance.Remark = params.Remark
// 工单流程信息
mWorkProgress := model.NewWorkProgress()
// 查询工单进度流程
// TODO工单流程
// 第一阶段:创建工单,默认创建时,直接完成
mWorkSchedule := model.NewWorkSchedule()
isExist := false
if isExist, err = mWorkSchedule.FirstSchedule(model2.WorkScheduleKindForRepair); err != nil {
return err
} else if !isExist {
return errors.New("操作错误,无工单流程信息,请先核实数据")
}
mWorkInstance.Schedule = mWorkSchedule.ID
// 下一流程
nextWorkSchedule := new(model.WorkScheduleInfo)
if nextWorkSchedule, err = mWorkSchedule.NextSchedule(mWorkInstance.IsAssist == model2.WorkInstanceAssistForYes); err != nil {
return err
}
if nextWorkSchedule == nil {
mWorkInstance.Status = model2.WorkInstanceStatusForComplete
}
return orm.GetDB().Transaction(func(tx *gorm.DB) error {
if err = model2.Create(mWorkInstance.WorkInstance, tx); err != nil {
return err
}
mWorkProgress.UID = c.UID
mWorkProgress.WorkID = mWorkInstance.ID
mWorkProgress.ScheduleID = mWorkInstance.Schedule
mWorkProgress.Status = model2.WorkProgressStatusForAgree
mWorkProgress.Remark = "发起工单"
if err = model2.Create(mWorkProgress.WorkProgress, tx); err != nil {
return err
}
if nextWorkSchedule != nil {
// 推送通知
go c.publish(nextWorkSchedule.Reviewer)
}
return nil
})
}
// Examine 审核操作
func (c *Instance) Examine(id uint64, status int, remark string, isAssist int) error {
_status := model2.WorkProgressStatus(status)
if _status != model2.WorkProgressStatusForAgree && _status != model2.WorkProgressStatusForRefuse {
return errors.New("操作错误,审核状态异常")
}
mWorkInstance := model.NewWorkInstance()
mWorkInstance.ID = id
isExist, err := model2.FirstField(mWorkInstance.WorkInstance, []string{"id", "schedule", "status"})
if err != nil {
return err
} else if !isExist {
return errors.New("操作错误,工单信息不存在")
} else if mWorkInstance.Status == model2.WorkInstanceStatusForComplete {
return errors.New("操作错误,当前工单信息已结束")
}
// 查询当前工单所在流程的信息
mWorkSchedule := model.NewWorkSchedule()
mWorkSchedule.ID = mWorkInstance.Schedule
if isExist, err = model2.First(mWorkSchedule); err != nil {
return err
} else if !isExist {
return errors.New("操作错误,未知的工单流程")
}
// 验证审核权限
isAuth := false
if isAuth, err = mWorkSchedule.ValidateAuth(c.UID); err != nil {
return err
} else if !isAuth {
return errors.New("操作错误,无权限审批")
}
if err = orm.GetDB().Transaction(func(tx *gorm.DB) error {
// 工单流程记录
mWorkProgress := model.NewWorkProgress()
mWorkProgress.UID = c.UID
mWorkProgress.WorkID = id
mWorkProgress.ScheduleID = mWorkSchedule.ID
mWorkProgress.Status = _status
mWorkProgress.Remark = remark
if err = model2.Create(mWorkProgress.WorkProgress, tx); err != nil {
return err
}
workUpdates := map[string]interface{}{
"status": model2.WorkInstanceStatusForComplete, "updated_at": time.Now(),
}
// 下一流程
nextScheduleInfo := new(model.WorkScheduleInfo)
// 拒绝审批,工单直接结束
if _status == model2.WorkProgressStatusForRefuse {
goto FINISH
}
// 下一流程信息
if nextScheduleInfo, err = mWorkSchedule.NextSchedule(model2.WorkInstanceAssist(isAssist) == model2.WorkInstanceAssistForYes); err != nil {
return err
}
// 无下一流程,工单直接完成
if nextScheduleInfo == nil || nextScheduleInfo.ID <= 0 {
goto FINISH
}
workUpdates["status"] = model2.WorkInstanceStatusForOngoing
workUpdates["schedule"] = nextScheduleInfo.ID
if nextScheduleInfo.IsNextStage {
workUpdates["is_assist"] = isAssist
}
FINISH:
if err = model2.Updates(mWorkInstance.WorkInstance, workUpdates, tx); err != nil {
return err
}
// 完成,创建维修工单
if workUpdates["status"] == model2.WorkInstanceStatusForComplete {
// 创建维修工单
mWorkRepair := model.NewWorkRepair()
mWorkRepair.WorkID = mWorkInstance.ID
if err = model2.Create(mWorkRepair.WorkRepair, tx); err != nil {
return err
}
}
// 推送通知
go c.publish(nextScheduleInfo.Reviewer)
return nil
}); err != nil {
return err
}
return nil
}
// Delete 删除操作
func (c *Instance) Delete(id uint64) error {
mWorkInstance := model.NewWorkInstance()
mWorkInstance.ID = id
return model2.Delete(mWorkInstance.WorkInstance)
}
func NewInstance() InstanceHandle {
return func(session *service.Session) *Instance {
return &Instance{session}
}
}