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, } log.Println(approvalRecordMap) 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 } type PersonJson struct { Name string Phone string Mail string DeptId int64 DeptName string PositionId int64 IsResign bool } // 大客人员选择 func FindCandidate(personArr []Person, entId int64, orderType string) map[string]interface{} { person := map[string]interface{}{} personEntity := PersonJson{} personMap := make(map[string]map[string]interface{}) phoneArr := make([]string, len(personArr)) persons := make([]PersonJson, len(personArr)) // Populate phoneArr and personMap for k, v := range personArr { phone := gconv.String(v.Phone) phoneArr[k] = fmt.Sprintf(`"%s"`, phone) personMap[phone] = map[string]interface{}{ "name": gconv.String(v.Name), } persons[k] = PersonJson{ Name: v.Name, Phone: v.Phone, IsResign: v.IsResign, } } // Fetch personal email and department information entUserArr := JyMysql.SelectBySql(fmt.Sprintf(` SELECT a.name, a.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 { 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"]) } } // Fetch position information positionArrMap := JyMysql.SelectBySql(fmt.Sprintf(` SELECT a.phone, b.id FROM base_service.base_user a 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 `, entId, fmt.Sprintf("(%s)", strings.Join(phoneArr, ",")))) if positionArrMap != nil { for _, v := range *positionArrMap { phone := gconv.String(v["phone"]) personMap[phone]["positionId"] = gconv.Int64(v["id"]) } } // Update personArr with additional information for k, v := range persons { phone := v.Phone if info, exists := personMap[phone]; exists { persons[k].Mail = gconv.String(info["mail"]) persons[k].DeptId = gconv.Int64(info["deptId"]) persons[k].DeptName = gconv.String(info["deptName"]) persons[k].PositionId = gconv.Int64(info["positionId"]) } } // Query the last person who created an order orderWorkMap := WorkOrder.SelectBySql(fmt.Sprintf(` SELECT * FROM order_work WHERE two_type = "%s" ORDER BY creator_time DESC LIMIT 1 `, orderType)) var k int if orderWorkMap == nil || len(*orderWorkMap) == 0 { k = findNextPersonIndex(persons, 0) } else { currentName := gconv.String((*orderWorkMap)[0]["current_name"]) k = findPersonIndexByName(persons, currentName) k = findNextPersonIndex(persons, k) } personEntity = persons[k] // Set person details person["positionId"] = personEntity.PositionId person["deptName"] = personEntity.DeptName person["deptId"] = personEntity.DeptId person["name"] = personEntity.Name person["mail"] = personEntity.Mail // Fetch department admin fetchDeptAdmin(person, personEntity.DeptId) // Fetch superior admin fetchSuperiorAdmin(person, personEntity.DeptId) return person } // Helper function to find the next person index func findNextPersonIndex(personArr []PersonJson, startIndex int) int { n := len(personArr) // 如果当前索引是最后一个,则从 0 开始 if startIndex >= n-1 { startIndex = -1 // 设置为 -1 以便在下次循环中变为 0 } for count := 0; count < n; count++ { // 限制最大循环次数 startIndex++ // 先加 1 if startIndex >= n { startIndex = 0 // 如果超出范围,则重置为 0 } if !personArr[startIndex].IsResign { return startIndex // 找到未辞职的员工,返回索引 } } return 0 // 如果没有找到,返回 -1 } // Helper function to find a person's index by name func findPersonIndexByName(personArr []PersonJson, name string) int { for i, v := range personArr { if v.Name == name { return i } } return 0 // Default to the first index if not found } // Fetch department admin details func fetchDeptAdmin(person map[string]interface{}, deptId int64) { deptMap := 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 deptMap != nil && len(*deptMap) > 0 { person["deptPersonName"] = gconv.String((*deptMap)[0]["name"]) person["deptPersonMail"] = gconv.String((*deptMap)[0]["mail"]) } } // Fetch superior admin details func fetchSuperiorAdmin(person map[string]interface{}, deptId int64) { superiorMap := 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 superiorMap != nil && len(*superiorMap) > 0 { person["superiorDepthPersonName"] = gconv.String((*superiorMap)[0]["name"]) person["superiorDepthPersonMail"] = gconv.String((*superiorMap)[0]["mail"]) } } // 编号查询 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) } }