package personnelapi
import (
"fmt"
"hr_server/models"
"hr_server/overall"
"hr_server/overall/overallhandle"
"strconv"
"strings"
"time"
"github.com/gin-gonic/gin"
)
//人员列表
func ( s * StaffApi ) StaffList ( c * gin . Context ) {
var requestData peopleList
c . ShouldBindJSON ( & requestData )
if requestData . Page < 0 {
requestData . Page = 1
}
if requestData . PageSize < 0 {
requestData . PageSize = 10
}
// gormDb := overall.CONSTANT_DB_HR.Model(&models.JobClass{}).Select("").Joins("left join administrative_organization_type as aot on aot.id = administrative_organization.organization_type")
var personnelModels models . Personnel
gormDb := overall . CONSTANT_DB_HR . Table ( fmt . Sprintf ( "%s p" , personnelModels . TableName ( ) ) ) . Select ( "p.*,pc.mobilephone,pc.gender,pc.isdoubleworker,pc.isveterans,pc.entrydate,pc.probationperiod,pc.planformaldate" ) . Joins ( "left join personnel_content as pc on pc.number = p.number" )
if requestData . Number != "" {
gormDb = gormDb . Where ( "p.number LIKE ?" , "%" + requestData . Number + "%" )
}
if requestData . Name != "" {
gormDb = gormDb . Where ( "p.name LIKE ?" , "%" + requestData . Name + "%" )
}
if requestData . HireClass != 0 {
gormDb = gormDb . Where ( "p.hire_class = ?" , requestData . HireClass )
}
if requestData . Company != 0 {
gormDb = gormDb . Where ( "p.company = ?" , requestData . Company )
}
if requestData . Deparment != "" {
gormDb = gormDb . Where ( "FIND_IN_SET(?,p.`deparment`)" , requestData . Deparment )
}
if requestData . AdminOrg != 0 {
gormDb = gormDb . Where ( "p.admin_org = ?" , requestData . AdminOrg )
}
if requestData . Position != 0 {
gormDb = gormDb . Where ( "p.position = ?" , requestData . Position )
}
if requestData . EmpType != 0 {
gormDb = gormDb . Where ( "p.emp_type = ?" , requestData . EmpType )
} else {
gormDb = gormDb . Where ( "p.emp_type IN ?" , overall . EmployeeStatusIng )
}
if requestData . Role != "" {
gormDb = gormDb . Where ( "FIND_IN_SET(?,p.`role`)" , requestData . Role )
}
gormDb = gormDb . Where ( "p.state = 1" )
var total int64
totalErr := gormDb . Count ( & total ) . Error
if totalErr != nil {
total = 0
}
var positionAry [ ] peopleOutList
errGorm := gormDb . Order ( "p.company ASC,p.deparment ASC,p.admin_org ASC,p.position ASC" ) . Limit ( requestData . PageSize ) . Offset ( overallhandle . LimitPage ( requestData . Page , requestData . PageSize ) ) . Find ( & positionAry ) . Error
fmt . Printf ( "%v\n" , positionAry )
for i , v := range positionAry {
positionAry [ i ] . KeyStr = strconv . FormatInt ( positionAry [ i ] . Key , 10 )
var getSpur models . Position
getWhe := overallhandle . MapOut ( )
getWhe [ "id" ] = v . Position
getSpur . GetCont ( getWhe , "name" )
positionAry [ i ] . PositionName = getSpur . Name
var getSpurDepart models . AdministrativeOrganization
getWheDepart := overallhandle . MapOut ( )
getWheDepart [ "id" ] = v . Company
getSpurDepart . GetCont ( getWheDepart , "name" )
positionAry [ i ] . CompanyName = getSpurDepart . Name
//获取部门
departmentAry := strings . Split ( v . Deparment , "," )
if len ( departmentAry ) > 0 {
var departCont [ ] getDepartmentInfo
departErr := overall . CONSTANT_DB_HR . Model ( & models . AdministrativeOrganization { } ) . Select ( "id,number,name" ) . Where ( "`id` IN ?" , departmentAry ) . Order ( "`organization_type` ASC" ) . Find ( & departCont ) . Error
if departErr == nil {
var departNameAry [ ] string
for _ , d_v := range departCont {
departNameAry = append ( departNameAry , d_v . Name )
}
if len ( departNameAry ) > 0 {
positionAry [ i ] . DeparmentName = strings . Join ( departNameAry , " " )
}
}
}
}
if errGorm != nil {
overallhandle . Result ( 105 , errGorm , c )
} else {
overallhandle . ResultList ( 0 , requestData . Page , requestData . PageSize , total , int64 ( len ( positionAry ) ) , positionAry , c )
}
}
//添加员工
func ( s * StaffApi ) AddStaff ( c * gin . Context ) {
var requestData addPersonnel
c . ShouldBindJSON ( & requestData )
if requestData . Number == "" {
overallhandle . Result ( 1 , requestData . Number , c , "工号不能为空!" )
return
}
if requestData . Name == "" {
overallhandle . Result ( 1 , requestData . Name , c , "姓名不能为空!" )
return
}
if requestData . Gender == 0 {
requestData . Gender = 1
}
if requestData . HireType == 0 {
requestData . HireType = 1
}
if requestData . PoliticalOutlook == 0 {
requestData . PoliticalOutlook = 1
}
if requestData . Company == 0 {
requestData . Company = 1
}
if len ( requestData . Department ) < 1 {
overallhandle . Result ( 1 , requestData . Department , c , "请您选择该员工归属哪个分厂(部室)!" )
return
}
if requestData . Position == 0 {
overallhandle . Result ( 1 , requestData . Position , c , "请分配员工职位(岗位)!" )
return
}
if requestData . EmpType == 0 {
requestData . EmpType = 2
}
if requestData . PositionGrade == 0 {
overallhandle . Result ( 1 , requestData . PositionGrade , c , "请分配员入职等级!" )
return
}
if requestData . ProbationPeriod == 0 {
requestData . ProbationPeriod = 3
}
if requestData . Constellation == 0 {
requestData . Constellation = 1
}
if requestData . Health == 0 {
requestData . Health = 2
}
if requestData . Maritalstatus == 0 {
requestData . Maritalstatus = 1
}
if requestData . Iisdoubleworker == 0 {
requestData . Iisdoubleworker = 2
} else {
if requestData . Iisdoubleworker == 1 {
if requestData . SpouseName == "" {
overallhandle . Result ( 1 , requestData . SpouseName , c , "请输入配偶姓名!" )
return
}
if requestData . SpouseCompany == "" {
overallhandle . Result ( 1 , requestData . SpouseName , c , "请输入配偶所在公司!" )
return
}
if requestData . SpouseDepartment == "" {
overallhandle . Result ( 1 , requestData . SpouseName , c , "请输入配偶所在部门!" )
return
}
if requestData . SpousePosition == "" {
overallhandle . Result ( 1 , requestData . SpouseName , c , "请输入配偶所在岗位!" )
return
}
if requestData . SpouseTel == "" {
overallhandle . Result ( 1 , requestData . SpouseName , c , "请输入配偶联系方式!" )
return
}
}
}
if requestData . Isveterans == 0 {
requestData . Isveterans = 2
} else {
if requestData . Isveterans == 1 && requestData . Veteransnumber == "" {
overallhandle . Result ( 1 , requestData . Veteransnumber , c , "请输入退役证编号!" )
return
}
}
if requestData . Password == "" {
requestData . Password = overall . CONSTANT_CONFIG . Appsetup . DefaultPassword
}
//员工档案主
var staffInfo models . Personnel
//判断工号是存在
whereAry := overallhandle . MapOut ( )
whereAry [ "number" ] = requestData . Number
if judgeNoErr := staffInfo . GetCont ( whereAry , "number" ) ; judgeNoErr == nil {
overallhandle . Result ( 1 , requestData , c , "该工号已经被使用!请不要重复使用!" )
return
}
staffInfo . Number = requestData . Number
staffInfo . Name = requestData . Name
staffInfo . HireClass = requestData . HireType
staffInfo . Position = requestData . Position
//获取职务信息
jobCont , jobErr := getJobInfo ( requestData . Position )
if jobErr == nil {
staffInfo . PositionLevel = jobCont . Dutid
staffInfo . JobClass = jobCont . Jobid
}
staffInfo . AdminOrg = requestData . AdminOrg
staffInfo . Company = requestData . Company
staffInfo . EmpType = requestData . EmpType
staffInfo . Deparment = strings . Join ( requestData . Department , "," )
staffInfo . Time = time . Now ( ) . Unix ( )
staffInfo . EiteTime = time . Now ( ) . Unix ( )
staffInfo . PositionGrade = requestData . PositionGrade
staffInfo . Icon = requestData . Icon
// staffInfo.Password = requestData.Password
var md5JiaMi overallhandle . Md5Encryption
md5JiaMi . Md5EncryptionInit ( requestData . Password )
md5Token := md5JiaMi . Md5EncryptionAlgorithm ( )
staffInfo . Password = md5Token
staffInfo . Key = overallhandle . TableNumber ( )
peopleMasterErr := overall . CONSTANT_DB_HR . Create ( & staffInfo ) . Error
if peopleMasterErr != nil {
overallhandle . Result ( 104 , staffInfo , c )
return
}
//员工档案附表
synPro . Add ( 1 )
go staffInfoCont ( requestData )
//双职工
if requestData . Iisdoubleworker == 1 {
synPro . Add ( 1 )
go doubleWorkerStaff ( requestData )
}
//人员变动记录
synPro . Add ( 1 )
go peopleGaiDong ( staffInfo )
//教育经历
if len ( requestData . EducationalExperience ) > 0 {
synPro . Add ( 1 )
go eduExperience ( requestData . Number , requestData . EducationalExperience )
}
//紧急联系人
if len ( requestData . EmergencyContact ) > 0 {
synPro . Add ( 1 )
go emeContact ( requestData . Number , requestData . EmergencyContact )
}
//家庭成员
if len ( requestData . MemberOfFamily ) > 0 {
synPro . Add ( 1 )
go familyPeople ( requestData . Number , requestData . MemberOfFamily )
}
synPro . Wait ( )
overallhandle . Result ( 0 , staffInfo , c )
}
//获取职务相关属性
func getJobInfo ( jobId int64 ) ( cont jobAttber , err error ) {
var positionInfo models . Position
// err = overall.CONSTANT_DB_HR.Table(fmt.Sprintf("%s p", positionInfo.TableName())).Select("p.id,p.number,p.name,p.person_in_charge,d.id as dutid,d.name as dutname,d.number as dutnumber,j.id as jobid,j.name as jobname").Joins("left join duties as d on d.id = p.duties").Joins("left join job_class as j on j.id = d.job_type").Where("`p`.`id` = ?", jobId).First(&cont).Error
err = overall . CONSTANT_DB_HR . Model ( & positionInfo ) . Select ( "position.id,position.number,position.name,position.person_in_charge,d.id as dutid,d.name as dutname,d.number as dutnumber,j.id as jobid,j.name as jobname" ) . Joins ( "left join duties as d on d.id = position.duties" ) . Joins ( "left join job_class as j on j.id = d.job_type" ) . Where ( "`position`.`id` = ?" , jobId ) . First ( & cont ) . Error
return
}
//员工档案详情
func staffInfoCont ( contData addPersonnel ) {
defer synPro . Done ( )
var staffAttribute models . PersonnelContent
staffAttribute . Number = contData . Number
staffAttribute . Idcardno = contData . IDCardNo
staffAttribute . Mobilephone = contData . Mobilephone
staffAttribute . Gender = contData . Gender
brrthday , _ := overallhandle . DateToTimeStamp ( fmt . Sprintf ( "%v 00:00:00" , contData . Birthday ) )
staffAttribute . Birthday = brrthday
staffAttribute . Myfolk = contData . Nation
staffAttribute . Nativeplace = contData . NativePlace
staffAttribute . Health = contData . Health
staffAttribute . Maritalstatus = contData . Maritalstatus
staffAttribute . Currentresidence = contData . CurrentResidence
staffAttribute . Time = time . Now ( ) . Unix ( )
staffAttribute . Constellation = contData . Constellation
staffAttribute . Isdoubleworker = contData . Iisdoubleworker
staffAttribute . Isveterans = contData . Isveterans
staffAttribute . Veteransnumber = contData . Veteransnumber
workInData := time . Now ( ) . Unix ( )
if contData . WorkingDate != "" {
workInData , _ = overallhandle . DateToTimeStamp ( fmt . Sprintf ( "%v 00:00:00" , contData . WorkingDate ) )
}
staffAttribute . Jobstartdate = workInData
entryData := time . Now ( ) . Unix ( )
if contData . EntryDate != "" {
entryData , _ = overallhandle . DateToTimeStamp ( fmt . Sprintf ( "%v 00:00:00" , contData . EntryDate ) )
}
staffAttribute . Entrydate = entryData
staffAttribute . Probationperiod = contData . ProbationPeriod
planformalData := time . Now ( ) . Unix ( )
if contData . ConfirmationDate != "" {
planformalData , _ = overallhandle . DateToTimeStamp ( fmt . Sprintf ( "%v 00:00:00" , contData . ConfirmationDate ) )
} else {
planformalData = overallhandle . GetFutureMonthTime ( entryData , contData . ProbationPeriod , 2 )
}
staffAttribute . Planformaldate = planformalData
staffAttribute . PoliticalOutlook = contData . PoliticalOutlook
overall . CONSTANT_DB_HR . Create ( & staffAttribute )
}
//双职工
func doubleWorkerStaff ( contData addPersonnel ) {
defer synPro . Done ( )
var doublePeople models . DoubleWorker
doublePeople . Number = contData . Number
doublePeople . Name = contData . SpouseName
doublePeople . Company = contData . SpouseCompany
doublePeople . Department = contData . SpouseDepartment
doublePeople . Position = contData . SpousePosition
doublePeople . Tel = contData . SpouseTel
doublePeople . Time = time . Now ( ) . Unix ( )
overall . CONSTANT_DB_HR . Create ( & doublePeople )
}
//人员变动记录
func peopleGaiDong ( contData models . Personnel ) {
defer synPro . Done ( )
var pcrInfo models . PersonnelChangeRecord
pcrInfo . Number = contData . Number
pcrInfo . Type = contData . HireClass
reason := ""
switch contData . HireClass {
case 1 :
reason = "雇佣入职"
case 2 :
reason = "再入职"
case 3 :
reason = "职位分配"
case 4 :
reason = "转正"
case 5 :
reason = "停薪留职"
case 6 :
reason = "退休"
case 7 :
reason = "辞退"
case 8 :
reason = "离职"
default :
reason = ""
}
pcrInfo . Reason = reason
pcrInfo . Position = contData . Position
pcrInfo . JobLevel = strconv . FormatInt ( contData . PositionLevel , 10 )
pcrInfo . JobGrade = contData . PositionGrade
pcrInfo . Company = contData . Company
pcrInfo . Department = contData . Deparment
pcrInfo . Adminorg = contData . AdminOrg
pcrInfo . Time = time . Now ( ) . Unix ( )
overall . CONSTANT_DB_HR . Create ( & pcrInfo )
}
//教育经历
func eduExperience ( number string , eduExpAry [ ] educationalExperience ) {
defer synPro . Done ( )
var leaExp [ ] models . PersonnelEducation
maxLevel := 1
for eei , eev := range eduExpAry {
if maxLevel < eev . Education {
maxLevel = eev . Education
}
var leaExpInfo models . PersonnelEducation
leaExpInfo . Number = number
leaExpInfo . Education = eev . Education
leaExpInfo . GraduationSchool = eev . GraduationSchool
leaExpInfo . Subject = eev . Subject
admissionTime , _ := overallhandle . DateToTimeStamp ( fmt . Sprintf ( "%v 00:00:00" , eev . AdmissionTime ) )
leaExpInfo . AdmissionTime = admissionTime
graduationTime , _ := overallhandle . DateToTimeStamp ( fmt . Sprintf ( "%v 00:00:00" , eev . GraduationTime ) )
leaExpInfo . GraduationTime = graduationTime
leaExpInfo . Time = time . Now ( ) . Unix ( )
if eei == 0 {
leaExpInfo . Level = 2
}
leaExp = append ( leaExp , leaExpInfo )
}
//设定最高学历
if len ( leaExp ) > 1 {
for mei , maxEdv := range leaExp {
if maxEdv . Education != 2 && maxEdv . Education == maxLevel {
leaExp [ mei ] . Level = 3
}
}
}
if len ( leaExp ) > 0 {
overall . CONSTANT_DB_HR . Create ( & leaExp )
}
}
//紧急联系人
func emeContact ( number string , contAry [ ] emergencyContact ) {
defer synPro . Done ( )
var emerContAry [ ] models . EmergencyContact
for _ , emev := range contAry {
var emerInfo models . EmergencyContact
emerInfo . Number = number
emerInfo . Name = emev . Name
emerInfo . Relationship = emev . Relationship
emerInfo . Tel = emev . Mobilephone
emerInfo . Time = time . Now ( ) . Unix ( )
emerInfo . State = 1
emerContAry = append ( emerContAry , emerInfo )
}
if len ( emerContAry ) > 0 {
overall . CONSTANT_DB_HR . Create ( & emerContAry )
}
}
//家庭成员
func familyPeople ( number string , contAry [ ] memberOfFamily ) {
defer synPro . Done ( )
var familyAry [ ] models . FamilyMembers
for _ , fv := range contAry {
var familyInfo models . FamilyMembers
familyInfo . Number = number
familyInfo . Relationship = fv . Relationship
familyInfo . Name = fv . Name
familyInfo . Company = fv . Company
familyInfo . Deparment = fv . Department
familyInfo . Postnme = fv . Position
familyInfo . Tel = fv . Mobilephone
familyInfo . PoliticalOutlook = fv . PoliticalOutlook
familyInfo . Time = time . Now ( ) . Unix ( )
familyAry = append ( familyAry , familyInfo )
}
if len ( familyAry ) > 0 {
overall . CONSTANT_DB_HR . Create ( & familyAry )
}
}
//分配权限
func ( s * StaffApi ) AllocationOfRights ( c * gin . Context ) {
var requestData allocationOfRightsToRole
err := c . ShouldBindJSON ( & requestData )
if err != nil {
overallhandle . Result ( 1 , requestData . Id , c , "数据格式错误!" )
return
}
if requestData . Id == "" {
overallhandle . Result ( 1 , requestData . Id , c , "ID不能为空!" )
return
}
if requestData . RoleId == "" {
overallhandle . Result ( 1 , requestData . RoleId , c , "请选择角色!" )
return
}
var setUserRole models . Personnel
judgeIsTrueWhere := overallhandle . MapOut ( )
judgeIsTrueWhere [ "id" ] = requestData . Id
getUserErr := setUserRole . GetCont ( judgeIsTrueWhere , "role" )
if getUserErr != nil {
overallhandle . Result ( 105 , getUserErr , c )
return
}
if setUserRole . Role != "" {
roleAry := strings . Split ( setUserRole . Role , "," )
if overallhandle . StringIsInMap ( requestData . RoleId , roleAry ) == false {
roleAry = append ( roleAry , requestData . RoleId )
saveData := overallhandle . MapOut ( )
saveData [ "eite_time" ] = time . Now ( ) . Unix ( )
saveData [ "role" ] = strings . Join ( roleAry , "," )
saveErr := setUserRole . EiteCont ( judgeIsTrueWhere , saveData )
if saveErr != nil {
overallhandle . Result ( 106 , saveErr , c )
return
}
overallhandle . Result ( 0 , setUserRole , c )
} else {
overallhandle . Result ( 0 , setUserRole , c )
}
} else {
saveData := overallhandle . MapOut ( )
saveData [ "eite_time" ] = time . Now ( ) . Unix ( )
saveData [ "role" ] = requestData . RoleId
saveErr := setUserRole . EiteCont ( judgeIsTrueWhere , saveData )
if saveErr != nil {
overallhandle . Result ( 106 , saveErr , c )
return
}
overallhandle . Result ( 0 , setUserRole , c )
}
}