// Copyright 2019 github.com. All rights reserved. // Use of this source code is governed by github.com. package model import ( "access-control-monitor/consts" "access-control-monitor/errors" "fmt" "time" "github.com/jaryhe/gopkgs/logger" "github.com/jinzhu/gorm" "go.uber.org/zap" ) type StaffAttendance struct { ID int64 `gorm:"primary_key"` ProjectId int64 `json:"project_id"` WorkNo string `json:"work_no"` Attendance string `json:"start_time"` InOut int `json:"in_out"` DayTime string `json:"day_time"` CreatedAt string `json:"created_at"` UpdatedAt string `json:"updated_at"` TabType int `gorm:"-"` // 用于判断表0 t_staff_attendance, 1-12 t_staff_attendance_monthx, 13 t_staff_attendance_day } const ( AttendanceTab = iota AttendanceTabMonth1 AttendanceTabMonth2 AttendanceTabMonth3 AttendanceTabMonth4 AttendanceTabMonth5 AttendanceTabMonth6 AttendanceTabMonth7 AttendanceTabMonth8 AttendanceTabMonth9 AttendanceTabMonth10 AttendanceTabMonth11 AttendanceTabMonth12 AttendanceTabDay ) func (p StaffAttendance) TableName() string { switch p.TabType { case AttendanceTab: return "t_staff_attendance" case AttendanceTabDay: return "t_staff_attendance_day" default: if p.TabType >=AttendanceTabMonth1 && p.TabType <= AttendanceTabMonth12 { return fmt.Sprintf("t_staff_attendance_month%d", p.TabType) } } return "t_staff_attendance" } // Insert 插入一条记录 func (p *StaffAttendance) Insert(db *gorm.DB) error { timeNow := time.Now().Format(consts.TimeSecondLayOut) p.CreatedAt = timeNow //p.UpdatedAt = timeNow err := db.Table(p.TableName()).Create(p).Error if err != nil { fields, _ := json.MarshalToString(*p) logger.Error("mysql", zap.String("sql", "insert into "+p.TableName()), zap.String("fields", fields), zap.String("error", err.Error())) } return err } func (p *StaffAttendance) Delete(db *gorm.DB, filter map[string]interface{}) error { cond, val, err := whereBuild(filter) if err != nil { return err } err = db.Table(p.TableName()).Where(cond, val...).Delete(p).Error if err != nil { fields, _ := json.MarshalToString(filter) logger.Error("mysql", zap.String("sql", "delete from "+p.TableName()), zap.String("fields", fields), zap.String("error", err.Error())) } return err } func (p *StaffAttendance) Save(db *gorm.DB) error { timeNow := time.Now().Format(consts.TimeSecondLayOut) p.CreatedAt = timeNow err := db.Table(p.TableName()).Save(p).Error if err != nil { fields, _ := json.MarshalToString(*p) logger.Error("mysql", zap.String("sql", "save " +p.TableName()), zap.String("fields", fields), zap.String("error", err.Error())) } return err } func (p *StaffAttendance) Update(db *gorm.DB) error { timeNow := time.Now().Format(consts.TimeSecondLayOut) p.UpdatedAt = timeNow err := db.Table(p.TableName()).Model(p).Updates(p).Error if err != nil { fields, _ := json.MarshalToString(*p) logger.Error("mysql", zap.String("sql", "update "+ p.TableName()), zap.String("fields", fields), zap.String("error", err.Error())) } return err } // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。 func (p *StaffAttendance) UpdateSome(db *gorm.DB, filed map[string]interface{}) error { if filed == nil { return errors.ParamsError } //timeNow := time.Now().Format(consts.TimeSecondLayOut) //filed["updated_at"] = timeNow err := db.Table(p.TableName()).Updates(filed).Error if err != nil { fields, _ := json.MarshalToString(filed) logger.Error("mysql", zap.String("sql", "update "+p.TableName()), zap.String("fields", fields), zap.String("error", err.Error())) } return err } func (p *StaffAttendance) Query(db *gorm.DB, filter map[string]interface{}) error { err := db.Table(p.TableName()).Where(filter).Find(p).Error if err != nil { fields, _ := json.MarshalToString(filter) logger.Error("mysql", zap.String("sql", "select from "+p.TableName()), zap.String("fields", fields), zap.String("error", err.Error())) } return err } func (p *StaffAttendance) QueryAll(db *gorm.DB, filter map[string]interface{}, out interface{}) error { err := db.Table(p.TableName()).Where(filter).Find(out).Error if err != nil { fields, _ := json.MarshalToString(filter) logger.Error("mysql", zap.String("sql", "select from "+p.TableName()), zap.String("fields", fields), zap.String("error", err.Error())) } return err }