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

const IotCattle = sequelize.define('IotCattle', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    allowNull: false,
    field: 'id'
  },
  orgId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    field: 'org_id',
    comment: '组织ID'
  },
  earNumber: {
    type: DataTypes.BIGINT,
    allowNull: false,
    field: 'ear_number',
    comment: '耳标号'
  },
  sex: {
    type: DataTypes.TINYINT,
    allowNull: false,
    field: 'sex',
    comment: '性别'
  },
  strain: {
    type: DataTypes.TINYINT,
    allowNull: false,
    field: 'strain',
    comment: '品系'
  },
  varieties: {
    type: DataTypes.TINYINT,
    allowNull: false,
    field: 'varieties',
    comment: '品种'
  },
  cate: {
    type: DataTypes.TINYINT,
    allowNull: false,
    field: 'cate',
    comment: '类别'
  },
  birthWeight: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false,
    field: 'birth_weight',
    comment: '出生体重'
  },
  birthday: {
    type: DataTypes.INTEGER,
    allowNull: false,
    field: 'birthday',
    comment: '出生日期'
  },
  penId: {
    type: DataTypes.INTEGER,
    allowNull: true,
    field: 'pen_id',
    comment: '栏舍ID'
  },
  intoTime: {
    type: DataTypes.INTEGER,
    allowNull: false,
    field: 'into_time',
    comment: '入栏时间'
  },
  parity: {
    type: DataTypes.INTEGER,
    allowNull: false,
    field: 'parity',
    comment: '胎次'
  },
  source: {
    type: DataTypes.TINYINT,
    allowNull: false,
    field: 'source',
    comment: '来源'
  },
  sourceDay: {
    type: DataTypes.INTEGER,
    allowNull: false,
    field: 'source_day',
    comment: '来源天数'
  },
  sourceWeight: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false,
    field: 'source_weight',
    comment: '来源体重'
  },
  weight: {
    type: DataTypes.DOUBLE(11, 2),
    allowNull: false,
    field: 'weight',
    comment: '当前体重'
  },
  event: {
    type: DataTypes.TINYINT,
    allowNull: false,
    field: 'event',
    comment: '事件'
  },
  eventTime: {
    type: DataTypes.INTEGER,
    allowNull: false,
    field: 'event_time',
    comment: '事件时间'
  },
  lactationDay: {
    type: DataTypes.INTEGER,
    allowNull: false,
    field: 'lactation_day',
    comment: '泌乳天数'
  },
  semenNum: {
    type: DataTypes.STRING(30),
    allowNull: false,
    field: 'semen_num',
    comment: '精液编号'
  },
  isWear: {
    type: DataTypes.TINYINT,
    allowNull: false,
    field: 'is_wear',
    comment: '是否佩戴设备'
  },
  batchId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    field: 'batch_id',
    comment: '批次ID'
  },
  imgs: {
    type: DataTypes.STRING(800),
    allowNull: false,
    field: 'imgs',
    comment: '图片'
  },
  isEleAuth: {
    type: DataTypes.TINYINT,
    allowNull: false,
    field: 'is_ele_auth',
    comment: '是否电子认证'
  },
  isQuaAuth: {
    type: DataTypes.TINYINT,
    allowNull: false,
    field: 'is_qua_auth',
    comment: '是否质量认证'
  },
  isDelete: {
    type: DataTypes.INTEGER,
    allowNull: false,
    field: 'is_delete',
    comment: '是否删除'
  },
  isOut: {
    type: DataTypes.INTEGER,
    allowNull: false,
    field: 'is_out',
    comment: '是否出栏'
  },
  createUid: {
    type: DataTypes.INTEGER,
    allowNull: false,
    field: 'create_uid',
    comment: '创建人ID'
  },
  createTime: {
    type: DataTypes.INTEGER,
    allowNull: false,
    field: 'create_time',
    comment: '创建时间'
  },
  algebra: {
    type: DataTypes.INTEGER,
    allowNull: false,
    field: 'algebra',
    comment: '代数'
  },
  colour: {
    type: DataTypes.TEXT,
    allowNull: false,
    field: 'colour',
    comment: '毛色'
  },
  infoWeight: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false,
    field: 'info_weight',
    comment: '信息体重'
  },
  descent: {
    type: DataTypes.TINYINT,
    allowNull: false,
    field: 'descent',
    comment: '血统'
  },
  isVaccin: {
    type: DataTypes.TINYINT,
    allowNull: false,
    field: 'is_vaccin',
    comment: '是否接种疫苗'
  },
  isInsemination: {
    type: DataTypes.TINYINT,
    allowNull: false,
    field: 'is_insemination',
    comment: '是否人工授精'
  },
  isInsure: {
    type: DataTypes.TINYINT,
    allowNull: false,
    field: 'is_insure',
    comment: '是否投保'
  },
  isMortgage: {
    type: DataTypes.TINYINT,
    allowNull: false,
    field: 'is_mortgage',
    comment: '是否抵押'
  },
  updateTime: {
    type: DataTypes.INTEGER,
    allowNull: false,
    field: 'update_time',
    comment: '更新时间'
  },
  breedBullTime: {
    type: DataTypes.INTEGER,
    allowNull: false,
    field: 'breed_bull_time',
    comment: '配种时间'
  },
  level: {
    type: DataTypes.TINYINT,
    allowNull: false,
    field: 'level',
    comment: '等级'
  },
  sixWeight: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false,
    field: 'six_weight',
    comment: '6月龄体重'
  },
  eighteenWeight: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false,
    field: 'eighteen_weight',
    comment: '18月龄体重'
  },
  twelveDayWeight: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false,
    field: 'twelve_day_weight',
    comment: '12日龄体重'
  },
  eighteenDayWeight: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false,
    field: 'eighteen_day_weight',
    comment: '18日龄体重'
  },
  xxivDayWeight: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false,
    field: 'xxiv_day_weight',
    comment: '24日龄体重'
  },
  semenBreedImgs: {
    type: DataTypes.STRING(800),
    allowNull: false,
    field: 'semen_breed_imgs',
    comment: '精液配种图片'
  },
  sellStatus: {
    type: DataTypes.SMALLINT,
    allowNull: false,
    field: 'sell_status',
    comment: '销售状态'
  },
  weightCalculateTime: {
    type: DataTypes.DATE,
    allowNull: true,
    field: 'weight_calculate_time',
    comment: '体重计算时间'
  },
  dayOfBirthday: {
    type: DataTypes.INTEGER,
    allowNull: true,
    field: 'day_of_birthday',
    comment: '出生天数'
  }
}, {
  tableName: 'iot_cattle',
  timestamps: false, // iot_cattle表没有created_at和updated_at字段
  comment: '物联网牛只表'
});

// 关联关系已在 models/index.js 中定义
// IotCattle.associate = (models) => {
//   // 关联到农场
//   IotCattle.belongsTo(models.Farm, {
//     foreignKey: 'orgId',
//     as: 'farm'
//   });

//   // 关联到批次
//   IotCattle.belongsTo(models.CattleBatch, {
//     foreignKey: 'batchId',
//     as: 'batch'
//   });

//   // 关联到栏舍
//   IotCattle.belongsTo(models.CattlePen, {
//     foreignKey: 'penId',
//     as: 'pen'
//   });

//   // 关联到围栏
//   IotCattle.belongsTo(models.ElectronicFence, {
//     foreignKey: 'fenceId',
//     as: 'fence'
//   });
// };

module.exports = IotCattle;
