Attendance.go 11 KB

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