const { sequelize } = require('./config/database');

async function addTestData() {
  try {
    console.log('开始添加测试数据...');

    // 添加supervision_tasks测试数据
    await sequelize.query(`
      INSERT INTO supervision_tasks (
        applicationNumber, contractNumber, productName, customerName, idType, idNumber,
        assetType, assetQuantity, supervisionStatus, importTime, startTime, endTime,
        loanAmount, interestRate, loanTerm, supervisorName, supervisorPhone, farmAddress,
        remarks, createdBy, updatedBy, createdAt, updatedAt
      ) VALUES 
      ('APP001', 'CON001', '养殖贷款', '张三', 'id_card', '110101199001010001',
       'cattle', 10, 'pending', NOW(), '2024-01-01', '2024-12-31',
       50000.00, 0.05, 12, '李监督员', '13800138000', '北京市朝阳区农场',
       '测试监管任务', 2, 2, NOW(), NOW()),
      ('APP002', 'CON002', '种植贷款', '李四', 'id_card', '110101199002020002',
       'sheep', 20, 'supervising', NOW(), '2024-02-01', '2024-11-30',
       30000.00, 0.06, 10, '王监督员', '13900139000', '北京市海淀区农场',
       '测试监管任务2', 2, 2, NOW(), NOW())
    `, { type: sequelize.QueryTypes.RAW });
    console.log('✅ 添加supervision_tasks测试数据成功');

    // 添加projects测试数据
    await sequelize.query(`
      INSERT INTO projects (
        name, status, farmName, supervisionObject, supervisionQuantity, supervisionPeriod,
        supervisionAmount, startTime, endTime, earTag, collar, host, loanOfficer,
        description, createdBy, updatedBy, createdAt, updatedAt
      ) VALUES 
      ('养殖项目1', 'supervision', '张三农场', 'cattle', 50, '12个月',
       100000.00, '2024-01-01', '2024-12-31', 30, 20, 10, '李贷款员',
       '测试养殖项目', 2, 2, NOW(), NOW()),
      ('种植项目1', 'completed', '李四农场', 'sheep', 100, '10个月',
       80000.00, '2024-02-01', '2024-11-30', 60, 40, 20, '王贷款员',
       '测试种植项目', 2, 2, NOW(), NOW())
    `, { type: sequelize.QueryTypes.RAW });
    console.log('✅ 添加projects测试数据成功');

    // 添加installation_tasks测试数据
    await sequelize.query(`
      INSERT INTO installation_tasks (
        applicationNumber, contractNumber, productName, customerName, idType, idNumber,
        assetType, equipmentToInstall, installationStatus, taskGenerationTime, completionTime,
        installationNotes, installerName, installerPhone, installationAddress,
        createdBy, updatedBy, createdAt, updatedAt
      ) VALUES 
      ('APP001', 'CON001', '养殖设备', '张三', 'ID_CARD', '110101199001010001',
       'cattle', '耳标设备', 'pending', NOW(), NULL,
       '测试安装任务', '安装员1', '13900139000', '北京市朝阳区农场',
       2, 2, NOW(), NOW()),
      ('APP002', 'CON002', '种植设备', '李四', 'ID_CARD', '110101199002020002',
       'sheep', '项圈设备', 'completed', NOW(), NOW(),
       '测试安装任务2', '安装员2', '14000140000', '北京市海淀区农场',
       2, 2, NOW(), NOW())
    `, { type: sequelize.QueryTypes.RAW });
    console.log('✅ 添加installation_tasks测试数据成功');

    // 添加completed_supervisions测试数据
    await sequelize.query(`
      INSERT INTO completed_supervisions (
        applicationNumber, contractNumber, customerName, supervisionPeriod, totalAmount,
        paidAmount, remainingAmount, settlementNotes, createdBy, updatedBy, createdAt, updatedAt
      ) VALUES 
      ('APP001', 'CON001', '张三', '12个月', 50000.00,
       30000.00, 20000.00, '已结清部分', 2, 2, NOW(), NOW()),
      ('APP002', 'CON002', '李四', '10个月', 30000.00,
       30000.00, 0.00, '已完全结清', 2, 2, NOW(), NOW())
    `, { type: sequelize.QueryTypes.RAW });
    console.log('✅ 添加completed_supervisions测试数据成功');

    // 添加loan_applications测试数据
    await sequelize.query(`
      INSERT INTO loan_applications (
        customer_name, customer_phone, customer_id_card, loan_amount, loan_term,
        interest_rate, application_date, status
      ) VALUES 
      ('张三', '13800138000', '110101199001010001', 50000.00, 12, 0.05, '2024-01-01', 'pending'),
      ('李四', '13900139000', '110101199002020002', 30000.00, 10, 0.06, '2024-01-02', 'approved')
    `, { type: sequelize.QueryTypes.RAW });
    console.log('✅ 添加loan_applications测试数据成功');

    // 添加loan_contracts测试数据
    await sequelize.query(`
      INSERT INTO loan_contracts (
        contract_number, customer_name, customer_phone, customer_id_card, loan_amount,
        loan_term, interest_rate, contract_date, status
      ) VALUES 
      ('CON001', '张三', '13800138000', '110101199001010001', 50000.00, 12, 0.05, '2024-01-01', 'active'),
      ('CON002', '李四', '13900139000', '110101199002020002', 30000.00, 10, 0.06, '2024-01-02', 'completed')
    `, { type: sequelize.QueryTypes.RAW });
    console.log('✅ 添加loan_contracts测试数据成功');

    console.log('\n✅ 所有测试数据添加完成！');
  } catch (error) {
    console.error('❌ 添加测试数据失败:', error.message);
  } finally {
    await sequelize.close();
  }
}

addTestData();
