Untitled

mail@pastecode.io avatar
unknown
javascript
2 months ago
5.0 kB
5
Indexable
Never
exports.createInspection = (req, res) => {
	const number_plate = req.body.number_plate;
	const wheel_formula = req.body.wheel_formula; // công thức bánh xe
	const wheel_tread = req.body.wheel_tread; // vết bánh xe
	const overall_dimension = req.body.overall_dimension; // kích thước bao
	const container_dimension = req.body.container_dimension; // kích thước lòng thùng xe
	const length_base = req.body.length_base; // chiều dài cơ sở
	const kerb_mass = req.body.kerb_mass; // khối lượng bản thân
	const designed_and_authorized_payload =
		req.body.designed_and_authorized_payload; // Khối lượng hàng CC theo TK/CP TGGT
	const designed_and_authorized_total_mass =
		req.body.designed_and_authorized_total_mass; // Khối lượng toàn bộ theo TK/CP TGGT
	const designed_and_authorized_towed_mass =
		req.body.designed_and_authorized_towed_mass; // Khối lượng kéo theo TK/CP TGGT
	const permissible_carry = req.body.permissible_carry; // Số lượng người cho phép chở
	const engine_displacement = req.body.engine_displacement; // Thể tích làm việc của động cơ
	const maximum_output_to_rpm_ratio = req.body.maximum_output_to_rpm_ratio; // Công suất lớn nhất/tốc độ quay
	const fuel = req.body.fuel; // Loại nhiên liệu
	const number_of_tires = req.body.number_of_tires; // Số lượng lốp
	const tire_size = req.body.tire_size; // Cỡ lốp/trục
	let queryString = utils.generateQueryString(req.query);

	connection.query(
		`SELECT *
        FROM inspections i
        INNER JOIN cars c ON c.car_id = i.car_id WHERE i.car_id = (SELECT car_id FROM cars WHERE number_plate = ?)`,
		[number_plate],
		(err, result, fields) => {
			if (err) {
				return res.status(500).json({
					status: "Failed",
					message: err,
				});
			} else {
				const inspection_date = new Date();
				const expired_date = new Date(inspection_date);
				expired_date.setMonth(expired_date.getMonth() + 6);

				// Truy vấn con để đếm số lần đăng kiểm
				connection.query(
					`SELECT COUNT(*) as count FROM inspections WHERE car_id = (SELECT car_id FROM cars WHERE number_plate = ?)`,
					[number_plate],
					(countErr, countResult, countFields) => {
						if (countErr) {
							return res.status(500).json({
								status: "Failed",
								message: countErr,
							});
						}

						const first_time = countResult[0].count === 0 ? 1 : 0;

						// Câu lệnh INSERT sử dụng giá trị của first_time được xác định ở trên
						connection.query(
							`INSERT INTO inspections (inspection_id, inspection_number, inspection_date, car_id, user_id, centre_id, specify, first_time, expired_date)
                        SELECT
                            COALESCE(MAX(inspection_id), 0) + 1,
                            CONCAT(YEAR(CURDATE()), '-', LPAD(COALESCE(MAX(CAST(SUBSTRING(inspection_number, 6) AS UNSIGNED)), 0) + 1, 6, '0')),
                            ?,
                            (SELECT car_id FROM cars WHERE number_plate = ?),
                            ?, ?, ?, ?,
                            ?
                        FROM inspections;
						UPDATE cars SET inspected = 1,
						wheel_formula = ?,
						wheel_tread = ?,
					    overall_dimension = ?,
						container_dimension = ?,
						length_base = ?,
						kerb_mass = ?,
						designed_and_authorized_payload = ?,
						designed_and_authorized_total_mass = ?,
						designed_and_authorized_towed_mass = ?, 
						permissible_carry = ?,
						engine_displacement = ?,
						maximum_output_to_rpm_ratio = ?,
						fuel = ?,
						number_of_tires_and_tire_size=?
							WHERE number_plate = ?`,
							[
								inspection_date,
								number_plate,
								req.user.user_id,
								req.user.centre_id,
								"carry_people$lte:9-personal+manufacture$lte:7~36~24",
								first_time,
								expired_date,
								wheel_formula,
								wheel_tread + " (mm)",
								overall_dimension + " (mm)",
								container_dimension + " (mm)",
								length_base + " (mm)",
								kerb_mass + " (kg)",
								designed_and_authorized_payload + " (kg)",
								designed_and_authorized_total_mass + " (kg)",
								designed_and_authorized_towed_mass + " (kg)",
								permissible_carry,
								engine_displacement + " (cm3)",
								maximum_output_to_rpm_ratio + "vph",
								fuel,
								number_of_tires + " tires, " + tire_size,
								number_plate,
							],
							(insertErr, insertResult, insertFields) => {
								if (insertErr) {
									return res.status(500).json({
										status: "Failed",
										message: insertErr,
									});
								} else {
									return res.status(201).json({
										status: "Success",
										message:
											"Create inspection successfully",
										data: insertResult,
									});
								}
							}
						);
					}
				);
			}
		}
	);
};
Leave a Comment