sendMail.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. package main
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/mail"
  5. "fmt"
  6. "log"
  7. )
  8. var (
  9. WarningSize = 350
  10. LimitSize = 400
  11. selfMail = `SELECT b.mail FROM jianyu.entniche_user b WHERE b.id IN (SELECT a.ent_user_id FROM jianyu_subjectdb_test.dwd_d_crm_department_level_succbi a WHERE a.position_id = ?)`
  12. infoSelf = `SELECT a.name, a.SZ_PID1, a.SZ_PID2, a.dept_name, b.role_id FROM jianyu_subjectdb_test.dwd_d_crm_department_level_succbi a INNER JOIN jianyu_subjectdb_test.dwd_f_crm_personnel_management b ON b.position_id = a.position_id WHERE a.position_id = ? AND a.resign = 0`
  13. topMail = `SELECT d.mail FROM jianyu.entniche_user d WHERE d.id IN (SELECT b.ent_user_id FROM jianyu_subjectdb.dwd_d_crm_department_level_succbi b INNER JOIN jianyu_subjectdb.dwd_f_crm_personnel_management c
  14. ON b.position_id = c.position_id AND c.resign = 0 WHERE b.SZ_PID3 IN (SELECT a.SZ_PID3 FROM jianyu_subjectdb.dwd_d_crm_department_level_succbi a WHERE a.position_id = ?) AND c.role_id = 3)`
  15. topMail3 = `SELECT d.mail FROM jianyu.entniche_user d WHERE d.id IN ( SELECT a.ent_user_id FROM Jianyu_subjectdb.dwd_d_crm_department_level_succbi a INNER JOIN jianyu_subjectdb.dwd_f_crm_personnel_management b
  16. ON a.position_id = b.position_id WHERE a.dept_name = '销售三部' AND a.resign = 0 AND b.role_id = 3)`
  17. dmInfo = `SELECT b.dept_name FROM Jianyu_subjectdb.dwd_d_crm_department_level_succbi b WHERE b.position_id IN (SELECT a.position_id FROM Jianyu_subjectdb.dwd_f_crm_personnel_management a
  18. WHERE assign_type = 1) GROUP BY b.dept_name`
  19. dmInfoCount = `SELECT COUNT(*) as num FROM Jianyu_subjectdb.dwd_f_crm_clue_info b WHERE b.position_id IN (SELECT a.position_id FROM Jianyu_subjectdb.dwd_d_crm_department_level_succbi a WHERE dept_name = ? AND resign = 0) GROUP BY b.position_id`
  20. )
  21. // @Author jianghan
  22. // @Description 个人预警/上限
  23. // @Date 2024/4/11
  24. func WarningPerl() {
  25. sql := `SELECT count(*) FROM dwd_f_crm_clue_info where position_id = ? and assign_type >= 1 and trailstatus <> '08'`
  26. sql1 := `UPDATE dwd_f_crm_personnel_management where position_id = ? SET send_mail = ?`
  27. pp := Mysql.SelectBySql(`SELECT name, position_id, send_mail FROM dwd_f_crm_personnel_management WHERE assign_type >= 1`)
  28. if pp != nil && len(*pp) > 0 {
  29. for _, m := range *pp {
  30. sendFlag := common.IntAll(m["send_mail"])
  31. posid := common.Int64All(m["position_id"])
  32. count := Mysql.CountBySql(sql, posid)
  33. if count >= int64(WarningSize) && sendFlag == 0 {
  34. WillWarningByPp(posid) // 发预警邮件
  35. _, _ = Mysql.ExecBySql(sql1, posid, 0)
  36. } else if count >= int64(LimitSize) && sendFlag == 0 {
  37. ExceedLimitByPp(posid) // 发上限邮件
  38. _, _ = Mysql.ExecBySql(sql1, posid, 0)
  39. } else if sendFlag == 1 { // 已发邮件
  40. if count < int64(WarningSize) {
  41. _, _ = Mysql.ExecBySql(sql1, posid, 0)
  42. }
  43. }
  44. }
  45. }
  46. }
  47. // @Author jianghan
  48. // @Description 部门预警/上限
  49. // @Date 2024/5/9
  50. func WarningDm() {
  51. dm := Mysql.SelectBySql(dmInfo)
  52. for _, m := range *dm {
  53. d1 := common.ObjToString(m["dept_name"])
  54. info := Mysql.SelectBySql(dmInfoCount, d1)
  55. send1, send2 := true, true // 预警,上限 发送标记
  56. for _, m1 := range *info {
  57. count := common.IntAll(m1["num"])
  58. if count < WarningSize {
  59. send1 = false
  60. }
  61. if count < LimitSize {
  62. send2 = false
  63. }
  64. }
  65. if send1 {
  66. }
  67. }
  68. }
  69. // @Author jianghan
  70. // @Description 个人私海线索即将达到预警提醒
  71. // @Date 2024/4/10
  72. func WillWarningByPp(posid int64) {
  73. title := "您的私海线索即将达到上限通知"
  74. content := "您的私海线索已超过%d条(不包含成交客户),即将达到私海线索上限%d条(不包含成交客户),请及时将无需跟进的线索退回公海,以避免无法接收新线索。"
  75. toMail := ""
  76. toCc := ""
  77. info := Mysql.SelectBySql(selfMail, posid)
  78. if info != nil && len(*info) > 0 {
  79. toMail = common.ObjToString((*info)[0]["mail"])
  80. }
  81. if toMail == "" {
  82. log.Println("未查询到邮箱地址, position id: ", posid)
  83. return
  84. }
  85. toCc = getCc(posid)
  86. content = fmt.Sprintf(content, WarningSize, LimitSize)
  87. sendInfo(toMail, toCc, title, content)
  88. }
  89. // @Author jianghan
  90. // @Description 部门人员私海线索即将达到预警提醒
  91. // @Date 2024/4/10
  92. func WillWarningByDm() {
  93. title := "“%s”私海线索即将达到上限通知"
  94. content := "“%s”的所有参与线索分配的电销人员,私海线索都已达到%d条(不包含成交客户),即将达到私海线索上限%d条(不包含成交客户),请及时通知电销人员将无需跟进的线索退回公海,以避免无法接收新线索。"
  95. toMail := ""
  96. toCc := ""
  97. title = fmt.Sprintf(title, "")
  98. content = fmt.Sprintf(content, "", WarningSize, LimitSize)
  99. // todo
  100. sendInfo(toMail, toCc, title, content)
  101. }
  102. // @Author jianghan
  103. // @Description 所有人私海线索即将达到预警提醒
  104. // @Date 2024/4/10
  105. func WillWarningByAll() {
  106. title := "所有电销人员私海线索即将达到上限通知"
  107. content := "所有参与线上线索自动分配的电销人员,私海线索都已达到%d条,请及时提醒销售部门将无需跟进的销售线索退回公海"
  108. toMail := ""
  109. toCc := ""
  110. content = fmt.Sprintf(content, WarningSize)
  111. sendInfo(toMail, toCc, title, content)
  112. }
  113. // @Author jianghan
  114. // @Description 个人私海线索达到上限提醒
  115. // @Date 2024/4/10
  116. func ExceedLimitByPp(posid int64) {
  117. title := "您的私海线索已达上限通知"
  118. content := "您的私海线索已达到私海线索上限%d条(不包含成交客户),当前无法接收新线索,请及时将无需跟进的线索退回公海"
  119. toMail := ""
  120. toCc := ""
  121. content = fmt.Sprintf(content, LimitSize)
  122. info := Mysql.SelectBySql(selfMail, posid)
  123. if info != nil && len(*info) > 0 {
  124. toMail = common.ObjToString((*info)[0]["mail"])
  125. }
  126. if toMail == "" {
  127. log.Println("未查询到邮箱地址, position id: ", posid)
  128. return
  129. }
  130. toCc = getCc(posid)
  131. sendInfo(toMail, toCc, title, content)
  132. }
  133. // @Author jianghan
  134. // @Description 部门所有人私海线索达到上限提醒
  135. // @Date 2024/4/10
  136. func ExceedLimitByDm() {
  137. title := "“%s”私海线索已达到上限通知"
  138. content := "“%s”的所有参与线索分配的电销人员,私海线索都已达到私海线索上限%d条(不包含成交客户),当前无法接收新线索,请及时通知电销人员将无需跟进的线索退回公海"
  139. toMail := ""
  140. toCc := ""
  141. title = fmt.Sprintf(title, "")
  142. content = fmt.Sprintf(content, "", LimitSize)
  143. sendInfo(toMail, toCc, title, content)
  144. }
  145. // @Author jianghan
  146. // @Description 自动释放私海线索通知
  147. // @Date 2024/4/10
  148. func AutoReleaseNots() {
  149. title := "自动释放私海线索通知"
  150. content := "所有参与线上线索自动分配的电销人员,私海线索都已达到%d条,为避免线上产生的新线索无法分配,系统已自动将线索状态为“商机线索(已建联,包含已拨打未接通)”、“潜在客户”、“沉睡客户”的线索退回公海,如有异议请联系销管处理;"
  151. toMail := ""
  152. toCc := ""
  153. content = fmt.Sprintf(content, "", LimitSize)
  154. gmail := &mail.GmailAuth{
  155. SmtpHost: "smtp.exmail.qq.com",
  156. SmtpPort: 465,
  157. User: "public03@topnet.net.cn",
  158. Pwd: "ue9Rg9Sf4CVtdm5a",
  159. }
  160. status := mail.GSendMail("剑鱼标讯", toMail, toCc, "", title, content, "", "", gmail)
  161. if status {
  162. log.Println("send mail success", title, toMail)
  163. }
  164. }
  165. // @Author jianghan
  166. // @Description 线上线索无法分配通知
  167. // @Date 2024/4/10
  168. func CantBeAssignedNots() {
  169. title := "【紧急】线上线索无法分配通知"
  170. content := "所有参与线上线索自动分配的电销人员,私海线索都已达到**条,且私海已无“潜在客户”、“沉睡客户”、“商机线索(已建联)”可释放,当前线上线索无法分配,请尽快处理,可前往【公海线索】查看“待分线索”条数"
  171. toMail := ""
  172. toCc := ""
  173. gmail := &mail.GmailAuth{
  174. SmtpHost: "smtp.exmail.qq.com",
  175. SmtpPort: 465,
  176. User: "public03@topnet.net.cn",
  177. Pwd: "ue9Rg9Sf4CVtdm5a",
  178. }
  179. status := mail.GSendMail("剑鱼标讯", toMail, toCc, "", title, content, "", "", gmail)
  180. if status {
  181. log.Println("send mail success", title, toMail)
  182. }
  183. }
  184. // @Author jianghan
  185. // @Description 客成移交线索失败提醒
  186. // @Date 2024/4/10
  187. func HandOverFail() {
  188. title := "客成线索移交失败通知"
  189. content := "您的私海线索已超过**条(不包含成交客户),已达到私海线索上限**条(不包含成交客户),导致“XXX线索名称(XXX手机号)”无法从客成到期自动移交至私海,请及时将无需跟进的线索退回公海,超过3个工作日(根据配置获取)不处理,该线索将自动退回至公海"
  190. toMail := ""
  191. toCc := ""
  192. sendInfo(toMail, toCc, title, content)
  193. }
  194. // @Author jianghan
  195. // @Description 订单用户创建线索失败提醒
  196. // @Date 2024/4/10
  197. func OrderCreateFail() {
  198. title := "订单用户创建线索失败通知"
  199. content := "您的私海线索已超过**条(不包含成交客户),已达到私海线索上限**条(不包含成交客户),导致订单系统的用户“XXX公司名称(XXX手机号)”无法进入您的私海,请及时将无需跟进的线索退回公海,超过3个工作日(根据配置获取)不处理,该线索将自动进入至公海。"
  200. toMail := ""
  201. toCc := ""
  202. sendInfo(toMail, toCc, title, content)
  203. }
  204. // @Author jianghan
  205. // @Description 发邮件
  206. // @Date 2024/5/8
  207. func sendInfo(to, cs, title, content string) {
  208. gmail := &mail.GmailAuth{
  209. SmtpHost: "smtp.exmail.qq.com",
  210. SmtpPort: 465,
  211. User: "public03@topnet.net.cn",
  212. Pwd: "ue9Rg9Sf4CVtdm5a",
  213. }
  214. status := mail.GSendMail("剑鱼标讯", to, cs, "", title, content, "", "", gmail)
  215. if status {
  216. log.Println("send mail success", title, to)
  217. }
  218. }
  219. // @Author jianghan
  220. // @Description 获取抄送人员邮箱
  221. //
  222. // 销售三部 (SZ_PID1=5432)、销售一部(分组)
  223. //
  224. // @Date 2024/5/8
  225. func getCc(posid int64) (to string) {
  226. info := Mysql.SelectBySql(infoSelf, posid)
  227. if info == nil && len(*info) == 0 {
  228. log.Println("为查询到个人信息, positionId: ", posid)
  229. return
  230. }
  231. if common.ObjToString((*info)[0]["dept_name"]) == "销售三部" {
  232. if common.IntAll((*info)[0]["role_id"]) == 1 {
  233. m := Mysql.SelectBySql(topMail3)
  234. if m != nil && len(*m) > 0 {
  235. to = common.ObjToString((*m)[0]["mail"])
  236. }
  237. }
  238. } else {
  239. if common.IntAll((*info)[0]["role_id"]) == 2 {
  240. m := Mysql.SelectBySql(topMail, posid)
  241. if m != nil && len(*m) > 0 {
  242. to = common.ObjToString((*m)[0]["mail"])
  243. }
  244. } else {
  245. to = "shenbingyi@topnet.net.cn"
  246. }
  247. }
  248. return
  249. }