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, DkPersonMap []map[string]interface{}, entId int64, productMap map[string]string) 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 } } //判断是否创建工单 dkPerson := "" dkdeptName := "" dkdeptId := "" dkProduct := []string{} dkPositionId := int64(0) personMap := map[string]interface{}{} if _, isOk := childMap["咨询产品"]; isOk { for _, v := range strings.Split(gconv.String(childMap["咨询产品"]), ",") { switch productMap[v] { case "dk": dkProduct = append(dkProduct, v) if dkPositionId == 0 { personMap = Findcandidate(DkPersonMap, entId) dkPositionId = gconv.Int64(personMap["positionId"]) dkPerson = gconv.String(personMap["name"]) dkdeptId = gconv.String(personMap["deptId"]) dkdeptName = gconv.String(personMap["deptName"]) } continue default: is_clue = 2 } } } 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(dkProduct, ","), "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": "dk", "department_no": dkdeptId, "department_name": dkdeptName, "update_time": nil, } ok3 := WorkOrder.InsertByTx(tx, "order_work", orderWorkMap) if ok3 <= 0 { 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, } WorkOrder.InsertByTx(tx, "approval_record", approvalRecordMap) log.Println(personMap) WorkMail(GmailAuth, personMap, strings.Join(dkProduct, ","), dkPerson, in.EntUserName, nowTime, work_order_no, phone, company) } } //先新增受理单主单 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 Findcandidate(DkPersonMap []map[string]interface{}, entId int64) map[string]interface{} { person := map[string]interface{}{} positionArr := []string{} personMap := map[string]map[string]interface{}{} positionId := int64(0) phoneArr := []string{} for _, v := range DkPersonMap { phoneArr = append(phoneArr, fmt.Sprintf(`"%s"`, gconv.String(v["phone"]))) personMap[gconv.String(v["phone"])] = map[string]interface{}{ "name": gconv.String(v["personName"]), } } //个人邮箱,部门信息获取 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 , ?) GROUP BY current_position_id ORDER BY count`, strings.Join(positionArr, ",")) if positionNumberMap != nil && len(*positionNumberMap) == len(DkPersonMap) { positionId = gconv.Int64((*positionNumberMap)[0]["current_position_id"]) } else if positionNumberMap != nil && len(*positionNumberMap) != len(DkPersonMap) { 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) /*toMail = "wanghao@topnet.net.cn" toCc = "wanghao@topnet.net.cn"*/ 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) } }