123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- // Copyright 2019 github.com. All rights reserved.
- // Use of this source code is governed by github.com.
- package model
- import (
- "fmt"
- "github.com/jinzhu/gorm"
- "go.uber.org/zap"
- "strings"
- "time"
- "github.com/jaryhe/gopkgs/logger"
- "smart-government-management/errors"
- )
- const (
- StaffAttendanceIn = 1
- StaffAttendanceOut = 2
- )
- const (
- AttendanceTab = iota
- AttendanceTabMonth1
- AttendanceTabMonth2
- AttendanceTabMonth3
- AttendanceTabMonth4
- AttendanceTabMonth5
- AttendanceTabMonth6
- AttendanceTabMonth7
- AttendanceTabMonth8
- AttendanceTabMonth9
- AttendanceTabMonth10
- AttendanceTabMonth11
- AttendanceTabMonth12
- AttendanceTabDay
- )
- type TStaffAttendance struct {
- Id int64 `gorm:"column:id"`
- ProjectId int64 `gorm:"column:project_id"`
- WorkNo string `gorm:"column:work_no"`
- Attendance string `gorm:"column:attendance"`
- InOut int64 `gorm:"column:in_out"`
- DayTime time.Time `gorm:"column:day_time"`
- CreatedAt time.Time `gorm:"column:created_at"`
- UpdatedAt time.Time `gorm:"column:updated_at"`
- TabType int `gorm:"-"` // 用于判断表0 t_staff_attendance, 1-12 t_staff_attendance_monthx, 13 t_staff_attendance_day
- }
- func (p TStaffAttendance) TableName() string {
- switch p.TabType {
- case AttendanceTabDay:
- return "db_smart_staff.t_staff_attendance_day"
- default:
- if p.TabType >=AttendanceTabMonth1 && p.TabType <= AttendanceTabMonth12 {
- return fmt.Sprintf("db_smart_staff.t_staff_attendance_month%d", p.TabType)
- }
- }
- return "db_smart_staff.t_staff_attendance_day"
- }
- func (p *TStaffAttendance) Insert(db *gorm.DB) error {
- return db.Create(p).Error
- }
- func (p *TStaffAttendance) Del(db *gorm.DB, where map[string]interface{}) error {
- cond, val, err := whereBuild(where)
- if err != nil {
- return err
- }
- return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
- }
- func (p *TStaffAttendance) Find(db *gorm.DB, where map[string]interface{}) error {
- cond, val, err := whereBuild(where)
- if err != nil {
- return err
- }
- return db.Table(p.TableName()).Where(cond, val...).First(p).Error
- }
- func (p *TStaffAttendance) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
- cond, val, err := whereBuild(where)
- if err != nil {
- return err
- }
- return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
- }
- func (p *TStaffAttendance) Save(db *gorm.DB) error {
- return db.Save(p).Error
- }
- func (p *TStaffAttendance) Count(db *gorm.DB, where map[string]interface{}) (int64, error) {
- if len(where) > 0 {
- cond, val, err := whereBuild(where)
- if err != nil {
- return 0, err
- }
- ret := int64(0)
- err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
- return ret, err
- }
- ret := int64(0)
- err := db.Table(p.TableName()).Count(&ret).Error
- return ret, err
- }
- func (p *TStaffAttendance) List(db *gorm.DB, where map[string]interface{}, page int) (list []TStaffAttendance, err error) {
- if len(where) > 0 {
- cond, val, err := whereBuild(where)
- if err != nil {
- return list, err
- }
- result := db.Table(p.TableName()).Where(cond, val...).Limit(PageSize).Offset(page).Find(&list)
- return list, result.Error
- }
- result := db.Table(p.TableName()).Limit(10).Offset(page).Find(&list)
- return list, result.Error
- }
- type TStaffAttendanceLast struct {
- WorkNo string
- Name string
- Photo string
- RecogTime string
- InOut int
- Attendance string
- Group string
- WorkType string
- }
- func (p *TStaffAttendance) Last(db *gorm.DB, projectId int64) (TStaffAttendanceLast, error) {
- result := TStaffAttendanceLast{}
- sql := fmt.Sprintf("select t1.work_no, t1.in_out, t1.attendance, t2.name, t2.photo, t2.group, t3.work_type from db_smart_staff.t_staff_attendance_day as t1 left join db_smart_staff.t_staff as t2 on t1.work_no=t2.work_no left join db_smart_staff.t_work_type_item as t3 on t2.work_type=t3.code")
- whereArray := []string{}
- if projectId > 0 {
- whereArray = append(whereArray,fmt.Sprintf("t1.project_id=%d", projectId))
- }
- where := ""
- for _, v := range whereArray {
- if where == "" {
- where = fmt.Sprintf(" where %s", v)
- continue
- }
- where = fmt.Sprintf("%s and %s", where, v)
- }
- sql = fmt.Sprintf("%s %s order by t1.updated_at desc limit 1", sql, where)
- ret := []TStaffAttendanceLast{}
- err := db.Raw(sql).Scan(&ret).Error
- if err != nil {
- logger.Error("mysql",
- zap.String("sql", sql),
- zap.String("error", err.Error()))
- return result, errors.DataBaseError
- }
- if len(ret) == 0 {
- return result, nil
- }
- for i, v := range ret {
- array := strings.Split(v.Attendance, "/")
- length := len(array)
- if length > 0 {
- tmp := array[length - 1]
- subLength := len(tmp)
- ret[i].RecogTime = tmp
- if subLength > 2 {
- if tmp[subLength-2:] == "-1" || tmp[subLength-2:] == "-2"{
- ret[i].RecogTime = tmp[:subLength-2]
- }
- }
- }
- }
- return ret[0], nil
- }
- type WorkTypeStaticItem struct {
- TypeCode int32
- TypeName string
- StaffCount int
- StaffAttendanceCount int
- Id int64
- }
- func staffWorkTypeSql(projectId int64)(string) {
- sql := fmt.Sprintf("select t1.code as type_code, t1.id, t1.work_type as type_name, count(distinct t2.work_no) as staff_count, count(distinct t3.work_no) as staff_attendance_count from db_smart_staff.t_work_type_item as t1 left join db_smart_staff.t_staff as t2 on t1.code=t2.work_type left join db_smart_staff.t_staff_attendance_day as t3 on t3.work_no=t2.work_no and t3.day_time='%s'", time.Now().Format("2006-01-02"))
- whereArray := []string{}
- if projectId > 0 {
- sql = fmt.Sprintf("select t1.code as type_code, t1.id, t1.work_type as type_name, count(distinct t2.work_no) as staff_count, count(distinct t3.work_no) as staff_attendance_count from db_smart_staff.t_work_type_item as t1 left join db_smart_staff.t_staff as t2 on t1.code=t2.work_type and t2.project_id=%d left join db_smart_staff.t_staff_attendance_day as t3 on t3.work_no=t2.work_no and t3.day_time='%s' and t3.project_id=%d", projectId, time.Now().Format("2006-01-02"), projectId)
- }
- where := ""
- for _, v := range whereArray {
- if where == "" {
- where = fmt.Sprintf(" where %s", v)
- continue
- }
- where = fmt.Sprintf("%s and %s", where, v)
- }
- sql = fmt.Sprintf("%s %s group by t1.code", sql, where)
- return sql
- }
- func StaffWorkTypeStaticList(db *gorm.DB, projectId int64) ([]WorkTypeStaticItem, error) {
- ret := []WorkTypeStaticItem{}
- var err error
- sql := staffWorkTypeSql(projectId)
- err = db.Raw(sql).Scan(&ret).Error
- if err != nil {
- logger.Error("mysql",
- zap.String("sql", sql),
- zap.String("error", err.Error()))
- return nil, errors.DataBaseError
- }
- return ret, nil
- }
- type StaffTypeStaticItem struct {
- TypeCode int32
- TypeName string
- StaffCount int
- StaffAttendanceCount int
- StaffInCount int
- }
- func staffTypeSql(projectId int64)(string) {
- sql := fmt.Sprintf("select t1.staff_code as type_code, t1.staff_type as type_name, count(distinct t2.work_no) as staff_count, count(distinct t3.work_no) as staff_attendance_count, count(distinct t4.work_no) as staff_in_count from db_smart_staff.t_staff_type_item as t1 left join db_smart_staff.t_staff as t2 on t1.staff_code=t2.staff_type left join db_smart_staff.t_staff_attendance_day as t3 on t3.work_no=t2.work_no and t3.day_time='%s' left join db_smart_staff.t_staff_attendance_day as t4 on t4.work_no=t2.work_no and t4.day_time='%s' and t4.in_out=1", time.Now().Format("2006-01-02"), time.Now().Format("2006-01-02"))
- whereArray := []string{}
- if projectId > 0 {
- sql = fmt.Sprintf("select t1.staff_code as type_code, t1.staff_type as type_name, count(distinct t2.work_no) as staff_count, count(distinct t3.work_no) as staff_attendance_count, count(distinct t4.work_no) as staff_in_count from db_smart_staff.t_staff_type_item as t1 left join db_smart_staff.t_staff as t2 on t1.staff_code=t2.staff_type and t2.project_id=%d left join db_smart_staff.t_staff_attendance_day as t3 on t3.work_no=t2.work_no and t3.day_time='%s' and t3.project_id=%d left join db_smart_staff.t_staff_attendance_day as t4 on t4.work_no=t2.work_no and t4.day_time='%s' and t4.in_out=1 and t4.project_id=%d", projectId, time.Now().Format("2006-01-02"), projectId, time.Now().Format("2006-01-02"), projectId)
- }
- where := ""
- for _, v := range whereArray {
- if where == "" {
- where = fmt.Sprintf(" where %s", v)
- continue
- }
- where = fmt.Sprintf("%s and %s", where, v)
- }
- sql = fmt.Sprintf("%s %s group by t1.staff_code order by staff_code", sql, where)
- return sql
- }
- func StaffTypeStaticList(db *gorm.DB, projectId int64) ([]StaffTypeStaticItem, error) {
- ret := []StaffTypeStaticItem{}
- var err error
- sql := staffTypeSql(projectId)
- err = db.Raw(sql).Scan(&ret).Error
- if err != nil {
- logger.Error("mysql",
- zap.String("sql", sql),
- zap.String("error", err.Error()))
- return nil, errors.DataBaseError
- }
- return ret, nil
- }
- type StaffCompanyStaticItem struct {
- CompanyName string
- StaffCount int
- StaffAttendanceCount int
- StaffAttendanceLabor int
- }
- const (
- StaffTypeLabor = 11
- )
- func staffCompanySql(projectId int64)(string) {
- sql := fmt.Sprintf("select t2.labor_company as company_name, count(distinct t2.work_no) as staff_count, count(distinct t3.work_no) as staff_attendance_count, count(distinct t4.work_no) as staff_attendance_labor from db_smart_staff.t_staff as t2 left join db_smart_staff.t_staff_attendance_day as t3 on t3.work_no=t2.work_no and t3.day_time='%s' left join db_smart_staff.t_staff_attendance_day as t4 on t4.work_no=t2.work_no and t4.day_time='%s' and t2.staff_type=%d" , time.Now().Format("2006-01-02"), time.Now().Format("2006-01-02"), StaffTypeLabor)
- whereArray := []string{}
- if projectId > 0 {
- whereArray = append(whereArray, fmt.Sprintf("t2.project_id=%d", projectId))
- }
- where := ""
- for _, v := range whereArray {
- if where == "" {
- where = fmt.Sprintf(" where %s", v)
- continue
- }
- where = fmt.Sprintf("%s and %s", where, v)
- }
- sql = fmt.Sprintf("%s %s group by t2.labor_company", sql, where)
- return sql
- }
- func StaffCompanyStaticList(db *gorm.DB, projectId int64) ([]StaffCompanyStaticItem, error) {
- ret := []StaffCompanyStaticItem{}
- var err error
- sql := staffCompanySql(projectId)
- err = db.Raw(sql).Scan(&ret).Error
- if err != nil {
- logger.Error("mysql",
- zap.String("sql", sql),
- zap.String("error", err.Error()))
- return nil, errors.DataBaseError
- }
- return ret, nil
- }
|