Files

104 lines
2.6 KiB
Go

package handlers
import (
"context"
"encoding/json"
"fmt"
"lone-services/pkg/utils"
"lone-services/pkg/wecom/message"
"lone-services/services/wecom/internal/dao"
"lone-services/services/wecom/internal/logic/message/builder"
msgutil "lone-services/services/wecom/internal/logic/message/utils"
)
type AddressNotifySecondHandler struct{}
func (h AddressNotifySecondHandler) Build(ctx context.Context, job dao.WeComJob) ([]*message.SendRequest, error) {
change, err := msgutil.GetLatestReviewChange(job.BizType, int64(job.BizId))
if err != nil {
return nil, err
}
fields := []dao.FieldConfig{
{Field: "type_name", Label: "门店名称"},
{Field: "name", Label: "收货人"},
{Field: "address", Label: "地址"},
{Field: "district", Label: "地区"},
{Field: "mobile", Label: "收货人电话"},
}
content, err := BuildAddressDiff(msgutil.DiffContent(change), fields)
if err != nil {
return nil, err
}
card := builder.TextCardParams{
ToUser: utils.GetConfigString("wecom.address_reviewer_ids"),
Desc: "地址通知",
Title: "地址审核通知",
Content: content,
}
req, err := builder.TextCard(card)
if err != nil {
return nil, err
}
return []*message.SendRequest{req}, nil
}
func BuildAddressDiff(content string, fields []dao.FieldConfig) ([]message.HorizontalContent, error) {
var data struct {
Old map[string]interface{} `json:"old"`
New map[string]interface{} `json:"new"`
}
if err := json.Unmarshal([]byte(content), &data); err != nil {
return nil, err
}
result := make([]message.HorizontalContent, 0)
districtOld := msgutil.GetMapString(data.Old, "district")
districtNew := msgutil.GetMapString(data.New, "district")
for _, field := range fields {
if field.Field == "district" {
continue
}
oldValue := msgutil.GetMapString(data.Old, field.Field)
newValue := msgutil.GetMapString(data.New, field.Field)
if field.Field == "type_name" && newValue == utils.StringEmpty {
newValue = oldValue
}
if field.Field == "address" {
if districtOld != utils.StringEmpty {
oldValue = districtOld + oldValue
}
if districtNew != utils.StringEmpty {
newValue = districtNew + newValue
}
}
if field.Field == "mobile" && oldValue != utils.StringEmpty {
if decryptOld, err := utils.Decrypt(oldValue); err == nil {
oldValue = decryptOld
}
}
value := newValue
if oldValue != utils.StringEmpty &&
oldValue != utils.StringZero &&
oldValue != newValue {
value = fmt.Sprintf("%s → %s", oldValue, newValue)
}
result = append(result, message.HorizontalContent{
KeyName: field.Label,
Value: value,
})
}
return result, nil
}