浏览代码

提取基础字段信息

boweniac 1 周之前
父节点
当前提交
ba27248607
共有 2 个文件被更改,包括 209 次插入22 次删除
  1. 205 21
      crontask/contact_form.go
  2. 4 1
      internal/logic/sop_task/get_sop_task_record_list_logic.go

+ 205 - 21
crontask/contact_form.go

@@ -7,6 +7,7 @@ import (
 	"github.com/google/uuid"
 	"github.com/zeromicro/go-zero/core/logx"
 	"net/http"
+	"strconv"
 	"time"
 	"wechat-api/ent/contact"
 	"wechat-api/ent/contactfield"
@@ -40,6 +41,92 @@ type FormData struct {
 func (l *CronTask) analyze() {
 	usageDetails := make(map[string]map[string]string)
 	contactFieldTemplates := make(map[string][]custom_types.ContactFieldTemplate)
+	template_type_text := "text"
+	template_type_radio := "radio"
+	template_type_date := "date"
+	template_sex_id := "sex"
+	template_sex_label := "性别"
+	template_sex_options_unknown_label := "未知"
+	template_sex_options_unknown_value := "未知"
+	template_sex_options_man_label := "男"
+	template_sex_options_man_value := "男"
+	template_sex_options_woman_label := "女"
+	template_sex_options_woman_value := "男"
+
+	template_phone_id := "phone"
+	template_phone_label := "手机号"
+
+	template_name_id := "name"
+	template_name_label := "姓名"
+
+	template_age_id := "age"
+	template_age_label := "年龄(以字符串形式返回阿拉伯数字)"
+
+	template_area_id := "area"
+	template_area_label := "地区"
+
+	template_birthday_id := "birthday"
+	template_birthday_label := "出生日期"
+
+	template_birtharea_id := "birtharea"
+	template_birtharea_label := "出生地"
+
+	template_idcard_no_id := "idcard_no"
+	template_idcard_no_label := "身份证号"
+
+	template_title_id := "title"
+	template_title_label := "称呼"
+	contactBasicFieldTemplates := []custom_types.ContactFieldTemplate{
+		{
+			Label: &template_sex_label,
+			Id:    &template_sex_id,
+			Type:  &template_type_radio,
+			Options: []custom_types.ContactFieldTemplateOptions{
+				{
+					Label: &template_sex_options_unknown_label,
+					Value: &template_sex_options_unknown_value,
+				}, {
+					Label: &template_sex_options_man_label,
+					Value: &template_sex_options_man_value,
+				}, {
+					Label: &template_sex_options_woman_label,
+					Value: &template_sex_options_woman_value,
+				},
+			},
+		}, {
+			Label: &template_phone_label,
+			Id:    &template_phone_id,
+			Type:  &template_type_text,
+		}, {
+			Label: &template_name_label,
+			Id:    &template_name_id,
+			Type:  &template_type_text,
+		}, {
+			Label: &template_age_label,
+			Id:    &template_age_id,
+			Type:  &template_type_text,
+		}, {
+			Label: &template_area_label,
+			Id:    &template_area_id,
+			Type:  &template_type_text,
+		}, {
+			Label: &template_birthday_label,
+			Id:    &template_birthday_id,
+			Type:  &template_type_date,
+		}, {
+			Label: &template_birtharea_label,
+			Id:    &template_birtharea_id,
+			Type:  &template_type_text,
+		}, {
+			Label: &template_idcard_no_label,
+			Id:    &template_idcard_no_id,
+			Type:  &template_type_text,
+		}, {
+			Label: &template_title_label,
+			Id:    &template_title_id,
+			Type:  &template_type_text,
+		},
+	}
 
 	var predicates []predicate.UsageDetail
 	predicates = append(predicates, usagedetail.TypeIn(1, 3))
@@ -81,7 +168,9 @@ func (l *CronTask) analyze() {
 	logx.Info("usageDetails: ", usageDetails)
 	for botID, template := range contactFieldTemplates {
 		if template == nil {
-			continue
+			template = contactBasicFieldTemplates
+		} else {
+			template = append(template, contactBasicFieldTemplates...)
 		}
 		for receiverID, messages := range usageDetails[botID] {
 			result, _ := openaiRequest(messages, template)
@@ -145,37 +234,123 @@ func openaiRequest(messages string, template []custom_types.ContactFieldTemplate
 }
 
 func (l *CronTask) UpdateContactFields(botID string, receiverID string, fields []ResponseItem) error {
-	logx.Infof("botID: ", botID)
-	logx.Infof("receiverID: ", receiverID)
-	logx.Infof("fields: ", fields)
+	basic_ids := []string{"sex", "phone", "name", "age", "area", "birthday", "birtharea", "idcard_no", "title"}
 	c, _ := l.svcCtx.DB.Contact.Query().Where(contact.WxWxidEQ(botID), contact.WxidEQ(receiverID)).First(l.ctx)
-	logx.Infof("c: ", c)
 	if c == nil {
 		return fmt.Errorf("Contact not find")
 	}
 	for _, field := range fields {
-		f, _ := l.svcCtx.DB.ContactField.Query().Where(contactfield.ContactID(c.ID), contactfield.FormID(field.DataIndex)).First(l.ctx)
-		if f == nil {
-			if field.Value != nil && len(field.Value) > 0 {
-				_, err := l.svcCtx.DB.ContactField.Create().
-					SetContactID(c.ID).
-					SetFormID(field.DataIndex).
-					SetValue(field.Value).
+		if contains(basic_ids, field.DataIndex) {
+			if len(field.Value) == 0 {
+				continue
+			}
+			value := 0
+			if field.DataIndex == "sex" {
+				if field.Value[0] == "男" {
+					value = 1
+				} else if field.Value[0] == "女" {
+					value = 2
+				}
+				_, err := l.svcCtx.DB.Contact.Update().
+					Where(contact.WxidEQ(receiverID)).
+					SetSex(value).
 					Save(l.ctx)
 				if err != nil {
-					return err
+					continue
 				}
-			}
-		} else {
-			if field.Value != nil {
-				if len(field.Value) == 0 {
-					field.Value = []string{""}
+			} else if field.DataIndex == "phone" {
+				_, err := l.svcCtx.DB.Contact.Update().
+					Where(contact.WxidEQ(receiverID)).
+					SetPhone(field.Value[0]).
+					Save(l.ctx)
+				if err != nil {
+					continue
+				}
+			} else if field.DataIndex == "name" {
+				_, err := l.svcCtx.DB.Contact.Update().
+					Where(contact.WxidEQ(receiverID)).
+					SetCname(field.Value[0]).
+					Save(l.ctx)
+				if err != nil {
+					continue
+				}
+			} else if field.DataIndex == "age" {
+				num, err := strconv.Atoi(field.Value[0])
+				if err != nil {
+					continue
 				}
-				_, err := l.svcCtx.DB.ContactField.UpdateOneID(f.ID).
-					SetValue(field.Value).
+				_, err = l.svcCtx.DB.Contact.Update().
+					Where(contact.WxidEQ(receiverID)).
+					SetCage(num).
 					Save(l.ctx)
 				if err != nil {
-					return err
+					continue
+				}
+			} else if field.DataIndex == "area" {
+				_, err := l.svcCtx.DB.Contact.Update().
+					Where(contact.WxidEQ(receiverID)).
+					SetCarea(field.Value[0]).
+					Save(l.ctx)
+				if err != nil {
+					continue
+				}
+			} else if field.DataIndex == "birthday" {
+				_, err := l.svcCtx.DB.Contact.Update().
+					Where(contact.WxidEQ(receiverID)).
+					SetCbirthday(field.Value[0]).
+					Save(l.ctx)
+				if err != nil {
+					continue
+				}
+			} else if field.DataIndex == "birtharea" {
+				_, err := l.svcCtx.DB.Contact.Update().
+					Where(contact.WxidEQ(receiverID)).
+					SetCbirtharea(field.Value[0]).
+					Save(l.ctx)
+				if err != nil {
+					continue
+				}
+			} else if field.DataIndex == "idcard_no" {
+				_, err := l.svcCtx.DB.Contact.Update().
+					Where(contact.WxidEQ(receiverID)).
+					SetCidcardNo(field.Value[0]).
+					Save(l.ctx)
+				if err != nil {
+					continue
+				}
+			} else if field.DataIndex == "title" {
+				_, err := l.svcCtx.DB.Contact.Update().
+					Where(contact.WxidEQ(receiverID)).
+					SetCtitle(field.Value[0]).
+					Save(l.ctx)
+				if err != nil {
+					continue
+				}
+			}
+		} else {
+			f, _ := l.svcCtx.DB.ContactField.Query().Where(contactfield.ContactID(c.ID), contactfield.FormID(field.DataIndex)).First(l.ctx)
+			if f == nil {
+				if field.Value != nil && len(field.Value) > 0 {
+					_, err := l.svcCtx.DB.ContactField.Create().
+						SetContactID(c.ID).
+						SetFormID(field.DataIndex).
+						SetValue(field.Value).
+						Save(l.ctx)
+					if err != nil {
+						continue
+					}
+				}
+			} else {
+				if field.Value != nil {
+					if len(field.Value) == 0 {
+						field.Value = []string{""}
+					}
+					_, err := l.svcCtx.DB.ContactField.UpdateOneID(f.ID).
+						SetValue(field.Value).
+						Save(l.ctx)
+					if err != nil {
+						continue
+					}
 				}
 			}
 		}
@@ -204,3 +379,12 @@ func ConvertFormData(input []custom_types.ContactFieldTemplate) []FormData {
 	}
 	return result
 }
+
+func contains(strs []string, target string) bool {
+	for _, s := range strs {
+		if s == target {
+			return true
+		}
+	}
+	return false
+}

+ 4 - 1
internal/logic/sop_task/get_sop_task_record_list_logic.go

@@ -62,9 +62,12 @@ func (l *GetSopTaskRecordListLogic) SopTaskRecord(sourceType int, sourceId uint6
 	stageSuccessCount, _ := l.svcCtx.DB.MessageRecords.Query().
 		Where(messagerecords.SourceTypeEQ(sourceType), messagerecords.SourceID(sourceId), messagerecords.Status(3)).
 		Count(l.ctx)
+	stageFailureCount, _ := l.svcCtx.DB.MessageRecords.Query().
+		Where(messagerecords.SourceTypeEQ(sourceType), messagerecords.SourceID(sourceId), messagerecords.Status(4)).
+		Count(l.ctx)
 	totalCountInt64 := int64(stageTotalCount)
 	successCountInt64 := int64(stageSuccessCount)
-	failureCount := totalCountInt64 - successCountInt64
+	failureCount := int64(stageFailureCount)
 	successRate := int64(0)
 	if totalCountInt64 != 0 {
 		successRate = successCountInt64 * 100 / totalCountInt64