leave.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. userlibs "git.baozhida.cn/ERP_libs/User"
  8. "git.baozhida.cn/ERP_libs/lib"
  9. beego "github.com/beego/beego/v2/server/web"
  10. "math"
  11. "strconv"
  12. "time"
  13. )
  14. type LeaveController struct {
  15. beego.Controller
  16. User userlibs.User
  17. }
  18. func (c *LeaveController) Prepare() {
  19. c.User = *Account.User_r
  20. }
  21. // 管理员请假审批列表 只显示待审核
  22. func (c *LeaveController) Leave_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. // 年月 2023-01
  33. T_month := c.GetString("T_month")
  34. if len(T_month) > 0 {
  35. _, err := time.Parse("2006-01", T_month)
  36. if err != nil {
  37. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  38. c.ServeJSON()
  39. return
  40. }
  41. }
  42. userList, _ := NatsServer.Read_User_List_All()
  43. Account.Read_User_All_Map(userList)
  44. r_jsons.Data, r_jsons.Num = Attendance.Read_Attendance_List("", c.User.T_uuid, T_month, 0, Attendance.AttendanceWaitAudit, page, page_z)
  45. r_jsons.Page = page
  46. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  47. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  48. c.ServeJSON()
  49. return
  50. }
  51. func (c *LeaveController) Leave_User_List() {
  52. var r_jsons lib.R_JSONS
  53. page, _ := c.GetInt("page")
  54. if page < 1 {
  55. page = 1
  56. }
  57. page_z, _ := c.GetInt("page_z")
  58. if page_z < 1 {
  59. page_z = conf.Page_size
  60. }
  61. T_uuid := c.GetString("T_uuid")
  62. if len(T_uuid) == 0 {
  63. T_uuid = c.User.T_uuid
  64. }
  65. // 年月 2023-01
  66. T_month := c.GetString("T_month")
  67. if len(T_month) > 0 {
  68. _, err := time.Parse("2006-01", T_month)
  69. if err != nil {
  70. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  71. c.ServeJSON()
  72. return
  73. }
  74. }
  75. r_jsons.Data, r_jsons.Num = Attendance.Read_Attendance_List(T_uuid, "", T_month, 0, 0, page, page_z)
  76. r_jsons.Page = page
  77. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  78. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  79. c.ServeJSON()
  80. return
  81. }
  82. func (c *LeaveController) Leave_Type_List() {
  83. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Attendance.Read_AttendanceType_All()}
  84. c.ServeJSON()
  85. return
  86. }
  87. func (c *LeaveController) Leave_Add() {
  88. T_type, _ := c.GetInt("T_type")
  89. T_start_time := c.GetString("T_start_time")
  90. T_end_time := c.GetString("T_end_time")
  91. T_duration, _ := c.GetInt("T_duration")
  92. T_text := c.GetString("T_text")
  93. T_approver := c.GetString("T_approver")
  94. startTime, is := lib.TimeStrToTime(T_start_time)
  95. if !is {
  96. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"}
  97. c.ServeJSON()
  98. return
  99. }
  100. endTime, is := lib.TimeStrToTime(T_end_time)
  101. if !is {
  102. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"}
  103. c.ServeJSON()
  104. return
  105. }
  106. var_ := Attendance.Attendance{
  107. T_uid: c.User.T_uuid,
  108. T_type: T_type,
  109. T_start_time: startTime,
  110. T_end_time: endTime,
  111. T_duration: T_duration,
  112. T_text: T_text,
  113. T_approver: T_approver,
  114. T_State: Attendance.AttendanceWaitAudit,
  115. }
  116. Id, err := Attendance.Add_Attendance(var_)
  117. if err != nil {
  118. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
  119. c.ServeJSON()
  120. return
  121. }
  122. NatsServer.AddUserLogs(c.User.T_uuid, "我的请假", "添加", var_)
  123. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
  124. c.ServeJSON()
  125. return
  126. }
  127. // 扣除
  128. func (c *LeaveController) Leave_Deduct() {
  129. T_type, _ := c.GetInt("T_type")
  130. T_duration, _ := c.GetInt("T_duration")
  131. T_text := c.GetString("T_text")
  132. T_uuid := c.GetString("T_uuid")
  133. var_ := Attendance.Attendance{
  134. T_uid: T_uuid,
  135. T_type: T_type,
  136. T_duration: T_duration,
  137. T_text: T_text,
  138. T_approver: c.User.T_uuid,
  139. T_State: Attendance.AttendancePass,
  140. }
  141. Id, err := Attendance.Add_Attendance(var_)
  142. if err != nil {
  143. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
  144. c.ServeJSON()
  145. return
  146. }
  147. NatsServer.AddUserLogs(c.User.T_uuid, "我的请假", "扣除", var_)
  148. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
  149. c.ServeJSON()
  150. return
  151. }
  152. func (c *LeaveController) Leave_Update() {
  153. T_id, _ := c.GetInt("T_id")
  154. T_type, _ := c.GetInt("T_type")
  155. T_start_time := c.GetString("T_start_time")
  156. T_end_time := c.GetString("T_end_time")
  157. T_duration, _ := c.GetInt("T_duration")
  158. T_text := c.GetString("T_text")
  159. T_approver := c.GetString("T_approver")
  160. leave, err := Attendance.Read_Attendance_ById(T_id)
  161. if err != nil {
  162. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  163. c.ServeJSON()
  164. return
  165. }
  166. if leave.T_State == Attendance.AttendancePass {
  167. c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权修改!"}
  168. c.ServeJSON()
  169. return
  170. }
  171. if T_type > 0 {
  172. leave.T_type = T_type
  173. }
  174. if len(T_start_time) > 0 {
  175. startTime, is := lib.TimeStrToTime(T_start_time)
  176. if !is {
  177. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"}
  178. c.ServeJSON()
  179. return
  180. }
  181. leave.T_start_time = startTime
  182. }
  183. if len(T_end_time) > 0 {
  184. endTime, is := lib.TimeStrToTime(T_end_time)
  185. if !is {
  186. c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式不正确"}
  187. c.ServeJSON()
  188. return
  189. }
  190. leave.T_end_time = endTime
  191. }
  192. if T_duration > 0 {
  193. leave.T_duration = T_duration
  194. }
  195. if len(T_text) > 0 {
  196. leave.T_text = T_text
  197. }
  198. if len(T_approver) > 0 {
  199. leave.T_approver = T_approver
  200. }
  201. leave.T_State = Attendance.AttendanceWaitAudit
  202. _, err = Attendance.Update_Attendance(leave, "T_type", "T_start_time", "T_end_time", "T_duration", "T_text", "T_approver", "T_State")
  203. if err != nil {
  204. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
  205. c.ServeJSON()
  206. return
  207. }
  208. NatsServer.AddUserLogs(c.User.T_uuid, "我的请假", "修改", leave)
  209. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_id}
  210. c.ServeJSON()
  211. return
  212. }
  213. func (c *LeaveController) Leave_Approval() {
  214. T_id, _ := c.GetInt("T_id")
  215. T_State, _ := c.GetInt("T_State")
  216. leave, err := Attendance.Read_Attendance_ById(T_id)
  217. if err != nil {
  218. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  219. c.ServeJSON()
  220. return
  221. }
  222. if leave.T_approver != c.User.T_uuid {
  223. c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权操作!"}
  224. c.ServeJSON()
  225. return
  226. }
  227. if T_State == 1 {
  228. leave.T_State = Attendance.AttendancePass
  229. }
  230. if T_State == 0 {
  231. leave.T_State = Attendance.AttendanceNotPass
  232. }
  233. id, err := Attendance.Update_Attendance(leave, "T_State")
  234. if err != nil {
  235. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
  236. c.ServeJSON()
  237. return
  238. }
  239. NatsServer.AddUserLogs(c.User.T_uuid, "我的请假", "审批", leave)
  240. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: id}
  241. c.ServeJSON()
  242. return
  243. }
  244. func (c *LeaveController) Leave_Del() {
  245. T_id, _ := c.GetInt("T_id")
  246. leave, err := Attendance.Read_Attendance_ById(T_id)
  247. if err != nil {
  248. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  249. c.ServeJSON()
  250. return
  251. }
  252. if leave.T_State == Attendance.AttendancePass {
  253. c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权删除!"}
  254. c.ServeJSON()
  255. return
  256. }
  257. id, err := Attendance.Delete_Attendance(leave)
  258. if err != nil {
  259. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  260. c.ServeJSON()
  261. return
  262. }
  263. NatsServer.AddUserLogs(c.User.T_uuid, "我的请假", "删除", strconv.Itoa(T_id))
  264. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: id}
  265. c.ServeJSON()
  266. return
  267. }