123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- package rcrt
- import (
- "errors"
- "hongze/hrms_api/global"
- "hongze/hrms_api/models/rcrt"
- "hongze/hrms_api/models/system"
- "hongze/hrms_api/services/resource"
- "hongze/hrms_api/utils"
- "os"
- "time"
- )
- func SetCvResult(adminInfo *system.SysAdmin, req *rcrt.CvResultReq) (err error, errMsg string) {
- cv := new(rcrt.Cv)
- //判断简历是否存在
- cvInfo, err := cv.GetCvByCvId(req.CvId)
- if err != nil {
- if err == utils.ErrNoRow {
- errMsg = "简历不存在或者已经被删除"
- err = errors.New(errMsg + "Err:" + err.Error())
- return
- }
- errMsg = "查询简历出错"
- err = errors.New(errMsg + "Err:" + err.Error())
- return
- }
- //判断简历状态
- if cvInfo.Status != 0 {
- errMsg = "简历状态已设置, 不允许重复设置"
- err = errors.New(errMsg)
- return
- }
- //更新简历
- cvInfo.Status = req.Status
- cvInfo.ResultAdminId = adminInfo.AdminId
- err = cvInfo.Update([]string{"status", "result_admin_id"})
- if err != nil {
- errMsg = "更新简历出错"
- err = errors.New(errMsg + "Err:" + err.Error())
- return
- }
- if req.Status == 1 {
- //新增对应的流程数据
- process := new(rcrt.Process)
- //判断简历流程是否已存在
- _, err = process.GetProcessByCvId(req.CvId)
- if err != nil {
- if err == utils.ErrNoRow {
- err = nil
- } else {
- errMsg = "查询招聘流程出错"
- err = errors.New(errMsg + "Err:" + err.Error())
- return
- }
- } else {
- //该流程已存在
- errMsg = "该流程已存在"
- err = errors.New(errMsg)
- return
- }
- //新增招聘流程
- process = &rcrt.Process{
- CvId: req.CvId,
- CvName: cvInfo.CvName,
- HrEmailId: cvInfo.HrEmailId,
- CvUrl: cvInfo.CvUrl,
- CvTitle: cvInfo.CvTitle,
- CvFileName: cvInfo.CvFileName,
- }
- err = process.Add()
- if err != nil {
- errMsg = "添加招聘流程出错"
- err = errors.New(errMsg + "Err:" + err.Error())
- return
- }
- } else if req.Status == 2 {
- //如果更新简历为淘汰状态,则新增淘汰简历
- outCv := new(rcrt.OutCv)
- //判断简历流程是否已存在
- _, err = outCv.GetOutCvByCvId(req.CvId)
- if err != nil {
- if err == utils.ErrNoRow {
- err = nil
- } else {
- errMsg = "查询淘汰简历出错"
- err = errors.New(errMsg + "Err:" + err.Error())
- return
- }
- } else {
- //该流程已存在
- errMsg = "该淘汰简历已存在"
- err = errors.New(errMsg)
- return
- }
- //新增招聘流程
- outCv.CvId = cvInfo.CvId
- outCv.CvName = cvInfo.CvName
- outCv.CvTitle = cvInfo.CvTitle
- outCv.CvUrl = cvInfo.CvUrl
- outCv.CvFileName = cvInfo.CvFileName
- outCv.Position = cvInfo.Position
- outCv.AdminId = adminInfo.AdminId
- outCv.OutStage = 1
- outCv.OutTime = time.Now()
- outCv.Enabled = 1
- err = outCv.Add()
- if err != nil {
- errMsg = "添加淘汰简历出错"
- err = errors.New(errMsg + "Err:" + err.Error())
- return
- }
- }
- return
- }
- func GetCVByDate(date time.Time) (list []*rcrt.Cv, err error) {
- cv := new(rcrt.Cv)
- list, err = cv.GetCvListByCondition("receive_time > ?", []interface{}{date.Format(utils.FormatDate)})
- return
- }
- func DownloadCv() (err error, errMsg string) {
- //下载邮箱附件
- emailService, err := resource.NewEmailImapService()
- if err != nil {
- return
- }
- attachmentChan := make(chan *resource.AttachmentListItem, 1000)
- errDone := make(chan error, 1)
- var emailUn string
- var emailPwd string
- if global.CONFIG.Serve.RunMode != "debug" {
- emailUn = "hr@hzinsights.com"
- emailPwd = "Hz123456"
- } else {
- //测试邮箱
- emailUn = "hzhr191@aliyun.com"
- emailPwd = "hongze123"
- }
- go func() {
- errDone <- emailService.DownLoadEmailCv(emailUn, emailPwd, attachmentChan)
- }()
- cv := new(rcrt.Cv)
- cvList := make([]*rcrt.Cv, 0)
- //上传到阿里云
- //查询最近7天已下载的文件,如果文件名已存在,则过滤
- list, tErr := GetCVByDate(time.Now().AddDate(0, 0, -7))
- if tErr != nil {
- errMsg = "下载附件失败, 查询已下载的文件出错" + tErr.Error()
- err = tErr
- return
- }
- cvMap := make(map[string]struct{}, 0)
- for _, v := range list {
- cvMap[v.CvTitle+"_"+v.ReceiveTime.Format(utils.FormatDateTime)] = struct{}{}
- }
- for v := range attachmentChan {
- if _, ok := cvMap[v.Title+"_"+v.Date.Format(utils.FormatDateTime)]; ok {
- // 简历已存在
- continue
- }
- if v.FileName != "" {
- saveToOssPath := "hrms/cv/" + v.Date.Format("200601/0102/15h/")
- saveToOssPath += v.FileName
- tErr = resource.UploadFileToAliyun("", v.FullPath, saveToOssPath)
- if tErr != nil {
- errMsg = "上传附件到阿里云失败:" + tErr.Error()
- err = tErr
- return
- }
- global.LOG.Info("上传附件到阿里云成功:" + v.FileName)
- loadUrl := resource.ResourceHost + saveToOssPath
- item := new(rcrt.Cv)
- item.CvTitle = v.Title
- item.CvName = v.Name
- item.Position = v.Position
- item.CvUrl = loadUrl
- item.CvFileName = v.FileName
- item.ReceiveTime = v.Date
- item.CreateTime = time.Now()
- item.ModifyTime = time.Now()
- if len(v.FromEmails) >= 1 {
- item.FromEmail = v.FromEmails[0]
- }
- cvList = append(cvList, item)
- }
- }
- if err = <-errDone; err != nil {
- errMsg = "下载附件失败: " + err.Error()
- return
- }
- if len(cvList) > 0 {
- //添加到数据库
- err = cv.AddInBatches(cvList)
- if err != nil {
- errMsg = "添加简历失败"
- return
- }
- }
- //删除本地文件夹
- dir, _ := os.Getwd()
- _ = os.RemoveAll(dir + "/download/cv/")
- return
- }
|