leave.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. package controllers
  2. import (
  3. "ERP_ams/Nats/NatsServer"
  4. "ERP_ams/conf"
  5. "ERP_ams/models/Account"
  6. "ERP_ams/models/Attendance"
  7. "fmt"
  8. userlibs "git.baozhida.cn/ERP_libs/User"
  9. "git.baozhida.cn/ERP_libs/lib"
  10. beego "github.com/beego/beego/v2/server/web"
  11. "math"
  12. "strconv"
  13. "time"
  14. )
  15. type LeaveController struct {
  16. beego.Controller
  17. User userlibs.User
  18. }
  19. func (c *LeaveController) Prepare() {
  20. c.User = *Account.User_r
  21. }
  22. // 管理员请假审批列表 只显示待审核
  23. func (c *LeaveController) Leave_List() {
  24. var r_jsons lib.R_JSONS
  25. page, _ := c.GetInt("page")
  26. if page < 1 {
  27. page = 1
  28. }
  29. page_z, _ := c.GetInt("page_z")
  30. if page_z < 1 {
  31. page_z = conf.Page_size
  32. }
  33. userList, _ := NatsServer.Read_User_List_All()
  34. Account.Read_User_All_Map(userList)
  35. r_jsons.Data, r_jsons.Num = Attendance.Read_Attendance_List("", c.User.T_uuid, 0, Attendance.AttendanceWaitAudit, page, page_z)
  36. r_jsons.Page = page
  37. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  38. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  39. c.ServeJSON()
  40. return
  41. }
  42. func (c *LeaveController) Leave_User_List() {
  43. var r_jsons lib.R_JSONS
  44. page, _ := c.GetInt("page")
  45. if page < 1 {
  46. page = 1
  47. }
  48. page_z, _ := c.GetInt("page_z")
  49. if page_z < 1 {
  50. page_z = conf.Page_size
  51. }
  52. T_uuid := c.GetString("T_uuid")
  53. if len(T_uuid) == 0 {
  54. T_uuid = c.User.T_uuid
  55. }
  56. userList, _ := NatsServer.Read_User_List_All()
  57. Account.Read_User_All_Map(userList)
  58. r_jsons.Data, r_jsons.Num = Attendance.Read_Attendance_List(T_uuid, "", 0, 0, page, page_z)
  59. r_jsons.Page = page
  60. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  61. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  62. c.ServeJSON()
  63. return
  64. }
  65. // 财务
  66. func (c *LeaveController) Leave_Finance_List() {
  67. var r_jsons lib.R_JSONS
  68. T_uuid := c.GetString("T_uuid")
  69. if len(T_uuid) == 0 {
  70. T_uuid = c.User.T_uuid
  71. }
  72. // 年月 2023-01
  73. T_month := c.GetString("T_month")
  74. if len(T_month) > 0 {
  75. _, err := time.Parse("2006-01", T_month)
  76. if err != nil {
  77. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  78. c.ServeJSON()
  79. return
  80. }
  81. }
  82. r_jsons.Data = Attendance.Read_Attendance_List_For_Finance(T_uuid, T_month)
  83. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  84. c.ServeJSON()
  85. return
  86. }
  87. func (c *LeaveController) Leave_Type_List() {
  88. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Attendance.Read_AttendanceType_All()}
  89. c.ServeJSON()
  90. return
  91. }
  92. func (c *LeaveController) Leave_Add() {
  93. T_type, _ := c.GetInt("T_type")
  94. T_start_time := c.GetString("T_start_time")
  95. T_end_time := c.GetString("T_end_time")
  96. T_duration, _ := c.GetInt("T_duration")
  97. T_text := c.GetString("T_text")
  98. T_approver := c.GetString("T_approver")
  99. startTime, is := lib.TimeStrToTime(T_start_time)
  100. if !is {
  101. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"}
  102. c.ServeJSON()
  103. return
  104. }
  105. endTime, is := lib.TimeStrToTime(T_end_time)
  106. if !is {
  107. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"}
  108. c.ServeJSON()
  109. return
  110. }
  111. if T_type == Attendance.AttendanceDaysOff {
  112. duration := Attendance.GetRemainingDaysOff(c.User.T_uuid)
  113. if T_duration > duration {
  114. c.Data["json"] = lib.JSONS{Code: 202, Msg: "超过可调休时长!"}
  115. c.ServeJSON()
  116. return
  117. }
  118. }
  119. var_ := Attendance.Attendance{
  120. T_uid: c.User.T_uuid,
  121. T_type: T_type,
  122. T_start_time: startTime,
  123. T_end_time: endTime,
  124. T_duration: T_duration,
  125. T_text: T_text,
  126. T_approver: T_approver,
  127. T_State: Attendance.AttendanceWaitAudit,
  128. }
  129. Id, err := Attendance.Add_Attendance(var_)
  130. if err != nil {
  131. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
  132. c.ServeJSON()
  133. return
  134. }
  135. NatsServer.AddUserLogs(c.User.T_uuid, "我的请假", "添加", var_)
  136. NatsServer.AddNews(T_approver, fmt.Sprintf("您有一条【%s】的请假审批待处理", c.User.T_name), "/leave")
  137. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
  138. c.ServeJSON()
  139. return
  140. }
  141. // 扣除
  142. func (c *LeaveController) Leave_Deduct() {
  143. T_type, _ := c.GetInt("T_type")
  144. T_duration, _ := c.GetInt("T_duration")
  145. T_text := c.GetString("T_text")
  146. T_uuid := c.GetString("T_uuid")
  147. var_ := Attendance.Attendance{
  148. T_uid: T_uuid,
  149. T_type: T_type,
  150. T_duration: T_duration,
  151. T_text: T_text,
  152. T_approver: c.User.T_uuid,
  153. T_State: Attendance.AttendancePass,
  154. }
  155. Id, err := Attendance.Add_Attendance(var_)
  156. if err != nil {
  157. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
  158. c.ServeJSON()
  159. return
  160. }
  161. NatsServer.AddUserLogs(c.User.T_uuid, "我的请假", "扣除", var_)
  162. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
  163. c.ServeJSON()
  164. return
  165. }
  166. func (c *LeaveController) Leave_Update() {
  167. T_id, _ := c.GetInt("T_id")
  168. T_type, _ := c.GetInt("T_type")
  169. T_start_time := c.GetString("T_start_time")
  170. T_end_time := c.GetString("T_end_time")
  171. T_duration, _ := c.GetInt("T_duration")
  172. T_text := c.GetString("T_text")
  173. T_approver := c.GetString("T_approver")
  174. leave, err := Attendance.Read_Attendance_ById(T_id)
  175. if err != nil {
  176. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  177. c.ServeJSON()
  178. return
  179. }
  180. if leave.T_State == Attendance.AttendancePass {
  181. c.Data["json"] = lib.JSONS{Code: 202, Msg: "请假审批已通过,禁止修改!"}
  182. c.ServeJSON()
  183. return
  184. }
  185. if T_type > 0 {
  186. leave.T_type = T_type
  187. }
  188. if len(T_start_time) > 0 {
  189. startTime, is := lib.TimeStrToTime(T_start_time)
  190. if !is {
  191. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"}
  192. c.ServeJSON()
  193. return
  194. }
  195. leave.T_start_time = startTime
  196. }
  197. if len(T_end_time) > 0 {
  198. endTime, is := lib.TimeStrToTime(T_end_time)
  199. if !is {
  200. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"}
  201. c.ServeJSON()
  202. return
  203. }
  204. leave.T_end_time = endTime
  205. }
  206. if T_duration > 0 {
  207. leave.T_duration = T_duration
  208. }
  209. if len(T_text) > 0 {
  210. leave.T_text = T_text
  211. }
  212. if len(T_approver) > 0 {
  213. leave.T_approver = T_approver
  214. }
  215. leave.T_State = Attendance.AttendanceWaitAudit
  216. _, err = Attendance.Update_Attendance(leave, "T_type", "T_start_time", "T_end_time", "T_duration", "T_text", "T_approver", "T_State")
  217. if err != nil {
  218. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
  219. c.ServeJSON()
  220. return
  221. }
  222. NatsServer.AddUserLogs(c.User.T_uuid, "我的请假", "修改", leave)
  223. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_id}
  224. c.ServeJSON()
  225. return
  226. }
  227. func (c *LeaveController) Leave_Approval() {
  228. T_id, _ := c.GetInt("T_id")
  229. T_State, _ := c.GetInt("T_State")
  230. leave, err := Attendance.Read_Attendance_ById(T_id)
  231. if err != nil {
  232. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  233. c.ServeJSON()
  234. return
  235. }
  236. if leave.T_approver != c.User.T_uuid {
  237. c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权操作!"}
  238. c.ServeJSON()
  239. return
  240. }
  241. if T_State == 1 {
  242. leave.T_State = Attendance.AttendancePass
  243. }
  244. if T_State == 0 {
  245. leave.T_State = Attendance.AttendanceNotPass
  246. }
  247. id, err := Attendance.Update_Attendance(leave, "T_State")
  248. if err != nil {
  249. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
  250. c.ServeJSON()
  251. return
  252. }
  253. NatsServer.AddUserLogs(c.User.T_uuid, "我的请假", "审批", leave)
  254. NatsServer.AddNews(leave.T_uid, "您的请假申请已通过", "/myOvertime")
  255. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: id}
  256. c.ServeJSON()
  257. return
  258. }
  259. func (c *LeaveController) Leave_Del() {
  260. T_id, _ := c.GetInt("T_id")
  261. leave, err := Attendance.Read_Attendance_ById(T_id)
  262. if err != nil {
  263. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  264. c.ServeJSON()
  265. return
  266. }
  267. if leave.T_State == Attendance.AttendancePass {
  268. c.Data["json"] = lib.JSONS{Code: 202, Msg: "请假审批已通过,禁止删除!"}
  269. c.ServeJSON()
  270. return
  271. }
  272. id, err := Attendance.Delete_Attendance(leave)
  273. if err != nil {
  274. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  275. c.ServeJSON()
  276. return
  277. }
  278. NatsServer.AddUserLogs(c.User.T_uuid, "我的请假", "删除", strconv.Itoa(T_id))
  279. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: id}
  280. c.ServeJSON()
  281. return
  282. }
  283. // 获取剩余调休时长
  284. func (c *LeaveController) Leave_RemainingDaysOff() {
  285. duration := Attendance.GetRemainingDaysOff(c.User.T_uuid)
  286. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: duration}
  287. c.ServeJSON()
  288. return
  289. }
  290. // 计算请假时长
  291. func (c *LeaveController) Leave_Duration() {
  292. T_start_time := c.GetString("T_start_time")
  293. T_end_time := c.GetString("T_end_time")
  294. startTime, is := lib.TimeStrToTime(T_start_time)
  295. if !is {
  296. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"}
  297. c.ServeJSON()
  298. return
  299. }
  300. endTime, is := lib.TimeStrToTime(T_end_time)
  301. if !is {
  302. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"}
  303. c.ServeJSON()
  304. return
  305. }
  306. //开始时间的小时和分钟
  307. s_h := startTime.Hour()
  308. s_mm := startTime.Minute()
  309. e_h := endTime.Hour()
  310. e_mm := endTime.Minute()
  311. diff_day := int(endTime.Sub(startTime).Hours() / 24) // 间隔天数
  312. var diff_hours float32 // 间隔小时
  313. var diff_minutes float32 // 间隔分钟
  314. if s_h < 9 {
  315. // 开始小时早于9点,从9点起算
  316. s_h = 9
  317. s_mm = 0
  318. }
  319. if e_h > 17 && e_mm > 30 {
  320. // 结束时间晚于17:30点,到17:30点止
  321. e_h = 17
  322. e_mm = 30
  323. }
  324. if e_mm < s_mm {
  325. // 结束分钟数<开始分钟数,向小时借
  326. e_mm += 60
  327. e_h--
  328. }
  329. diff_minutes = float32(e_mm - s_mm)
  330. if diff_day > 1 {
  331. // 跨天
  332. diff_hours = 17.5 - float32(s_h) + float32(e_h-9)
  333. // 如果开始时间小于12点 请假小时数-1
  334. // 如果结束时间大于13点,请假小时数-1
  335. if s_h <= 12 {
  336. diff_hours = diff_hours - 1
  337. }
  338. if e_h >= 13 {
  339. diff_hours = diff_hours - 1
  340. }
  341. } else {
  342. // 不跨天
  343. // 开始时间-结束时间跨越午休时间,间隔小时数-1
  344. diff_hours = float32(e_h - s_h)
  345. if s_h <= 12 && e_h >= 13 {
  346. diff_hours = float32(e_h - s_h - 1)
  347. }
  348. }
  349. if diff_day > 1 {
  350. diff_day -= 1
  351. }
  352. diff_day_hours := float32(diff_day) * 7.5
  353. diff_m := int((diff_day_hours+diff_hours)*60 + diff_minutes) // 分钟数量
  354. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: diff_m}
  355. c.ServeJSON()
  356. return
  357. }