/**
 * 牛只用途控制器
 * 处理cattle_user表的CRUD操作
 */

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

class CattleUserController {
  /**
   * 获取所有牛只用途
   */
  async getAllUsers(req, res) {
    try {
      const [users] = await sequelize.query('SELECT * FROM cattle_user ORDER BY id ASC')
      
      res.json({
        success: true,
        data: users,
        message: '获取牛只用途列表成功'
      })
    } catch (error) {
      console.error('获取牛只用途列表失败:', error)
      res.status(500).json({
        success: false,
        message: '获取牛只用途列表失败',
        error: error.message
      })
    }
  }

  /**
   * 根据ID获取牛只用途
   */
  async getUserById(req, res) {
    try {
      const { id } = req.params
      const [users] = await sequelize.query('SELECT * FROM cattle_user WHERE id = ?', [id])
      
      if (users.length === 0) {
        return res.status(404).json({
          success: false,
          message: '牛只用途不存在'
        })
      }

      res.json({
        success: true,
        data: users[0],
        message: '获取牛只用途成功'
      })
    } catch (error) {
      console.error('获取牛只用途失败:', error)
      res.status(500).json({
        success: false,
        message: '获取牛只用途失败',
        error: error.message
      })
    }
  }

  /**
   * 创建牛只用途
   */
  async createUser(req, res) {
    try {
      const { name, description } = req.body

      if (!name) {
        return res.status(400).json({
          success: false,
          message: '用途名称不能为空'
        })
      }

      const [result] = await sequelize.query(
        'INSERT INTO cattle_user (name, description) VALUES (?, ?)',
        [name, description || null]
      )

      res.status(201).json({
        success: true,
        data: { id: result.insertId, name, description },
        message: '创建牛只用途成功'
      })
    } catch (error) {
      console.error('创建牛只用途失败:', error)
      res.status(500).json({
        success: false,
        message: '创建牛只用途失败',
        error: error.message
      })
    }
  }

  /**
   * 更新牛只用途
   */
  async updateUser(req, res) {
    try {
      const { id } = req.params
      const { name, description } = req.body

      if (!name) {
        return res.status(400).json({
          success: false,
          message: '用途名称不能为空'
        })
      }

      const [result] = await sequelize.query(
        'UPDATE cattle_user SET name = ?, description = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?',
        [name, description || null, id]
      )

      if (result.affectedRows === 0) {
        return res.status(404).json({
          success: false,
          message: '牛只用途不存在'
        })
      }

      res.json({
        success: true,
        data: { id: parseInt(id), name, description },
        message: '更新牛只用途成功'
      })
    } catch (error) {
      console.error('更新牛只用途失败:', error)
      res.status(500).json({
        success: false,
        message: '更新牛只用途失败',
        error: error.message
      })
    }
  }

  /**
   * 删除牛只用途
   */
  async deleteUser(req, res) {
    try {
      const { id } = req.params

      const [result] = await sequelize.query('DELETE FROM cattle_user WHERE id = ?', [id])

      if (result.affectedRows === 0) {
        return res.status(404).json({
          success: false,
          message: '牛只用途不存在'
        })
      }

      res.json({
        success: true,
        message: '删除牛只用途成功'
      })
    } catch (error) {
      console.error('删除牛只用途失败:', error)
      res.status(500).json({
        success: false,
        message: '删除牛只用途失败',
        error: error.message
      })
    }
  }
}

module.exports = new CattleUserController()
