// Copyright 2019 github.com. All rights reserved. // Use of this source code is governed by github.com. package v1 import ( "access-control-monitor/errors" "access-control-monitor/model/v1" param_v1 "access-control-monitor/param/v1" "access-control-monitor/utils" "fmt" "go.uber.org/zap" "strings" "github.com/jinzhu/gorm" "github.com/jaryhe/gopkgs/database" "github.com/jaryhe/gopkgs/logger" "github.com/jaryhe/gopkgs/tasker/transtasker" ) // 添加考勤数据至月表 func addAttendanceToMonth(db *gorm.DB, content []param_v1.AttendanceContent, projectId int64) error { for _, v := range content { attendance := &model.StaffAttendance{} dayTime := strings.Split(v.RecogTime, " ")[0] month := utils.GetMonthFromDate(dayTime) if month < 1 { logger.Error("addAttendanceToMonth", zap.String("param", dayTime), zap.String("error", "get month failed")) return errors.ServiceError } attendance.TabType = month filter := map[string]interface{}{"project_id": projectId, "work_no": v.WorkNo, "day_time": dayTime} err := attendance.Query(db, filter) // 当天有考勤数据 if err == nil { attendanceStr := fmt.Sprintf("%s/%s-%d", attendance.Attendance, v.RecogTime, v.IsIn) fileds := map[string]interface{}{"attendance": attendanceStr, "in_out": v.IsIn, "updated_at":v.RecogTime} err := attendance.UpdateSome(db, fileds) if err != nil { return errors.DataBaseError } } else { // 无考勤数据 if err == gorm.ErrRecordNotFound { attendance.WorkNo = v.WorkNo attendance.ProjectId = projectId attendance.DayTime = dayTime attendance.InOut = v.IsIn attendance.UpdatedAt = v.RecogTime attendance.Attendance = fmt.Sprintf("%s-%d", v.RecogTime, v.IsIn) err := attendance.Insert(db) if err != nil { return errors.DataBaseError } } else { return errors.DataBaseError } } } return nil } // 添加考勤数据至当天表 func addAttendanceToDay(db *gorm.DB, content []param_v1.AttendanceContent, projectId int64) error { attendance := &model.StaffAttendance{ TabType:model.AttendanceTabDay, } return addAttendance(db, content, projectId, attendance) } // 添加考勤数据至总表 func addAttendanceToTotal(db *gorm.DB, content []param_v1.AttendanceContent, projectId int64) error { attendance := &model.StaffAttendance{ TabType:model.AttendanceTab, } return addAttendance(db, content, projectId, attendance) } func addAttendance(db *gorm.DB, content []param_v1.AttendanceContent, projectId int64, attendance *model.StaffAttendance) error { for _, v := range content { dayTime := strings.Split(v.RecogTime, " ")[0] filter := map[string]interface{}{"project_id": projectId, "work_no": v.WorkNo, "day_time": dayTime} err := attendance.Query(database.DB(), filter) // 当天有考勤数据 if err == nil { attendanceStr := fmt.Sprintf("%s/%s-%d", attendance.Attendance, v.RecogTime, v.IsIn) fileds := map[string]interface{}{"attendance": attendanceStr, "in_out": v.IsIn, "updated_at":v.RecogTime} err := attendance.UpdateSome(database.DB(), fileds) if err != nil { return errors.DataBaseError } } else { // 无考勤数据 if err == gorm.ErrRecordNotFound { attendance.WorkNo = v.WorkNo attendance.ProjectId = projectId attendance.DayTime = dayTime attendance.InOut = v.IsIn attendance.UpdatedAt = v.RecogTime attendance.Attendance = fmt.Sprintf("%s-%d", v.RecogTime, v.IsIn) err := attendance.Insert(database.DB()) if err != nil { return errors.DataBaseError } } else { return errors.DataBaseError } } } return nil } // 添加考勤数据 func AddAttendance(content []param_v1.AttendanceContent, projectId int64) error { dayTask := func (db *gorm.DB) error { return addAttendanceToDay(db, content, projectId) } monthTask := func(db *gorm.DB) error { return addAttendanceToMonth(db, content, projectId) } totalTask := func(db *gorm.DB) error { return nil //return addAttendanceToTotal(db, content, projectId) } err := transtasker.Exec(database.DB(), transtasker.NewTask(dayTask), transtasker.NewTask(monthTask), transtasker.NewTask(totalTask)) return err }