acceptance.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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, productMap []ProductMap, entId int64) 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. selectedProductMap := map[string]SelectProductMap{}
  80. productArr := []string{}
  81. if _, isOk := childMap["咨询产品"]; isOk {
  82. for _, v := range strings.Split(gconv.String(childMap["咨询产品"]), ",") {
  83. for _, product := range productMap {
  84. if strings.Contains(product.Product, v) {
  85. selectedProduct := selectedProductMap[product.ProductCode]
  86. selectedProduct.PersonArr = product.PersonArr
  87. selectedProduct.Product = append(selectedProduct.Product, v)
  88. productArr = append(productArr, v)
  89. selectedProductMap[product.ProductCode] = selectedProduct
  90. continue
  91. }
  92. }
  93. }
  94. if len(strings.Split(gconv.String(childMap["咨询产品"]), ",")) != len(productArr) {
  95. is_clue = 2
  96. }
  97. }
  98. for k, v := range selectedProductMap {
  99. if !AddOrderWork(k, acceptance_no, nowTime, phone, company, tx, in, v, entId) {
  100. return false
  101. }
  102. }
  103. }
  104. //先新增受理单主单
  105. acceptanceMap["is_clue"] = is_clue
  106. ok1 := WorkOrder.InsertByTx(tx, "order_acceptance", acceptanceMap)
  107. if ok1 <= 0 {
  108. logx.Info("受理单创建失败")
  109. return false
  110. }
  111. //工单处理
  112. return true
  113. })
  114. if ok {
  115. return acceptance_no
  116. }
  117. return ""
  118. }
  119. func AddOrderWork(orderType, acceptance_no, nowTime, phone, company string, tx *sql.Tx, in *biservice.AcceptanceReq, selectPersonMap SelectProductMap, entId int64) bool {
  120. dkPerson := ""
  121. dkdeptName := ""
  122. dkdeptId := ""
  123. dkPositionId := int64(0)
  124. personMap := map[string]interface{}{}
  125. personMap = FindCandidate(selectPersonMap.PersonArr, entId, orderType)
  126. dkPositionId = gconv.Int64(personMap["positionId"])
  127. dkPerson = gconv.String(personMap["name"])
  128. dkdeptId = gconv.String(personMap["deptId"])
  129. dkdeptName = gconv.String(personMap["deptName"])
  130. if dkPositionId != 0 {
  131. work_order_no := fmt.Sprintf("GD%s%s", time.Now().Format(date.Date_yyyyMMdd), FindNumber("gd"))
  132. orderWorkMap := map[string]interface{}{
  133. "work_order_no": work_order_no,
  134. "acceptance_no": acceptance_no,
  135. "type": strings.Join(selectPersonMap.Product, ","),
  136. "status": 1,
  137. "initiator_name": in.EntUserName,
  138. "initiator_position_id": in.PositionId,
  139. "current_name": dkPerson,
  140. "current_position_id": dkPositionId,
  141. "is_delete": 1,
  142. "creator_name": in.EntUserName,
  143. "creator_position_id": in.PositionId,
  144. "creator_time": nowTime,
  145. "two_type": orderType,
  146. "department_no": dkdeptId,
  147. "department_name": dkdeptName,
  148. "update_time": nil,
  149. }
  150. logx.Info(orderWorkMap, "11111", selectPersonMap)
  151. ok3 := WorkOrder.InsertByTx(tx, "order_work", orderWorkMap)
  152. if ok3 <= 0 {
  153. log.Println("工单保存失败")
  154. return false
  155. }
  156. //发送邮件
  157. //日志添加
  158. approvalRecordMap := map[string]interface{}{
  159. "work_order_no": work_order_no,
  160. "status": 1,
  161. "new_status": nil,
  162. "handle_name": dkPerson,
  163. "handle_position_id": dkPositionId,
  164. "handle_dept_id": dkdeptId,
  165. "handle_dept_name": dkdeptName,
  166. "creator_name": in.EntUserName,
  167. "creator_position_id": in.PositionId,
  168. "is_delete": 1,
  169. "creator_time": nowTime,
  170. }
  171. log.Println(approvalRecordMap)
  172. ok4 := WorkOrder.InsertByTx(tx, "approval_record", approvalRecordMap)
  173. if ok4 <= 0 {
  174. log.Println("工单记录保存失败")
  175. return false
  176. }
  177. log.Println(personMap)
  178. log.Println(GmailAuth, personMap, strings.Join(selectPersonMap.Product, ","), dkPerson, in.EntUserName, nowTime, work_order_no, phone, company)
  179. WorkMail(GmailAuth, personMap, strings.Join(selectPersonMap.Product, ","), dkPerson, in.EntUserName, nowTime, work_order_no, phone, company)
  180. }
  181. return true
  182. }
  183. type PersonJson struct {
  184. Name string
  185. Phone string
  186. Mail string
  187. DeptId int64
  188. DeptName string
  189. PositionId int64
  190. IsResign bool
  191. }
  192. // 大客人员选择
  193. func FindCandidate(personArr []Person, entId int64, orderType string) map[string]interface{} {
  194. person := map[string]interface{}{}
  195. personEntity := PersonJson{}
  196. personMap := make(map[string]map[string]interface{})
  197. phoneArr := make([]string, len(personArr))
  198. persons := make([]PersonJson, len(personArr))
  199. // Populate phoneArr and personMap
  200. for k, v := range personArr {
  201. phone := gconv.String(v.Phone)
  202. phoneArr[k] = fmt.Sprintf(`"%s"`, phone)
  203. personMap[phone] = map[string]interface{}{
  204. "name": gconv.String(v.Name),
  205. }
  206. persons[k] = PersonJson{
  207. Name: v.Name,
  208. Phone: v.Phone,
  209. IsResign: v.IsResign,
  210. }
  211. }
  212. // Fetch personal email and department information
  213. entUserArr := JyMysql.SelectBySql(fmt.Sprintf(`
  214. SELECT a.name, a.mail, b.dept_id AS deptId, a.phone, c.name AS deptName
  215. FROM entniche_user a
  216. INNER JOIN entniche_department_user b ON a.ent_id = %d AND a.phone IN %s AND a.id = b.user_id
  217. INNER JOIN entniche_department c ON b.dept_id = c.id
  218. `, entId, fmt.Sprintf("(%s)", strings.Join(phoneArr, ","))))
  219. if entUserArr != nil {
  220. for _, v := range *entUserArr {
  221. phone := gconv.String(v["phone"])
  222. personMap[phone]["mail"] = gconv.String(v["mail"])
  223. personMap[phone]["deptId"] = gconv.String(v["deptId"])
  224. personMap[phone]["deptName"] = gconv.String(v["deptName"])
  225. }
  226. }
  227. // Fetch position information
  228. positionArrMap := JyMysql.SelectBySql(fmt.Sprintf(`
  229. SELECT a.phone, b.id
  230. FROM base_service.base_user a
  231. INNER JOIN base_service.base_position b ON b.ent_id = %d AND a.phone IN %s AND b.user_id = a.id AND b.type = 1
  232. `, entId, fmt.Sprintf("(%s)", strings.Join(phoneArr, ","))))
  233. if positionArrMap != nil {
  234. for _, v := range *positionArrMap {
  235. phone := gconv.String(v["phone"])
  236. personMap[phone]["positionId"] = gconv.Int64(v["id"])
  237. }
  238. }
  239. // Update personArr with additional information
  240. for k, v := range persons {
  241. phone := v.Phone
  242. if info, exists := personMap[phone]; exists {
  243. persons[k].Mail = gconv.String(info["mail"])
  244. persons[k].DeptId = gconv.Int64(info["deptId"])
  245. persons[k].DeptName = gconv.String(info["deptName"])
  246. persons[k].PositionId = gconv.Int64(info["positionId"])
  247. }
  248. }
  249. // Query the last person who created an order
  250. orderWorkMap := WorkOrder.SelectBySql(fmt.Sprintf(`
  251. SELECT * FROM order_work WHERE two_type = "%s" ORDER BY creator_time DESC LIMIT 1
  252. `, orderType))
  253. var k int
  254. if orderWorkMap == nil || len(*orderWorkMap) == 0 {
  255. k = findNextPersonIndex(persons, 0)
  256. } else {
  257. currentName := gconv.String((*orderWorkMap)[0]["current_name"])
  258. k = findPersonIndexByName(persons, currentName)
  259. k = findNextPersonIndex(persons, k)
  260. }
  261. personEntity = persons[k]
  262. // Set person details
  263. person["positionId"] = personEntity.PositionId
  264. person["deptName"] = personEntity.DeptName
  265. person["deptId"] = personEntity.DeptId
  266. person["name"] = personEntity.Name
  267. person["mail"] = personEntity.Mail
  268. // Fetch department admin
  269. fetchDeptAdmin(person, personEntity.DeptId)
  270. // Fetch superior admin
  271. fetchSuperiorAdmin(person, personEntity.DeptId)
  272. return person
  273. }
  274. // Helper function to find the next person index
  275. func findNextPersonIndex(personArr []PersonJson, startIndex int) int {
  276. n := len(personArr)
  277. // 如果当前索引是最后一个,则从 0 开始
  278. if startIndex >= n-1 {
  279. startIndex = -1 // 设置为 -1 以便在下次循环中变为 0
  280. }
  281. for count := 0; count < n; count++ { // 限制最大循环次数
  282. startIndex++ // 先加 1
  283. if startIndex >= n {
  284. startIndex = 0 // 如果超出范围,则重置为 0
  285. }
  286. if !personArr[startIndex].IsResign {
  287. return startIndex // 找到未辞职的员工,返回索引
  288. }
  289. }
  290. return 0 // 如果没有找到,返回 -1
  291. }
  292. // Helper function to find a person's index by name
  293. func findPersonIndexByName(personArr []PersonJson, name string) int {
  294. for i, v := range personArr {
  295. if v.Name == name {
  296. return i
  297. }
  298. }
  299. return 0 // Default to the first index if not found
  300. }
  301. // Fetch department admin details
  302. func fetchDeptAdmin(person map[string]interface{}, deptId int64) {
  303. deptMap := JyMysql.SelectBySql(`SELECT c.name AS name, c.mail AS mail
  304. FROM entniche_department_user a
  305. INNER JOIN entniche_user_role b ON a.dept_id = ? AND a.user_id = b.user_id AND b.role_id != ""
  306. INNER JOIN entniche_user c ON a.user_id = c.id`, deptId)
  307. if deptMap != nil && len(*deptMap) > 0 {
  308. person["deptPersonName"] = gconv.String((*deptMap)[0]["name"])
  309. person["deptPersonMail"] = gconv.String((*deptMap)[0]["mail"])
  310. }
  311. }
  312. // Fetch superior admin details
  313. func fetchSuperiorAdmin(person map[string]interface{}, deptId int64) {
  314. superiorMap := JyMysql.SelectBySql(`SELECT c.*
  315. FROM entniche_department d
  316. INNER JOIN entniche_department_user a ON d.id = ? AND d.pid = a.dept_id
  317. INNER JOIN entniche_user_role b ON a.user_id = b.user_id AND b.role_id != ""
  318. INNER JOIN entniche_user c ON a.user_id = c.id`, deptId)
  319. if superiorMap != nil && len(*superiorMap) > 0 {
  320. person["superiorDepthPersonName"] = gconv.String((*superiorMap)[0]["name"])
  321. person["superiorDepthPersonMail"] = gconv.String((*superiorMap)[0]["mail"])
  322. }
  323. }
  324. // 编号查询
  325. func FindNumber(moudle string) string {
  326. today := time.Now().Format("2006-01-02")
  327. yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
  328. key := fmt.Sprintf("%s_%s", today, moudle)
  329. yesterdayKey := fmt.Sprintf("%s_%s", yesterday, moudle)
  330. if ok, _ := redis.Exists("newother", yesterdayKey); ok {
  331. //删除之前数据
  332. redis.Del("newother", yesterdayKey)
  333. }
  334. count := redis.Incr("newother", key)
  335. log.Println("编号获取", moudle, fmt.Sprintf("%04d", count))
  336. return fmt.Sprintf("%04d", count)
  337. }
  338. func WorkMail(gmailAuth []*mail.GmailAuth, personMap map[string]interface{}, productStr string, personName1, personName2, createTimeStr, acceptance_no, phone, company string) {
  339. orderType := fmt.Sprintf(`客户咨询线索(%s)`, productStr)
  340. title := fmt.Sprintf("%s通知", orderType)
  341. if personName1 == gconv.String(personMap["deptPersonName"]) {
  342. personMap["deptPersonMail"] = ""
  343. }
  344. if personName1 == gconv.String(personMap["superiorDepthPersonName"]) {
  345. personMap["superiorDepthPersonMail"] = ""
  346. }
  347. 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))))
  348. toMail := gconv.String(personMap["mail"])
  349. mailArr := []string{}
  350. if gconv.String(common.If(gconv.String(personMap["deptPersonMail"]) == "", "", gconv.String(personMap["deptPersonMail"]))) != "" {
  351. mailArr = append(mailArr, gconv.String(common.If(gconv.String(personMap["deptPersonMail"]) == "", "", gconv.String(personMap["deptPersonMail"]))))
  352. }
  353. if gconv.String(common.If(gconv.String(personMap["superiorDepthPersonMail"]) == "", "", gconv.String(personMap["superiorDepthPersonMail"]))) != "" {
  354. mailArr = append(mailArr, gconv.String(common.If(gconv.String(personMap["superiorDepthPersonMail"]) == "", "", gconv.String(personMap["superiorDepthPersonMail"]))))
  355. }
  356. toCc := strings.Join(mailArr, ",")
  357. if len(mailArr) > 0 {
  358. toMail = fmt.Sprintf("%s|%s", toMail, toCc)
  359. }
  360. log.Println(toMail, title, content)
  361. for k, v := range gmailAuth {
  362. fool := mail.GSendMail_q("剑鱼标讯", toMail, "", "", title, content, "", "", v)
  363. if fool {
  364. logx.Info(toMail, fmt.Sprintf("使用%s发送邮件成功", v.User))
  365. break
  366. }
  367. if k < len(gmailAuth)-1 {
  368. logx.Info(toMail, fmt.Sprintf("使用%s发送邮件失败!3s后使用其他邮箱尝试", v.User))
  369. } else {
  370. logx.Info(toMail, fmt.Sprintf("使用%s发送邮件失败!", v.User))
  371. }
  372. time.Sleep(time.Second * 3)
  373. }
  374. }