135 lines
6.4 KiB
JavaScript
135 lines
6.4 KiB
JavaScript
const express = require('express');
|
|||
|
|
const db = require('../db');
|
||
|
|
const { getMonthRange } = require('../utils/helpers');
|
||
|
|
|
||
|
|
const router = express.Router();
|
||
|
|
|
||
|
|
// 新增记录
|
||
|
|
router.post('/', (req, res) => {
|
||
|
|
const { type, category, amount, quantity, unit_price, rebate, boss, partner, source, destination, note, created_at } = req.body;
|
||
|
|
const stmt = db.prepare(`
|
||
|
|
INSERT INTO records (user_id, type, category, amount, quantity, unit_price, rebate, boss, partner, source, destination, note, created_at)
|
||
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||
|
|
`);
|
||
|
|
const result = stmt.run(req.user.id, type, category, amount, quantity || null, unit_price || null, rebate || null, boss || null, partner || null, source || null, destination || null, note || null, created_at || new Date().toISOString());
|
||
|
|
res.json({ id: result.lastInsertRowid });
|
||
|
|
});
|
||
|
|
|
||
|
|
// 接单列表
|
||
|
|
router.get('/orders', (req, res) => {
|
||
|
|
const { page = 1, limit = 20 } = req.query;
|
||
|
|
const offset = (Number(page) - 1) * Number(limit);
|
||
|
|
const { startDate, endDate } = getMonthRange(req.query.year, req.query.month);
|
||
|
|
|
||
|
|
const total = db.prepare(
|
||
|
|
`SELECT COUNT(*) as count FROM records WHERE user_id = ? AND category = '接单' AND DATE(created_at, 'localtime') >= ? AND DATE(created_at, 'localtime') < ?`
|
||
|
|
).get(req.user.id, startDate, endDate).count;
|
||
|
|
|
||
|
|
const records = db.prepare(
|
||
|
|
`SELECT * FROM records WHERE user_id = ? AND category = '接单' AND DATE(created_at, 'localtime') >= ? AND DATE(created_at, 'localtime') < ? ORDER BY created_at DESC LIMIT ? OFFSET ?`
|
||
|
|
).all(req.user.id, startDate, endDate, Number(limit), offset);
|
||
|
|
|
||
|
|
res.json({ records, total, page: Number(page), limit: Number(limit) });
|
||
|
|
});
|
||
|
|
|
||
|
|
// 红包及其他收入列表
|
||
|
|
router.get('/other-income', (req, res) => {
|
||
|
|
const { page = 1, limit = 20 } = req.query;
|
||
|
|
const offset = (Number(page) - 1) * Number(limit);
|
||
|
|
const { startDate, endDate } = getMonthRange(req.query.year, req.query.month);
|
||
|
|
|
||
|
|
const total = db.prepare(
|
||
|
|
`SELECT COUNT(*) as count FROM records WHERE user_id = ? AND type = 'income' AND category != '接单' AND category != '派单' AND DATE(created_at, 'localtime') >= ? AND DATE(created_at, 'localtime') < ?`
|
||
|
|
).get(req.user.id, startDate, endDate).count;
|
||
|
|
|
||
|
|
const records = db.prepare(
|
||
|
|
`SELECT * FROM records WHERE user_id = ? AND type = 'income' AND category != '接单' AND category != '派单' AND DATE(created_at, 'localtime') >= ? AND DATE(created_at, 'localtime') < ? ORDER BY created_at DESC LIMIT ? OFFSET ?`
|
||
|
|
).all(req.user.id, startDate, endDate, Number(limit), offset);
|
||
|
|
|
||
|
|
res.json({ records, total, page: Number(page), limit: Number(limit) });
|
||
|
|
});
|
||
|
|
|
||
|
|
// 红包收入列表
|
||
|
|
router.get('/red-envelope', (req, res) => {
|
||
|
|
const { page = 1, limit = 20 } = req.query;
|
||
|
|
const offset = (Number(page) - 1) * Number(limit);
|
||
|
|
const { startDate, endDate } = getMonthRange(req.query.year, req.query.month);
|
||
|
|
|
||
|
|
const total = db.prepare(
|
||
|
|
`SELECT COUNT(*) as count FROM records WHERE user_id = ? AND type = 'income' AND category = '红包收入' AND DATE(created_at, 'localtime') >= ? AND DATE(created_at, 'localtime') < ?`
|
||
|
|
).get(req.user.id, startDate, endDate).count;
|
||
|
|
|
||
|
|
const records = db.prepare(
|
||
|
|
`SELECT * FROM records WHERE user_id = ? AND type = 'income' AND category = '红包收入' AND DATE(created_at, 'localtime') >= ? AND DATE(created_at, 'localtime') < ? ORDER BY created_at DESC LIMIT ? OFFSET ?`
|
||
|
|
).all(req.user.id, startDate, endDate, Number(limit), offset);
|
||
|
|
|
||
|
|
res.json({ records, total, page: Number(page), limit: Number(limit) });
|
||
|
|
});
|
||
|
|
|
||
|
|
// 奶茶收入列表
|
||
|
|
router.get('/milk-tea', (req, res) => {
|
||
|
|
const { page = 1, limit = 20 } = req.query;
|
||
|
|
const offset = (Number(page) - 1) * Number(limit);
|
||
|
|
const { startDate, endDate } = getMonthRange(req.query.year, req.query.month);
|
||
|
|
|
||
|
|
const total = db.prepare(
|
||
|
|
`SELECT COUNT(*) as count FROM records WHERE user_id = ? AND type = 'income' AND category = '奶茶' AND DATE(created_at, 'localtime') >= ? AND DATE(created_at, 'localtime') < ?`
|
||
|
|
).get(req.user.id, startDate, endDate).count;
|
||
|
|
|
||
|
|
const records = db.prepare(
|
||
|
|
`SELECT * FROM records WHERE user_id = ? AND type = 'income' AND category = '奶茶' AND DATE(created_at, 'localtime') >= ? AND DATE(created_at, 'localtime') < ? ORDER BY created_at DESC LIMIT ? OFFSET ?`
|
||
|
|
).all(req.user.id, startDate, endDate, Number(limit), offset);
|
||
|
|
|
||
|
|
res.json({ records, total, page: Number(page), limit: Number(limit) });
|
||
|
|
});
|
||
|
|
|
||
|
|
// 派单列表
|
||
|
|
router.get('/dispatches', (req, res) => {
|
||
|
|
const { page = 1, limit = 20 } = req.query;
|
||
|
|
const offset = (Number(page) - 1) * Number(limit);
|
||
|
|
const { startDate, endDate } = getMonthRange(req.query.year, req.query.month);
|
||
|
|
|
||
|
|
const total = db.prepare(
|
||
|
|
`SELECT COUNT(*) as count FROM records WHERE user_id = ? AND category = '派单' AND DATE(created_at, 'localtime') >= ? AND DATE(created_at, 'localtime') < ?`
|
||
|
|
).get(req.user.id, startDate, endDate).count;
|
||
|
|
|
||
|
|
const records = db.prepare(
|
||
|
|
`SELECT * FROM records WHERE user_id = ? AND category = '派单' AND DATE(created_at, 'localtime') >= ? AND DATE(created_at, 'localtime') < ? ORDER BY created_at DESC LIMIT ? OFFSET ?`
|
||
|
|
).all(req.user.id, startDate, endDate, Number(limit), offset);
|
||
|
|
|
||
|
|
res.json({ records, total, page: Number(page), limit: Number(limit) });
|
||
|
|
});
|
||
|
|
|
||
|
|
// 获取记录列表
|
||
|
|
router.get('/', (req, res) => {
|
||
|
|
const { type, page = 1, limit = 20 } = req.query;
|
||
|
|
const offset = (page - 1) * limit;
|
||
|
|
let sql = 'SELECT * FROM records WHERE user_id = ?';
|
||
|
|
const params = [req.user.id];
|
||
|
|
if (type) {
|
||
|
|
sql += ' AND type = ?';
|
||
|
|
params.push(type);
|
||
|
|
}
|
||
|
|
sql += ' ORDER BY created_at DESC LIMIT ? OFFSET ?';
|
||
|
|
params.push(Number(limit), Number(offset));
|
||
|
|
res.json(db.prepare(sql).all(...params));
|
||
|
|
});
|
||
|
|
|
||
|
|
// 更新记录
|
||
|
|
router.put('/:id', (req, res) => {
|
||
|
|
const { quantity, unit_price, rebate, amount, boss, partner, source, destination, note, created_at } = req.body;
|
||
|
|
db.prepare(`
|
||
|
|
UPDATE records SET quantity = ?, unit_price = ?, rebate = ?, amount = ?, boss = ?, partner = ?, source = ?, destination = ?, note = ?, created_at = ?
|
||
|
|
WHERE id = ? AND user_id = ?
|
||
|
|
`).run(quantity || null, unit_price || null, rebate || null, amount, boss || null, partner || null, source || null, destination || null, note || null, created_at || null, req.params.id, req.user.id);
|
||
|
|
res.json({ success: true });
|
||
|
|
});
|
||
|
|
|
||
|
|
// 删除记录
|
||
|
|
router.delete('/:id', (req, res) => {
|
||
|
|
db.prepare('DELETE FROM records WHERE id = ? AND user_id = ?').run(req.params.id, req.user.id);
|
||
|
|
res.json({ success: true });
|
||
|
|
});
|
||
|
|
|
||
|
|
module.exports = router;
|