package postpc
import (
"key_performance_indicators/models/modelskpi"
"key_performance_indicators/overall"
"key_performance_indicators/overall/publicmethod"
"strconv"
"strings"
"time"
"github.com/gin-gonic/gin"
)
//岗位指标相关操作
// 获取岗位指标详情
func ( a * ApiMethod ) GetPostTarget ( c * gin . Context ) {
var receivedValue publicmethod . PublicId
err := c . ShouldBindJSON ( & receivedValue )
if err != nil {
publicmethod . Result ( 100 , err , c )
return
}
if receivedValue . Id == "" {
publicmethod . Result ( 101 , receivedValue , c )
return
}
var postTargetCont modelskpi . PostTarget
err = postTargetCont . GetCont ( map [ string ] interface { } { "`id`" : receivedValue . Id } )
if err != nil {
publicmethod . Result ( 107 , err , c )
return
}
var sendData getPostOneTarget
sendData . Id = postTargetCont . Id
sendData . Title = postTargetCont . Title //标题"`
sendData . Type = postTargetCont . Type //1:定性考核;2:定量考核"`
sendData . State = postTargetCont . State //:状态(1:启用;2:禁用;3:删除)"`
sendData . Time = postTargetCont . Time //创建时间"`
sendData . Share = postTargetCont . Share //1:共用;2:私用"`
sendData . ReleDepart = postTargetCont . ReleDepart //相关部门"`
sendData . DepartmentsPost = postTargetCont . DepartmentsPost //相关岗位"`
sendData . Dimension = postTargetCont . Dimension //维度"`
sendData . Key = postTargetCont . Key //UUID"`
sendData . Report = postTargetCont . Report //上报人"`
sendData . Unit = postTargetCont . Unit //单位"`
sendData . Cycle = postTargetCont . Cycle //1:班;2:天;3:周;4:月;5:季度;6:年"`
sendData . Cycleattr = postTargetCont . Cycleattr //辅助计数"`
sendData . ScoringMethod = postTargetCont . ScoringMethod //计分方式(1:自动;2:手动)"`
sendData . VisibleRange = postTargetCont . VisibleRange //可见范围"`
sendData . VisibleGroup = postTargetCont . VisibleGroup //可见范围(集团)"`
_ , sendData . RelevantPostsMan , _ = getTargetAboutPost ( postTargetCont . ReleDepart , postTargetCont . Dimension , postTargetCont . Id )
publicmethod . Result ( 0 , sendData , c )
}
/ *
获取岗位指标相关岗位
@ departmentId 部门Id
@ dimensionId 维度
@ targetId 指标
* /
func getTargetAboutPost ( departmentId , dimensionId , targetId int64 ) ( postId [ ] int64 , postPeopleList [ ] postPeople , err error ) {
err = overall . CONSTANT_DB_KPI . Model ( & modelskpi . TargetDepartment { } ) . Distinct ( "`post_id`" ) . Where ( "`state` = 1 AND `level` = 2 AND `department_id` = ? AND `dimension_id` = ? AND `target_id` = ?" ) . Find ( & postId ) . Error
if len ( postId ) > 0 {
for i := 0 ; i < len ( postId ) ; i ++ {
var postmanCont postPeople
postmanCont . Id = strconv . FormatInt ( postId [ i ] , 10 )
_ , postmanCont . Operator , _ = getTargetAboutPostMan ( departmentId , postId [ i ] , dimensionId , targetId )
postPeopleList = append ( postPeopleList , postmanCont )
}
}
return
}
/ *
获取岗位指标相关岗位提报人
@ departmentId 部门Id
@ postid 岗位
@ dimensionId 维度
@ targetId 指标
* /
func getTargetAboutPostMan ( departmentId , postid , dimensionId , targetId int64 ) ( peopleId [ ] int64 , postPeople [ ] string , err error ) {
err = overall . CONSTANT_DB_KPI . Model ( & modelskpi . TargetReport { } ) . Distinct ( "`man_key`" ) . Where ( "`state` = 1 AND `type` = 2 AND `department_id` = ? AND `post_id` = ? AND `dimension_id` = ? AND `target_id` = ?" ) . Find ( & peopleId ) . Error
if len ( peopleId ) > 0 {
for i := 0 ; i < len ( peopleId ) ; i ++ {
postPeople = append ( postPeople , strconv . FormatInt ( peopleId [ i ] , 10 ) )
}
}
return
}
// 添加岗位指标
func ( a * ApiMethod ) AddPostTarget ( c * gin . Context ) {
var receivedValue addPostTarget
c . ShouldBindJSON ( & receivedValue )
if receivedValue . Title == "" {
publicmethod . Result ( 1 , receivedValue , c , "请输入指标名称!" )
return
}
if receivedValue . Type == 0 {
receivedValue . Type = 2
}
if receivedValue . ScoringMethod == 0 {
receivedValue . ScoringMethod = 1
}
if receivedValue . RelevantDepartments == "" {
publicmethod . Result ( 1 , receivedValue , c , "请选择该指标归属部门!" )
return
}
if len ( receivedValue . RelevantPostsMan ) < 1 {
publicmethod . Result ( 1 , receivedValue , c , "请选择该指标归属岗位!" )
return
}
if receivedValue . Dimension == "" {
publicmethod . Result ( 1 , receivedValue , c , "请选择指标归属维度!" )
return
}
if receivedValue . Unit == "" {
publicmethod . Result ( 1 , receivedValue , c , "请输入该指标计算单位!" )
return
}
if receivedValue . Cycle == 0 {
receivedValue . Cycle = 1
}
if receivedValue . CycleAttr == 0 {
receivedValue . CycleAttr = 1
}
var saveData modelskpi . PostTarget
saveData . Title = receivedValue . Title //标题"`
saveData . Type = receivedValue . Type //1:定性考核;2:定量考核"`
saveData . State = 1 //状态(1:启用;2:禁用;3:删除)"`
saveData . Time = time . Now ( ) . Unix ( ) //创建时间"`
saveData . Share = 2 //1:共用;2:私用"`
departmentId , _ := strconv . ParseInt ( receivedValue . RelevantDepartments , 10 , 64 )
saveData . ReleDepart = departmentId //相关部门"`
// saveData.DepartmentsPost = strings.Join(receivedValue.DepartmentsPost, ",") //相关岗位"`
dimensionId , _ := strconv . ParseInt ( receivedValue . Dimension , 10 , 64 )
saveData . Dimension = dimensionId //维度"`
saveData . Key = publicmethod . GetUUid ( 1 ) //UUID"`
// saveData.Report = strings.Join(receivedValue.Report, ",") //上报人"`
saveData . Unit = receivedValue . Unit //单位"`
saveData . Cycle = receivedValue . Cycle //1:班;2:天;3:周;4:月;5:季度;6:年"`
saveData . Cycleattr = receivedValue . CycleAttr //辅助计数"`
saveData . ScoringMethod = receivedValue . ScoringMethod //计分方式(1:自动;2:手动)"`
saveData . VisibleRange = strings . Join ( receivedValue . VisibleRange , "," ) //可见范围"`
saveData . VisibleGroup = strings . Join ( receivedValue . VisibleGroup , "," ) //可见范围(集团)"`
//获取岗位与提报人
var departAny [ ] string
var peopletAny [ ] string
for _ , v := range receivedValue . RelevantPostsMan {
if publicmethod . IsInTrue [ string ] ( v . Id , departAny ) == false {
departAny = append ( departAny , v . Id )
}
if len ( v . Operator ) > 0 {
for _ , ov := range v . Operator {
if publicmethod . IsInTrue [ string ] ( ov , peopletAny ) == false {
peopletAny = append ( peopletAny , ov )
}
}
}
}
saveData . DepartmentsPost = strings . Join ( departAny , "," ) //相关岗位"`
saveData . Report = strings . Join ( peopletAny , "," ) //上报人"`
err := overall . CONSTANT_DB_KPI . Create ( & saveData ) . Error
if err != nil {
publicmethod . Result ( 104 , err , c )
return
}
//关联部门岗位
if len ( departAny ) > 0 {
syncSetinges . Add ( 1 )
go EditTargetTableDimenAboutPostOfDepart ( dimensionId , saveData . Id , 0 , 0 , departmentId , departAny , 2 , receivedValue . Type )
}
//关联考核岗位和提报人
for _ , dv := range receivedValue . RelevantPostsMan {
postid , _ := strconv . ParseInt ( dv . Id , 10 , 64 )
syncSetinges . Add ( 1 )
go DepartAboutPostTargetReport ( dimensionId , saveData . Id , 0 , 0 , departmentId , postid , dv . Operator , 2 , receivedValue . Type )
}
syncSetinges . Wait ( )
publicmethod . Result ( 0 , err , c )
}
/ *
编辑指标 、 栏目 、 细则关联部门
协程处理
部门指标岗位提报人关联通用函数
@ dimensionId 维度
@ targetId 指标ID
@ targetSunId 栏目ID
@ bylawsId 指标细则
@ departmentId 接受考核部门
@ postId 岗位
@ class 级别 ( 1 : 部门级 ; 2 : 岗位级 )
@ nature 1 : 定性考核 ; 2 : 定量考核
EditTargetTableDimenAboutPostOfDepart ( dimensionId , targetId , targetSunId , bylawsId , departmentId int64 , postId [ ] string , class , nature int )
* /
func EditTargetTableDimenAboutPostOfDepart ( dimensionId , targetId , targetSunId , bylawsId , departmentId int64 , postId [ ] string , class , nature int ) {
defer syncSetinges . Done ( )
//将不属于的信息禁用
otherSaveData := publicmethod . MapOut [ string ] ( )
otherSaveData [ "`state`" ] = 2
otherSaveData [ "`time`" ] = time . Now ( ) . Unix ( )
//判断岗位是否存在
where := publicmethod . MapOut [ string ] ( )
where [ "`type`" ] = class
if targetId != 0 {
where [ "`dimension_id`" ] = dimensionId
}
if targetId != 0 {
where [ "`target_id`" ] = targetId
}
if targetSunId != 0 {
where [ "`target_sun_id`" ] = targetSunId
}
if bylawsId != 0 {
where [ "`target_bylaws`" ] = bylawsId
}
if departmentId != 0 {
where [ "`department_id`" ] = departmentId
}
if len ( postId ) < 1 {
overall . CONSTANT_DB_KPI . Model ( & modelskpi . TargetReport { } ) . Where ( where ) . Updates ( & otherSaveData )
} else {
//获取关联部门
var aboutDepartment [ ] string
for _ , v := range postId {
if publicmethod . IsInTrue [ string ] ( v , aboutDepartment ) == false {
aboutDepartment = append ( aboutDepartment , v )
//判断该岗位是否已经关联
var tarDeparCont modelskpi . TargetDepartment
judgeAboutErr := overall . CONSTANT_DB_KPI . Model ( & modelskpi . TargetDepartment { } ) . Select ( "`id`,`state`" ) . Where ( where ) . Where ( "`post_id` = ?" , v ) . First ( & tarDeparCont ) . Error
if judgeAboutErr == nil {
tarDeparCont . EiteCont ( map [ string ] interface { } { "`id`" : tarDeparCont . Id } , map [ string ] interface { } { "`state`" : 1 , "`time`" : time . Now ( ) . Unix ( ) } )
} else {
xiZe := 3
if targetId != 0 && targetSunId != 0 && bylawsId == 0 {
xiZe = 2
} else if targetId != 0 && targetSunId == 0 && bylawsId == 0 {
xiZe = 1
}
tarDeparCont . Dimension = dimensionId //维度"`
tarDeparCont . TargetId = targetId //指标ID"`
tarDeparCont . TargetSunId = targetSunId //子目标"`
tarDeparCont . TargetBylaws = bylawsId //指标细则"`
tarDeparCont . Type = xiZe //类型(1:指标;2:子目标;3:细则)"`
tarDeparCont . DepartmentId = departmentId //部门ID"`
postIdInt64 , _ := strconv . ParseInt ( v , 10 , 64 )
tarDeparCont . PostId = postIdInt64 //岗位ID"`
tarDeparCont . State = 1 //状态(1:启用;2:禁用;3:删除)"`
tarDeparCont . Time = time . Now ( ) . Unix ( ) //写入时间"`
tarDeparCont . Class = nature //1:定性考核;2:定量考核"`
tarDeparCont . Level = class //级别(1:部门级;2:岗位级)"`
overall . CONSTANT_DB_KPI . Create ( & tarDeparCont )
}
}
}
//清除不需要关联的部门
if len ( aboutDepartment ) > 0 {
overall . CONSTANT_DB_KPI . Model ( & modelskpi . TargetDepartment { } ) . Where ( where ) . Not ( map [ string ] interface { } { "`post_id`" : aboutDepartment } ) . Updates ( & otherSaveData )
}
}
}