|
|
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
|
|
|
} |
...
|
...
|
|