fonchain-fiee/pkg/service/import/binding.go
2025-08-11 13:29:49 +08:00

200 lines
5.9 KiB
Go

package imports
import (
"context"
"errors"
"fmt"
"fonchain-fiee/api/accountFiee"
account "fonchain-fiee/api/accountFiee"
"fonchain-fiee/api/bundle"
apiCast "fonchain-fiee/api/cast"
"fonchain-fiee/cmd/config"
"fonchain-fiee/pkg/e"
modelCast "fonchain-fiee/pkg/model/cast"
"fonchain-fiee/pkg/service"
"fonchain-fiee/pkg/service/cast"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/gin-gonic/gin"
"github.com/xuri/excelize/v2"
)
type artu struct {
}
func ImportBind(c *gin.Context) {
var failedRecords []failedRecord
// 1. 上传文件
excelFile, err := c.FormFile("excel")
if err != nil {
c.JSON(400, gin.H{"error": "缺少 Excel 文件 excel"})
return
}
// 2. 保存临时文件
tempDir := "tmp"
os.MkdirAll(tempDir, 0755)
excelPath := filepath.Join(tempDir, "artists.xlsx")
if err = c.SaveUploadedFile(excelFile, excelPath); err != nil {
c.JSON(500, gin.H{"error": "保存 Excel 失败"})
return
}
defer os.RemoveAll(tempDir)
// 3. 读取 Excel 画家名单
artists, err := readArtistAccountInfo(excelPath)
if err != nil {
c.JSON(500, gin.H{"error": "读取 Excel 失败"})
return
}
// 4. 绑定账号
for _, artist := range artists {
for accountType, accountInfo := range artist.Account {
if artist.Name == "韩风霞" || artist.Name == "荣小松" {
if accountType == 2 {
continue
}
}
res, err := service.AccountFieeProvider.UserList(context.Background(), &account.UserListRequest{
Name: artist.Name,
})
if err != nil {
failedRecords = append(failedRecords, failedRecord{
name: artist.Name,
msg: fmt.Sprintf("获取用户信息失败: %s", err.Error()),
})
log.Printf(fmt.Sprintf("获取用户信息失败: %s", err.Error()))
continue
}
if res != nil && len(res.UserList) > 0 {
var infoResp *account.UserInfoResponse
var err error
// 查询艺人的信息
if config.AppConfig.System.AppMode != "dev" {
infoResp, err = service.AccountFieeProvider.Info(context.Background(), &accountFiee.InfoRequest{
ID: res.UserList[0].Id,
Domain: "app",
})
if err != nil {
failedRecords = append(failedRecords, failedRecord{
name: artist.Name,
msg: fmt.Sprintf("查询艺人的信息失败: %s", err.Error()),
})
log.Printf(fmt.Sprintf("查询艺人的信息失败: %s", err.Error()))
continue
}
} else {
infoResp = &accountFiee.UserInfoResponse{
Name: "小波",
TelNum: "18288888888",
}
}
if _, ok := apiCast.PlatformIDENUM_name[int32(accountType)]; !ok {
failedRecords = append(failedRecords, failedRecord{
name: artist.Name,
msg: errors.New("查询平台信息失败:").Error(),
})
log.Printf(errors.New("查询平台信息失败:").Error())
continue
}
if err = cast.CheckUserBundleBalance(int32(res.UserList[0].Id), modelCast.BalanceTypeAccountValue); err != nil {
failedRecords = append(failedRecords, failedRecord{
name: artist.Name,
msg: fmt.Sprintf("查询检查用户账户数量失败: %s", err.Error()),
})
log.Printf(fmt.Sprintf("查询检查用户账户数量失败: %s", err.Error()))
continue
}
_, err = service.BundleProvider.AddBundleBalance(context.Background(), &bundle.AddBundleBalanceReq{
UserId: int32(res.UserList[0].Id),
AccountConsumptionNumber: 1,
})
if err != nil {
failedRecords = append(failedRecords, failedRecord{
name: artist.Name,
msg: fmt.Sprintf("增加账户数量失败: %s", err.Error()),
})
log.Printf(fmt.Sprintf("增加账户数量失败: %s", err.Error()))
continue
}
_, err = service.CastProvider.UpdateMediaAccount(c, &apiCast.UpdateMediaAccountReq{
ArtistUuid: strconv.FormatUint(res.UserList[0].Id, 10),
PlatformID: accountType,
PlatformUserName: artist.Name,
PlatformUserID: accountInfo,
ArtistName: infoResp.Name,
ArtistPhone: infoResp.TelNum,
ArtistPhoneAreaCode: infoResp.TelAreaCode,
})
if err != nil {
failedRecords = append(failedRecords, failedRecord{
name: artist.Name,
msg: fmt.Sprintf("绑定账户信息失败: %s", err.Error()),
})
log.Printf(fmt.Sprintf("绑定账户信息失败: %s", err.Error()))
_, err = service.BundleProvider.AddBundleBalance(context.Background(), &bundle.AddBundleBalanceReq{
UserId: int32(res.UserList[0].Id),
AccountConsumptionNumber: -1,
})
failedRecords = append(failedRecords, failedRecord{
name: artist.Name,
msg: fmt.Sprintf("绑定失败后减少余额失败: %s", err.Error()),
})
log.Println(fmt.Sprintf("绑定失败后减少余额失败: %s", err.Error()), errors.New(e.GetMsg(e.InvalidParams)))
continue
}
}
}
}
// 5. 返回结果
service.Success(c, failedRecords)
}
func readArtistAccountInfo(excelPath string) ([]ArtistAccount, error) {
f, err := excelize.OpenFile(excelPath)
if err != nil {
return nil, err
}
defer f.Close()
sheetName := f.GetSheetName(0)
rows, err := f.GetRows(sheetName)
if err != nil {
return nil, err
}
log.Println("start read excel...")
var artists []ArtistAccount
for i, row := range rows {
tmp := ArtistAccount{
Account: make(map[apiCast.PlatformIDENUM]string),
Name: strings.TrimSpace(row[1]),
}
if i == 0 || len(row) < 2 {
continue
}
if i == 58 {
break
}
youtube, _ := f.GetCellValue(sheetName, fmt.Sprintf("C%d", i+1))
if youtube != "" {
tmp.Account[2] = strings.TrimSpace(youtube)
}
ins, _ := f.GetCellValue(sheetName, fmt.Sprintf("D%d", i+1))
if ins != "" {
tmp.Account[3] = strings.TrimSpace(ins)
}
tiktok, _ := f.GetCellValue(sheetName, fmt.Sprintf("E%d", i+1))
if tiktok != "" {
tmp.Account[1] = strings.TrimSpace(tiktok)
}
artists = append(artists, tmp)
}
return artists, nil
}