From 978918e5aa6963967c7c47859319d67a08814d94 Mon Sep 17 00:00:00 2001 From: henry Date: Tue, 16 Nov 2021 11:46:44 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E5=AE=8C=E5=96=84=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + app/api/dashboard.go | 17 ++++-- app/common/init.go | 2 +- app/common/model/manage_supplier_evaluate.go | 20 +++++++ app/controller/account/instance.go | 3 +- app/controller/dashboard/instance.go | 16 ++++- app/controller/dashboard/supplier.go | 9 ++- app/controller/work/repair.go | 63 ++++++++++++++++++-- app/model/manage_supplier.go | 32 +++++++++- app/model/manage_supplier_evaluate.go | 13 ++++ app/model/work_repair.go | 4 ++ router/router.go | 1 - utils/array.go | 21 +++++++ utils/array_test.go | 10 ++++ 14 files changed, 193 insertions(+), 19 deletions(-) create mode 100644 app/common/model/manage_supplier_evaluate.go create mode 100644 app/model/manage_supplier_evaluate.go diff --git a/.gitignore b/.gitignore index 5dea15a..d88d71d 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ doc/ log/ dist/ +data/ cmd/ctl/main cmd/ctl/ctl main diff --git a/app/api/dashboard.go b/app/api/dashboard.go index 754f124..a1fb6c4 100644 --- a/app/api/dashboard.go +++ b/app/api/dashboard.go @@ -32,6 +32,11 @@ type Dashboard struct{} * @apiSuccess (200) {String} data.notice.id ID * @apiSuccess (200) {String} data.notice.title 公告标题 * @apiSuccess (200) {Time} data.notice.created_at 公告创建时间 +* @apiSuccess (200) {Object} data.evaluate 评价信息 +* @apiSuccess (200) {String} data.evaluate.name ID +* @apiSuccess (200) {Number} data.evaluate.praise 好评数 +* @apiSuccess (200) {Number} data.evaluate.middle 中评数 +* @apiSuccess (200) {Number} data.evaluate.negative 差评数 * @apiSuccess (200) {Number} code 成功响应状态码! * @apiSuccess (200) {String} msg 成功提示 * @@ -48,11 +53,17 @@ type Dashboard struct{} * "breakdown_title": "" * "username": "" * "created_at": "" -* ] +* ], * "notice": [ * "id": "", * "title": "", * "created_at": "2021-11-01" +* ], +* "evaluate": [ +* "name": "", +* "praise": 1, +* "middle": 0, +* "negative": 0, * ] * } * } @@ -104,7 +115,3 @@ func (*Dashboard) Repair(c *gin.Context) { data, err := dashboard.NewRepair()(getSession()(c).(*service.Session)).Static(form.Date) APIResponse(err, data)(c) } - -func (*Dashboard) Score(c *gin.Context) { - -} diff --git a/app/common/init.go b/app/common/init.go index 88238f8..7024869 100644 --- a/app/common/init.go +++ b/app/common/init.go @@ -50,7 +50,7 @@ func initModel() { // 公告管理 &synchronized{iModel: model.NewManageNotice()}, // 功能信息 - &synchronized{iModel: model.NewManageSupplier()}, + &synchronized{iModel: model.NewManageSupplier()}, &synchronized{iModel: model.NewManageSupplierEvaluate()}, &synchronized{iModel: model.NewManageEquipment()}, &synchronized{iModel: model.NewManageEquipmentMaterial()}, &synchronized{iModel: model.NewManageMaterial()}, &synchronized{iModel: model.NewManageMaterialSupplier()}, &synchronized{iModel: model.NewManageMaterialPurchase()}, &synchronized{iModel: model.NewManageMaterialWarehouse()}, diff --git a/app/common/model/manage_supplier_evaluate.go b/app/common/model/manage_supplier_evaluate.go new file mode 100644 index 0000000..ea9e0f5 --- /dev/null +++ b/app/common/model/manage_supplier_evaluate.go @@ -0,0 +1,20 @@ +package model + +// ManageSupplierEvaluate 供应商评价数据模型 +type ManageSupplierEvaluate struct { + Model + SupplierID uint64 `gorm:"column:supplier_id;type:int(11);default:0;comment:供应商ID" json:"-"` + Praise int `gorm:"column:praise;type:int(8);default:0;comment:好评" json:"praise"` + Middle int `gorm:"column:middle;type:int(8);default:0;comment:中评" json:"middle"` + Negative int `gorm:"column:negative;type:int(8);default:0;comment:差评" json:"negative"` + ModelDeleted + ModelAt +} + +func (m *ManageSupplierEvaluate) TableName() string { + return "manage_supplier_evaluate" +} + +func NewManageSupplierEvaluate() *ManageSupplierEvaluate { + return &ManageSupplierEvaluate{} +} diff --git a/app/controller/account/instance.go b/app/controller/account/instance.go index 98b208b..c621d86 100644 --- a/app/controller/account/instance.go +++ b/app/controller/account/instance.go @@ -68,7 +68,8 @@ func (c *Instance) Login(account, password, captchaKey, captchaValue, ip string) service.Publish(config.EventForAccountLoginProduce, session.TenantID, session.UID, ip) return &InstanceLoginResponse{Token: session.Token, EffectTime: config.SettingInfo.TokenEffectTime, - WsUrl: config.SystemConfig[config.WsDomain].(string)}, nil + //WsUrl: config.SystemConfig[config.WsDomain].(string)}, nil + WsUrl: ""}, nil } // Logout 退出请求 diff --git a/app/controller/dashboard/instance.go b/app/controller/dashboard/instance.go index c102ce1..2fa0008 100644 --- a/app/controller/dashboard/instance.go +++ b/app/controller/dashboard/instance.go @@ -15,8 +15,9 @@ type InstanceHandle func(session *service.Session) *Instance type ( // InstanceInfo 列表信息 InstanceInfo struct { - Workbench []*instanceInfoForWorkbench `json:"workbench"` - Notice []*instanceInfoForNotice `json:"notice"` + Workbench []*instanceInfoForWorkbench `json:"workbench"` + Notice []*instanceInfoForNotice `json:"notice"` + Evaluate []*model.ManageSupplierEvaluateInfo `json:"evaluate"` } // instanceInfoForWorkbench 待办事项 instanceInfoForWorkbench struct { @@ -33,6 +34,9 @@ type ( Title string `json:"title"` CreatedAt time.Time `json:"created_at"` } + // instanceInfoForEvaluate 好评信息 + instanceInfoForEvaluate struct { + } ) var defaultDataLimit int = 10 @@ -97,6 +101,11 @@ func (c *Instance) notice(limit int) ([]*instanceInfoForNotice, error) { return list, nil } +func (c *Instance) evaluate(limit int) ([]*model.ManageSupplierEvaluateInfo, error) { + mManageSupplier := model.NewManageSupplier() + return mManageSupplier.Evaluate(model2.ManageSupplierKindForMaterial, limit) +} + // Index 首页信息 func (c *Instance) Index() (*InstanceInfo, error) { out := new(InstanceInfo) @@ -108,6 +117,9 @@ func (c *Instance) Index() (*InstanceInfo, error) { if out.Notice, err = c.notice(defaultDataLimit); err != nil { return nil, err } + if out.Evaluate, err = c.evaluate(defaultDataLimit); err != nil { + return nil, err + } return out, nil } diff --git a/app/controller/dashboard/supplier.go b/app/controller/dashboard/supplier.go index f6d0d0b..4324ac2 100644 --- a/app/controller/dashboard/supplier.go +++ b/app/controller/dashboard/supplier.go @@ -1,7 +1,8 @@ package dashboard import ( - "ArmedPolice/app/controller/basic" + model2 "ArmedPolice/app/common/model" + "ArmedPolice/app/model" "ArmedPolice/app/service" ) @@ -9,8 +10,10 @@ type Supplier struct{ *service.Session } type SupplierHandle func(session *service.Session) *Supplier -func (c *Supplier) StaticScore() (*basic.PageDataResponse, error) { - return &basic.PageDataResponse{Data: nil, Count: 0}, nil +func (c *Supplier) StaticEvaluate() (interface{}, error) { + mManageSupplier := model.NewManageSupplier() + out, err := mManageSupplier.Evaluate(model2.ManageSupplierKindForMaterial, 10) + return out, err } func NewSupplier() SupplierHandle { diff --git a/app/controller/work/repair.go b/app/controller/work/repair.go index 76109e0..ea348c2 100644 --- a/app/controller/work/repair.go +++ b/app/controller/work/repair.go @@ -163,7 +163,7 @@ func (c *Repair) Evaluate(id uint64, score float64) error { mWorkRepair := model.NewWorkRepair() mWorkRepair.ID = id - isExist, err := model2.FirstField(mWorkRepair.WorkRepair, []string{"id", "status", "is_evaluate"}) + isExist, err := model2.FirstField(mWorkRepair.WorkRepair, []string{"id", "work_id", "status", "is_evaluate"}) if err != nil { return err @@ -176,12 +176,65 @@ func (c *Repair) Evaluate(id uint64, score float64) error { if mWorkRepair.IsEvaluate != model2.WorkRepairStatusEvaluateForNot { return errors.New("操作错误,当前工单已评价,不可重复评价") } - if err = model2.Updates(mWorkRepair.WorkRepair, map[string]interface{}{ - "is_evaluate": model2.WorkRepairStatusEvaluateForYes, "evaluate_score": score, "updated_at": time.Now(), - }); err != nil { + // 查询工单下使用的器材供应商信息 + 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 } - return nil + _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 = 2 + } 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 + }) } func NewRepair() RepairHandle { diff --git a/app/model/manage_supplier.go b/app/model/manage_supplier.go index aa9ad68..cc7bb40 100644 --- a/app/model/manage_supplier.go +++ b/app/model/manage_supplier.go @@ -1,11 +1,41 @@ package model -import "ArmedPolice/app/common/model" +import ( + "ArmedPolice/app/common/model" + "ArmedPolice/serve/orm" + "fmt" +) type ManageSupplier struct { *model.ManageSupplier } +type ManageSupplierEvaluateInfo struct { + Name string `json:"name"` + Praise int `json:"praise"` + Middle int `json:"middle"` + Negative int `json:"negative"` +} + +// Evaluate 评分统计 +// Deprecated: Test +func (m *ManageSupplier) Evaluate(kind model.ManageSupplierKind, limit int) ([]*ManageSupplierEvaluateInfo, error) { + db := orm.GetDB().Table(m.TableName()+" AS s"). + Select("s.name", "e.praise", "e.middle", "e.negative"). + Joins(fmt.Sprintf("LEFT JOIN %s AS e ON s.id = e.supplier_id AND e.is_deleted = %d", + model.NewManageSupplierEvaluate().TableName(), model.DeleteStatusForNot)). + Where("s.is_deleted = ?", model.DeleteStatusForNot). + Where("s.kind = ?", kind). + Where("e.id > ?", 0) + + out := make([]*ManageSupplierEvaluateInfo, 0) + + err := db.Order("e.praise " + model.OrderModeToDesc).Order("e.negative " + model.OrderModeToAsc). + Limit(limit).Scan(&out).Error + + return out, err +} + func NewManageSupplier() *ManageSupplier { return &ManageSupplier{model.NewManageSupplier()} } diff --git a/app/model/manage_supplier_evaluate.go b/app/model/manage_supplier_evaluate.go new file mode 100644 index 0000000..e72ee29 --- /dev/null +++ b/app/model/manage_supplier_evaluate.go @@ -0,0 +1,13 @@ +package model + +import ( + "ArmedPolice/app/common/model" +) + +type ManageSupplierEvaluate struct { + *model.ManageSupplierEvaluate +} + +func NewManageSupplierEvaluate() *ManageSupplierEvaluate { + return &ManageSupplierEvaluate{model.NewManageSupplierEvaluate()} +} diff --git a/app/model/work_repair.go b/app/model/work_repair.go index e47b19b..01901f1 100644 --- a/app/model/work_repair.go +++ b/app/model/work_repair.go @@ -20,6 +20,10 @@ type WorkRepairInfo struct { BreakdownTitle string `json:"breakdown_title"` } +type WorkRepairEvaluateInfo struct { + Name string `json:"name"` +} + // Repairs 维修工单信息 func (m *WorkRepair) Repairs(page, pageSize int, count *int64, where ...*model.ModelWhere) ([]*WorkRepairInfo, error) { db := orm.GetDB().Table(m.TableName()+" r"). diff --git a/router/router.go b/router/router.go index 847df93..5dd29fe 100644 --- a/router/router.go +++ b/router/router.go @@ -69,7 +69,6 @@ func (this *Router) registerAPI() { _api := new(api.Dashboard) dashboardV1.GET("/", _api.Index) dashboardV1.POST("/repair", _api.Repair) - dashboardV1.GET("/score", _api.Score) } // Account 接口管理 accountV1 := v1.Group("/account") diff --git a/utils/array.go b/utils/array.go index 30e3226..7d7798a 100644 --- a/utils/array.go +++ b/utils/array.go @@ -49,3 +49,24 @@ func ArrayStrings(src interface{}) []string { } return out } + +func ArrayUnique(src interface{}) []interface{} { + val := reflect.ValueOf(src) + + kind := val.Kind() + + out := make([]interface{}, 0) + + if kind == reflect.Slice || kind == reflect.Array { + for i := 0; i < val.Len(); i++ { + for _, v := range out { + if v == val.Index(i).Interface() { + goto BREAK + } + } + out = append(out, val.Index(i).Interface()) + BREAK: + } + } + return out +} diff --git a/utils/array_test.go b/utils/array_test.go index df7b738..43a2f8f 100644 --- a/utils/array_test.go +++ b/utils/array_test.go @@ -38,3 +38,13 @@ func TestArrayStrings(t *testing.T) { t.Log(b) t.Log(reflect.TypeOf(b).String()) } + +func TestArrayUnique(t *testing.T) { + a := []uint64{1, 2, 3, 4, 5, 5, 5} + b := ArrayUnique(a) + t.Log(b) + + for _, v := range b { + fmt.Printf(reflect.TypeOf(v).String()) + } +}