acceptance.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package service
  2. import (
  3. "app.yhyue.com/moapp/jybase/date"
  4. "app.yhyue.com/moapp/jybase/redis"
  5. . "bp.jydev.jianyu360.cn/BaseService/biService/entity"
  6. "bp.jydev.jianyu360.cn/BaseService/biService/rpc/biservice"
  7. "database/sql"
  8. "fmt"
  9. "github.com/gogf/gf/v2/util/gconv"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. "log"
  12. "strings"
  13. "time"
  14. )
  15. func AddAcceptance(in *biservice.AcceptanceReq, DkPersonMap []map[string]interface{}, entId int64, productMap map[string]string) string {
  16. nowTime := time.Now().Format(date.Date_Full_Layout)
  17. //编号处理
  18. is_clue := 1
  19. deptName := ""
  20. if in.DeptId != "" {
  21. //部门名称查询
  22. deptData := JyMysql.FindOne("entniche_department", map[string]interface{}{
  23. "id": in.DeptId,
  24. }, "name", "")
  25. if deptData != nil && len(*deptData) > 0 {
  26. deptName = gconv.String((*deptData)["name"])
  27. }
  28. }
  29. acceptance_no := fmt.Sprintf("SLD%s%s", time.Now().Format(date.Date_Short_Layout), FindNumber("sld"))
  30. acceptanceMap := map[string]interface{}{
  31. "acceptance_no": acceptance_no,
  32. "propose_type": in.ProposeType,
  33. "propose_time": in.ProposeTime,
  34. "channel": in.Channel,
  35. "acceptance_type": in.AcceptanceType,
  36. "status": in.Status,
  37. "initiator_name": in.EntUserName,
  38. "initiator_position_id": in.PositionId,
  39. "department_no": in.DeptId,
  40. "remark": in.Remark,
  41. "is_delete": 1,
  42. "creator_name": in.EntUserName,
  43. "creator_position_id": in.PositionId,
  44. "creator_time": nowTime,
  45. "department_name": deptName,
  46. }
  47. ok := WorkOrder.ExecTx("受理单处理", func(tx *sql.Tx) bool {
  48. //新增受理单子单
  49. childMap := gconv.Map(in.ParamData)
  50. if childMap != nil && len(childMap) > 0 {
  51. innerArr := []interface{}{}
  52. innerStr := []string{"acceptance_no", "field_name", "field_value", "creator_name", "creator_position_id", "creator_time"}
  53. for k, v := range childMap {
  54. innerArr = append(innerArr, acceptance_no)
  55. innerArr = append(innerArr, k)
  56. innerArr = append(innerArr, v)
  57. innerArr = append(innerArr, in.EntUserName)
  58. innerArr = append(innerArr, in.PositionId)
  59. innerArr = append(innerArr, nowTime)
  60. }
  61. if len(innerArr) > 0 {
  62. ok2, ok3 := WorkOrder.InsertBatchByTx(tx, "order_acceptance_children", innerStr, innerArr)
  63. if ok2 <= 0 && ok3 <= 0 {
  64. return false
  65. }
  66. }
  67. //判断是否创建工单
  68. dkPerson := ""
  69. dkProduct := []string{}
  70. dkPositionId := int64(0)
  71. personMap := map[string]interface{}{}
  72. if _, isOk := childMap["咨询产品"]; isOk {
  73. for _, v := range strings.Split(gconv.String(childMap["咨询产品"]), ",") {
  74. switch productMap[v] {
  75. case "dk":
  76. dkProduct = append(dkProduct, v)
  77. if dkPositionId == 0 {
  78. personMap = Findcandidate(DkPersonMap, entId)
  79. dkPositionId = gconv.Int64(personMap["positionId"])
  80. }
  81. continue
  82. default:
  83. is_clue = 2
  84. }
  85. }
  86. }
  87. if dkPositionId != 0 {
  88. work_order_no := fmt.Sprintf("GD%s%s", time.Now().Format(date.Date_Short_Layout), FindNumber("gd"))
  89. orderWorkMap := map[string]interface{}{
  90. "work_order_no": work_order_no,
  91. "acceptance_no": acceptance_no,
  92. "type": strings.Join(dkProduct, ","),
  93. "status": 1,
  94. "initiator_name": in.EntUserName,
  95. "initiator_position_id": in.PositionId,
  96. "current_name": dkPerson,
  97. "current_position_id": dkPositionId,
  98. "is_delete": 1,
  99. "creator_name": in.EntUserName,
  100. "creator_position_id": in.PositionId,
  101. "creator_time": nowTime,
  102. "two_type": "dk",
  103. "department_no": in.DeptId,
  104. "department_name": deptName,
  105. }
  106. ok3 := WorkOrder.InsertByTx(tx, "order_work", orderWorkMap)
  107. if ok3 <= 0 {
  108. return false
  109. }
  110. //发送邮件
  111. log.Println(personMap)
  112. }
  113. }
  114. //先新增受理单主单
  115. acceptanceMap["is_clue"] = is_clue
  116. ok1 := WorkOrder.InsertByTx(tx, "order_acceptance", acceptanceMap)
  117. if ok1 <= 0 {
  118. logx.Info("受理单创建失败")
  119. return false
  120. }
  121. //工单处理
  122. return true
  123. })
  124. if ok {
  125. return acceptance_no
  126. }
  127. return ""
  128. }
  129. // 大客人员选择
  130. func Findcandidate(DkPersonMap []map[string]interface{}, entId int64) map[string]interface{} {
  131. person := map[string]interface{}{}
  132. positionArr := []string{}
  133. personMap := map[string]map[string]interface{}{}
  134. positionId := int64(0)
  135. phoneArr := []string{}
  136. for _, v := range DkPersonMap {
  137. phoneArr = append(phoneArr, fmt.Sprintf(`"%s"`, gconv.String(v["phone"])))
  138. personMap[gconv.String(v["phone"])] = map[string]interface{}{
  139. "name": gconv.String(v["name"]),
  140. }
  141. }
  142. //个人邮箱,部门信息获取
  143. entUserArr := JyMysql.SelectBySql(fmt.Sprintf(`SELECT
  144. a.name as name,a.mail as mail,b.dept_id as deptId,a.phone
  145. FROM
  146. entniche_user a
  147. INNER JOIN entniche_department_user b ON a.ent_id =%d
  148. AND a.phone IN %s
  149. and a.id=b.user_id`, entId, fmt.Sprintf("(%s)", strings.Join(phoneArr, ","))))
  150. if entUserArr != nil && len(*entUserArr) > 0 {
  151. for _, v := range *entUserArr {
  152. phone := gconv.String(v["phone"])
  153. personMap[phone]["mail"] = gconv.String(v["mail"])
  154. personMap[phone]["deptId"] = gconv.String(v["deptId"])
  155. }
  156. }
  157. //
  158. positionArrMap := JyTidb.SelectBySql(fmt.Sprintf(`select a.phone,b.id from
  159. base_user a
  160. INNER JOIN base_position b
  161. on b.ent_id=%d and a.phone in %s and b.user_id=a.id and b.type=1`,
  162. entId, fmt.Sprintf("(%s)", strings.Join(phoneArr, ","))))
  163. if positionArrMap != nil && len(*positionArrMap) > 0 {
  164. for _, v := range *positionArrMap {
  165. phone := gconv.String(v["phone"])
  166. positionid := gconv.Int64(v["id"])
  167. for k := range personMap {
  168. if k == phone {
  169. personMap[phone]["positionId"] = positionid
  170. positionArr = append(positionArr, gconv.String(positionid))
  171. }
  172. }
  173. }
  174. }
  175. //查询工单数量
  176. positionNumberMap := WorkOrder.SelectBySql(`SELECT
  177. current_position_id,
  178. COUNT( 1 ) AS count
  179. FROM
  180. order_work
  181. WHERE
  182. current_position_id IN (?)
  183. ORDER BY
  184. count`, strings.Join(positionArr, ","))
  185. if positionNumberMap != nil && len(*positionNumberMap) > 0 {
  186. positionId = gconv.Int64((*positionNumberMap)[0]["current_position_id"])
  187. } else {
  188. positionId = gconv.Int64(positionArr[0])
  189. }
  190. if positionId == 0 {
  191. positionId = gconv.Int64(positionArr[0])
  192. }
  193. //商机管理员
  194. deptId := int64(0)
  195. for _, v := range personMap {
  196. positionid := gconv.Int64(v["positionId"])
  197. if positionId == positionid {
  198. person = v
  199. deptId = gconv.Int64(v["deptId"])
  200. }
  201. }
  202. //本部门管理员查询
  203. depthMap := JyMysql.SelectBySql(`SELECT
  204. c.name as name,c.mail as mail
  205. FROM
  206. entniche_department_user a
  207. INNER JOIN entniche_user_role b ON a.dept_id = ?
  208. AND a.user_id = b.user_id
  209. AND b.role_id =2
  210. INNER JOIN entniche_user c ON a.user_id = c.id`, deptId)
  211. if depthMap != nil && len(*depthMap) > 0 {
  212. person["deptPersonName"] = gconv.String((*depthMap)[0]["name"])
  213. person["deptPersonMail"] = gconv.String((*depthMap)[0]["mail"])
  214. }
  215. //商机管理员查询
  216. superiorDepthMap := JyMysql.SelectBySql(`SELECT
  217. c.*
  218. FROM
  219. entniche_department d
  220. INNER JOIN entniche_department_user a ON d.id = ?
  221. AND d.pid = a.dept_id
  222. INNER JOIN entniche_user_role b ON a.user_id = b.user_id
  223. AND b.role_id = 1
  224. INNER JOIN entniche_user c ON a.user_id = c.id`, deptId)
  225. if superiorDepthMap != nil && len(*superiorDepthMap) > 0 {
  226. person["superiorDepthPersonName"] = gconv.String((*superiorDepthMap)[0]["name"])
  227. person["superiorDepthPersonMail"] = gconv.String((*superiorDepthMap)[0]["mail"])
  228. }
  229. return person
  230. }
  231. // 编号查询
  232. func FindNumber(moudle string) string {
  233. today := time.Now().Format("2006-01-02")
  234. yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
  235. key := fmt.Sprintf("%s_%s", today, moudle)
  236. yesterdayKey := fmt.Sprintf("%s_%s", yesterday, moudle)
  237. if ok, _ := redis.Exists("newother", yesterdayKey); ok {
  238. //删除之前数据
  239. redis.Del("newother", yesterdayKey)
  240. }
  241. count := redis.Incr("newother", key)
  242. return fmt.Sprintf("%04d", count)
  243. }