WheelPairs.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace app\models\entity;
  3. use Yii;
  4. use app\models\WpLogs;
  5. /**
  6. * This is the model class for table "so_wheel_pairs".
  7. *
  8. * @property int $id
  9. * @property string $token
  10. * @property string $number_mnf
  11. * @property int $loco_id
  12. * @property string $location
  13. * @property int $dep_loco_number [int(10)]
  14. * @property int $dep_section_number [int(10)]
  15. * @property int $depo_id [smallint(5)]
  16. * @property int $account_id [int(5)]
  17. * @property int $section_id [int(5)]
  18. *
  19. * @property Section $section
  20. * @property LocomotiveSeries $loco
  21. */
  22. class WheelPairs extends \yii\db\ActiveRecord
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function tableName()
  28. {
  29. return 'so_wheel_pairs';
  30. }
  31. public static function getDb()
  32. {
  33. return \Yii::$app->slave_db;
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function rules()
  39. {
  40. return [
  41. [[ 'location'], 'required'],
  42. [[ 'location'], 'safe'],
  43. //['token', 'default', 'value' => 'Отсутствует'],
  44. ['number_mnf', 'default', 'value' => 'Отсутствует'],
  45. //[['loco_id'], 'integer'],
  46. [['token'], 'string', 'max' => 255],
  47. [['number_mnf'], 'string', 'max' => 55],
  48. [['location'], 'string', 'max' => 5],
  49. //['token', 'unique'],
  50. [['token','section_id'], 'unique', 'targetAttribute' => ['token', 'section_id']],
  51. [['location','section_id'], 'unique', 'targetAttribute' => ['location', 'section_id']],
  52. //[['loco_id'], 'exist', 'skipOnError' => true, 'targetClass' => Locomotive::className(), 'targetAttribute' => ['loco_id' => 'id']],
  53. ];
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function attributeLabels()
  59. {
  60. return [
  61. 'id' => 'ID',
  62. 'token' => 'Номер метки',
  63. 'number_mnf' => 'Number Mnf',
  64. 'loco_id' => 'Loco ID',
  65. 'location' => 'Location',
  66. ];
  67. }
  68. /**
  69. * @return \yii\db\ActiveQuery
  70. */
  71. public function getLoco()
  72. {
  73. return $this->hasOne(LocomotiveSeries::class, ['id' => 'loco_id']);
  74. }
  75. /**
  76. * @return \yii\db\ActiveQuery
  77. */
  78. public function getOilstat()
  79. {
  80. return $this->hasMany(Oilstat::class, ['token' => 'token'])->orderBy(['date' => SORT_DESC]);
  81. }
  82. /**
  83. * @return \yii\db\ActiveQuery
  84. */
  85. public function getSection()
  86. {
  87. return $this->hasOne(Section::class, ['id' => 'section_id']);
  88. }
  89. /**
  90. * @param WheelPairs[] $wheelPairs
  91. * @param Oilstat[] $oilstat
  92. */
  93. public function updateWheelPair(array $wheelPairs, array $oilstat, $id = null )
  94. {
  95. $count = 0;
  96. $oilstat = array_reverse($oilstat);
  97. foreach ($wheelPairs as $pair) {
  98. $WpLogs = new WpLogs();
  99. $WpLogs->id_session = $id;
  100. $WpLogs->old_val = 'token => ' . $pair->token . ' loco_id => ' . $pair->loco_id . ' location => ' . $pair->location . ' section_id => ' . $pair->section_id;
  101. $pair->property = '';
  102. if (in_array($oilstat[$count]->token, Yii::$app->params['materials'])) {
  103. $pair->property = $oilstat[$count]->token;
  104. $pair->token = 'null/'.$pair->location .'/'. $pair->section_id;
  105. } else {
  106. $pair->token = $oilstat[$count]->token;
  107. }
  108. $WpLogs->new_val = 'token => ' . $pair->token . ' loco_id => ' . $pair->loco_id . ' location => ' . $pair->location . ' section_id => ' . $pair->section_id;
  109. $WpLogs->save();
  110. //var_dump('prop - ' . $pair->property,'token - '.$pair->token, 'time - '. $oilstat[$count]->date,'location - '. $pair->location);
  111. $pair->save();
  112. //var_dump($pair->errors);
  113. $count++;
  114. }
  115. }
  116. }