v1.0.20: 需求迭代(0808文档) - 文件管理迁入竞品资料+企业筛选弹窗(多条件且/或)+搜索框内嵌字段+渠道计划费用字段/状态行内编辑
This commit is contained in:
+58
-21
@@ -1,8 +1,9 @@
|
||||
<?php
|
||||
/**
|
||||
* 渠道新增计划保存接口 POST /api/channel/plan_save.php
|
||||
* 入参:id(可选,编辑时传)/ channel_type(渠道类别=source_channel 枚举,必填)/
|
||||
* source_detail / industry / start_date / end_date / remark / status(待启动|已执行|错过)
|
||||
* 入参:id(可选,编辑时传)/ channel_type(渠道类别=source_channel 枚举,新增时必填)/
|
||||
* source_detail / industry / start_date / end_date / remark / status(待启动|已执行|错过)/ cost(费用(元))
|
||||
* 说明:编辑(传 id)时仅更新提交的字段,便于列表行内实时改状态/费用。
|
||||
*/
|
||||
require_once __DIR__ . '/../common/db.php';
|
||||
require_once __DIR__ . '/../common/response.php';
|
||||
@@ -13,34 +14,55 @@ require_once __DIR__ . '/../common/logger.php';
|
||||
checkAjax();
|
||||
checkPermission('channel');
|
||||
|
||||
$channelType = trim($_POST['channel_type'] ?? '');
|
||||
if ($channelType === '') {
|
||||
Response::error('渠道类别(channel_type)为必填项', 400);
|
||||
}
|
||||
$status = trim($_POST['status'] ?? '待启动');
|
||||
if (!in_array($status, ['待启动', '已执行', '错过'], true)) {
|
||||
$status = '待启动';
|
||||
$id = (int)($_POST['id'] ?? 0);
|
||||
|
||||
// 新增时渠道类别必填;编辑时不强制(可能只改状态/费用)
|
||||
if ($id <= 0) {
|
||||
$channelType = trim($_POST['channel_type'] ?? '');
|
||||
if ($channelType === '') {
|
||||
Response::error('渠道类别(channel_type)为必填项', 400);
|
||||
}
|
||||
}
|
||||
|
||||
$status = trim($_POST['status'] ?? '');
|
||||
if ($status !== '' && !in_array($status, ['待启动', '已执行', '错过'], true)) {
|
||||
Response::error('状态反馈取值不合法', 400);
|
||||
}
|
||||
$cost = trim($_POST['cost'] ?? '');
|
||||
if ($cost !== '' && !is_numeric($cost)) {
|
||||
Response::error('费用(元)必须为数字', 400);
|
||||
}
|
||||
|
||||
$id = (int)($_POST['id'] ?? 0);
|
||||
$pdo = DB::getInstance()->getPdo();
|
||||
|
||||
$fields = [
|
||||
'channel_type' => $channelType,
|
||||
'source_detail' => trim($_POST['source_detail'] ?? '') !== '' ? trim($_POST['source_detail']) : null,
|
||||
'industry' => trim($_POST['industry'] ?? '') !== '' ? trim($_POST['industry']) : null,
|
||||
'start_date' => trim($_POST['start_date'] ?? '') !== '' ? $_POST['start_date'] : null,
|
||||
'end_date' => trim($_POST['end_date'] ?? '') !== '' ? $_POST['end_date'] : null,
|
||||
'remark' => trim($_POST['remark'] ?? '') !== '' ? trim($_POST['remark']) : null,
|
||||
'status' => $status,
|
||||
];
|
||||
/** 从 POST 收集需要写入的字段(空串转 null) */
|
||||
$fields = [];
|
||||
$collect = function ($key) use (&$fields) {
|
||||
if (isset($_POST[$key])) {
|
||||
$v = trim((string)$_POST[$key]);
|
||||
$fields[$key] = $v !== '' ? $v : null;
|
||||
}
|
||||
};
|
||||
|
||||
if ($id > 0) {
|
||||
$check = $pdo->prepare("SELECT id FROM channel_plans WHERE id = ? AND is_active = 1");
|
||||
$check = $pdo->prepare("SELECT id, status FROM channel_plans WHERE id = ? AND is_active = 1");
|
||||
$check->execute([$id]);
|
||||
if (!$check->fetch()) {
|
||||
$cur = $check->fetch();
|
||||
if (!$cur) {
|
||||
Response::error('计划不存在');
|
||||
}
|
||||
// 只更新提交的字段
|
||||
foreach (['channel_type', 'source_detail', 'industry', 'start_date', 'end_date', 'remark', 'status'] as $k) {
|
||||
$collect($k);
|
||||
}
|
||||
if (isset($_POST['cost'])) {
|
||||
$c = trim((string)$_POST['cost']);
|
||||
$fields['cost'] = $c !== '' ? round((float)$c, 2) : null;
|
||||
}
|
||||
// 若只改了状态为「已执行」而原费用为空,前端会单独提交 cost;此处不自动置值
|
||||
if (!$fields) {
|
||||
Response::error('没有需要更新的字段', 400);
|
||||
}
|
||||
[$sets, $params] = buildUpdate($fields);
|
||||
$params[] = $id;
|
||||
$pdo->prepare("UPDATE channel_plans SET $sets WHERE id = ?")->execute($params);
|
||||
@@ -48,6 +70,21 @@ if ($id > 0) {
|
||||
Response::success(null, '更新成功');
|
||||
}
|
||||
|
||||
// 新增:渠道类别必填
|
||||
$channelType = trim($_POST['channel_type'] ?? '');
|
||||
if ($channelType === '') {
|
||||
Response::error('渠道类别(channel_type)为必填项', 400);
|
||||
}
|
||||
$fields = [
|
||||
'channel_type' => $channelType,
|
||||
'source_detail' => strOrNull($_POST['source_detail'] ?? null),
|
||||
'industry' => strOrNull($_POST['industry'] ?? null),
|
||||
'start_date' => strOrNull($_POST['start_date'] ?? null),
|
||||
'end_date' => strOrNull($_POST['end_date'] ?? null),
|
||||
'remark' => strOrNull($_POST['remark'] ?? null),
|
||||
'status' => $status !== '' ? $status : '待启动',
|
||||
'cost' => $cost !== '' ? round((float)$cost, 2) : null,
|
||||
];
|
||||
[$sql, $params] = buildInsert($fields);
|
||||
$pdo->prepare("INSERT INTO channel_plans $sql")->execute($params);
|
||||
$newId = (int)$pdo->lastInsertId();
|
||||
|
||||
Reference in New Issue
Block a user