staff_attendance.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package model
  4. import (
  5. "fmt"
  6. "github.com/jinzhu/gorm"
  7. "go.uber.org/zap"
  8. "strings"
  9. "time"
  10. "github.com/jaryhe/gopkgs/logger"
  11. "smart-government-management/errors"
  12. )
  13. const (
  14. StaffAttendanceIn = 1
  15. StaffAttendanceOut = 2
  16. )
  17. const (
  18. AttendanceTab = iota
  19. AttendanceTabMonth1
  20. AttendanceTabMonth2
  21. AttendanceTabMonth3
  22. AttendanceTabMonth4
  23. AttendanceTabMonth5
  24. AttendanceTabMonth6
  25. AttendanceTabMonth7
  26. AttendanceTabMonth8
  27. AttendanceTabMonth9
  28. AttendanceTabMonth10
  29. AttendanceTabMonth11
  30. AttendanceTabMonth12
  31. AttendanceTabDay
  32. )
  33. type TStaffAttendance struct {
  34. Id int64 `gorm:"column:id"`
  35. ProjectId int64 `gorm:"column:project_id"`
  36. WorkNo string `gorm:"column:work_no"`
  37. Attendance string `gorm:"column:attendance"`
  38. InOut int64 `gorm:"column:in_out"`
  39. DayTime time.Time `gorm:"column:day_time"`
  40. CreatedAt time.Time `gorm:"column:created_at"`
  41. UpdatedAt time.Time `gorm:"column:updated_at"`
  42. TabType int `gorm:"-"` // 用于判断表0 t_staff_attendance, 1-12 t_staff_attendance_monthx, 13 t_staff_attendance_day
  43. }
  44. func (p TStaffAttendance) TableName() string {
  45. switch p.TabType {
  46. case AttendanceTabDay:
  47. return "db_smart_staff.t_staff_attendance_day"
  48. default:
  49. if p.TabType >=AttendanceTabMonth1 && p.TabType <= AttendanceTabMonth12 {
  50. return fmt.Sprintf("db_smart_staff.t_staff_attendance_month%d", p.TabType)
  51. }
  52. }
  53. return "db_smart_staff.t_staff_attendance_day"
  54. }
  55. func (p *TStaffAttendance) Insert(db *gorm.DB) error {
  56. return db.Create(p).Error
  57. }
  58. func (p *TStaffAttendance) Del(db *gorm.DB, where map[string]interface{}) error {
  59. cond, val, err := whereBuild(where)
  60. if err != nil {
  61. return err
  62. }
  63. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  64. }
  65. func (p *TStaffAttendance) Find(db *gorm.DB, where map[string]interface{}) error {
  66. cond, val, err := whereBuild(where)
  67. if err != nil {
  68. return err
  69. }
  70. return db.Table(p.TableName()).Where(cond, val...).First(p).Error
  71. }
  72. func (p *TStaffAttendance) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  73. cond, val, err := whereBuild(where)
  74. if err != nil {
  75. return err
  76. }
  77. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  78. }
  79. func (p *TStaffAttendance) Save(db *gorm.DB) error {
  80. return db.Save(p).Error
  81. }
  82. func (p *TStaffAttendance) Count(db *gorm.DB, where map[string]interface{}) (int64, error) {
  83. if len(where) > 0 {
  84. cond, val, err := whereBuild(where)
  85. if err != nil {
  86. return 0, err
  87. }
  88. ret := int64(0)
  89. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  90. return ret, err
  91. }
  92. ret := int64(0)
  93. err := db.Table(p.TableName()).Count(&ret).Error
  94. return ret, err
  95. }
  96. func (p *TStaffAttendance) List(db *gorm.DB, where map[string]interface{}, page int) (list []TStaffAttendance, err error) {
  97. if len(where) > 0 {
  98. cond, val, err := whereBuild(where)
  99. if err != nil {
  100. return list, err
  101. }
  102. result := db.Table(p.TableName()).Where(cond, val...).Limit(PageSize).Offset(page).Find(&list)
  103. return list, result.Error
  104. }
  105. result := db.Table(p.TableName()).Limit(10).Offset(page).Find(&list)
  106. return list, result.Error
  107. }
  108. type TStaffAttendanceLast struct {
  109. WorkNo string
  110. Name string
  111. Photo string
  112. RecogTime string
  113. InOut int
  114. Attendance string
  115. Group string
  116. WorkType string
  117. }
  118. func (p *TStaffAttendance) Last(db *gorm.DB, projectId int64) (TStaffAttendanceLast, error) {
  119. result := TStaffAttendanceLast{}
  120. 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")
  121. whereArray := []string{}
  122. if projectId > 0 {
  123. whereArray = append(whereArray,fmt.Sprintf("t1.project_id=%d", projectId))
  124. }
  125. where := ""
  126. for _, v := range whereArray {
  127. if where == "" {
  128. where = fmt.Sprintf(" where %s", v)
  129. continue
  130. }
  131. where = fmt.Sprintf("%s and %s", where, v)
  132. }
  133. sql = fmt.Sprintf("%s %s order by t1.updated_at desc limit 1", sql, where)
  134. ret := []TStaffAttendanceLast{}
  135. err := db.Raw(sql).Scan(&ret).Error
  136. if err != nil {
  137. logger.Error("mysql",
  138. zap.String("sql", sql),
  139. zap.String("error", err.Error()))
  140. return result, errors.DataBaseError
  141. }
  142. if len(ret) == 0 {
  143. return result, nil
  144. }
  145. for i, v := range ret {
  146. array := strings.Split(v.Attendance, "/")
  147. length := len(array)
  148. if length > 0 {
  149. tmp := array[length - 1]
  150. subLength := len(tmp)
  151. ret[i].RecogTime = tmp
  152. if subLength > 2 {
  153. if tmp[subLength-2:] == "-1" || tmp[subLength-2:] == "-2"{
  154. ret[i].RecogTime = tmp[:subLength-2]
  155. }
  156. }
  157. }
  158. }
  159. return ret[0], nil
  160. }
  161. type WorkTypeStaticItem struct {
  162. TypeCode int32
  163. TypeName string
  164. StaffCount int
  165. StaffAttendanceCount int
  166. Id int64
  167. }
  168. func staffWorkTypeSql(projectId int64)(string) {
  169. 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"))
  170. whereArray := []string{}
  171. if projectId > 0 {
  172. 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)
  173. }
  174. where := ""
  175. for _, v := range whereArray {
  176. if where == "" {
  177. where = fmt.Sprintf(" where %s", v)
  178. continue
  179. }
  180. where = fmt.Sprintf("%s and %s", where, v)
  181. }
  182. sql = fmt.Sprintf("%s %s group by t1.code", sql, where)
  183. return sql
  184. }
  185. func StaffWorkTypeStaticList(db *gorm.DB, projectId int64) ([]WorkTypeStaticItem, error) {
  186. ret := []WorkTypeStaticItem{}
  187. var err error
  188. sql := staffWorkTypeSql(projectId)
  189. err = db.Raw(sql).Scan(&ret).Error
  190. if err != nil {
  191. logger.Error("mysql",
  192. zap.String("sql", sql),
  193. zap.String("error", err.Error()))
  194. return nil, errors.DataBaseError
  195. }
  196. return ret, nil
  197. }
  198. type StaffTypeStaticItem struct {
  199. TypeCode int32
  200. TypeName string
  201. StaffCount int
  202. StaffAttendanceCount int
  203. StaffInCount int
  204. }
  205. func staffTypeSql(projectId int64)(string) {
  206. 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"))
  207. whereArray := []string{}
  208. if projectId > 0 {
  209. 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)
  210. }
  211. where := ""
  212. for _, v := range whereArray {
  213. if where == "" {
  214. where = fmt.Sprintf(" where %s", v)
  215. continue
  216. }
  217. where = fmt.Sprintf("%s and %s", where, v)
  218. }
  219. sql = fmt.Sprintf("%s %s group by t1.staff_code order by staff_code", sql, where)
  220. return sql
  221. }
  222. func StaffTypeStaticList(db *gorm.DB, projectId int64) ([]StaffTypeStaticItem, error) {
  223. ret := []StaffTypeStaticItem{}
  224. var err error
  225. sql := staffTypeSql(projectId)
  226. err = db.Raw(sql).Scan(&ret).Error
  227. if err != nil {
  228. logger.Error("mysql",
  229. zap.String("sql", sql),
  230. zap.String("error", err.Error()))
  231. return nil, errors.DataBaseError
  232. }
  233. return ret, nil
  234. }
  235. type StaffCompanyStaticItem struct {
  236. CompanyName string
  237. StaffCount int
  238. StaffAttendanceCount int
  239. StaffAttendanceLabor int
  240. }
  241. const (
  242. StaffTypeLabor = 11
  243. )
  244. func staffCompanySql(projectId int64)(string) {
  245. 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)
  246. whereArray := []string{}
  247. if projectId > 0 {
  248. whereArray = append(whereArray, fmt.Sprintf("t2.project_id=%d", projectId))
  249. }
  250. where := ""
  251. for _, v := range whereArray {
  252. if where == "" {
  253. where = fmt.Sprintf(" where %s", v)
  254. continue
  255. }
  256. where = fmt.Sprintf("%s and %s", where, v)
  257. }
  258. sql = fmt.Sprintf("%s %s group by t2.labor_company", sql, where)
  259. return sql
  260. }
  261. func StaffCompanyStaticList(db *gorm.DB, projectId int64) ([]StaffCompanyStaticItem, error) {
  262. ret := []StaffCompanyStaticItem{}
  263. var err error
  264. sql := staffCompanySql(projectId)
  265. err = db.Raw(sql).Scan(&ret).Error
  266. if err != nil {
  267. logger.Error("mysql",
  268. zap.String("sql", sql),
  269. zap.String("error", err.Error()))
  270. return nil, errors.DataBaseError
  271. }
  272. return ret, nil
  273. }