lz_pdo_查询以及批增数据

$result = $DB->prepare("SELECT calendar_date, calendar_date_name, calendar_day_off, calendar_day_worked
                        FROM `admin_calendar`
                        WHERE calendar_date BETWEEN :start - INTERVAL 2 MONTH AND :end + INTERVAL 2 MONTH
                        AND (calendar_day_off <> 0
                        OR calendar_day_worked <> 0)
                        ORDER BY `calendar_date` DESC");
$result->bindvalue(':start', $_GET['start'], PDO::PARAM_STR);
$result->bindvalue(':end', $_GET['end'], PDO::PARAM_STR);
if (!$result->execute()) {
    Flight::error(new Exception(errorInfo($query)));
}
$items = [];
while ($row = $result->fetch(PDO::FETCH_BOTH)) {
    $row_array['id'] = $row['calendar_date'];
    $row_array['name'] = $row['calendar_date_name'];
    $row_array['calendar_day_off'] = $row['calendar_day_off'];
    $row_array['calendar_day_worked'] = $row['calendar_day_worked'];

    if ($row['calendar_day_off'] == 1) {
        $row_array['color'] = '#6BC6B6';
    } elseif ($row['calendar_day_worked'] == 1) {
        $row_array['color'] = '#EE4C4E';
    }
    array_push($items, $row_array);
}

Flight::json(['data' => ['items' => $items]]);

 批增:

<?php

/**
 * @api {post} /eamic/ws/rest/v1/19099toolMovement 写入工具移动数据
 * @apiParam (Request body)  tool_id 
 * @apiParam (Request body) tool_movement_time 
 * @apiParam (Request body) tool_movement_nature 
 * @apiParam (Request body) tool_movement_employee_id 
 * @apiParam (Request body) tool_movement_wo_id 
 * @apiParam (Request body) tool_movement_remarks 
 * @apiHeaderExample {json} Header-Example:
 *     {
 *       "Authorization": "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="(用真实验证信息替换),
 *       "Content-Type": "application/json"
 *     }
{
    "data": {
        "totalItems": 2,
        "success": 2,
        "failed": 0,
        "create": "2022-06-23 18:03:38",
        "details": [
            {
                "tool_id": 360,
                "insert_tool_id": 437
            },
            {
                "tool_id": 360,
                "insert_tool_id": 438
            }
        ]
    }
}
 */
// 获取请求体,@的作用为屏蔽警告,可去除。
$post = @file_get_contents('php://input');
// 解析成数组
$post =  json_decode($post, true);


$query = $DB->prepare("    
    INSERT INTO sp_tool_movement (
        tool_id,
        tool_movement_time,
        tool_movement_employee_id,
        tool_movement_nature,
        tool_movement_wo_id,
        tool_movement_remarks
    )
    VALUES (
        :tool_id,
        :tool_movement_time,
        :tool_movement_employee_id,
        :tool_movement_nature, 
        :tool_movement_wo_id,
        :tool_movement_remarks
    )
");
// 定义一个空的数组,也就是返回的参数详情
$details = [];
// 返回的成功还是失败
$success = 0;
// 定义一个空列
$failed = 0;

foreach ($post as $datum) {

    if (!isset($datum['tool_id'])) {
        $failed += 1;
        $details[] = ['tool_id' => '', 'result' => 'failed: tool_id not provided.'];
        continue;
    }
    $query->bindvalue('tool_id', $datum['tool_id']);
    $query->bindvalue(
        'tool_movement_time',
        isset($datum['tool_movement_time']) ? $datum['tool_movement_time'] : ''
    );
    $query->bindvalue(
        'tool_movement_employee_id',
        isset($datum['tool_movement_employee_id']) ? $datum['tool_movement_employee_id'] : ''
    );
    $query->bindvalue(
        ':tool_movement_nature',
        isset($datum['tool_movement_nature']) ? $datum['tool_movement_nature'] : ''
    );
    $query->bindvalue(
        ':tool_movement_wo_id',
        isset($datum['tool_movement_wo_id']) ? $datum['tool_movement_wo_id'] : ''
    );
    $query->bindvalue(
        ':tool_movement_remarks',
        isset($datum['tool_movement_remarks']) ? $datum['tool_movement_remarks'] : ''
    );
    if (!$query->execute()) {
        Flight::error(new RuntimeException(errorInfo($query)));
    }

    if ($query->rowCount()) {
        $success += $query->rowCount();
    }

    $details[] = [
        'tool_id' => $datum['tool_id'],
        'insert_tool_id' => (int) $DB->lastInsertId(),
    ];
}

$result = [
    'totalItems' => count($data),
    'success' => $success,
    'failed' => $failed,
    'create' => $tmp_now,
    'details' => $details,
];
update_log($result);

 

posted @ 2022-06-17 17:18  王越666  阅读(24)  评论(0编辑  收藏  举报