* Task 任务通过平台、Action 区分任务 */
(c *gin.Context, relayMode int)
| 23 | Task 任务通过平台、Action 区分任务 |
| 24 | */ |
| 25 | func RelayTaskSubmit(c *gin.Context, relayMode int) (taskErr *dto.TaskError) { |
| 26 | platform := constant.TaskPlatform(c.GetString("platform")) |
| 27 | relayInfo := relaycommon.GenTaskRelayInfo(c) |
| 28 | |
| 29 | adaptor := GetTaskAdaptor(platform) |
| 30 | if adaptor == nil { |
| 31 | return service.TaskErrorWrapperLocal(fmt.Errorf("invalid api platform: %s", platform), "invalid_api_platform", http.StatusBadRequest) |
| 32 | } |
| 33 | adaptor.Init(relayInfo) |
| 34 | // get & validate taskRequest 获取并验证文本请求 |
| 35 | taskErr = adaptor.ValidateRequestAndSetAction(c, relayInfo) |
| 36 | if taskErr != nil { |
| 37 | return |
| 38 | } |
| 39 | |
| 40 | modelName := relayInfo.OriginModelName |
| 41 | if modelName == "" { |
| 42 | modelName = service.CoverTaskActionToModelName(platform, relayInfo.Action) |
| 43 | } |
| 44 | modelPrice, success := ratio_setting.GetModelPrice(modelName, true) |
| 45 | if !success { |
| 46 | defaultPrice, ok := ratio_setting.GetDefaultModelRatioMap()[modelName] |
| 47 | if !ok { |
| 48 | modelPrice = 0.1 |
| 49 | } else { |
| 50 | modelPrice = defaultPrice |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // 预扣 |
| 55 | groupRatio := ratio_setting.GetGroupRatio(relayInfo.UsingGroup) |
| 56 | var ratio float64 |
| 57 | userGroupRatio, hasUserGroupRatio := ratio_setting.GetGroupGroupRatio(relayInfo.UserGroup, relayInfo.UsingGroup) |
| 58 | if hasUserGroupRatio { |
| 59 | ratio = modelPrice * userGroupRatio |
| 60 | } else { |
| 61 | ratio = modelPrice * groupRatio |
| 62 | } |
| 63 | userQuota, err := model.GetUserQuota(relayInfo.UserId, false) |
| 64 | if err != nil { |
| 65 | taskErr = service.TaskErrorWrapper(err, "get_user_quota_failed", http.StatusInternalServerError) |
| 66 | return |
| 67 | } |
| 68 | quota := int(ratio * common.QuotaPerUnit) |
| 69 | if userQuota-quota < 0 { |
| 70 | taskErr = service.TaskErrorWrapperLocal(errors.New("user quota is not enough"), "quota_not_enough", http.StatusForbidden) |
| 71 | return |
| 72 | } |
| 73 | |
| 74 | if relayInfo.OriginTaskID != "" { |
| 75 | originTask, exist, err := model.GetByTaskId(relayInfo.UserId, relayInfo.OriginTaskID) |
| 76 | if err != nil { |
| 77 | taskErr = service.TaskErrorWrapper(err, "get_origin_task_failed", http.StatusInternalServerError) |
| 78 | return |
| 79 | } |
| 80 | if !exist { |
| 81 | taskErr = service.TaskErrorWrapperLocal(errors.New("task_origin_not_exist"), "task_not_exist", http.StatusBadRequest) |
| 82 | return |
no test coverage detected