sendMail.go 15 KB

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