作者 yangfu

评分配置修改

package controllers
import (
"encoding/json"
"oppmg/common/log"
"oppmg/protocol"
"oppmg/services/config"
)
//BulletinController 公告
type ConfigController struct {
BaseController
}
//ConfigScore 评分配置模式
//@router /configScore [post]
func (this *ConfigController) ConfigScore() {
var msg *protocol.ResponseMessage
defer func() {
this.ResposeJson(msg)
}()
var request *protocol.ConfigScoreRequest
if err := json.Unmarshal(this.Ctx.Input.RequestBody, &request); err != nil {
log.Error("json 解析失败", err)
msg = protocol.BadRequestParam("1")
return
}
uid := this.GetUserId()
companyId := this.GetCompanyId()
if companyId <= 0 {
log.Debug("companyId:%d err", companyId)
msg = protocol.BadRequestParam("1")
return
}
if b, m := this.Valid(request); !b {
msg = m
return
}
rsp, err := config.ConfigScore(uid, companyId, request)
msg = protocol.NewReturnResponse(rsp, err)
return
}
//GetConfigScore 获取评分配置模式
//@router /getConfigScore [post]
func (this *ConfigController) GetConfigScore() {
var msg *protocol.ResponseMessage
defer func() {
this.ResposeJson(msg)
}()
var request *protocol.GetConfigScoreRequest
if err := json.Unmarshal(this.Ctx.Input.RequestBody, &request); err != nil {
log.Error("json 解析失败", err)
msg = protocol.BadRequestParam("1")
return
}
uid := this.GetUserId()
companyId := this.GetCompanyId()
if companyId <= 0 {
log.Debug("companyId:%d err", companyId)
msg = protocol.BadRequestParam("1")
return
}
if b, m := this.Valid(request); !b {
msg = m
return
}
rsp, err := config.GetConfigScore(uid, companyId, request)
msg = protocol.NewReturnResponse(rsp, err)
return
}
... ...
package models
import (
"fmt"
"time"
"github.com/astaxie/beego/orm"
)
type SysConfig struct {
Id int `orm:"column(id);auto" description:"唯一编号"`
Key string `orm:"column(key);size(50);null" description:"自定义键值 score:评分模式配置"`
Content string `orm:"column(content);size(1000);null" description:"配置内容 json"`
CreateAt time.Time `orm:"column(create_at);type(timestamp);null"`
UpdateAt time.Time `orm:"column(update_at);type(timestamp);null"`
CompanyId int `orm:"column(company_id);null" description:"公司编号"`
}
func (t *SysConfig) TableName() string {
return "sys_config"
}
func init() {
orm.RegisterModel(new(SysConfig))
}
var (
KeyScore = "score"
)
// AddSysConfig insert a new SysConfig into database and returns
// last inserted Id on success.
func AddSysConfig(m *SysConfig) (id int64, err error) {
o := orm.NewOrm()
id, err = o.Insert(m)
return
}
// GetSysConfigById retrieves SysConfig by Id. Returns error if
// Id doesn't exist
func GetSysConfigById(id int) (v *SysConfig, err error) {
o := orm.NewOrm()
v = &SysConfig{Id: id}
if err = o.Read(v); err == nil {
return v, nil
}
return nil, err
}
// UpdateSysConfig updates SysConfig by Id and returns error if
// the record to be updated doesn't exist
func UpdateSysConfigById(m *SysConfig) (err error) {
o := orm.NewOrm()
v := SysConfig{Id: m.Id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Update(m); err == nil {
fmt.Println("Number of records updated in database:", num)
}
}
return
}
// DeleteSysConfig deletes SysConfig by Id and returns error if
// the record to be deleted doesn't exist
func DeleteSysConfig(id int) (err error) {
o := orm.NewOrm()
v := SysConfig{Id: id}
// ascertain id exists in the database
if err = o.Read(&v); err == nil {
var num int64
if num, err = o.Delete(&SysConfig{Id: id}); err == nil {
fmt.Println("Number of records deleted in database:", num)
}
}
return
}
func GetSysConfigByCompanyId(cid int, key string) (v *SysConfig, err error) {
o := orm.NewOrm()
sql := "select * from sys_config where `key`=? and company_id=?"
if err = o.Raw(sql, key, cid).QueryRow(&v); err == nil {
return v, nil
}
return nil, err
}
... ...
... ... @@ -134,7 +134,7 @@ type TemplateItem struct {
type VisibleObject struct {
Id int `json:"id"`
Name string `json:"name"`
Type int `json:"type"` //0:指定人员 1:部门
Type int `json:"type"` //0:指定人员 1:部门 2.公司所有人
}
/*TemplateEditVisible */
... ...
... ... @@ -20,6 +20,7 @@ type BulletinReleaseRequest struct {
//AllowCondition int `json:"allow_condition"`
QuestionSwitch int `json:"question_switch"`
Receiver []VisibleObject `json:"receiver"`
SendToAll int `json:"send_to_all"` //所有人 1:是 0:否
Question Question `json:"question"`
Cover Cover `json:"cover"`
IsPublish int `json:"is_publish"` //是否直接发布 0:否 1:直接发布
... ...
package protocol
/*ConfigScore 评分配置模式*/
type ConfigScoreRequest struct {
Id int `json:"id"`
ScoreConfig
}
type ScoreConfig struct {
DiscoveryScore *DiscoveryScore `json:"discoveryScore"`
SumScore *SumScore `json:"sumScore"`
BasicScore *ScoreRange `json:"basicScore"`
ExtraScore *ScoreRange `json:"extraScore"`
ValueScore *ScoreRange `json:"valueScore"`
}
type ConfigScoreResponse struct {
}
/*GetConfigScore */
type GetConfigScoreRequest struct {
}
type GetConfigScoreResponse struct {
ScoreConfig
}
//发现评分计算规则
type DiscoveryScore struct {
BasicFactor float64 `json:"basicFactor"` //基础分系数
ExtraFactor float64 `json:"extraFactor"` //附加分系数
ValueFactor float64 `json:"valueFactor"` //价值分系数
}
//总分计算规则
type SumScore struct {
DiscoveryFactor float64 `json:"discoveryFactor"` //发现分系数
CatchFactor float64 `json:"catchFactor"` //抓住分系数
}
type ScoreRange struct {
Min float64 `json:"min"` //最小分
Max float64 `json:"max"` //最大分
Step float64 `json:"step"` //步长
}
... ...
... ... @@ -60,6 +60,10 @@ var errmessge ErrorMap = map[string]string{
"10065": "编码长度最多6个字符",
//公司相关
"12001": "未找到公司信息",
//评分配置相关
"12101": "分值范围不符合要求",
"12102": "评分规则不符合要求",
}
//错误码转换 ,兼容需要
... ...
... ... @@ -92,6 +92,10 @@ func init() {
beego.NSNamespace("/my",
beego.NSRouter("/reset_password", &controllers.MyController{}, "post:ResetPassword"),
),
beego.NSNamespace("/config",
beego.NSRouter("/score", &controllers.ConfigController{}, "post:ConfigScore"),
beego.NSRouter("/score/get", &controllers.ConfigController{}, "post:GetConfigScore"),
),
)
nsAuth := beego.NewNamespace("/auth",
... ...
package config
import (
"encoding/json"
"github.com/astaxie/beego/orm"
"oppmg/common/log"
"oppmg/models"
"oppmg/protocol"
"oppmg/utils"
"time"
)
//评分配置模式
func ConfigScore(uid, companyId int64, request *protocol.ConfigScoreRequest) (rsp *protocol.ConfigScoreResponse, err error) {
var (
scoreConfig *models.SysConfig
mapUpdate = make(map[string]interface{})
data []byte
)
rsp = &protocol.ConfigScoreResponse{}
if data, err = json.Marshal(request.ScoreConfig); err != nil {
log.Error(err.Error())
return
}
//更新
if request.Id > 0 {
if scoreConfig, err = models.GetSysConfigById(request.Id); err != nil {
log.Error(err.Error())
return
}
if scoreConfig.CompanyId != int(companyId) {
err = protocol.NewErrWithMessage("10027") //无权限
return
}
goto UPDATE
//更新
return
}
if !(checkScoreRange(request.BasicScore) && checkScoreRange(request.ExtraScore) && checkScoreRange(request.ValueScore)) {
err = protocol.NewErrWithMessage("12101")
return
}
if request.DiscoveryScore != nil {
c := request.DiscoveryScore
if !(checkFactor(c.BasicFactor) && checkFactor(c.ExtraFactor) && checkFactor(c.ValueFactor)) {
err = protocol.NewErrWithMessage("12102")
return
}
} else {
err = protocol.NewErrWithMessage("12102")
return
}
if request.SumScore != nil {
if !(checkSumScoreFactor(request.SumScore.DiscoveryFactor) && checkSumScoreFactor(request.SumScore.CatchFactor)) {
err = protocol.NewErrWithMessage("12102")
return
}
} else {
err = protocol.NewErrWithMessage("12102")
return
}
if scoreConfig, err = models.GetSysConfigByCompanyId(int(companyId), models.KeyScore); err != nil {
if err == orm.ErrNoRows {
err = nil
//新增一个配置
scoreConfig = &models.SysConfig{
Key: models.KeyScore,
Content: string(data),
CreateAt: time.Now(),
UpdateAt: time.Now(),
CompanyId: int(companyId),
}
if _, err = models.AddSysConfig(scoreConfig); err != nil {
log.Error(err.Error())
return
}
return
}
log.Error(err.Error())
return
} else {
goto UPDATE
}
UPDATE:
{
mapUpdate["Content"] = string(data)
mapUpdate["UpdateAt"] = time.Now()
if err = utils.UpdateTableByMap(scoreConfig, mapUpdate); err != nil {
log.Error(err.Error())
return
}
}
return
}
//获取评分配置
func GetConfigScore(uid, companyId int64, request *protocol.GetConfigScoreRequest) (rsp *protocol.GetConfigScoreResponse, err error) {
var (
scoreConfig *models.SysConfig
)
rsp = &protocol.GetConfigScoreResponse{}
if scoreConfig, err = models.GetSysConfigByCompanyId(int(companyId), models.KeyScore); err != nil {
log.Error(err.Error())
return
}
if err = json.Unmarshal([]byte(scoreConfig.Content), &rsp); err != nil {
log.Error(err.Error())
return
}
return
}
//检查分值范围
func checkScoreRange(scoreRange *protocol.ScoreRange) (result bool) {
//err = protocol.NewErrWithMessage("12101")
result = false
if scoreRange.Min < 0 {
return
}
if scoreRange.Min >= scoreRange.Max {
return
}
if scoreRange.Step < 0.1 {
return
}
if scoreRange.Step < scoreRange.Min || scoreRange.Step > scoreRange.Max {
return
}
result = true
//err =nil
return
}
//检查发现分计算规则
func checkFactor(factor float64) (result bool) {
result = false
//err = protocol.NewErrWithMessage("12102")
if factor < 0.1 || factor > 1 {
return
}
result = true
//err = nil
return
}
//检查总分计算规则
func checkSumScoreFactor(factor float64) (result bool) {
result = false
//err = protocol.NewErrWithMessage("12102")
if factor < 0.1 || factor > 10 {
return
}
result = true
//err = nil
return
}
... ...