作者 tangxvhui

成果管理

... ... @@ -11,9 +11,9 @@ type AchievementController struct {
BaseController
}
//List 成果列表
//AchievementList 成果列表
//@router /achievement/list
func (c AchievementController) List() {
func (c AchievementController) AchievementList() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
... ... @@ -33,3 +33,43 @@ func (c AchievementController) List() {
msg = protocol.NewReturnResponse(rspdata, nil)
return
}
//AddAchievement 添加成果列表
//@router /achievement/add
func (c AchievementController) AddAchievement() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
var param protocol.RequestAddAchievement
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
log.Error("json 解析失败 err:%s", err)
msg = protocol.BadRequestParam("1")
return
}
companyid := c.GetCompanyId()
err := serveachievement.AddAchievement(&param, companyid)
msg = protocol.NewReturnResponse(nil, err)
return
}
//EditAchievement 编辑成果
//@router /achievement/edit
func (c AchievementController) EditAchievement() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
var param protocol.RequestEditAchievement
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
log.Error("json 解析失败 err:%s", err)
msg = protocol.BadRequestParam("1")
return
}
companyid := c.GetCompanyId()
err := serveachievement.EditAchievement(&param, companyid)
msg = protocol.NewReturnResponse(nil, err)
return
}
... ...
package models
import (
"errors"
"fmt"
"reflect"
"strings"
"oppmg/common/log"
"time"
"github.com/astaxie/beego/orm"
)
type Achievement struct {
Id int `orm:"column(id);pk" description:"id 主键"`
Id int64 `orm:"column(id);auto" description:"id 主键"`
CompanyId int64 `orm:"column(company_id);null" description:"公司编号 表company.id"`
DepartmentId int64 `orm:"column(department_id);null" description:"表department.id 部门id (创建成果指定的部门)"`
UserCompanyId int64 `orm:"column(user_company_id)" description:"把握人 表user_company.id id"`
... ... @@ -39,15 +37,20 @@ func init() {
// AddAchievement insert a new Achievement into database and returns
// last inserted Id on success.
func AddAchievement(m *Achievement) (id int64, err error) {
o := orm.NewOrm()
func AddAchievement(m *Achievement, om ...orm.Ormer) (id int64, err error) {
var o orm.Ormer
if len(om) > 0 {
o = om[0]
} else {
o = orm.NewOrm()
}
id, err = o.Insert(m)
return
}
// GetAchievementById retrieves Achievement by Id. Returns error if
// Id doesn't exist
func GetAchievementById(id int) (v *Achievement, err error) {
func GetAchievementById(id int64) (v *Achievement, err error) {
o := orm.NewOrm()
v = &Achievement{Id: id}
if err = o.Read(v); err == nil {
... ... @@ -56,102 +59,30 @@ func GetAchievementById(id int) (v *Achievement, err error) {
return nil, err
}
// GetAllAchievement retrieves all Achievement matches certain condition. Returns empty list if
// no records exist
func GetAllAchievement(query map[string]string, fields []string, sortby []string, order []string,
offset int64, limit int64) (ml []interface{}, err error) {
o := orm.NewOrm()
qs := o.QueryTable(new(Achievement))
// query k=v
for k, v := range query {
// rewrite dot-notation to Object__Attribute
k = strings.Replace(k, ".", "__", -1)
if strings.Contains(k, "isnull") {
qs = qs.Filter(k, (v == "true" || v == "1"))
} else {
qs = qs.Filter(k, v)
}
}
// order by:
var sortFields []string
if len(sortby) != 0 {
if len(sortby) == len(order) {
// 1) for each sort field, there is an associated order
for i, v := range sortby {
orderby := ""
if order[i] == "desc" {
orderby = "-" + v
} else if order[i] == "asc" {
orderby = v
} else {
return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")
}
sortFields = append(sortFields, orderby)
}
qs = qs.OrderBy(sortFields...)
} else if len(sortby) != len(order) && len(order) == 1 {
// 2) there is exactly one order, all the sorted fields will be sorted by this order
for _, v := range sortby {
orderby := ""
if order[0] == "desc" {
orderby = "-" + v
} else if order[0] == "asc" {
orderby = v
} else {
return nil, errors.New("Error: Invalid order. Must be either [asc|desc]")
}
sortFields = append(sortFields, orderby)
}
} else if len(sortby) != len(order) && len(order) != 1 {
return nil, errors.New("Error: 'sortby', 'order' sizes mismatch or 'order' size is not 1")
}
} else {
if len(order) != 0 {
return nil, errors.New("Error: unused 'order' fields")
}
}
var l []Achievement
qs = qs.OrderBy(sortFields...)
if _, err = qs.Limit(limit, offset).All(&l, fields...); err == nil {
if len(fields) == 0 {
for _, v := range l {
ml = append(ml, v)
}
// UpdateAchievement updates Achievement by Id and returns error if
// the record to be updated doesn't exist
func UpdateAchievementById(m *Achievement, cols []string, om ...orm.Ormer) (err error) {
var o orm.Ormer
if len(om) > 0 {
o = om[0]
} else {
// trim unused fields
for _, v := range l {
m := make(map[string]interface{})
val := reflect.ValueOf(v)
for _, fname := range fields {
m[fname] = val.FieldByName(fname).Interface()
o = orm.NewOrm()
}
ml = append(ml, m)
m.UpdateAt = time.Now()
if len(cols) > 0 {
cols = append(cols, "UpdateAt")
}
}
return ml, nil
}
return nil, err
}
// UpdateAchievement updates Achievement by Id and returns error if
// the record to be updated doesn't exist
func UpdateAchievementById(m *Achievement) (err error) {
o := orm.NewOrm()
v := Achievement{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)
}
if num, err = o.Update(m, cols...); err == nil {
log.Info("Number of records updated in database:", num)
}
return
}
// DeleteAchievement deletes Achievement by Id and returns error if
// the record to be deleted doesn't exist
func DeleteAchievement(id int) (err error) {
func DeleteAchievement(id int64) (err error) {
o := orm.NewOrm()
v := Achievement{Id: id}
// ascertain id exists in the database
... ...
package protocol
//ProviderData 成果提供者
type AchievementProvider struct {
UserCompanyId int64 `json:"user_company_id"` //用户的id
UserName string `json:"user_name"` //用户名称
DepartmentId int64 `json:"department_id"` //部门id
UserGraspScore float64 `json:"user_grasp_score"` //把握人得分
}
//AchievementChance 成果的机会列表
type AchievementChance struct {
Id int64 `json:"id"`
Code string `json:"code"`
}
//RequestAddAchievement 添加成果
type RequestAddAchievement struct {
ChanceData []AchievementChance `json:"chance_data"`
... ... @@ -14,18 +28,19 @@ type RequestAddAchievement struct {
Images []string `json:"image"` //图片
}
//ProviderData 成果提供者
type AchievementProvider struct {
UserCompanyId int64 `json:"user_company_id"` //用户的id
UserName string `json:"user_name"` //用户名称
DepartmentId int64 `json:"department_id"` //部门id
UserGraspScore int64 `json:"user_grasp_score"` //把握人得分
}
//AchievementChance 成果的机会列表
type AchievementChance struct {
Id int64 `json:"id"`
Code string `json:"code"`
//RequestAddAchievement 添加成果
type RequestEditAchievement struct {
AchievementId int64 `json:"achievement_id"`
ChanceData []AchievementChance `json:"chance_data"`
TypeIdA int `json:"type_id_a"` //机会一级分类 chance_type
TypeIdB int `json:"type_id_b"` //机会二级分类
DepartmentId int64 `json:"department_id"` //把握人的部门
UserCompanyId int64 `json:"user_company_id"` //把握人的id
GraspScore float64 `json:"grasp_score"` //把握分
UserGraspScore float64 `json:"user_grasp_score"` //把握人总得分
Provider []AchievementProvider `json:"provider"` //机会提供者
SourceContent string `json:"source_content"` //成果描述文本
Images []string `json:"image"` //图片
}
// ResponseRankSeasonList 赛季列表
... ...
... ... @@ -21,13 +21,22 @@ type RankSeasonItem struct {
Status int8 `json:"status" orm:"column(status)"`
}
//RankRangeItem 赛季参与人列表项
type RankRangeItem struct {
//RankRangeListItem 赛季参与人列表项
type RankRangeListItem struct {
Id int64 `json:"id" orm:"column(id)"`
Name string `json:"name" orm:"column(name)"`
RangeType int8 `json:"range_type" orm:"column(type)"`
Status int8 `json:"status" orm:"column(status)"`
RangeData string `json:"range_data" orm:"-"`
SortNum int `json:"sort_num"`
}
type RankRangeList []RankRangeListItem
func (a RankRangeList) Len() int { return len(a) }
func (a RankRangeList) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a RankRangeList) Less(i, j int) bool {
return a[i].SortNum < a[j].SortNum
}
//ResponseRankRangeInfo 赛季参与人详情
... ...
... ... @@ -121,7 +121,9 @@ func init() {
beego.NSRouter("/item/edit", &controllers.RankController{}, "post:RankItemEdit"),
),
beego.NSNamespace("/achievement",
beego.NSRouter("/list", &controllers.AchievementController{}, "post:List"),
beego.NSRouter("/list", &controllers.AchievementController{}, "post:AchievementList"),
beego.NSRouter("/add", &controllers.AchievementController{}, "post:AddAchievement"),
beego.NSRouter("/edit", &controllers.AchievementController{}, "post:EditAchievement"),
),
)
... ...
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
}
... ...
... ... @@ -248,12 +248,12 @@ func EditRankPeriod(id int64, beginTime int64, endTime int64, name string, compa
}
//获取赛季参与人列表
func GetRankRangeList(companyid int64, rankTypeId int64) []protocol.RankRangeItem {
func GetRankRangeList(companyid int64, rankTypeId int64) []protocol.RankRangeListItem {
var (
err error
rspData = make([]protocol.RankRangeItem, 0)
rspData = make([]protocol.RankRangeListItem, 0)
)
datasql := `SELECT a.id ,a.name,a.type,a.status FROM rank_range AS a WHERE company_id=? AND rank_type_id=?`
datasql := `SELECT a.id ,a.name,a.type,a.status,a.sort_num FROM rank_range AS a WHERE company_id=? AND rank_type_id=?`
err = utils.ExecuteQueryAll(&rspData, datasql, companyid, rankTypeId)
if err != nil {
log.Error("获取参与人列表失败:%s", err)
... ... @@ -266,6 +266,7 @@ func GetRankRangeList(companyid int64, rankTypeId int64) []protocol.RankRangeIte
}
rspData[i].RangeData = strings.Join(nameList, ",")
}
sort.Sort(protocol.RankRangeList(rspData))
return rspData
}
... ...