Attendance.go 11 KB

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