cv.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package rcrt
  2. import (
  3. "errors"
  4. "hongze/hrms_api/global"
  5. "hongze/hrms_api/models/rcrt"
  6. "hongze/hrms_api/models/system"
  7. "hongze/hrms_api/services/resource"
  8. "hongze/hrms_api/utils"
  9. "os"
  10. "time"
  11. )
  12. func SetCvResult(adminInfo *system.SysAdmin, req *rcrt.CvResultReq) (err error, errMsg string) {
  13. cv := new(rcrt.Cv)
  14. //判断简历是否存在
  15. cvInfo, err := cv.GetCvByCvId(req.CvId)
  16. if err != nil {
  17. if err == utils.ErrNoRow {
  18. errMsg = "简历不存在或者已经被删除"
  19. err = errors.New(errMsg + "Err:" + err.Error())
  20. return
  21. }
  22. errMsg = "查询简历出错"
  23. err = errors.New(errMsg + "Err:" + err.Error())
  24. return
  25. }
  26. //判断简历状态
  27. if cvInfo.Status != 0 {
  28. errMsg = "简历状态已设置, 不允许重复设置"
  29. err = errors.New(errMsg)
  30. return
  31. }
  32. //更新简历
  33. cvInfo.Status = req.Status
  34. cvInfo.ResultAdminId = adminInfo.AdminId
  35. err = cvInfo.Update([]string{"status", "result_admin_id"})
  36. if err != nil {
  37. errMsg = "更新简历出错"
  38. err = errors.New(errMsg + "Err:" + err.Error())
  39. return
  40. }
  41. if req.Status == 1 {
  42. //新增对应的流程数据
  43. process := new(rcrt.Process)
  44. //判断简历流程是否已存在
  45. _, err = process.GetProcessByCvId(req.CvId)
  46. if err != nil {
  47. if err == utils.ErrNoRow {
  48. err = nil
  49. } else {
  50. errMsg = "查询招聘流程出错"
  51. err = errors.New(errMsg + "Err:" + err.Error())
  52. return
  53. }
  54. } else {
  55. //该流程已存在
  56. errMsg = "该流程已存在"
  57. err = errors.New(errMsg)
  58. return
  59. }
  60. //新增招聘流程
  61. process = &rcrt.Process{
  62. CvId: req.CvId,
  63. CvName: cvInfo.CvName,
  64. HrEmailId: cvInfo.HrEmailId,
  65. CvUrl: cvInfo.CvUrl,
  66. CvTitle: cvInfo.CvTitle,
  67. CvFileName: cvInfo.CvFileName,
  68. }
  69. err = process.Add()
  70. if err != nil {
  71. errMsg = "添加招聘流程出错"
  72. err = errors.New(errMsg + "Err:" + err.Error())
  73. return
  74. }
  75. } else if req.Status == 2 {
  76. //如果更新简历为淘汰状态,则新增淘汰简历
  77. outCv := new(rcrt.OutCv)
  78. //判断简历流程是否已存在
  79. _, err = outCv.GetOutCvByCvId(req.CvId)
  80. if err != nil {
  81. if err == utils.ErrNoRow {
  82. err = nil
  83. } else {
  84. errMsg = "查询淘汰简历出错"
  85. err = errors.New(errMsg + "Err:" + err.Error())
  86. return
  87. }
  88. } else {
  89. //该流程已存在
  90. errMsg = "该淘汰简历已存在"
  91. err = errors.New(errMsg)
  92. return
  93. }
  94. //新增招聘流程
  95. outCv.CvId = cvInfo.CvId
  96. outCv.CvName = cvInfo.CvName
  97. outCv.CvTitle = cvInfo.CvTitle
  98. outCv.CvUrl = cvInfo.CvUrl
  99. outCv.CvFileName = cvInfo.CvFileName
  100. outCv.Position = cvInfo.Position
  101. outCv.AdminId = adminInfo.AdminId
  102. outCv.OutStage = 1
  103. outCv.OutTime = time.Now()
  104. outCv.Enabled = 1
  105. err = outCv.Add()
  106. if err != nil {
  107. errMsg = "添加淘汰简历出错"
  108. err = errors.New(errMsg + "Err:" + err.Error())
  109. return
  110. }
  111. }
  112. return
  113. }
  114. func GetCVByDate(date time.Time) (list []*rcrt.Cv, err error) {
  115. cv := new(rcrt.Cv)
  116. list, err = cv.GetCvListByCondition("receive_time > ?", []interface{}{date.Format(utils.FormatDate)})
  117. return
  118. }
  119. func DownloadCv() (err error, errMsg string) {
  120. //下载邮箱附件
  121. emailService, err := resource.NewEmailImapService()
  122. if err != nil {
  123. return
  124. }
  125. attachmentChan := make(chan *resource.AttachmentListItem, 1000)
  126. errDone := make(chan error, 1)
  127. var emailUn string
  128. var emailPwd string
  129. if global.CONFIG.Serve.RunMode != "debug" {
  130. emailUn = "hr@hzinsights.com"
  131. emailPwd = "Hz123456"
  132. } else {
  133. //测试邮箱
  134. emailUn = "hzhr191@aliyun.com"
  135. emailPwd = "hongze123"
  136. }
  137. go func() {
  138. errDone <- emailService.DownLoadEmailCv(emailUn, emailPwd, attachmentChan)
  139. }()
  140. cv := new(rcrt.Cv)
  141. cvList := make([]*rcrt.Cv, 0)
  142. //上传到阿里云
  143. //查询最近7天已下载的文件,如果文件名已存在,则过滤
  144. list, tErr := GetCVByDate(time.Now().AddDate(0, 0, -7))
  145. if tErr != nil {
  146. errMsg = "下载附件失败, 查询已下载的文件出错" + tErr.Error()
  147. err = tErr
  148. return
  149. }
  150. cvMap := make(map[string]struct{}, 0)
  151. for _, v := range list {
  152. cvMap[v.CvTitle+"_"+v.ReceiveTime.Format(utils.FormatDateTime)] = struct{}{}
  153. }
  154. for v := range attachmentChan {
  155. if _, ok := cvMap[v.Title+"_"+v.Date.Format(utils.FormatDateTime)]; ok {
  156. // 简历已存在
  157. continue
  158. }
  159. if v.FileName != "" {
  160. saveToOssPath := "hrms/cv/" + v.Date.Format("200601/0102/15h/")
  161. saveToOssPath += v.FileName
  162. tErr = resource.UploadFileToAliyun("", v.FullPath, saveToOssPath)
  163. if tErr != nil {
  164. errMsg = "上传附件到阿里云失败:" + tErr.Error()
  165. err = tErr
  166. return
  167. }
  168. global.LOG.Info("上传附件到阿里云成功:" + v.FileName)
  169. loadUrl := resource.ResourceHost + saveToOssPath
  170. item := new(rcrt.Cv)
  171. item.CvTitle = v.Title
  172. item.CvName = v.Name
  173. item.Position = v.Position
  174. item.CvUrl = loadUrl
  175. item.CvFileName = v.FileName
  176. item.ReceiveTime = v.Date
  177. item.CreateTime = time.Now()
  178. item.ModifyTime = time.Now()
  179. if len(v.FromEmails) >= 1 {
  180. item.FromEmail = v.FromEmails[0]
  181. }
  182. cvList = append(cvList, item)
  183. }
  184. }
  185. if err = <-errDone; err != nil {
  186. errMsg = "下载附件失败: " + err.Error()
  187. return
  188. }
  189. if len(cvList) > 0 {
  190. //添加到数据库
  191. err = cv.AddInBatches(cvList)
  192. if err != nil {
  193. errMsg = "添加简历失败"
  194. return
  195. }
  196. }
  197. //删除本地文件夹
  198. dir, _ := os.Getwd()
  199. _ = os.RemoveAll(dir + "/download/cv/")
  200. return
  201. }