|
|
package achievement
|
|
|
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
"oppmg/common/log"
|
|
|
"oppmg/models"
|
|
|
"oppmg/protocol"
|
|
|
"oppmg/utils"
|
|
|
"time"
|
|
|
|
|
|
"github.com/astaxie/beego/orm"
|
|
|
)
|
|
|
|
|
|
func AddAchievement(addData *protocol.RequestAddAchievement, companyid int64) error {
|
|
|
// var (
|
|
|
// achievementData *models.Achievement
|
|
|
// chanceList []models.AchievementChance
|
|
|
// providerList []models.AchievementProvider
|
|
|
// err error
|
|
|
// )
|
|
|
// imgData,
|
|
|
// //操作achievement 成果表
|
|
|
// achievementData = &models.Achievement{
|
|
|
// CompanyId: companyid,
|
|
|
// DepartmentId: addData.DepartmentId,
|
|
|
// UserCompanyId: addData.UserCompanyId,
|
|
|
// ChanceTypeId: addData.TypeIdA,
|
|
|
// AuditTemplateId: addData.TypeIdB,
|
|
|
// SourceContent: addData.SourceContent,
|
|
|
// GraspScore: addData.GraspScore,
|
|
|
// UserGraspScore: addData.UserGraspScore,
|
|
|
// }
|
|
|
//addAchievementProvider 操作achievement_provider 成果提供人表
|
|
|
func addAchievementProvider(addData []protocol.AchievementProvider, achievementId int64, om orm.Ormer) error {
|
|
|
var (
|
|
|
providerList []models.AchievementProvider
|
|
|
err error
|
|
|
)
|
|
|
nowTime := time.Now()
|
|
|
for _, v := range addData {
|
|
|
m := models.AchievementProvider{
|
|
|
AchievementId: achievementId,
|
|
|
UserCompanyId: v.UserCompanyId,
|
|
|
UserGraspScore: v.UserGraspScore,
|
|
|
CreateAt: nowTime,
|
|
|
}
|
|
|
providerList = append(providerList, m)
|
|
|
}
|
|
|
_, err = om.InsertMulti(10, providerList)
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
//addAchievementChance 操作 achievement_chance 机会来源表
|
|
|
func addAchievementChance(addData []protocol.AchievementChance, achievementId int64, om orm.Ormer) error {
|
|
|
var (
|
|
|
chanceList []models.AchievementChance
|
|
|
err error
|
|
|
)
|
|
|
nowTime := time.Now()
|
|
|
for _, v := range addData {
|
|
|
m := models.AchievementChance{
|
|
|
AchievementId: achievementId,
|
|
|
ChanceId: v.Id,
|
|
|
ChanceCode: v.Code,
|
|
|
CreateAt: nowTime,
|
|
|
}
|
|
|
chanceList = append(chanceList, m)
|
|
|
}
|
|
|
_, err = om.InsertMulti(10, chanceList)
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
//AddAchievement 添加成果
|
|
|
func AddAchievement(addData *protocol.RequestAddAchievement, companyid int64) error {
|
|
|
var (
|
|
|
achievementData *models.Achievement
|
|
|
err error
|
|
|
)
|
|
|
nowTime := time.Now()
|
|
|
achievementData = &models.Achievement{
|
|
|
CompanyId: companyid,
|
|
|
DepartmentId: addData.DepartmentId,
|
|
|
UserCompanyId: addData.UserCompanyId,
|
|
|
ChanceTypeId: addData.TypeIdA,
|
|
|
AuditTemplateId: addData.TypeIdB,
|
|
|
SourceContent: addData.SourceContent,
|
|
|
GraspScore: addData.GraspScore,
|
|
|
UserGraspScore: addData.UserGraspScore,
|
|
|
CreateAt: nowTime,
|
|
|
UpdateAt: nowTime,
|
|
|
}
|
|
|
if imgData, err := json.Marshal(addData.Images); err == nil {
|
|
|
achievementData.Images = string(imgData)
|
|
|
} else {
|
|
|
achievementData.Images = "[]"
|
|
|
}
|
|
|
o := orm.NewOrm()
|
|
|
o.Begin()
|
|
|
//操作achievement 成果表
|
|
|
achievementId, err := models.AddAchievement(achievementData, o)
|
|
|
if err != nil {
|
|
|
log.Error("添加Achievement数据失败:%s", err)
|
|
|
o.Rollback()
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
//操作 achievement_chance 机会来源表
|
|
|
err = addAchievementChance(addData.ChanceData, achievementId, o)
|
|
|
if err != nil {
|
|
|
log.Error("添加achievement_chance失败:%s", err)
|
|
|
o.Rollback()
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
//操作achievement_provider 成果提供人表
|
|
|
|
|
|
err = addAchievementProvider(addData.Provider, achievementId, o)
|
|
|
if err != nil {
|
|
|
log.Error("添加achievement_provider失败:%s", err)
|
|
|
o.Rollback()
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
o.Commit()
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func EditAchievement() error {
|
|
|
func EditAchievement(editData *protocol.RequestEditAchievement, companyid int64) error {
|
|
|
var (
|
|
|
achievementData *models.Achievement
|
|
|
err error
|
|
|
)
|
|
|
achievementData, err = models.GetAchievementById(editData.AchievementId)
|
|
|
if err != nil {
|
|
|
log.Error("获取achievement失败:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
nowTime := time.Now()
|
|
|
achievementData.UpdateAt = nowTime
|
|
|
achievementData.DepartmentId = editData.DepartmentId
|
|
|
achievementData.UserCompanyId = editData.UserCompanyId
|
|
|
achievementData.ChanceTypeId = editData.TypeIdA
|
|
|
achievementData.AuditTemplateId = editData.TypeIdB
|
|
|
achievementData.SourceContent = editData.SourceContent
|
|
|
achievementData.GraspScore = editData.GraspScore
|
|
|
achievementData.UserGraspScore = editData.UserGraspScore
|
|
|
achievementData.UpdateAt = nowTime
|
|
|
cols := []string{
|
|
|
"UpdateAt", "DepartmentId", "UserCompanyId", "ChanceTypeId", "AuditTemplateId", "SourceContent", "GraspScore",
|
|
|
}
|
|
|
o := orm.NewOrm()
|
|
|
o.Begin()
|
|
|
//更新Achievement 数据
|
|
|
err = models.UpdateAchievementById(achievementData, cols, o)
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Info("更新Achievement数据失败:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
//删除achievement_chance旧数据
|
|
|
_, err = o.QueryTable(&models.AchievementChance{}).
|
|
|
Filter("achievement_id", editData.AchievementId).
|
|
|
Delete()
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Info("删除achievement_chance数据失败:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
//删除achievement_prover旧数据
|
|
|
_, err = o.QueryTable(&models.AchievementProvider{}).
|
|
|
Filter("achievement_id", editData.AchievementId).
|
|
|
Delete()
|
|
|
if err != nil {
|
|
|
o.Rollback()
|
|
|
log.Info("删除achievement_chance数据失败:%s", err)
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
err = addAchievementChance(editData.ChanceData, achievementData.Id, o)
|
|
|
if err != nil {
|
|
|
log.Error("添加achievement_chance失败:%s", err)
|
|
|
o.Rollback()
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
//操作achievement_provider 成果提供人表
|
|
|
err = addAchievementProvider(editData.Provider, achievementData.Id, o)
|
|
|
if err != nil {
|
|
|
log.Error("添加achievement_provider失败:%s", err)
|
|
|
o.Rollback()
|
|
|
return protocol.NewErrWithMessage("1")
|
|
|
}
|
|
|
o.Commit()
|
|
|
return nil
|
|
|
}
|
|
|
|
...
|
...
|
|