Overtime.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. beego "github.com/beego/beego/v2/server/web"
  9. userlibs "gogs.baozhida.cn/zoie/ERP_libs/User"
  10. "gogs.baozhida.cn/zoie/ERP_libs/lib"
  11. "math"
  12. "strconv"
  13. )
  14. type OvertimeController struct {
  15. beego.Controller
  16. User userlibs.User
  17. }
  18. func (c *OvertimeController) Prepare() {
  19. c.User = *Account.User_r
  20. }
  21. // 管理员加班审批列表 只显示待审核
  22. func (c *OvertimeController) Overtime_List() {
  23. var r_jsons lib.R_JSONS
  24. page, _ := c.GetInt("page")
  25. if page < 1 {
  26. page = 1
  27. }
  28. page_z, _ := c.GetInt("page_z")
  29. if page_z < 1 {
  30. page_z = conf.Page_size
  31. }
  32. userList, _ := NatsServer.Read_User_List_All()
  33. Account.Read_User_All_Map(userList)
  34. r_jsons.Data, r_jsons.Num = Attendance.Read_Attendance_List("", c.User.T_uuid, 1, Attendance.AttendanceWaitAudit, page, page_z)
  35. r_jsons.Page = page
  36. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  37. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  38. c.ServeJSON()
  39. return
  40. }
  41. func (c *OvertimeController) Overtime_User_List() {
  42. var r_jsons lib.R_JSONS
  43. page, _ := c.GetInt("page")
  44. if page < 1 {
  45. page = 1
  46. }
  47. page_z, _ := c.GetInt("page_z")
  48. if page_z < 1 {
  49. page_z = conf.Page_size
  50. }
  51. T_uuid := c.GetString("T_uuid")
  52. if len(T_uuid) == 0 {
  53. T_uuid = c.User.T_uuid
  54. }
  55. userList, _ := NatsServer.Read_User_List_All()
  56. Account.Read_User_All_Map(userList)
  57. r_jsons.Data, r_jsons.Num = Attendance.Read_Attendance_List(T_uuid, "", 1, 0, page, page_z)
  58. r_jsons.Page = page
  59. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  60. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  61. c.ServeJSON()
  62. return
  63. }
  64. func (c *OvertimeController) Overtime_Add() {
  65. T_start_time := c.GetString("T_start_time")
  66. T_prove_img := c.GetString("T_prove_img")
  67. T_end_time := c.GetString("T_end_time")
  68. T_duration, _ := c.GetInt("T_duration")
  69. T_text := c.GetString("T_text")
  70. T_approver := c.GetString("T_approver")
  71. startTime, is := lib.TimeStrToTime(T_start_time)
  72. if !is {
  73. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"}
  74. c.ServeJSON()
  75. return
  76. }
  77. endTime, is := lib.TimeStrToTime(T_end_time)
  78. if !is {
  79. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"}
  80. c.ServeJSON()
  81. return
  82. }
  83. if startTime.After(endTime) {
  84. c.Data["json"] = lib.JSONS{Code: 202, Msg: "开始时间不能大于结束时间"}
  85. c.ServeJSON()
  86. return
  87. }
  88. var_ := Attendance.Attendance{
  89. T_uid: c.User.T_uuid,
  90. T_type: Attendance.AttendanceOvertime,
  91. T_start_time: startTime,
  92. T_prove_img: T_prove_img,
  93. T_end_time: endTime,
  94. T_duration: T_duration,
  95. T_text: T_text,
  96. T_approver: T_approver,
  97. T_State: Attendance.AttendanceWaitAudit,
  98. }
  99. _, err := Attendance.Add_Attendance(var_)
  100. if err != nil {
  101. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
  102. c.ServeJSON()
  103. return
  104. }
  105. NatsServer.AddUserLogs(c.User.T_uuid, "我的加班", "添加", var_)
  106. NatsServer.AddNews(T_approver, fmt.Sprintf("【加班审批】您有一条%s的加班审批待处理", c.User.T_name), conf.OvertimeApprovalNewsUrl)
  107. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: var_.Id}
  108. c.ServeJSON()
  109. return
  110. }
  111. func (c *OvertimeController) Overtime_Update() {
  112. T_id, _ := c.GetInt("T_id")
  113. T_type, _ := c.GetInt("T_type")
  114. T_start_time := c.GetString("T_start_time")
  115. T_prove_img := c.GetString("T_prove_img")
  116. T_end_time := c.GetString("T_end_time")
  117. T_duration, _ := c.GetInt("T_duration")
  118. T_text := c.GetString("T_text")
  119. T_approver := c.GetString("T_approver")
  120. leave, err := Attendance.Read_Attendance_ById(T_id)
  121. if err != nil {
  122. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  123. c.ServeJSON()
  124. return
  125. }
  126. if leave.T_State == Attendance.AttendancePass {
  127. c.Data["json"] = lib.JSONS{Code: 202, Msg: "加班审批已通过,禁止修改!"}
  128. c.ServeJSON()
  129. return
  130. }
  131. if T_type > 0 {
  132. leave.T_type = T_type
  133. }
  134. if len(T_start_time) > 0 {
  135. startTime, is := lib.TimeStrToTime(T_start_time)
  136. if !is {
  137. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"}
  138. c.ServeJSON()
  139. return
  140. }
  141. leave.T_start_time = startTime
  142. }
  143. leave.T_prove_img = T_prove_img
  144. if len(T_end_time) > 0 {
  145. endTime, is := lib.TimeStrToTime(T_end_time)
  146. if !is {
  147. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"}
  148. c.ServeJSON()
  149. return
  150. }
  151. leave.T_end_time = endTime
  152. }
  153. if T_duration > 0 {
  154. leave.T_duration = T_duration
  155. }
  156. if len(T_text) > 0 {
  157. leave.T_text = T_text
  158. }
  159. if len(T_approver) > 0 {
  160. leave.T_approver = T_approver
  161. }
  162. leave.T_State = Attendance.AttendanceWaitAudit
  163. _, err = Attendance.Update_Attendance(leave, "T_type", "T_start_time", "T_prove_img", "T_end_time", "T_duration", "T_text", "T_approver", "T_State")
  164. if err != nil {
  165. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
  166. c.ServeJSON()
  167. return
  168. }
  169. NatsServer.AddUserLogs(c.User.T_uuid, "我的加班", "修改", leave)
  170. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_id}
  171. c.ServeJSON()
  172. return
  173. }
  174. // 审批
  175. func (c *OvertimeController) Overtime_Approval() {
  176. T_id, _ := c.GetInt("T_id")
  177. T_State, _ := c.GetInt("T_State")
  178. leave, err := Attendance.Read_Attendance_ById(T_id)
  179. if err != nil {
  180. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  181. c.ServeJSON()
  182. return
  183. }
  184. if leave.T_approver != c.User.T_uuid {
  185. c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权操作!"}
  186. c.ServeJSON()
  187. return
  188. }
  189. if T_State == 1 {
  190. leave.T_State = Attendance.AttendancePass
  191. }
  192. if T_State == 0 {
  193. leave.T_State = Attendance.AttendanceNotPass
  194. }
  195. id, err := Attendance.Update_Attendance(leave, "T_State")
  196. if err != nil {
  197. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
  198. c.ServeJSON()
  199. return
  200. }
  201. NatsServer.AddUserLogs(c.User.T_uuid, "加班审批", "审批", leave)
  202. NatsServer.AddNews(leave.T_uid, "【加班申请】您的加班申请已通过", conf.MyOvertimeNewsUrl)
  203. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: id}
  204. c.ServeJSON()
  205. return
  206. }
  207. // 删除加班
  208. func (c *OvertimeController) Overtime_Del() {
  209. T_id, _ := c.GetInt("T_id")
  210. leave, err := Attendance.Read_Attendance_ById(T_id)
  211. if err != nil {
  212. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  213. c.ServeJSON()
  214. return
  215. }
  216. if leave.T_State == Attendance.AttendancePass {
  217. c.Data["json"] = lib.JSONS{Code: 202, Msg: "加班审批已通过,禁止删除!"}
  218. c.ServeJSON()
  219. return
  220. }
  221. id, err := Attendance.Delete_Attendance(leave)
  222. if err != nil {
  223. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  224. c.ServeJSON()
  225. return
  226. }
  227. NatsServer.AddUserLogs(c.User.T_uuid, "我的加班", "删除", strconv.Itoa(T_id))
  228. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: id}
  229. c.ServeJSON()
  230. return
  231. }
  232. // 加班统计
  233. func (c *OvertimeController) Overtime_Stat() {
  234. page, _ := c.GetInt("page")
  235. if page < 1 {
  236. page = 1
  237. }
  238. page_z, _ := c.GetInt("page_z")
  239. if page_z < 1 {
  240. page_z = conf.Page_size
  241. }
  242. T_uuid := c.GetString("T_uuid")
  243. if len(T_uuid) == 0 {
  244. T_uuid = c.User.T_uuid
  245. }
  246. attendance, cnt := Attendance.Read_Attendance_List_For_Stat(T_uuid, 0, 9999)
  247. userList, _ := NatsServer.Read_User_List_All()
  248. Account.Read_User_All_Map(userList)
  249. var duration int
  250. for i := len(attendance) - 1; i >= 0; i-- {
  251. if attendance[i].T_type == Attendance.AttendanceDaysOff || attendance[i].T_type == Attendance.AttendanceShiftPerf || attendance[i].T_type > 100 {
  252. duration -= attendance[i].T_duration
  253. attendance[i].T_duration = -attendance[i].T_duration
  254. } else {
  255. duration += attendance[i].T_duration
  256. }
  257. attendance[i].RemainingTime = duration
  258. }
  259. type R_JSONS struct {
  260. //必须的大写开头
  261. Data interface{}
  262. RemainingTime int // 剩余时长
  263. Num int64
  264. Page int
  265. Page_size int
  266. }
  267. var r_jsons R_JSONS
  268. offset := (page - 1) * page_z
  269. limit := offset + page_z
  270. if limit > int(cnt) {
  271. limit = int(cnt)
  272. }
  273. r_jsons.Num = cnt
  274. r_jsons.RemainingTime = Attendance.GetRemainingDaysOff(T_uuid)
  275. r_jsons.Data = attendance[offset:limit]
  276. r_jsons.Page = page
  277. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  278. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  279. c.ServeJSON()
  280. return
  281. }