audit.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package service
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/admin/ep/merlin/model"
  6. "go-common/library/ecode"
  7. )
  8. // DelayMachineEndTime Delay Machine End Time.
  9. func (s *Service) DelayMachineEndTime(c context.Context, machineID int64, username string) (status int, err error) {
  10. var (
  11. machine *model.Machine
  12. machineLog = &model.MachineLog{}
  13. )
  14. status = -1
  15. //查询机器延期状态
  16. if machine, err = s.dao.QueryMachine(machineID); err != nil {
  17. return
  18. }
  19. //机器必须存在,机器状态必须是有效的,延期状态必须为可自动延期,结束时间必须是前后7天之内
  20. if machine == nil || machine.DelayStatus != model.DelayStatusAuto || machine.Status < 0 || machine.EndTime.Before(time.Now().AddDate(0, 0, -8)) || machine.EndTime.After(time.Now().AddDate(0, 0, 8)) {
  21. err = ecode.MerlinDelayMachineErr
  22. return
  23. }
  24. //执行延期操作
  25. if err = s.dao.UpdateMachineEndTime(machineID, model.DelayStatusApply, machine.EndTime.AddDate(0, 0, 30)); err != nil {
  26. machineLog.OperateResult = model.OperationFailedForMachineLog
  27. } else {
  28. machineLog.OperateResult = model.OperationSuccessForMachineLog
  29. status = 0
  30. }
  31. //记录操作日志
  32. machineLog.OperateType = model.DelayMachineEndTime
  33. machineLog.Username = username
  34. machineLog.MachineID = machineID
  35. err = s.dao.InsertMachineLog(machineLog)
  36. return
  37. }
  38. // ApplyDelayMachineEndTime Apply Delay Machine End Time.
  39. func (s *Service) ApplyDelayMachineEndTime(c context.Context, username string, applyEndTime *model.ApplyEndTimeRequest) (status int, err error) {
  40. var (
  41. machine *model.Machine
  42. machineLog = &model.MachineLog{}
  43. )
  44. status = -1
  45. //查询机器延期状态
  46. if machine, err = s.dao.QueryMachine(applyEndTime.MachineID); err != nil {
  47. return
  48. }
  49. //申请延期时,机器必须手动申请延期过,且机器状态为正常
  50. if machine == nil || machine.DelayStatus != model.DelayStatusApply || machine.Status < 0 {
  51. err = ecode.MerlinApplyMachineErr
  52. return
  53. }
  54. machineLog.OperateResult = model.OperationFailedForMachineLog
  55. var applyET time.Time
  56. if applyET, err = time.ParseInLocation(model.TimeFormat, applyEndTime.ApplyEndTime, time.Local); err != nil {
  57. return
  58. }
  59. //延长日期超过过期时间三个月或小于当前过期时间,驳回
  60. if machine.EndTime.AddDate(0, 3, 1).Before(applyET) || machine.EndTime.After(applyET) {
  61. err = ecode.MerlinApplyMachineByApplyEndTimeMore3MErr
  62. return
  63. }
  64. ar := &model.ApplicationRecord{
  65. Applicant: username,
  66. MachineID: machine.ID,
  67. ApplyEndTime: applyET,
  68. Status: model.ApplyDelayInit,
  69. Auditor: applyEndTime.Auditor,
  70. }
  71. //添加延期申请记录
  72. if err = s.dao.InsertApplicationRecordAndUpdateMachineDelayStatus(ar, machine.ID, model.DelayStatusDisable); err == nil {
  73. machineLog.OperateResult = model.OperationSuccessForMachineLog
  74. status = 0
  75. //发送邮件通知
  76. s.SendMailApplyDelayMachineEndTime(c, applyEndTime.Auditor, username, machine.ID, machine.EndTime, applyET)
  77. }
  78. //记录操作日志
  79. machineLog.OperateType = model.DelayMachineEndTime
  80. machineLog.Username = username
  81. machineLog.MachineID = machine.ID
  82. if err = s.dao.InsertMachineLog(machineLog); err != nil {
  83. return
  84. }
  85. return
  86. }
  87. // CancelMachineEndTime Cancel Machine End Time.
  88. func (s *Service) CancelMachineEndTime(c context.Context, auditID int64, username string) (status int, err error) {
  89. var (
  90. applicationRecord *model.ApplicationRecord
  91. machineLog = &model.MachineLog{}
  92. machine *model.Machine
  93. )
  94. status = -1
  95. if applicationRecord, err = s.dao.FindApplicationRecordsByID(auditID); err != nil {
  96. return
  97. }
  98. //查询机器延期状态
  99. if machine, err = s.dao.QueryMachine(applicationRecord.MachineID); err != nil {
  100. return
  101. }
  102. //取消延期时,审批状态应为 ApplyDelayInit,且用户名是当时申请延期名,机器状态为正常
  103. if applicationRecord == nil || applicationRecord.Status != model.ApplyDelayInit || applicationRecord.Applicant != username || machine.Status < 0 {
  104. err = ecode.MerlinCancelMachineErr
  105. return
  106. }
  107. machineLog.OperateResult = model.OperationFailedForMachineLog
  108. if err = s.dao.UpdateAuditStatusAndUpdateMachineDelayStatus(applicationRecord.MachineID, auditID, model.DelayStatusApply, model.ApplyDelayCancel); err == nil {
  109. machineLog.OperateResult = model.OperationSuccessForMachineLog
  110. status = 0
  111. }
  112. //记录操作日志
  113. machineLog.OperateType = model.CancelDelayMachineEndTime
  114. machineLog.Username = username
  115. machineLog.MachineID = applicationRecord.MachineID
  116. if err = s.dao.InsertMachineLog(machineLog); err != nil {
  117. return
  118. }
  119. return
  120. }
  121. // AuditMachineEndTime Audit Machine End Time.
  122. func (s *Service) AuditMachineEndTime(c context.Context, auditID int64, username string, auditResult bool, comment string) (status int, err error) {
  123. var (
  124. applicationRecord *model.ApplicationRecord
  125. machineLog = &model.MachineLog{}
  126. machine *model.Machine
  127. )
  128. status = -1
  129. if applicationRecord, err = s.dao.FindApplicationRecordsByID(auditID); err != nil {
  130. return
  131. }
  132. //查询机器延期状态
  133. if machine, err = s.dao.QueryMachine(applicationRecord.MachineID); err != nil {
  134. return
  135. }
  136. //审核时,延期状态必须为ApplyDelayInit,同时用户名为审核者名一致,机器状态为正常
  137. if applicationRecord == nil || applicationRecord.Status != model.ApplyDelayInit || applicationRecord.Auditor != username || machine.Status < 0 {
  138. err = ecode.MerlinAuditMachineErr
  139. return
  140. }
  141. machineLog.OperateResult = model.OperationFailedForMachineLog
  142. if auditResult {
  143. //批准 //修改机器状态和延期时间,审批状态
  144. err = s.dao.UpdateAuditStatusAndUpdateMachineEndTime(applicationRecord.MachineID, auditID, model.DelayStatusApply, model.ApplyDelayApprove, applicationRecord.ApplyEndTime, comment)
  145. } else {
  146. //驳回 //修改机器状态,审批状态
  147. err = s.dao.UpdateAuditStatusAndUpdateMachineDelayStatusComment(applicationRecord.MachineID, auditID, model.DelayStatusApply, model.ApplyDelayDecline, comment)
  148. }
  149. //发送邮件通知
  150. if err == nil {
  151. machineLog.OperateResult = model.OperationSuccessForMachineLog
  152. status = 0
  153. s.SendMailAuditResult(c, applicationRecord.Auditor, applicationRecord.Applicant, applicationRecord.MachineID, auditResult)
  154. }
  155. //记录操作日志
  156. machineLog.OperateType = model.AuditDelayMachineEndTime
  157. machineLog.Username = username
  158. machineLog.MachineID = applicationRecord.MachineID
  159. if err = s.dao.InsertMachineLog(machineLog); err != nil {
  160. return
  161. }
  162. return
  163. }
  164. // GetApplicationRecordsByApplicant Get Application Records By Applicant.
  165. func (s *Service) GetApplicationRecordsByApplicant(c context.Context, username string, pn, ps int) (p *model.PaginateApplicationRecord, err error) {
  166. var (
  167. total int64
  168. applicationRecords []*model.ApplicationRecord
  169. )
  170. if total, applicationRecords, err = s.dao.FindApplicationRecordsByApplicant(username, pn, ps); err != nil {
  171. return
  172. }
  173. p = &model.PaginateApplicationRecord{
  174. PageNum: pn,
  175. PageSize: ps,
  176. Total: total,
  177. ApplicationRecords: applicationRecords,
  178. }
  179. return
  180. }
  181. // GetApplicationRecordsByAuditor Get Application Records By Auditor.
  182. func (s *Service) GetApplicationRecordsByAuditor(c context.Context, username string, pn, ps int) (p *model.PaginateApplicationRecord, err error) {
  183. var (
  184. total int64
  185. applicationRecords []*model.ApplicationRecord
  186. )
  187. if total, applicationRecords, err = s.dao.FindApplicationRecordsByAuditor(username, pn, ps); err != nil {
  188. return
  189. }
  190. p = &model.PaginateApplicationRecord{
  191. PageNum: pn,
  192. PageSize: ps,
  193. Total: total,
  194. ApplicationRecords: applicationRecords,
  195. }
  196. return
  197. }
  198. // GetApplicationRecordsByMachineID Get Application Records By MachineID.
  199. func (s *Service) GetApplicationRecordsByMachineID(c context.Context, machineID int64, pn, ps int) (p *model.PaginateApplicationRecord, err error) {
  200. var (
  201. total int64
  202. applicationRecords []*model.ApplicationRecord
  203. )
  204. if total, applicationRecords, err = s.dao.FindApplicationRecordsByMachineID(machineID, pn, ps); err != nil {
  205. return
  206. }
  207. p = &model.PaginateApplicationRecord{
  208. PageNum: pn,
  209. PageSize: ps,
  210. Total: total,
  211. ApplicationRecords: applicationRecords,
  212. }
  213. return
  214. }