Attendance.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. package Attendance
  2. import (
  3. "ERP_ams/logs"
  4. "ERP_ams/models/Account"
  5. "fmt"
  6. "github.com/beego/beego/v2/adapter/orm"
  7. orm2 "github.com/beego/beego/v2/client/orm"
  8. "gogs.baozhida.cn/zoie/ERP_libs/lib"
  9. "strings"
  10. "time"
  11. )
  12. const (
  13. AttendanceDelete = iota
  14. AttendancePass
  15. AttendanceNotPass
  16. AttendanceWaitAudit
  17. )
  18. // 考勤
  19. type Attendance struct {
  20. Id int `orm:"column(ID);size(11);auto;pk"`
  21. T_uid string `orm:"index;size(32);null"` // 用户uuid
  22. T_type int `orm:"size(20);default(0)"` // 加班类型 加班
  23. T_start_time time.Time `orm:"type(timestamp);null;"` // 开始时间
  24. T_prove_img string `orm:"type(text);null;"` // 请假证明
  25. T_end_time time.Time `orm:"type(timestamp);null;"` // 结束时间
  26. T_duration int `orm:"size(8);null"` // 加班时长
  27. T_text string `orm:"type(text);null"` // 请假内容
  28. T_approver string `orm:"size(32);null"` // 审批人
  29. T_State int `orm:"size(2);default(2)"` // 0 删除 1 通过 2 未通过 3 待审核
  30. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  31. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  32. }
  33. func (t *Attendance) TableName() string {
  34. return "attendance"
  35. }
  36. func init() {
  37. //注册模型
  38. orm2.Debug = true
  39. orm.RegisterModel(new(Attendance))
  40. }
  41. type Attendance_R struct {
  42. Id int
  43. T_uid string
  44. T_user_name string
  45. T_type int
  46. T_type_name string
  47. T_start_time string
  48. T_prove_img string
  49. T_end_time string
  50. T_duration int
  51. T_text string
  52. T_approver string // 审批人uuid
  53. T_approver_name string // 审批人名称
  54. T_State int
  55. CreateTime string
  56. UpdateTime string
  57. }
  58. type Attendance_S struct {
  59. Id int
  60. T_type int
  61. T_type_name string
  62. T_duration int
  63. T_text string
  64. T_approver string // 审批人uuid
  65. T_approver_name string // 审批人名称
  66. T_State int
  67. RemainingTime int // 剩余时长
  68. CreateTime string
  69. UpdateTime string
  70. }
  71. // ---------------- 特殊方法 -------------------
  72. func AttendanceToAttendance_R(t Attendance) (r Attendance_R) {
  73. r.Id = t.Id
  74. r.T_uid = t.T_uid
  75. r.T_type = t.T_type
  76. T_type_name := Read_AttendanceType_Get(t.T_type)
  77. if t.T_type > 100 {
  78. T_type_name = "扣除-" + Read_AttendanceType_Get(t.T_type-100)
  79. }
  80. r.T_type_name = T_type_name
  81. r.T_user_name = Account.Read_User_T_name_Get(t.T_uid)
  82. r.T_start_time = t.T_start_time.Format("2006-01-02 15:04:05")
  83. r.T_prove_img = t.T_prove_img
  84. r.T_end_time = t.T_end_time.Format("2006-01-02 15:04:05")
  85. r.T_duration = t.T_duration
  86. r.T_text = t.T_text
  87. r.T_approver = t.T_approver
  88. r.T_approver_name = Account.Read_User_T_name_Get(t.T_approver)
  89. r.T_State = t.T_State
  90. r.CreateTime = t.CreateTime.Format("2006-01-02 15:04:05")
  91. r.UpdateTime = t.UpdateTime.Format("2006-01-02 15:04:05")
  92. if t.T_type > 100 {
  93. r.T_start_time = "-"
  94. r.T_end_time = "-"
  95. }
  96. return r
  97. }
  98. func AttendanceToAttendance_S(t Attendance) (r Attendance_S) {
  99. r.Id = t.Id
  100. r.T_type = t.T_type
  101. T_type_name := Read_AttendanceType_Get(t.T_type)
  102. if t.T_type > 100 {
  103. T_type_name = "扣除-" + Read_AttendanceType_Get(t.T_type-100)
  104. }
  105. r.T_type_name = T_type_name
  106. r.T_duration = t.T_duration
  107. r.T_text = t.T_text
  108. r.T_approver = t.T_approver
  109. r.T_approver_name = Account.Read_User_T_name_Get(t.T_approver)
  110. r.T_State = t.T_State
  111. r.CreateTime = t.CreateTime.Format("2006-01-02 15:04:05")
  112. r.UpdateTime = t.UpdateTime.Format("2006-01-02 15:04:05")
  113. return r
  114. }
  115. // 获取 ById
  116. func Read_Attendance_ById(id int) (r Attendance, err error) {
  117. o := orm.NewOrm()
  118. r = Attendance{Id: id}
  119. err = o.Read(&r)
  120. if err != nil {
  121. logs.Error(lib.FuncName(), err)
  122. return r, err
  123. }
  124. return r, err
  125. }
  126. // 添加
  127. func Add_Attendance(m Attendance) (id int64, err error) {
  128. o := orm.NewOrm()
  129. id, err = o.Insert(&m)
  130. if err != nil {
  131. logs.Error(lib.FuncName(), err)
  132. return
  133. }
  134. return
  135. }
  136. // 修改
  137. func Update_Attendance(m Attendance, cols ...string) (id int64, err error) {
  138. o := orm.NewOrm()
  139. id, err = o.Update(&m, cols...)
  140. if err != nil {
  141. logs.Error(lib.FuncName(), err)
  142. return
  143. }
  144. fmt.Println("Number of records updated in database:", id)
  145. return id, nil
  146. }
  147. // 删除
  148. func Delete_Attendance(m Attendance) (id int64, err error) {
  149. o := orm.NewOrm()
  150. m.T_State = 0
  151. id, err = o.Update(&m, "T_State")
  152. if err != nil {
  153. logs.Error(lib.FuncName(), err)
  154. return
  155. }
  156. fmt.Println("Number of records updated in database:", id)
  157. return id, nil
  158. }
  159. // 获取列表
  160. func Read_Attendance_List(T_uid, T_approver string, T_overtime, T_state, page, page_z int) (r []Attendance_R, cnt int64) {
  161. o := orm.NewOrm()
  162. // 也可以直接使用 Model 结构体作为表名
  163. var map_r []Attendance
  164. qs := o.QueryTable(new(Attendance))
  165. var offset int64
  166. if page <= 1 {
  167. offset = 0
  168. } else {
  169. offset = int64((page - 1) * page_z)
  170. }
  171. cond := orm.NewCondition()
  172. cond = cond.And("T_State__gt", AttendanceDelete)
  173. if len(T_uid) > 0 {
  174. cond = cond.And("T_uid", T_uid)
  175. }
  176. // 审批人
  177. if len(T_approver) > 0 {
  178. cond = cond.And("T_approver", T_approver)
  179. }
  180. if T_state > 0 {
  181. cond = cond.And("T_State", T_state)
  182. }
  183. if T_overtime == 1 {
  184. cond = cond.And("T_type", AttendanceOvertime)
  185. } else {
  186. cond = cond.AndNot("T_type", AttendanceOvertime)
  187. }
  188. var err error
  189. if page_z == 9999 {
  190. _, err = qs.SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&map_r)
  191. } else {
  192. _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&map_r)
  193. }
  194. if err != nil {
  195. logs.Error(lib.FuncName(), err)
  196. return
  197. }
  198. cnt, err = qs.SetCond((*orm2.Condition)(cond)).Count()
  199. if err != nil {
  200. logs.Error(lib.FuncName(), err)
  201. return
  202. }
  203. for _, v := range map_r {
  204. r = append(r, AttendanceToAttendance_R(v))
  205. }
  206. return r, cnt
  207. }
  208. // 财务查看的请假列表
  209. func Read_Attendance_List_For_Finance2(T_uid, T_month string) (r []Attendance_R) {
  210. o := orm.NewOrm()
  211. // 也可以直接使用 Model 结构体作为表名
  212. var map_r []Attendance
  213. qs := o.QueryTable(new(Attendance))
  214. cond := orm.NewCondition()
  215. if len(T_month) == 0 {
  216. T_month = time.Now().Format("2006-01")
  217. }
  218. list := []int{AttendanceSick, AttendancePersonal}
  219. cond = cond.And("T_State", AttendancePass).And("T_uid", T_uid).And("T_type__in", list).And("T_end_time__startswith", T_month)
  220. _, err := qs.SetCond((*orm2.Condition)(cond)).OrderBy("Id").All(&map_r)
  221. if err != nil {
  222. logs.Error(lib.FuncName(), err)
  223. return
  224. }
  225. var conut int
  226. for _, v := range map_r {
  227. // 如果开始日期不是本月
  228. //if !strings.Contains(v.T_start_time.Format("2006-01-02 15:04:05"), T_month) {
  229. //
  230. //}
  231. r = append(r, AttendanceToAttendance_R(v))
  232. conut += v.T_duration
  233. }
  234. if len(map_r) > 0 {
  235. r = append(r, Attendance_R{
  236. T_type_name: "统计",
  237. T_duration: conut,
  238. })
  239. }
  240. return r
  241. }
  242. func Read_Attendance_List_For_Finance(T_uid, T_month string) (r []Attendance_R) {
  243. o := orm.NewOrm()
  244. // 也可以直接使用 Model 结构体作为表名
  245. var map_r []Attendance
  246. qs := o.QueryTable(new(Attendance))
  247. cond := orm.NewCondition()
  248. if len(T_month) == 0 {
  249. T_month = time.Now().Format("2006-01")
  250. }
  251. list := Get_LeaveType_List()
  252. cond = cond.And("T_State", AttendancePass).And("T_uid", T_uid).
  253. AndCond(cond.Or("T_type__in", list).Or("T_type__gt", 100)).
  254. AndCond(cond.Or("T_start_time__startswith", T_month).Or("T_end_time__startswith", T_month))
  255. _, err := qs.SetCond((*orm2.Condition)(cond)).OrderBy("Id").All(&map_r)
  256. if err != nil {
  257. logs.Error(lib.FuncName(), err)
  258. return
  259. }
  260. var conut int
  261. for _, v := range map_r {
  262. // 如果开始日期不是本月
  263. month, _ := lib.MonthStrToTime(T_month)
  264. if !strings.Contains(v.T_start_time.Format("2006-01-02 15:04:05"), T_month) {
  265. v.T_start_time = time.Date(month.Year(), month.Month(), 1, 9, 0, 0, 0, time.Local)
  266. v.T_duration = GetLeaveDuration(v.T_start_time, v.T_end_time)
  267. }
  268. if !strings.Contains(v.T_end_time.Format("2006-01-02 15:04:05"), T_month) {
  269. lastDays := month.AddDate(0, 1, -1)
  270. v.T_end_time = time.Date(month.Year(), month.Month(), lastDays.Day(), 17, 30, 0, 0, time.Local)
  271. v.T_duration = GetLeaveDuration(v.T_start_time, v.T_end_time)
  272. }
  273. r = append(r, AttendanceToAttendance_R(v))
  274. conut += v.T_duration
  275. }
  276. if len(map_r) > 0 {
  277. r = append(r, Attendance_R{
  278. T_type_name: "统计",
  279. T_duration: conut,
  280. })
  281. }
  282. return r
  283. }
  284. // 获取列表
  285. func Read_Attendance_List_For_Stat(T_uid string, page, page_z int) (r []Attendance_S, cnt int64) {
  286. o := orm.NewOrm()
  287. // 也可以直接使用 Model 结构体作为表名
  288. var map_r []Attendance
  289. qs := o.QueryTable(new(Attendance))
  290. var offset int64
  291. if page <= 1 {
  292. offset = 0
  293. } else {
  294. offset = int64((page - 1) * page_z)
  295. }
  296. var T_type_list = []int{AttendanceOvertime, AttendanceDaysOff, AttendanceShiftPerf}
  297. cond := orm.NewCondition()
  298. cond = cond.And("T_uid", T_uid).And("T_State", AttendancePass).
  299. AndCond(cond.Or("T_type__in", T_type_list).Or("T_type__gt", 100))
  300. var err error
  301. if page_z == 9999 {
  302. _, err = qs.SetCond((*orm2.Condition)(cond)).OrderBy("-T_start_time").All(&map_r)
  303. } else {
  304. _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond)).OrderBy("-T_start_time").All(&map_r)
  305. }
  306. if err != nil {
  307. logs.Error(lib.FuncName(), err)
  308. return
  309. }
  310. cnt, err = qs.SetCond((*orm2.Condition)(cond)).Count()
  311. if err != nil {
  312. logs.Error(lib.FuncName(), err)
  313. return
  314. }
  315. for _, v := range map_r {
  316. r = append(r, AttendanceToAttendance_S(v))
  317. }
  318. return r, cnt
  319. }
  320. func GetRemainingDaysOff(T_uuid string) int {
  321. attendance, _ := Read_Attendance_List_For_Stat(T_uuid, 0, 9999)
  322. var duration int
  323. for i := 0; i < len(attendance); i++ {
  324. // 调休,转绩效 减时长
  325. if attendance[i].T_type == AttendanceDaysOff || attendance[i].T_type == AttendanceShiftPerf || attendance[i].T_type > 100 {
  326. duration -= attendance[i].T_duration
  327. } else {
  328. // 加班 加时长
  329. duration += attendance[i].T_duration
  330. }
  331. }
  332. return duration
  333. }
  334. // GetLeaveDuration 计算请假时长 返回分钟数
  335. // 规则:早上9:00 下午17:30 午休1小时不计入请假时长
  336. func GetLeaveDuration(startTime, endTime time.Time) int {
  337. //开始时间的小时和分钟
  338. s_h := startTime.Hour()
  339. s_mm := startTime.Minute()
  340. e_h := endTime.Hour()
  341. e_mm := endTime.Minute()
  342. diff_day := int(endTime.Sub(startTime).Hours() / 24) // 间隔天数
  343. var diff_hours float32 // 间隔小时
  344. var diff_minutes float32 // 间隔分钟
  345. if s_h < 9 {
  346. // 开始小时早于8点30,从9点30起算
  347. s_h = 8
  348. s_mm = 30
  349. }
  350. if e_h > 17 && e_mm > 30 {
  351. // 结束时间晚于17:30点,到17:30点止
  352. e_h = 17
  353. e_mm = 30
  354. }
  355. if e_mm < s_mm {
  356. // 结束分钟数<开始分钟数,向小时借
  357. e_mm += 60
  358. e_h--
  359. }
  360. diff_minutes = float32(e_mm - s_mm)
  361. if diff_day > 1 {
  362. // 跨天
  363. diff_hours = 17.5 - float32(s_h) + float32(e_h-9)
  364. // 如果开始时间小于12点 请假小时数-1
  365. // 如果结束时间大于13点,请假小时数-1
  366. if s_h <= 12 {
  367. diff_hours = diff_hours - 1
  368. }
  369. if e_h >= 13 {
  370. diff_hours = diff_hours - 1
  371. }
  372. } else {
  373. // 不跨天
  374. // 开始时间-结束时间跨越午休时间,间隔小时数-1
  375. diff_hours = float32(e_h - s_h)
  376. if s_h <= 12 && e_h >= 13 {
  377. diff_hours = float32(e_h - s_h - 1)
  378. }
  379. }
  380. if diff_day > 1 {
  381. diff_day -= 1
  382. }
  383. diff_day_hours := float32(diff_day) * 8
  384. diff := int((diff_day_hours+diff_hours)*60 + diff_minutes) // 分钟数量
  385. return diff
  386. }