Overtime.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. )
  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. var_ := Attendance.Attendance{
  84. T_uid: c.User.T_uuid,
  85. T_type: Attendance.AttendanceOvertime,
  86. T_start_time: startTime,
  87. T_prove_img: T_prove_img,
  88. T_end_time: endTime,
  89. T_duration: T_duration,
  90. T_text: T_text,
  91. T_approver: T_approver,
  92. T_State: Attendance.AttendanceWaitAudit,
  93. }
  94. _, err := Attendance.Add_Attendance(var_)
  95. if err != nil {
  96. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
  97. c.ServeJSON()
  98. return
  99. }
  100. NatsServer.AddUserLogs(c.User.T_uuid, "我的加班", "添加", var_)
  101. NatsServer.AddNews(T_approver, fmt.Sprintf("【加班审批】您有一条%s的加班审批待处理", c.User.T_name), conf.OvertimeApprovalNewsUrl)
  102. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: var_.Id}
  103. c.ServeJSON()
  104. return
  105. }
  106. func (c *OvertimeController) Overtime_Update() {
  107. T_id, _ := c.GetInt("T_id")
  108. T_type, _ := c.GetInt("T_type")
  109. T_start_time := c.GetString("T_start_time")
  110. T_prove_img := c.GetString("T_prove_img")
  111. T_end_time := c.GetString("T_end_time")
  112. T_duration, _ := c.GetInt("T_duration")
  113. T_text := c.GetString("T_text")
  114. T_approver := c.GetString("T_approver")
  115. leave, err := Attendance.Read_Attendance_ById(T_id)
  116. if err != nil {
  117. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  118. c.ServeJSON()
  119. return
  120. }
  121. if leave.T_State == Attendance.AttendancePass {
  122. c.Data["json"] = lib.JSONS{Code: 202, Msg: "加班审批已通过,禁止修改!"}
  123. c.ServeJSON()
  124. return
  125. }
  126. if T_type > 0 {
  127. leave.T_type = T_type
  128. }
  129. if len(T_start_time) > 0 {
  130. startTime, is := lib.TimeStrToTime(T_start_time)
  131. if !is {
  132. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"}
  133. c.ServeJSON()
  134. return
  135. }
  136. leave.T_start_time = startTime
  137. }
  138. leave.T_prove_img = T_prove_img
  139. if len(T_end_time) > 0 {
  140. endTime, is := lib.TimeStrToTime(T_end_time)
  141. if !is {
  142. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"}
  143. c.ServeJSON()
  144. return
  145. }
  146. leave.T_end_time = endTime
  147. }
  148. if T_duration > 0 {
  149. leave.T_duration = T_duration
  150. }
  151. if len(T_text) > 0 {
  152. leave.T_text = T_text
  153. }
  154. if len(T_approver) > 0 {
  155. leave.T_approver = T_approver
  156. }
  157. leave.T_State = Attendance.AttendanceWaitAudit
  158. _, err = Attendance.Update_Attendance(leave, "T_type", "T_start_time", "T_prove_img", "T_end_time", "T_duration", "T_text", "T_approver", "T_State")
  159. if err != nil {
  160. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
  161. c.ServeJSON()
  162. return
  163. }
  164. NatsServer.AddUserLogs(c.User.T_uuid, "我的加班", "修改", leave)
  165. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_id}
  166. c.ServeJSON()
  167. return
  168. }
  169. // 审批
  170. func (c *OvertimeController) Overtime_Approval() {
  171. T_id, _ := c.GetInt("T_id")
  172. T_State, _ := c.GetInt("T_State")
  173. leave, err := Attendance.Read_Attendance_ById(T_id)
  174. if err != nil {
  175. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  176. c.ServeJSON()
  177. return
  178. }
  179. if leave.T_approver != c.User.T_uuid {
  180. c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权操作!"}
  181. c.ServeJSON()
  182. return
  183. }
  184. if T_State == 1 {
  185. leave.T_State = Attendance.AttendancePass
  186. }
  187. if T_State == 0 {
  188. leave.T_State = Attendance.AttendanceNotPass
  189. }
  190. id, err := Attendance.Update_Attendance(leave, "T_State")
  191. if err != nil {
  192. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
  193. c.ServeJSON()
  194. return
  195. }
  196. NatsServer.AddUserLogs(c.User.T_uuid, "加班审批", "审批", leave)
  197. NatsServer.AddNews(leave.T_uid, "【加班申请】您的加班申请已通过", conf.MyOvertimeNewsUrl)
  198. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: id}
  199. c.ServeJSON()
  200. return
  201. }
  202. // 删除加班
  203. func (c *OvertimeController) Overtime_Del() {
  204. T_id, _ := c.GetInt("T_id")
  205. leave, err := Attendance.Read_Attendance_ById(T_id)
  206. if err != nil {
  207. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  208. c.ServeJSON()
  209. return
  210. }
  211. if leave.T_State == Attendance.AttendancePass {
  212. c.Data["json"] = lib.JSONS{Code: 202, Msg: "加班审批已通过,禁止删除!"}
  213. c.ServeJSON()
  214. return
  215. }
  216. id, err := Attendance.Delete_Attendance(leave)
  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, "我的加班", "删除", strconv.Itoa(T_id))
  223. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: id}
  224. c.ServeJSON()
  225. return
  226. }
  227. // 加班统计
  228. func (c *OvertimeController) Overtime_Stat() {
  229. page, _ := c.GetInt("page")
  230. if page < 1 {
  231. page = 1
  232. }
  233. page_z, _ := c.GetInt("page_z")
  234. if page_z < 1 {
  235. page_z = conf.Page_size
  236. }
  237. T_uuid := c.GetString("T_uuid")
  238. if len(T_uuid) == 0 {
  239. T_uuid = c.User.T_uuid
  240. }
  241. attendance, cnt := Attendance.Read_Attendance_List_For_Stat(T_uuid, page, page_z)
  242. userList, _ := NatsServer.Read_User_List_All()
  243. Account.Read_User_All_Map(userList)
  244. var duration int
  245. for i := 0; i < len(attendance); i++ {
  246. if attendance[i].T_type == Attendance.AttendanceDaysOff || attendance[i].T_type == Attendance.AttendanceShiftPerf || attendance[i].T_type > 100 {
  247. duration -= attendance[i].T_duration
  248. attendance[i].T_duration = -attendance[i].T_duration
  249. } else {
  250. duration += attendance[i].T_duration
  251. }
  252. attendance[i].RemainingTime = duration
  253. }
  254. type R_JSONS struct {
  255. //必须的大写开头
  256. Data interface{}
  257. RemainingTime int // 剩余时长
  258. Num int64
  259. Page int
  260. Page_size int
  261. }
  262. var r_jsons R_JSONS
  263. r_jsons.Num = cnt
  264. r_jsons.RemainingTime = Attendance.GetRemainingDaysOff(T_uuid)
  265. r_jsons.Data = attendance
  266. r_jsons.Page = page
  267. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  268. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  269. c.ServeJSON()
  270. return
  271. }