# 骆驼加油定时任务

<cite>
**本文档引用的文件**   
- [main.go](file://main.go)
- [cron.go](file://utility/cron/cron.go)
- [camel_oil.go](file://internal/service/camel_oil.go)
- [cron_tasks.go](file://internal/logic/camel_oil/cron_tasks.go)
- [camel_oil.go](file://internal/consts/camel_oil.go)
- [camel_oil_tables.sql](file://sql/camel_oil_tables.sql)
</cite>

## 目录
1. [项目结构](#项目结构)
2. [核心组件](#核心组件)
3. [骆驼加油模块定时任务](#骆驼加油模块定时任务)
4. [定时任务注册机制](#定时任务注册机制)
5. [数据库设计](#数据库设计)
6. [状态管理与枚举](#状态管理与枚举)
7. [业务逻辑分析](#业务逻辑分析)

## 项目结构

```mermaid
graph TD
A[kami_backend] --> B[api]
A --> C[internal]
A --> D[utility]
A --> E[sql]
B --> B1[camel_oil]
B1 --> B11[v1]
B11 --> B111[account.go]
B11 --> B112[order.go]
C --> C1[controller]
C1 --> C11[camel_oil]
C11 --> C111[camel_oil_v1_submit_order.go]
C11 --> C112[camel_oil_v1_order_callback.go]
C --> C2[logic]
C2 --> C21[camel_oil]
C21 --> C211[cron_tasks.go]
C21 --> C212[account_login.go]
C21 --> C213[order.go]
C --> C3[service]
C3 --> C31[camel_oil.go]
C --> C4[consts]
C4 --> C41[camel_oil.go]
C --> C5[dao]
C5 --> C51[v_1_camel_oil_account.go]
C5 --> C52[v_1_camel_oil_order.go]
D --> D1[cron]
D1 --> D11[cron.go]
E --> E1[camel_oil_tables.sql]
```

**Diagram sources**
- [main.go](file://main.go#L1-L56)
- [utility/cron/cron.go](file://utility/cron/cron.go#L1-L96)

**Section sources**
- [main.go](file://main.go#L1-L56)
- [utility/cron/cron.go](file://utility/cron/cron.go#L1-L96)

## 核心组件

骆驼加油模块的核心组件包括定时任务调度器、服务接口、业务逻辑处理和数据库访问层。这些组件协同工作，实现了账号管理、订单处理和状态监控等功能。

**Section sources**
- [internal/service/camel_oil.go](file://internal/service/camel_oil.go#L1-L102)
- [internal/logic/camel_oil/cron_tasks.go](file://internal/logic/camel_oil/cron_tasks.go#L1-L242)

## 骆驼加油模块定时任务

```mermaid
graph TD
A[定时任务调度] --> B[账号登录任务]
A --> C[验证码检测任务]
A --> D[订单支付状态检测任务]
A --> E[账号日重置任务]
B --> B1[每5分钟执行]
B1 --> B2[检查可用订单容量]
B2 --> B3[容量≤50时触发登录]
C --> C1[每5秒执行]
C1 --> C2[查询待验证码账号]
C2 --> C3[从野猪平台检测验证码]
C3 --> C4[调用骆驼加油平台登录]
D --> D1[每10秒执行]
D1 --> D2[查询待支付订单]
D2 --> D3[检测支付状态]
D3 --> D4[更新订单状态]
E --> E1[每日00:00执行]
E1 --> E2[重置在线账号日订单数]
E2 --> E3[处理暂停账号]
E3 --> E4[恢复或标记失效]
```

**Diagram sources**
- [utility/cron/cron.go](file://utility/cron/cron.go#L74-L95)
- [internal/logic/camel_oil/cron_tasks.go](file://internal/logic/camel_oil/cron_tasks.go#L19-L241)

**Section sources**
- [utility/cron/cron.go](file://utility/cron/cron.go#L74-L95)
- [internal/logic/camel_oil/cron_tasks.go](file://internal/logic/camel_oil/cron_tasks.go#L19-L241)

## 定时任务注册机制

```mermaid
sequenceDiagram
participant Main as main.go
participant Cron as cron.go
participant Service as camel_oil.go
participant Logic as cron_tasks.go
Main->>Cron : main()启动
Cron->>Cron : Register(ctx)
Cron->>Cron : registerCamelOilTasks(ctx)
Cron->>Service : 调用CronAccountLoginTask()
Service->>Logic : 执行具体逻辑
Logic->>Logic : 检查容量并触发登录
Cron->>Service : 调用CronVerifyCodeCheckTask()
Service->>Logic : 执行验证码检测
Cron->>Service : 调用CronOrderPaymentCheckTask()
Service->>Logic : 检测订单支付状态
Cron->>Service : 调用CronAccountDailyResetTask()
Service->>Logic : 执行日重置任务
```

**Diagram sources**
- [main.go](file://main.go#L24-L54)
- [utility/cron/cron.go](file://utility/cron/cron.go#L14-L95)
- [internal/service/camel_oil.go](file://internal/service/camel_oil.go#L63-L68)
- [internal/logic/camel_oil/cron_tasks.go](file://internal/logic/camel_oil/cron_tasks.go#L19-L241)

**Section sources**
- [main.go](file://main.go#L24-L54)
- [utility/cron/cron.go](file://utility/cron/cron.go#L14-L95)

## 数据库设计

```mermaid
erDiagram
camel_oil_account {
bigint id PK
varchar(128) account_name
varchar(20) phone UK
text token
tinyint status
datetime token_expire_at
datetime last_login_at
datetime last_used_at
int daily_order_count
date daily_order_date
int total_order_count
text failure_reason
text remark
datetime created_at
datetime updated_at
datetime deleted_at
}
camel_oil_order {
bigint id PK
varchar(64) order_no UK
varchar(128) merchant_order_id
bigint account_id FK
varchar(128) account_name
varchar(128) platform_order_no
decimal(10,2) amount
text alipay_url
tinyint status
tinyint pay_status
tinyint notify_status
int notify_count
datetime last_check_at
datetime paid_at
text attach
text failure_reason
datetime created_at
datetime updated_at
datetime deleted_at
}
camel_oil_account_history {
bigint id PK
varchar(36) history_uuid UK
bigint account_id FK
varchar(32) change_type
tinyint status_before
tinyint status_after
int failure_count
text remark
datetime created_at
datetime updated_at
datetime deleted_at
}
camel_oil_order_history {
bigint id PK
varchar(36) history_uuid UK
varchar(64) order_no FK
varchar(32) change_type
bigint account_id FK
varchar(128) account_name
text raw_data
text remark
datetime created_at
datetime updated_at
datetime deleted_at
}
camel_oil_account ||--o{ camel_oil_order : "1:N"
camel_oil_account ||--o{ camel_oil_account_history : "1:N"
camel_oil_order ||--o{ camel_oil_order_history : "1:N"
```

**Diagram sources**
- [sql/camel_oil_tables.sql](file://sql/camel_oil_tables.sql#L1-L119)
- [internal/consts/camel_oil.go](file://internal/consts/camel_oil.go#L7-L229)

**Section sources**
- [sql/camel_oil_tables.sql](file://sql/camel_oil_tables.sql#L1-L119)

## 状态管理与枚举

```mermaid
classDiagram
class CamelOilAccountStatus {
+Pending : 0
+SendCode : 1
+Online : 2
+Paused : 3
+Invalid : 4
}
class CamelOilOrderStatus {
+Pending : 0
+Processing : 1
+Completed : 2
+Failed : 3
}
class CamelOilPayStatus {
+Unpaid : 0
+Paid : 1
+Refunded : 2
+Timeout : 3
}
class CamelOilNotifyStatus {
+Pending : 0
+Success : 1
+Failed : 2
}
class CamelOilAccountChangeType {
+Create
+Login
+Offline
+LoginFail
+Pause
+Resume
+Invalidate
+OrderBind
+OrderComplete
+Update
+Delete
}
class CamelOilOrderChangeType {
+Create
+Submit
+GetPayUrl
+CheckPay
+Paid
+Timeout
+Fail
+CallbackSuccess
+CallbackFail
}
```

**Diagram sources**
- [internal/consts/camel_oil.go](file://internal/consts/camel_oil.go#L7-L183)

**Section sources**
- [internal/consts/camel_oil.go](file://internal/consts/camel_oil.go#L7-L183)

## 业务逻辑分析

```mermaid
flowchart TD
A[开始] --> B{检查可用订单容量}
B --> |≤50| C[触发账号登录任务]
B --> |>50| D[无需登录]
C --> E[从野猪平台获取手机号]
E --> F[创建账号记录]
F --> G[调用骆驼加油平台发送验证码]
G --> H{发送成功?}
H --> |是| I[更新状态为发送验证码]
H --> |否| J[标记账号失效]
I --> K[验证码检测任务]
K --> L{收到验证码?}
L --> |是| M[调用骆驼加油平台登录]
L --> |否| N[继续等待]
M --> O{登录成功?}
O --> |是| P[保存Token，更新状态为在线]
O --> |否| Q[标记账号登录失败]
P --> R[账号可用于下单]
Q --> S[标记账号失效]
T[订单支付状态检测] --> U[查询待支付订单]
U --> V{存在订单?}
V --> |是| W[检测支付状态]
W --> X{已支付?}
X --> |是| Y[更新订单为已支付]
X --> |否| Z{超1小时?}
Z --> |是| AA[标记支付超时]
Z --> |否| AB[继续等待]
Y --> AC[完成]
AA --> AC
AB --> AC
```

**Diagram sources**
- [internal/logic/camel_oil/cron_tasks.go](file://internal/logic/camel_oil/cron_tasks.go#L19-L241)
- [internal/logic/camel_oil/account_login.go](file://internal/logic/camel_oil/account_login.go#L1-L137)
- [internal/logic/camel_oil/order.go](file://internal/logic/camel_oil/order.go#L1-L165)

**Section sources**
- [internal/logic/camel_oil/cron_tasks.go](file://internal/logic/camel_oil/cron_tasks.go#L19-L241)
- [internal/logic/camel_oil/account_login.go](file://internal/logic/camel_oil/account_login.go#L1-L137)
- [internal/logic/camel_oil/order.go](file://internal/logic/camel_oil/order.go#L1-L165)