fonchain-fiee/pkg/service/approval/approval_position.go
2025-02-19 14:24:15 +08:00

341 lines
13 KiB
Go

package approval
import (
"context"
"errors"
"fmt"
"github.com/fonchain_enterprise/fonchain-main/api/account"
api "github.com/fonchain_enterprise/fonchain-main/api/approval"
"github.com/fonchain_enterprise/fonchain-main/api/employee"
"github.com/fonchain_enterprise/fonchain-main/api/oa"
"github.com/fonchain_enterprise/fonchain-main/api/recruit"
"github.com/fonchain_enterprise/fonchain-main/pkg/e"
"github.com/fonchain_enterprise/fonchain-main/pkg/model/login"
"github.com/fonchain_enterprise/fonchain-main/pkg/service"
employee1 "github.com/fonchain_enterprise/fonchain-main/pkg/service/employee"
"github.com/gin-gonic/gin"
"strconv"
"strings"
"time"
)
func postApproveSetStatusEnter(userInfo login.Info, detail *api.CreateRequest) (err error) {
fmt.Println("++++++++++++++++++++ postApproveSetStatusEnter =============================")
fmt.Printf("detail : %+v\n", detail)
fmt.Println("++++++++++++++++++++ postApproveSetStatusEnter =============================")
if detail.Status == e.ApprovalWorkStatusFail {
fmt.Println("++++++++++++++++++++ postApproveSetStatusEnter ApprovalWorkStatusFail =============================")
err = rejectPostApprove(userInfo, detail.ID, detail.Reply)
if err != nil {
//service.Error(c, e.Error, err)
return err
}
} else if detail.Status == e.ApprovalWorkStatusOk {
fmt.Println("++++++++++++++++++++ postApproveSetStatusEnter ApprovalWorkStatusOk =============================")
err = passPostApprove(userInfo, detail.ID)
if err != nil {
//service.Error(c, e.Error, err)
return err
}
}
return nil
}
func createPostApprove(c *gin.Context, userInfo login.Info, res *api.ApprovalResponse, departmentID int64) (err error) {
createReq := api.DetailRequest{ID: res.ID}
createDetail, detailErr := service.ApprovalProvider.Detail(c, &createReq)
if detailErr != nil {
return detailErr
}
dealProcessForm := new(recruit.DealProcessProto)
dealProcessForm.Id = int64(createDetail.ID) // 审批申请的ID
dealProcessForm.Command = "position-submit"
dealProcessForm.Type = "post-approve" // 人才招聘 的 类型
dealProcessForm.User = &recruit.UserProto{ // 登录用户
Id: int64(userInfo.ID),
Nickname: userInfo.NickName,
}
dealProcessForm.Entity = &recruit.EntityProto{ // 岗位信息
Position: &recruit.PositionProto{
Applier: userInfo.NickName, // 申请人 昵称
ApplierId: int64(userInfo.ID), // 申请人 ID
Post: createDetail.StaffSupply.PositionName, // 申请岗位
PostCapacity: int32(createDetail.StaffSupply.Num), // 岗位数量
PostRequirement: createDetail.StaffSupply.JobAsk, // 岗位要求
WantedDate: createDetail.StaffSupply.JoinInTime, // 期待到职日期
ApproveCountLast: int32(len(createDetail.WorkFlows)), // 待审核人数
PostCountLast: int32(createDetail.StaffSupply.Num), // 岗位待招人数
},
}
if createDetail.StaffSupply != nil {
if createDetail.StaffSupply.DepartmentId != "" {
tempDepartmentId, err := strconv.Atoi(createDetail.StaffSupply.DepartmentId)
if err == nil {
dealProcessForm.Entity.Position.ApplyDepartmentId = int32(tempDepartmentId)
}
}
if createDetail.StaffSupply.PositionId != "" {
tempPositionId, err := strconv.Atoi(createDetail.StaffSupply.PositionId)
if err == nil {
dealProcessForm.Entity.Position.ApplyPositionId = int32(tempPositionId)
}
}
}
approver := make([]string, 0)
for i := 0; i < len(createDetail.WorkFlows); i++ {
approver = append(approver, createDetail.WorkFlows[i].Name)
dealProcessForm.Entity.Position.ApproverIds = append(dealProcessForm.Entity.Position.ApproverIds, int64(createDetail.WorkFlows[i].UserID)) // 审批人 ID
}
copyUsers := make([]string, 0)
for i := 0; i < len(createDetail.CopyUsers); i++ {
copyUsers = append(copyUsers, createDetail.CopyUsers[i].Name)
dealProcessForm.Entity.Position.CopiedIds = append(dealProcessForm.Entity.Position.CopiedIds, int64(createDetail.CopyUsers[i].ID)) // 抄送人 ID
}
dealProcessForm.Entity.Position.Approver = strings.Join(approver, ",") // 审批人 名称
dealProcessForm.Entity.Position.Copied = strings.Join(copyUsers, ",") // 抄送人 名称
for i := 0; i < len(userInfo.PositionUsers); i++ {
if departmentID == int64(userInfo.PositionUsers[i].DepartmentId) {
//dealProcessForm.Entity.Position.ApplierDepartment = userInfo.PositionUsers[i].DepartmentName // 申请人的 部门
dealProcessForm.Entity.Position.ApplierPosition = userInfo.PositionUsers[i].PositionName // 申请人的 岗位
}
}
dealProcessForm.Entity.Position.ApplierDepartment = createDetail.StaffSupply.DepartmentName // 归属部门
fmt.Println("++++++++++++++++++++ dealProcessForm =============================")
fmt.Printf("dealProcessForm : %+v\n", dealProcessForm)
fmt.Println("++++++++++++++++++++ dealProcessForm =============================")
dealProcessFormResp := new(recruit.ResultVo)
dealProcessFormResp, err = service.GrpcRecruitClientImpl.PostSubmit(context.Background(), dealProcessForm)
if err != nil {
fmt.Println("++++++++++++++++++++ PostSubmit =============================")
fmt.Printf("PostSubmit : %+v\n", err)
fmt.Printf("dealProcessFormResp : %+v\n", dealProcessFormResp)
fmt.Println("++++++++++++++++++++ PostSubmit =============================")
//service.Error(c, e.Error, errors.New(e.ErrorCreateTalents))
return errors.New(e.ErrorCreateTalents)
}
return returnRecruitError(dealProcessFormResp)
}
func passPostApprove(userInfo login.Info, approvalID uint64) (err error) {
dealProcessForm := new(recruit.DealProcessProto)
dealProcessForm.Entity = new(recruit.EntityProto)
dealProcessForm.Entity.Detail = new(recruit.DetailProto)
dealProcessForm.User = &recruit.UserProto{ // 登录用户
Id: int64(userInfo.ID),
Nickname: userInfo.NickName,
}
dealProcessForm.Id = int64(approvalID) // 审批申请的ID
dealProcessForm.Type = "post-approve" // 人才招聘 的 类型
dealProcessForm.Command = "position-pass"
dealProcessForm.Entity.Detail.Approver = int64(userInfo.ID)
dealProcessForm.Entity.Detail.Result = "通过"
dealProcessFormResp := new(recruit.ResultVo)
dealProcessFormResp, err = service.GrpcRecruitClientImpl.PostPass(context.Background(), dealProcessForm)
fmt.Println("++++++++++++++++++++ PostPass =============================")
fmt.Printf("PostPass : %+v\n", err)
fmt.Printf("dealProcessFormResp : %+v\n", dealProcessFormResp)
fmt.Println("++++++++++++++++++++ PostPass =============================")
if err != nil {
//service.Error(c, e.Error, errors.New(e.ErrorCreateTalents))
return errors.New(e.ErrorCreateTalents)
}
return returnRecruitError(dealProcessFormResp)
}
func rejectPostApprove(userInfo login.Info, approvalID uint64, reason string) (err error) {
dealProcessForm := new(recruit.DealProcessProto)
dealProcessForm.Entity = new(recruit.EntityProto)
dealProcessForm.Entity.Detail = new(recruit.DetailProto)
dealProcessForm.User = &recruit.UserProto{ // 登录用户
Id: int64(userInfo.ID),
Nickname: userInfo.NickName,
}
dealProcessForm.Id = int64(approvalID) // 审批申请的ID
dealProcessForm.Type = "post-approve" // 人才招聘 的 类型
dealProcessForm.Command = "position-reject"
dealProcessForm.Entity.Detail.Approver = int64(userInfo.ID)
dealProcessForm.Entity.Detail.Result = "不通过"
dealProcessForm.Entity.Detail.RejectReason = reason
dealProcessFormResp := new(recruit.ResultVo)
dealProcessFormResp, err = service.GrpcRecruitClientImpl.PostReject(context.Background(), dealProcessForm)
if err != nil {
fmt.Println("++++++++++++++++++++ PostSubmit =============================")
fmt.Printf("PostSubmit : %+v\n", err)
fmt.Printf("dealProcessFormResp : %+v\n", dealProcessFormResp)
fmt.Println("++++++++++++++++++++ PostSubmit =============================")
//service.Error(c, e.Error, errors.New(e.ErrorCreateTalents))
return errors.New(e.ErrorCreateTalents)
}
return returnRecruitError(dealProcessFormResp)
}
func checkPostApprove(userInfo login.Info, approvalID uint64) (err error) {
dealProcessForm := new(recruit.DealProcessProto)
dealProcessForm.Entity = new(recruit.EntityProto)
dealProcessForm.Entity.Copied = new(recruit.CopiedProto)
dealProcessForm.User = &recruit.UserProto{ // 登录用户
Id: int64(userInfo.ID),
Nickname: userInfo.NickName,
}
dealProcessForm.Id = int64(approvalID) // 审批申请的ID
dealProcessForm.Type = "post-approve" // 人才招聘 的 类型
dealProcessForm.Command = "position-check"
dealProcessForm.Entity.Copied.Copied = int64(userInfo.ID)
dealProcessFormResp := new(recruit.ResultVo)
dealProcessFormResp, err = service.GrpcRecruitClientImpl.PostCheck(context.Background(), dealProcessForm)
if err != nil {
fmt.Println("++++++++++++++++++++ PostSubmit =============================")
fmt.Printf("PostSubmit : %+v\n", err)
fmt.Printf("dealProcessFormResp : %+v\n", dealProcessFormResp)
fmt.Println("++++++++++++++++++++ PostSubmit =============================")
//service.Error(c, e.Error, errors.New(e.ErrorCreateTalents))
return errors.New(e.ErrorCreateTalents)
}
return returnRecruitError(dealProcessFormResp)
}
func rollbackPost(userInfo login.Info, approvalID uint64) (err error) {
dealProcessForm := new(recruit.DealProcessProto)
dealProcessForm.Id = int64(approvalID) // 审批申请的ID
dealProcessForm.Command = "position-rollback"
dealProcessForm.Type = "post-approve" // 人才招聘 的 类型
dealProcessForm.User = &recruit.UserProto{ // 登录用户
Id: int64(userInfo.ID),
Nickname: userInfo.NickName,
}
dealProcessFormResp, err := service.GrpcRecruitClientImpl.PostRollback(context.Background(), dealProcessForm)
if err != nil {
fmt.Println("++++++++++++++++++++ Post Rollback =============================")
fmt.Printf("Post Rollback : %+v\n", err)
fmt.Printf("dealProcessFormResp : %+v\n", dealProcessFormResp)
fmt.Println("++++++++++++++++++++ Post Rollback =============================")
//service.Error(c, e.Error, errors.New(e.ErrorRollbackTalents))
return errors.New(e.ErrorRollbackTalents)
}
return returnRecruitError(dealProcessFormResp)
}
func returnRecruitError(res *recruit.ResultVo) (err error) {
if res.Code != 0 {
//service.Error(c, e.Error, errors.New(res.Msg))
return errors.New(res.Msg)
}
return err
}
func turnoverUpdateAccountEmployee(c *gin.Context, info *api.TurnoverApply) error {
//查询员工档案
req := &employee.CreateTurnoverRecordRequest{
ResignationDate: info.LeftTime,
Reason: info.Reason,
Remark: "审批申请离职",
}
infoRes, err := service.GrpcEmployeeProvider.QueryBasicInfoAboutEmployees(context.Background(), &employee.QueryBasicInfoAboutEmployeesRequest{
UserId: []uint64{info.StaffUID},
})
if err != nil {
return err
}
if len(infoRes.List) == 0 {
return errors.New("获取用户档案错误")
}
req.EmployeeFileId = infoRes.List[0].EmployeeFileId
userReq := &account.InfoRequest{
ID: infoRes.List[0].UserId,
}
userInfo, err := service.AccountProvider.Info(context.Background(), userReq)
// 更新用户信息(如果需要)
err = employee1.UpdateUserWorkingCondition(c, userInfo, req)
if err != nil {
return err
}
_, err = service.GrpcEmployeeProvider.CreateTurnoverRecord(c, req)
if err != nil {
return err
}
// 更新年假扣除
//updateDeductAnnualLeave(info)
//service.Success(c, basicInfo)
return nil
}
func updateDeductAnnualLeaveZeroClearing(info *api.TurnoverApply) {
t, err := time.Parse("2006-01-02", info.LeftTime)
if err != nil {
fmt.Println("解析日期失败:", err)
return
}
year := int32(t.Year())
_, err = service.GrpcOAImpl.UpdateDeductAnnualLeave(context.Background(), &oa.UpdateDeductAnnualLeaveReq{
StaffUID: info.StaffUID,
Year: year,
DeductDays: 0,
})
if err != nil {
return
}
}
func UpdateDeductAnnualLeave(info *api.TurnoverApply, createInfo login.Info) {
t, err := time.Parse("2006-01-02", info.LeftTime)
if err != nil {
fmt.Println("解析日期失败:", err)
return
}
year := int32(t.Year())
// 计算当年 1 月 1 日至离职日期的天数
startOfYear := time.Date(int(year), time.January, 1, 0, 0, 0, 0, time.UTC)
daysWorked := int(t.Sub(startOfYear).Hours() / 24) // 只取整数
userInfo, err := service.GrpcEmployeeProvider.FindEmployeeFileList(context.Background(), &employee.FindEmployeeFileListRequest{
UserId: createInfo.ID,
})
if err != nil || len(userInfo.EmployeeFileList) == 0 {
return
}
serviceLength := userInfo.EmployeeFileList[0].ServiceLength
if serviceLength < 1 {
return
}
// 确定年假基数
var annualLeaveBase int
switch {
case serviceLength >= 20:
annualLeaveBase = 15
case serviceLength >= 10:
annualLeaveBase = 10
case serviceLength >= 5:
annualLeaveBase = 7
default:
annualLeaveBase = 5
}
// 计算应有年假天数(取整数)
eligibleLeave := (daysWorked * annualLeaveBase) / 365
// 计算需扣除的年假天数
deductDays := int32(annualLeaveBase - eligibleLeave)
_, err = service.GrpcOAImpl.UpdateDeductAnnualLeave(context.Background(), &oa.UpdateDeductAnnualLeaveReq{
StaffUID: createInfo.ID,
Year: year,
DeductDays: deductDays,
})
if err != nil {
return
}
}