acceptance.go 13 KB

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