package controllers import ( "ERP_ams/Nats/NatsServer" "ERP_ams/conf" "ERP_ams/models/Account" "ERP_ams/models/Attendance" "fmt" userlibs "git.baozhida.cn/ERP_libs/User" "git.baozhida.cn/ERP_libs/lib" beego "github.com/beego/beego/v2/server/web" "math" "strconv" "time" ) type LeaveController struct { beego.Controller User userlibs.User } func (c *LeaveController) Prepare() { c.User = *Account.User_r } // 管理员请假审批列表 只显示待审核 func (c *LeaveController) Leave_List() { var r_jsons lib.R_JSONS page, _ := c.GetInt("page") if page < 1 { page = 1 } page_z, _ := c.GetInt("page_z") if page_z < 1 { page_z = conf.Page_size } userList, _ := NatsServer.Read_User_List_All() Account.Read_User_All_Map(userList) r_jsons.Data, r_jsons.Num = Attendance.Read_Attendance_List("", c.User.T_uuid, 0, Attendance.AttendanceWaitAudit, page, page_z) r_jsons.Page = page r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z))) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } func (c *LeaveController) Leave_User_List() { var r_jsons lib.R_JSONS page, _ := c.GetInt("page") if page < 1 { page = 1 } page_z, _ := c.GetInt("page_z") if page_z < 1 { page_z = conf.Page_size } T_uuid := c.GetString("T_uuid") if len(T_uuid) == 0 { T_uuid = c.User.T_uuid } userList, _ := NatsServer.Read_User_List_All() Account.Read_User_All_Map(userList) r_jsons.Data, r_jsons.Num = Attendance.Read_Attendance_List(T_uuid, "", 0, 0, page, page_z) r_jsons.Page = page r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z))) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } // 财务 func (c *LeaveController) Leave_Finance_List() { var r_jsons lib.R_JSONS T_uuid := c.GetString("T_uuid") if len(T_uuid) == 0 { T_uuid = c.User.T_uuid } // 年月 2023-01 T_month := c.GetString("T_month") if len(T_month) > 0 { _, err := time.Parse("2006-01", T_month) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"} c.ServeJSON() return } } r_jsons.Data = Attendance.Read_Attendance_List_For_Finance(T_uuid, T_month) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons} c.ServeJSON() return } func (c *LeaveController) Leave_Type_List() { T_deduct, _ := c.GetInt("T_deduct") c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Attendance.Read_AttendanceType_All(T_deduct)} c.ServeJSON() return } func (c *LeaveController) Leave_Add() { T_type, _ := c.GetInt("T_type") T_start_time := c.GetString("T_start_time") T_end_time := c.GetString("T_end_time") T_duration, _ := c.GetInt("T_duration") T_text := c.GetString("T_text") T_approver := c.GetString("T_approver") startTime, is := lib.TimeStrToTime(T_start_time) if !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"} c.ServeJSON() return } endTime, is := lib.TimeStrToTime(T_end_time) if !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"} c.ServeJSON() return } if T_type == Attendance.AttendanceDaysOff { duration := Attendance.GetRemainingDaysOff(c.User.T_uuid) if T_duration > duration { c.Data["json"] = lib.JSONS{Code: 202, Msg: "超过可调休时长!"} c.ServeJSON() return } } var_ := Attendance.Attendance{ T_uid: c.User.T_uuid, T_type: T_type, T_start_time: startTime, T_end_time: endTime, T_duration: T_duration, T_text: T_text, T_approver: T_approver, T_State: Attendance.AttendanceWaitAudit, } Id, err := Attendance.Add_Attendance(var_) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"} c.ServeJSON() return } NatsServer.AddUserLogs(c.User.T_uuid, "我的请假", "添加", var_) NatsServer.AddNews(T_approver, fmt.Sprintf("【请假审批】您有一条%s的请假审批待处理", c.User.T_name), conf.LeaveApprovalNewsUrl) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id} c.ServeJSON() return } // 扣除 func (c *LeaveController) Leave_Deduct() { T_type, _ := c.GetInt("T_type") T_duration, _ := c.GetInt("T_duration") T_month := c.GetString("T_month") T_text := c.GetString("T_text") T_uuid := c.GetString("T_uuid") month, is := lib.MonthStrToTime(T_month) if !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"} c.ServeJSON() return } duration := Attendance.GetRemainingDaysOff(T_uuid) if T_duration > duration { c.Data["json"] = lib.JSONS{Code: 202, Msg: "超过加班总时长!"} c.ServeJSON() return } month = month.AddDate(0, 1, -1) var_ := Attendance.Attendance{ T_uid: T_uuid, T_type: T_type + 100, T_duration: T_duration, T_text: T_text, T_approver: c.User.T_uuid, T_start_time: month, T_end_time: month, T_State: Attendance.AttendancePass, } Id, err := Attendance.Add_Attendance(var_) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"} c.ServeJSON() return } NatsServer.AddUserLogs(c.User.T_uuid, "我的请假", "扣除", var_) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id} c.ServeJSON() return } func (c *LeaveController) Leave_Update() { T_id, _ := c.GetInt("T_id") T_type, _ := c.GetInt("T_type") T_start_time := c.GetString("T_start_time") T_end_time := c.GetString("T_end_time") T_duration, _ := c.GetInt("T_duration") T_text := c.GetString("T_text") T_approver := c.GetString("T_approver") leave, err := Attendance.Read_Attendance_ById(T_id) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"} c.ServeJSON() return } if leave.T_State == Attendance.AttendancePass { c.Data["json"] = lib.JSONS{Code: 202, Msg: "请假审批已通过,禁止修改!"} c.ServeJSON() return } if T_type > 0 { leave.T_type = T_type } if len(T_start_time) > 0 { startTime, is := lib.TimeStrToTime(T_start_time) if !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"} c.ServeJSON() return } leave.T_start_time = startTime } if len(T_end_time) > 0 { endTime, is := lib.TimeStrToTime(T_end_time) if !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"} c.ServeJSON() return } leave.T_end_time = endTime } if T_duration > 0 { leave.T_duration = T_duration } if len(T_text) > 0 { leave.T_text = T_text } if len(T_approver) > 0 { leave.T_approver = T_approver } leave.T_State = Attendance.AttendanceWaitAudit _, err = Attendance.Update_Attendance(leave, "T_type", "T_start_time", "T_end_time", "T_duration", "T_text", "T_approver", "T_State") if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"} c.ServeJSON() return } NatsServer.AddUserLogs(c.User.T_uuid, "我的请假", "修改", leave) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_id} c.ServeJSON() return } func (c *LeaveController) Leave_Approval() { T_id, _ := c.GetInt("T_id") T_State, _ := c.GetInt("T_State") leave, err := Attendance.Read_Attendance_ById(T_id) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"} c.ServeJSON() return } if leave.T_approver != c.User.T_uuid { c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权操作!"} c.ServeJSON() return } if T_State == 1 { leave.T_State = Attendance.AttendancePass } if T_State == 0 { leave.T_State = Attendance.AttendanceNotPass } id, err := Attendance.Update_Attendance(leave, "T_State") if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"} c.ServeJSON() return } NatsServer.AddUserLogs(c.User.T_uuid, "我的请假", "审批", leave) NatsServer.AddNews(leave.T_uid, "【请假申请】您的请假申请已通过", conf.MyLeaveNewsUrl) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: id} c.ServeJSON() return } func (c *LeaveController) Leave_Del() { T_id, _ := c.GetInt("T_id") leave, err := Attendance.Read_Attendance_ById(T_id) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"} c.ServeJSON() return } if leave.T_State == Attendance.AttendancePass { c.Data["json"] = lib.JSONS{Code: 202, Msg: "请假审批已通过,禁止删除!"} c.ServeJSON() return } id, err := Attendance.Delete_Attendance(leave) if err != nil { c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"} c.ServeJSON() return } NatsServer.AddUserLogs(c.User.T_uuid, "我的请假", "删除", strconv.Itoa(T_id)) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: id} c.ServeJSON() return } // 获取剩余调休时长 func (c *LeaveController) Leave_RemainingDaysOff() { duration := Attendance.GetRemainingDaysOff(c.User.T_uuid) c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: duration} c.ServeJSON() return } // 计算请假时长 func (c *LeaveController) Leave_Duration() { T_start_time := c.GetString("T_start_time") T_end_time := c.GetString("T_end_time") startTime, is := lib.TimeStrToTime(T_start_time) if !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"} c.ServeJSON() return } endTime, is := lib.TimeStrToTime(T_end_time) if !is { c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"} c.ServeJSON() return } //开始时间的小时和分钟 s_h := startTime.Hour() s_mm := startTime.Minute() e_h := endTime.Hour() e_mm := endTime.Minute() diff_day := int(endTime.Sub(startTime).Hours() / 24) // 间隔天数 var diff_hours float32 // 间隔小时 var diff_minutes float32 // 间隔分钟 if s_h < 9 { // 开始小时早于9点,从9点起算 s_h = 9 s_mm = 0 } if e_h >= 17 && e_mm > 30 { // 结束时间晚于17:30点,到17:30点止 e_h = 17 e_mm = 30 } if e_mm < s_mm { // 结束分钟数<开始分钟数,向小时借 e_mm += 60 e_h-- } diff_minutes = float32(e_mm - s_mm) if diff_day > 1 { // 跨天 diff_hours = 17.5 - float32(s_h) + float32(e_h-9) // 如果开始时间小于12点 请假小时数-1 // 如果结束时间大于13点,请假小时数-1 if s_h <= 12 { diff_hours = diff_hours - 1 } if e_h >= 13 { diff_hours = diff_hours - 1 } } else { // 不跨天 // 开始时间-结束时间跨越午休时间,间隔小时数-1 diff_hours = float32(e_h - s_h) if s_h <= 12 && e_h >= 13 { diff_hours = float32(e_h - s_h - 1) } } if diff_day > 1 { diff_day -= 1 } diff_day_hours := float32(diff_day) * 7.5 diff_m := int((diff_day_hours+diff_hours)*60 + diff_minutes) // 分钟数量 c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: diff_m} c.ServeJSON() return }