acceptance.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. package service
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/date"
  5. "app.yhyue.com/moapp/jybase/mail"
  6. "app.yhyue.com/moapp/jybase/redis"
  7. . "bp.jydev.jianyu360.cn/BaseService/biService/entity"
  8. "bp.jydev.jianyu360.cn/BaseService/biService/rpc/biservice"
  9. "database/sql"
  10. "fmt"
  11. "github.com/gogf/gf/v2/util/gconv"
  12. "github.com/zeromicro/go-zero/core/logx"
  13. "log"
  14. "strings"
  15. "time"
  16. )
  17. func AddAcceptance(in *biservice.AcceptanceReq, DkPersonMap []map[string]interface{}, entId int64, productMap map[string]string) string {
  18. nowTime := time.Now().Format(date.Date_Full_Layout)
  19. //编号处理
  20. is_clue := 1
  21. deptName := ""
  22. if in.DeptId != "" {
  23. //部门名称查询
  24. deptData := JyMysql.FindOne("entniche_department", map[string]interface{}{
  25. "id": in.DeptId,
  26. }, "name", "")
  27. if deptData != nil && len(*deptData) > 0 {
  28. deptName = gconv.String((*deptData)["name"])
  29. }
  30. }
  31. acceptance_no := fmt.Sprintf("SLD%s%s", time.Now().Format(date.Date_Short_Layout), FindNumber("sld"))
  32. acceptanceMap := map[string]interface{}{
  33. "acceptance_no": acceptance_no,
  34. "propose_type": in.ProposeType,
  35. "propose_time": in.ProposeTime,
  36. "channel": in.Channel,
  37. "acceptance_type": in.AcceptanceType,
  38. "status": in.Status,
  39. "initiator_name": in.EntUserName,
  40. "initiator_position_id": in.PositionId,
  41. "department_no": in.DeptId,
  42. "remark": in.Remark,
  43. "is_delete": 1,
  44. "creator_name": in.EntUserName,
  45. "creator_position_id": in.PositionId,
  46. "creator_time": nowTime,
  47. "department_name": deptName,
  48. }
  49. ok := WorkOrder.ExecTx("受理单处理", func(tx *sql.Tx) bool {
  50. //新增受理单子单
  51. childMap := gconv.Map(in.ParamData)
  52. if childMap != nil && len(childMap) > 0 {
  53. phone := ""
  54. company := ""
  55. innerArr := []interface{}{}
  56. innerStr := []string{"acceptance_no", "field_name", "field_value", "creator_name", "creator_position_id", "creator_time"}
  57. for k, v := range childMap {
  58. innerArr = append(innerArr, acceptance_no)
  59. innerArr = append(innerArr, k)
  60. innerArr = append(innerArr, v)
  61. innerArr = append(innerArr, in.EntUserName)
  62. innerArr = append(innerArr, in.PositionId)
  63. innerArr = append(innerArr, nowTime)
  64. switch k {
  65. case "公司名称":
  66. company = gconv.String(v)
  67. case "联系方式num":
  68. phone = gconv.String(v)
  69. }
  70. }
  71. if len(innerArr) > 0 {
  72. ok2, ok3 := WorkOrder.InsertBatchByTx(tx, "order_acceptance_children", innerStr, innerArr)
  73. if ok2 <= 0 && ok3 <= 0 {
  74. return false
  75. }
  76. }
  77. //判断是否创建工单
  78. dkPerson := ""
  79. dkdeptName := ""
  80. dkdeptId := ""
  81. dkProduct := []string{}
  82. dkPositionId := int64(0)
  83. personMap := map[string]interface{}{}
  84. if _, isOk := childMap["咨询产品"]; isOk {
  85. for _, v := range strings.Split(gconv.String(childMap["咨询产品"]), ",") {
  86. switch productMap[v] {
  87. case "dk":
  88. dkProduct = append(dkProduct, v)
  89. if dkPositionId == 0 {
  90. personMap = Findcandidate(DkPersonMap, entId)
  91. dkPositionId = gconv.Int64(personMap["positionId"])
  92. dkPerson = gconv.String(personMap["name"])
  93. dkdeptId = gconv.String(personMap["deptId"])
  94. dkdeptName = gconv.String(personMap["deptName"])
  95. }
  96. continue
  97. default:
  98. is_clue = 2
  99. }
  100. }
  101. }
  102. if dkPositionId != 0 {
  103. work_order_no := fmt.Sprintf("GD%s%s", time.Now().Format(date.Date_Short_Layout), FindNumber("gd"))
  104. orderWorkMap := map[string]interface{}{
  105. "work_order_no": work_order_no,
  106. "acceptance_no": acceptance_no,
  107. "type": strings.Join(dkProduct, ","),
  108. "status": 1,
  109. "initiator_name": in.EntUserName,
  110. "initiator_position_id": in.PositionId,
  111. "current_name": dkPerson,
  112. "current_position_id": dkPositionId,
  113. "is_delete": 1,
  114. "creator_name": in.EntUserName,
  115. "creator_position_id": in.PositionId,
  116. "creator_time": nowTime,
  117. "two_type": "dk",
  118. "department_no": dkdeptId,
  119. "department_name": dkdeptName,
  120. }
  121. ok3 := WorkOrder.InsertByTx(tx, "order_work", orderWorkMap)
  122. if ok3 <= 0 {
  123. return false
  124. }
  125. //发送邮件
  126. //日志添加
  127. approvalRecordMap := map[string]interface{}{
  128. "work_order_no": work_order_no,
  129. "status": 1,
  130. "handle_name": dkPerson,
  131. "handle_position_id": dkPositionId,
  132. "handle_dept_id": dkdeptId,
  133. "handle_dept_name": dkdeptName,
  134. "creator_name": in.EntUserName,
  135. "creator_position_id": in.PositionId,
  136. "is_delete": 1,
  137. "creator_time": nowTime,
  138. }
  139. WorkOrder.InsertByTx(tx, "approval_record", approvalRecordMap)
  140. log.Println(personMap)
  141. WorkMail(GmailAuth, personMap, strings.Join(dkProduct, ","), dkPerson, in.EntUserName, nowTime, work_order_no, phone, company)
  142. }
  143. }
  144. //先新增受理单主单
  145. acceptanceMap["is_clue"] = is_clue
  146. ok1 := WorkOrder.InsertByTx(tx, "order_acceptance", acceptanceMap)
  147. if ok1 <= 0 {
  148. logx.Info("受理单创建失败")
  149. return false
  150. }
  151. //工单处理
  152. return true
  153. })
  154. if ok {
  155. return acceptance_no
  156. }
  157. return ""
  158. }
  159. // 大客人员选择
  160. func Findcandidate(DkPersonMap []map[string]interface{}, entId int64) map[string]interface{} {
  161. person := map[string]interface{}{}
  162. positionArr := []string{}
  163. personMap := map[string]map[string]interface{}{}
  164. positionId := int64(0)
  165. phoneArr := []string{}
  166. for _, v := range DkPersonMap {
  167. phoneArr = append(phoneArr, fmt.Sprintf(`"%s"`, gconv.String(v["phone"])))
  168. personMap[gconv.String(v["phone"])] = map[string]interface{}{
  169. "name": gconv.String(v["personName"]),
  170. }
  171. }
  172. //个人邮箱,部门信息获取
  173. entUserArr := JyMysql.SelectBySql(fmt.Sprintf(`SELECT
  174. a.name as name,a.mail as mail,b.dept_id as deptId,a.phone,c.name as deptName
  175. FROM
  176. entniche_user a
  177. INNER JOIN entniche_department_user b ON a.ent_id =%d
  178. AND a.phone IN %s
  179. and a.id=b.user_id
  180. inner join entniche_department c on b.dept_id=c.id
  181. `, entId, fmt.Sprintf("(%s)", strings.Join(phoneArr, ","))))
  182. if entUserArr != nil && len(*entUserArr) > 0 {
  183. for _, v := range *entUserArr {
  184. phone := gconv.String(v["phone"])
  185. personMap[phone]["mail"] = gconv.String(v["mail"])
  186. personMap[phone]["deptId"] = gconv.String(v["deptId"])
  187. personMap[phone]["deptName"] = gconv.String(v["deptName"])
  188. }
  189. }
  190. //
  191. positionArrMap := JyTidb.SelectBySql(fmt.Sprintf(`select a.phone,b.id from
  192. base_user a
  193. INNER JOIN base_position b
  194. on b.ent_id=%d and a.phone in %s and b.user_id=a.id and b.type=1`,
  195. entId, fmt.Sprintf("(%s)", strings.Join(phoneArr, ","))))
  196. if positionArrMap != nil && len(*positionArrMap) > 0 {
  197. for _, v := range *positionArrMap {
  198. phone := gconv.String(v["phone"])
  199. positionid := gconv.Int64(v["id"])
  200. for k := range personMap {
  201. if k == phone {
  202. personMap[phone]["positionId"] = positionid
  203. positionArr = append(positionArr, gconv.String(positionid))
  204. }
  205. }
  206. }
  207. }
  208. //查询工单数量
  209. positionNumberMap := WorkOrder.SelectBySql(`SELECT
  210. current_position_id,
  211. COUNT( 1 ) AS count
  212. FROM
  213. order_work
  214. WHERE
  215. FIND_IN_SET(current_position_id , ?)
  216. GROUP BY current_position_id
  217. ORDER BY
  218. count`, strings.Join(positionArr, ","))
  219. if positionNumberMap != nil && len(*positionNumberMap) == len(DkPersonMap) {
  220. positionId = gconv.Int64((*positionNumberMap)[0]["current_position_id"])
  221. } else if positionNumberMap != nil && len(*positionNumberMap) != len(DkPersonMap) {
  222. for _, v := range positionArr {
  223. count := int64(0)
  224. for _, v1 := range *positionNumberMap {
  225. id := gconv.String(v1["current_position_id"])
  226. if id == v {
  227. count = gconv.Int64(v1["count"])
  228. }
  229. }
  230. if count == 0 {
  231. positionId = gconv.Int64(v)
  232. break
  233. }
  234. }
  235. } else {
  236. positionId = gconv.Int64(positionArr[0])
  237. }
  238. if positionId == 0 {
  239. positionId = gconv.Int64(positionArr[0])
  240. }
  241. //商机管理员
  242. deptId := int64(0)
  243. for _, v := range personMap {
  244. positionid := gconv.Int64(v["positionId"])
  245. if positionId == positionid {
  246. person = v
  247. deptId = gconv.Int64(v["deptId"])
  248. }
  249. }
  250. //本部门管理员查询
  251. depthMap := JyMysql.SelectBySql(`SELECT
  252. c.name as name,c.mail as mail
  253. FROM
  254. entniche_department_user a
  255. INNER JOIN entniche_user_role b ON a.dept_id = ?
  256. AND a.user_id = b.user_id
  257. AND b.role_id =2
  258. INNER JOIN entniche_user c ON a.user_id = c.id`, deptId)
  259. if depthMap != nil && len(*depthMap) > 0 {
  260. person["deptPersonName"] = gconv.String((*depthMap)[0]["name"])
  261. person["deptPersonMail"] = gconv.String((*depthMap)[0]["mail"])
  262. }
  263. //商机管理员查询
  264. superiorDepthMap := JyMysql.SelectBySql(`SELECT
  265. c.*
  266. FROM
  267. entniche_department d
  268. INNER JOIN entniche_department_user a ON d.id = ?
  269. AND d.pid = a.dept_id
  270. INNER JOIN entniche_user_role b ON a.user_id = b.user_id
  271. AND b.role_id = 1
  272. INNER JOIN entniche_user c ON a.user_id = c.id`, deptId)
  273. if superiorDepthMap != nil && len(*superiorDepthMap) > 0 {
  274. person["superiorDepthPersonName"] = gconv.String((*superiorDepthMap)[0]["name"])
  275. person["superiorDepthPersonMail"] = gconv.String((*superiorDepthMap)[0]["mail"])
  276. }
  277. return person
  278. }
  279. // 编号查询
  280. func FindNumber(moudle string) string {
  281. today := time.Now().Format("2006-01-02")
  282. yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
  283. key := fmt.Sprintf("%s_%s", today, moudle)
  284. yesterdayKey := fmt.Sprintf("%s_%s", yesterday, moudle)
  285. if ok, _ := redis.Exists("newother", yesterdayKey); ok {
  286. //删除之前数据
  287. redis.Del("newother", yesterdayKey)
  288. }
  289. count := redis.Incr("newother", key)
  290. return fmt.Sprintf("%04d", count)
  291. }
  292. func WorkMail(gmailAuth []*mail.GmailAuth, personMap map[string]interface{}, productStr string, personName1, personName2, createTimeStr, acceptance_no, phone, company string) {
  293. orderType := fmt.Sprintf(`客户咨询线索(%s)`, productStr)
  294. title := fmt.Sprintf("%s通知", orderType)
  295. if personName1 == gconv.String(personMap["deptPersonName"]) {
  296. personMap["deptPersonMail"] = ""
  297. }
  298. if personName1 == gconv.String(personMap["superiorDepthPersonName"]) {
  299. personMap["superiorDepthPersonMail"] = ""
  300. }
  301. content := fmt.Sprintf(`%s,您好,“%s”于%s新增了1条"%s”(工单编号:%s)%s%s,请及时前往【剑鱼PC工作台-任务单-工单管理-我负责的】进行工单处理。`, personName1, personName2, createTimeStr, orderType, acceptance_no, gconv.String(common.If(phone == "", "", fmt.Sprintf(`,客户联系方式为:%s`, phone))), gconv.String(common.If(company == "", "", fmt.Sprintf(`,公司名称:%s`, company))))
  302. toMail := gconv.String(personMap["mail"])
  303. toCc := fmt.Sprintf("%s%s", gconv.String(common.If(gconv.String(personMap["deptPersonMail"]) == "", "", gconv.String(personMap["deptPersonMail"]))), gconv.String(common.If(gconv.String(personMap["superiorDepthPersonMail"]) == "", "", gconv.String(personMap["superiorDepthPersonMail"]))))
  304. log.Println(toMail, toCc, title, content)
  305. /*toMail = "wanghao@topnet.net.cn"
  306. toCc = "wanghao@topnet.net.cn"*/
  307. for k, v := range gmailAuth {
  308. fool := mail.GSendMail_q("剑鱼标讯", toMail, toCc, "", title, content, "", "", v)
  309. if fool {
  310. logx.Info(toMail, fmt.Sprintf("使用%s发送邮件成功", v.User))
  311. break
  312. }
  313. if k < len(gmailAuth)-1 {
  314. logx.Info(toMail, fmt.Sprintf("使用%s发送邮件失败!3s后使用其他邮箱尝试", v.User))
  315. } else {
  316. logx.Info(toMail, fmt.Sprintf("使用%s发送邮件失败!", v.User))
  317. }
  318. time.Sleep(time.Second * 3)
  319. }
  320. }