You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

166 line
5.1 KiB

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: admin
  5. * Date: 2017/2/14
  6. * Time: 17:59
  7. */
  8. namespace common\models;
  9. use Yii;
  10. use yii\base\Exception;
  11. use yii\base\UserException;
  12. /**
  13. * @property User $login_user
  14. *
  15. */
  16. class zModel extends \yii\db\ActiveRecord
  17. {
  18. public $login_user = null;
  19. public function __construct(array $config = [])
  20. {
  21. $user = Yii::$app->user->identity;
  22. //不能去除是User对象的判断,会死循环
  23. if ($user == null && get_called_class() !== 'common\models\User') {
  24. // Yii::$app->controller->redirect('http://' . CS_DOMAIN);
  25. $user = new User();
  26. $user->MAIN_CORP_ID2 = 9999;
  27. $this->login_user = $user;
  28. } else {
  29. $this->login_user = $user;
  30. }
  31. parent::__construct($config);
  32. }
  33. /**
  34. * User: wangxj
  35. *
  36. * 有cancel_flag字段的表,删除操作全部置cancel_flag为1
  37. *
  38. * @params
  39. *
  40. * @return
  41. */
  42. public function delete()
  43. {
  44. if (!$this->isTransactional(self::OP_DELETE)) {
  45. if (isset($this->cancel_flag)) {
  46. $this->cancel_flag = 1;
  47. $this->save(false, ['cancel_flag']);
  48. return true;
  49. } elseif (isset($this->CANCEL_FLAG)) {
  50. $this->CANCEL_FLAG = 1;
  51. $this->save(false, ['CANCEL_FLAG']);
  52. return true;
  53. } else
  54. return $this->deleteInternal();
  55. }
  56. $transaction = static::getDb()->beginTransaction();
  57. try {
  58. if (property_exists($this, 'cancel_flag')) {
  59. $this->cancel_flag = 1;
  60. $this->save(false, ['cancel_flag']);
  61. $result = true;
  62. } elseif (property_exists($this, 'CANCEL_FLAG')) {
  63. $this->CANCEL_FLAG = 1;
  64. $this->save(false, ['CANCEL_FLAG']);
  65. $result = true;
  66. } else {
  67. $result = $this->deleteInternal();
  68. }
  69. if ($result === false) {
  70. $transaction->rollBack();
  71. } else {
  72. $transaction->commit();
  73. }
  74. return $result;
  75. } catch (\Exception $e) {
  76. $transaction->rollBack();
  77. throw $e;
  78. }
  79. }
  80. /**
  81. * User: wangxj
  82. *
  83. * 修改cancel_flag标识用,默认修改为1
  84. *
  85. * @params
  86. *
  87. * @return
  88. */
  89. public function cancelFlag()
  90. {
  91. if (isset($this->cancel_flag)) {
  92. $this->cancel_flag = 1;
  93. $this->save(false, ['cancel_flag']);
  94. return true;
  95. } elseif (isset($this->CANCEL_FLAG)) {
  96. $this->CANCEL_FLAG = 1;
  97. $this->save(false, ['CANCEL_FLAG']);
  98. return true;
  99. }
  100. throw new UserException("系统错误");
  101. }
  102. public function validateBothRequired($attribute, $params)
  103. {
  104. if ($this->isNewRecord) {
  105. if ($this->send_bus_res_id == '' || $this->send_bus_driver_res_id == '')
  106. $this->addError($attribute, $params['message']);
  107. }
  108. }
  109. public function beforeSave($insert)
  110. {
  111. if ($this->isNewRecord) {
  112. if ($this->hasAttribute('create_time'))
  113. $this->create_time = date('Y-m-d H:i:s');
  114. if ($this->hasAttribute('CREATE_TIME'))
  115. $this->CREATE_TIME = date('Y-m-d H:i:s');
  116. if ($this->hasAttribute('create_user_id'))
  117. $this->create_user_id = isset($this->create_user_id) ? $this->create_user_id : Yii::$app->user->id;
  118. if ($this->hasAttribute('CREATE_USER_ID'))
  119. $this->CREATE_USER_ID = isset($this->CREATE_USER_ID) ? $this->CREATE_USER_ID : Yii::$app->user->id;
  120. }
  121. if ($this->hasAttribute('update_time'))
  122. $this->update_time = date('Y-m-d H:i:s');
  123. if ($this->hasAttribute('UPDATE_TIME'))
  124. $this->UPDATE_TIME = date('Y-m-d H:i:s');
  125. if ($this->hasAttribute('update_user_id'))
  126. $this->update_user_id = isset($this->update_user_id) ? $this->update_user_id : Yii::$app->user->id;
  127. if ($this->hasAttribute('UPDATE_USER_ID'))
  128. $this->UPDATE_USER_ID = isset($this->UPDATE_USER_ID) ? $this->UPDATE_USER_ID : Yii::$app->user->id;
  129. return parent::beforeSave($insert);
  130. }
  131. /**
  132. * User: wangxj
  133. *
  134. * 关于运营主体,根据情况,返回运营主体字符串
  135. * @param $main string 哪个系统的运营主体,1个账号同时存在 cs运营主体和车队运营主体
  136. * @param $result string|object
  137. * @param $type integer 0为字符串 1为对象
  138. *
  139. */
  140. public static function sqlMainCrop($main, &$result = null, $type = 0)
  141. {
  142. if (Yii::$app->user->isGuest) {
  143. throw new Exception('未登录!');
  144. }
  145. if ($type == 0) {
  146. $result = $result === null ? '' : $result;
  147. if (Yii::$app->user->identity->$main != 0) {
  148. $result .= " and $main = " . Yii::$app->user->identity->$main;
  149. }
  150. } else {
  151. $result->andFilterWhere([$main => Yii::$app->user->identity->$main]);
  152. }
  153. return $result;
  154. }
  155. }