123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- package service
- import (
- "app.yhyue.com/moapp/jybase/common"
- "app.yhyue.com/moapp/jybase/date"
- "app.yhyue.com/moapp/jybase/mail"
- "app.yhyue.com/moapp/jybase/redis"
- . "bp.jydev.jianyu360.cn/BaseService/biService/entity"
- "bp.jydev.jianyu360.cn/BaseService/biService/rpc/biservice"
- "database/sql"
- "fmt"
- "github.com/gogf/gf/v2/util/gconv"
- "github.com/zeromicro/go-zero/core/logx"
- "log"
- "strings"
- "time"
- )
- func AddAcceptance(in *biservice.AcceptanceReq, productMap []ProductMap, entId int64) string {
- in.ParamData = strings.ReplaceAll(in.ParamData, "\n", "")
- nowTime := time.Now().Format(date.Date_Full_Layout)
- //编号处理
- is_clue := 1
- deptName := ""
- if in.DeptId != "" {
- //部门名称查询
- deptData := JyMysql.FindOne("entniche_department", map[string]interface{}{
- "id": in.DeptId,
- }, "name", "")
- if deptData != nil && len(*deptData) > 0 {
- deptName = gconv.String((*deptData)["name"])
- }
- }
- acceptance_no := fmt.Sprintf("SLD%s%s", time.Now().Format(date.Date_yyyyMMdd), FindNumber("sld"))
- acceptanceMap := map[string]interface{}{
- "acceptance_no": acceptance_no,
- "propose_type": in.ProposeType,
- "propose_time": in.ProposeTime,
- "channel": in.Channel,
- "acceptance_type": in.AcceptanceType,
- "status": in.Status,
- "initiator_name": in.EntUserName,
- "initiator_position_id": in.PositionId,
- "department_no": in.DeptId,
- "remark": in.Remark,
- "is_delete": 1,
- "creator_name": in.EntUserName,
- "creator_position_id": in.PositionId,
- "creator_time": nowTime,
- "department_name": deptName,
- }
- ok := WorkOrder.ExecTx("受理单处理", func(tx *sql.Tx) bool {
- //新增受理单子单
- childMap := gconv.Map(in.ParamData)
- if childMap != nil && len(childMap) > 0 {
- phone := ""
- company := ""
- innerArr := []interface{}{}
- innerStr := []string{"acceptance_no", "field_name", "field_value", "creator_name", "creator_position_id", "creator_time"}
- for k, v := range childMap {
- innerArr = append(innerArr, acceptance_no)
- innerArr = append(innerArr, k)
- innerArr = append(innerArr, v)
- innerArr = append(innerArr, in.EntUserName)
- innerArr = append(innerArr, in.PositionId)
- innerArr = append(innerArr, nowTime)
- switch k {
- case "公司名称":
- company = gconv.String(v)
- case "联系方式num":
- phone = gconv.String(v)
- }
- }
- if len(innerArr) > 0 {
- ok2, ok3 := WorkOrder.InsertBatchByTx(tx, "order_acceptance_children", innerStr, innerArr)
- if ok2 <= 0 && ok3 <= 0 {
- return false
- }
- }
- //判断是否创建工单
- selectedProductMap := map[string]SelectProductMap{}
- productArr := []string{}
- if _, isOk := childMap["咨询产品"]; isOk {
- for _, v := range strings.Split(gconv.String(childMap["咨询产品"]), ",") {
- for _, product := range productMap {
- if strings.Contains(product.Product, v) {
- selectedProduct := selectedProductMap[product.ProductCode]
- selectedProduct.PersonArr = product.PersonArr
- selectedProduct.Product = append(selectedProduct.Product, v)
- productArr = append(productArr, v)
- selectedProductMap[product.ProductCode] = selectedProduct
- continue
- }
- }
- }
- if len(strings.Split(gconv.String(childMap["咨询产品"]), ",")) != len(productArr) {
- is_clue = 2
- }
- }
- for k, v := range selectedProductMap {
- if !AddOrderWork(k, acceptance_no, nowTime, phone, company, tx, in, v, entId) {
- return false
- }
- }
- }
- //先新增受理单主单
- acceptanceMap["is_clue"] = is_clue
- ok1 := WorkOrder.InsertByTx(tx, "order_acceptance", acceptanceMap)
- if ok1 <= 0 {
- logx.Info("受理单创建失败")
- return false
- }
- //工单处理
- return true
- })
- if ok {
- return acceptance_no
- }
- return ""
- }
- func AddOrderWork(orderType, acceptance_no, nowTime, phone, company string, tx *sql.Tx, in *biservice.AcceptanceReq, selectPersonMap SelectProductMap, entId int64) bool {
- dkPerson := ""
- dkdeptName := ""
- dkdeptId := ""
- dkPositionId := int64(0)
- personMap := map[string]interface{}{}
- personMap = Findcandidate(selectPersonMap.PersonArr, entId, orderType)
- dkPositionId = gconv.Int64(personMap["positionId"])
- dkPerson = gconv.String(personMap["name"])
- dkdeptId = gconv.String(personMap["deptId"])
- dkdeptName = gconv.String(personMap["deptName"])
- if dkPositionId != 0 {
- work_order_no := fmt.Sprintf("GD%s%s", time.Now().Format(date.Date_yyyyMMdd), FindNumber("gd"))
- orderWorkMap := map[string]interface{}{
- "work_order_no": work_order_no,
- "acceptance_no": acceptance_no,
- "type": strings.Join(selectPersonMap.Product, ","),
- "status": 1,
- "initiator_name": in.EntUserName,
- "initiator_position_id": in.PositionId,
- "current_name": dkPerson,
- "current_position_id": dkPositionId,
- "is_delete": 1,
- "creator_name": in.EntUserName,
- "creator_position_id": in.PositionId,
- "creator_time": nowTime,
- "two_type": orderType,
- "department_no": dkdeptId,
- "department_name": dkdeptName,
- "update_time": nil,
- }
- logx.Info(orderWorkMap, "11111", selectPersonMap)
- ok3 := WorkOrder.InsertByTx(tx, "order_work", orderWorkMap)
- if ok3 <= 0 {
- log.Println("工单保存失败")
- return false
- }
- //发送邮件
- //日志添加
- approvalRecordMap := map[string]interface{}{
- "work_order_no": work_order_no,
- "status": 1,
- "new_status": nil,
- "handle_name": dkPerson,
- "handle_position_id": dkPositionId,
- "handle_dept_id": dkdeptId,
- "handle_dept_name": dkdeptName,
- "creator_name": in.EntUserName,
- "creator_position_id": in.PositionId,
- "is_delete": 1,
- "creator_time": nowTime,
- }
- ok4 := WorkOrder.InsertByTx(tx, "approval_record", approvalRecordMap)
- if ok4 <= 0 {
- log.Println("工单记录保存失败")
- return false
- }
- log.Println(personMap)
- log.Println(GmailAuth, personMap, strings.Join(selectPersonMap.Product, ","), dkPerson, in.EntUserName, nowTime, work_order_no, phone, company)
- WorkMail(GmailAuth, personMap, strings.Join(selectPersonMap.Product, ","), dkPerson, in.EntUserName, nowTime, work_order_no, phone, company)
- }
- return true
- }
- // 大客人员选择
- func Findcandidate(personArr []Person, entId int64, orderType string) map[string]interface{} {
- person := map[string]interface{}{}
- positionArr := []string{}
- personMap := map[string]map[string]interface{}{}
- positionId := int64(0)
- phoneArr := []string{}
- personNameArr := []string{}
- for _, v := range personArr {
- phoneArr = append(phoneArr, fmt.Sprintf(`"%s"`, v.Phone))
- personNameArr = append(personNameArr, fmt.Sprintf(`"%s"`, v.Name))
- personMap[gconv.String(v.Phone)] = map[string]interface{}{
- "name": gconv.String(v.Name),
- }
- }
- //个人邮箱,部门信息获取
- entUserArr := JyMysql.SelectBySql(fmt.Sprintf(`SELECT
- a.name as name,a.mail as mail,b.dept_id as deptId,a.phone,c.name as deptName
- FROM
- entniche_user a
- INNER JOIN entniche_department_user b ON a.ent_id =%d
- AND a.phone IN %s
- and a.id=b.user_id
- inner join entniche_department c on b.dept_id=c.id
- `, entId, fmt.Sprintf("(%s)", strings.Join(phoneArr, ","))))
- if entUserArr != nil && len(*entUserArr) > 0 {
- for _, v := range *entUserArr {
- phone := gconv.String(v["phone"])
- personMap[phone]["mail"] = gconv.String(v["mail"])
- personMap[phone]["deptId"] = gconv.String(v["deptId"])
- personMap[phone]["deptName"] = gconv.String(v["deptName"])
- }
- }
- //
- positionArrMap := JyTidb.SelectBySql(fmt.Sprintf(`select a.phone,b.id from
- base_user a
- INNER JOIN base_position b
- on b.ent_id=%d and a.phone in %s and b.user_id=a.id and b.type=1`,
- entId, fmt.Sprintf("(%s)", strings.Join(phoneArr, ","))))
- if positionArrMap != nil && len(*positionArrMap) > 0 {
- for _, v := range *positionArrMap {
- phone := gconv.String(v["phone"])
- positionid := gconv.Int64(v["id"])
- for k := range personMap {
- if k == phone {
- personMap[phone]["positionId"] = positionid
- positionArr = append(positionArr, gconv.String(positionid))
- }
- }
- }
- }
- //查询工单数量
- positionNumberMap := WorkOrder.SelectBySql(`SELECT
- current_position_id,
- COUNT( 1 ) AS count
- FROM
- order_work
- WHERE
- FIND_IN_SET(current_position_id , ?) and two_type=?
- GROUP BY current_position_id
- ORDER BY
- count ,FIELD(current_name,?) asc `, strings.Join(positionArr, ","), orderType, strings.Join(personNameArr, ","))
- if positionNumberMap != nil && len(*positionNumberMap) == len(personArr) {
- positionId = gconv.Int64((*positionNumberMap)[0]["current_position_id"])
- } else if positionNumberMap != nil && len(*positionNumberMap) != len(personArr) {
- for _, v := range positionArr {
- count := int64(0)
- for _, v1 := range *positionNumberMap {
- id := gconv.String(v1["current_position_id"])
- if id == v {
- count = gconv.Int64(v1["count"])
- }
- }
- if count == 0 {
- positionId = gconv.Int64(v)
- break
- }
- }
- } else {
- positionId = gconv.Int64(positionArr[0])
- }
- if positionId == 0 {
- positionId = gconv.Int64(positionArr[0])
- }
- //商机管理员
- deptId := int64(0)
- for _, v := range personMap {
- positionid := gconv.Int64(v["positionId"])
- if positionId == positionid {
- person = v
- deptId = gconv.Int64(v["deptId"])
- }
- }
- //本部门管理员查询
- depthMap := JyMysql.SelectBySql(`SELECT
- c.name as name,c.mail as mail
- FROM
- entniche_department_user a
- INNER JOIN entniche_user_role b ON a.dept_id = ?
- AND a.user_id = b.user_id
- AND b.role_id !=""
- INNER JOIN entniche_user c ON a.user_id = c.id`, deptId)
- if depthMap != nil && len(*depthMap) > 0 {
- person["deptPersonName"] = gconv.String((*depthMap)[0]["name"])
- person["deptPersonMail"] = gconv.String((*depthMap)[0]["mail"])
- }
- //商机管理员查询
- superiorDepthMap := JyMysql.SelectBySql(`SELECT
- c.*
- FROM
- entniche_department d
- INNER JOIN entniche_department_user a ON d.id = ?
- AND d.pid = a.dept_id
- INNER JOIN entniche_user_role b ON a.user_id = b.user_id
- AND b.role_id !=""
- INNER JOIN entniche_user c ON a.user_id = c.id`, deptId)
- if superiorDepthMap != nil && len(*superiorDepthMap) > 0 {
- person["superiorDepthPersonName"] = gconv.String((*superiorDepthMap)[0]["name"])
- person["superiorDepthPersonMail"] = gconv.String((*superiorDepthMap)[0]["mail"])
- }
- return person
- }
- // 编号查询
- func FindNumber(moudle string) string {
- today := time.Now().Format("2006-01-02")
- yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
- key := fmt.Sprintf("%s_%s", today, moudle)
- yesterdayKey := fmt.Sprintf("%s_%s", yesterday, moudle)
- if ok, _ := redis.Exists("newother", yesterdayKey); ok {
- //删除之前数据
- redis.Del("newother", yesterdayKey)
- }
- count := redis.Incr("newother", key)
- log.Println("编号获取", moudle, fmt.Sprintf("%04d", count))
- return fmt.Sprintf("%04d", count)
- }
- func WorkMail(gmailAuth []*mail.GmailAuth, personMap map[string]interface{}, productStr string, personName1, personName2, createTimeStr, acceptance_no, phone, company string) {
- orderType := fmt.Sprintf(`客户咨询线索(%s)`, productStr)
- title := fmt.Sprintf("%s通知", orderType)
- if personName1 == gconv.String(personMap["deptPersonName"]) {
- personMap["deptPersonMail"] = ""
- }
- if personName1 == gconv.String(personMap["superiorDepthPersonName"]) {
- personMap["superiorDepthPersonMail"] = ""
- }
- 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))))
- toMail := gconv.String(personMap["mail"])
- mailArr := []string{}
- if gconv.String(common.If(gconv.String(personMap["deptPersonMail"]) == "", "", gconv.String(personMap["deptPersonMail"]))) != "" {
- mailArr = append(mailArr, gconv.String(common.If(gconv.String(personMap["deptPersonMail"]) == "", "", gconv.String(personMap["deptPersonMail"]))))
- }
- if gconv.String(common.If(gconv.String(personMap["superiorDepthPersonMail"]) == "", "", gconv.String(personMap["superiorDepthPersonMail"]))) != "" {
- mailArr = append(mailArr, gconv.String(common.If(gconv.String(personMap["superiorDepthPersonMail"]) == "", "", gconv.String(personMap["superiorDepthPersonMail"]))))
- }
- toCc := strings.Join(mailArr, ",")
- if len(mailArr) > 0 {
- toMail = fmt.Sprintf("%s|%s", toMail, toCc)
- }
- log.Println(toMail, "", title, content)
- for k, v := range gmailAuth {
- fool := mail.GSendMail_q("剑鱼标讯", toMail, "", "", title, content, "", "", v)
- if fool {
- logx.Info(toMail, fmt.Sprintf("使用%s发送邮件成功", v.User))
- break
- }
- if k < len(gmailAuth)-1 {
- logx.Info(toMail, fmt.Sprintf("使用%s发送邮件失败!3s后使用其他邮箱尝试", v.User))
- } else {
- logx.Info(toMail, fmt.Sprintf("使用%s发送邮件失败!", v.User))
- }
- time.Sleep(time.Second * 3)
- }
- }
|