|
- <?php
- /**
- * Created by PhpStorm.
- * User: admin
- * Date: 2017/2/14
- * Time: 17:59
- */
-
- namespace common\models;
-
- use Yii;
- use yii\base\Exception;
- use yii\base\UserException;
-
- /**
- * @property User $login_user
- *
- */
- class zModel extends \yii\db\ActiveRecord
- {
- public $login_user = null;
-
- public function __construct(array $config = [])
- {
- $user = Yii::$app->user->identity;
- //不能去除是User对象的判断,会死循环
- if ($user == null && get_called_class() !== 'common\models\User') {
- // Yii::$app->controller->redirect('http://' . CS_DOMAIN);
- $user = new User();
- $user->MAIN_CORP_ID2 = 9999;
- $this->login_user = $user;
- } else {
- $this->login_user = $user;
- }
- parent::__construct($config);
- }
-
- /**
- * User: wangxj
- *
- * 有cancel_flag字段的表,删除操作全部置cancel_flag为1
- *
- * @params
- *
- * @return
- */
- public function delete()
- {
- if (!$this->isTransactional(self::OP_DELETE)) {
- if (isset($this->cancel_flag)) {
- $this->cancel_flag = 1;
- $this->save(false, ['cancel_flag']);
- return true;
- } elseif (isset($this->CANCEL_FLAG)) {
- $this->CANCEL_FLAG = 1;
- $this->save(false, ['CANCEL_FLAG']);
- return true;
- } else
- return $this->deleteInternal();
- }
-
- $transaction = static::getDb()->beginTransaction();
- try {
- if (property_exists($this, 'cancel_flag')) {
- $this->cancel_flag = 1;
- $this->save(false, ['cancel_flag']);
- $result = true;
- } elseif (property_exists($this, 'CANCEL_FLAG')) {
- $this->CANCEL_FLAG = 1;
- $this->save(false, ['CANCEL_FLAG']);
- $result = true;
- } else {
- $result = $this->deleteInternal();
- }
- if ($result === false) {
- $transaction->rollBack();
- } else {
- $transaction->commit();
- }
- return $result;
- } catch (\Exception $e) {
- $transaction->rollBack();
- throw $e;
- }
- }
-
- /**
- * User: wangxj
- *
- * 修改cancel_flag标识用,默认修改为1
- *
- * @params
- *
- * @return
- */
- public function cancelFlag()
- {
- if (isset($this->cancel_flag)) {
- $this->cancel_flag = 1;
- $this->save(false, ['cancel_flag']);
- return true;
- } elseif (isset($this->CANCEL_FLAG)) {
- $this->CANCEL_FLAG = 1;
- $this->save(false, ['CANCEL_FLAG']);
- return true;
- }
- throw new UserException("系统错误");
- }
-
- public function validateBothRequired($attribute, $params)
- {
- if ($this->isNewRecord) {
- if ($this->send_bus_res_id == '' || $this->send_bus_driver_res_id == '')
- $this->addError($attribute, $params['message']);
- }
- }
-
- public function beforeSave($insert)
- {
- if ($this->isNewRecord) {
- if ($this->hasAttribute('create_time'))
- $this->create_time = date('Y-m-d H:i:s');
- if ($this->hasAttribute('CREATE_TIME'))
- $this->CREATE_TIME = date('Y-m-d H:i:s');
- if ($this->hasAttribute('create_user_id'))
- $this->create_user_id = isset($this->create_user_id) ? $this->create_user_id : Yii::$app->user->id;
- if ($this->hasAttribute('CREATE_USER_ID'))
- $this->CREATE_USER_ID = isset($this->CREATE_USER_ID) ? $this->CREATE_USER_ID : Yii::$app->user->id;
- }
- if ($this->hasAttribute('update_time'))
- $this->update_time = date('Y-m-d H:i:s');
- if ($this->hasAttribute('UPDATE_TIME'))
- $this->UPDATE_TIME = date('Y-m-d H:i:s');
- if ($this->hasAttribute('update_user_id'))
- $this->update_user_id = isset($this->update_user_id) ? $this->update_user_id : Yii::$app->user->id;
- if ($this->hasAttribute('UPDATE_USER_ID'))
- $this->UPDATE_USER_ID = isset($this->UPDATE_USER_ID) ? $this->UPDATE_USER_ID : Yii::$app->user->id;
- return parent::beforeSave($insert);
- }
-
- /**
- * User: wangxj
- *
- * 关于运营主体,根据情况,返回运营主体字符串
- * @param $main string 哪个系统的运营主体,1个账号同时存在 cs运营主体和车队运营主体
- * @param $result string|object
- * @param $type integer 0为字符串 1为对象
- *
- */
- public static function sqlMainCrop($main, &$result = null, $type = 0)
- {
- if (Yii::$app->user->isGuest) {
- throw new Exception('未登录!');
- }
- if ($type == 0) {
- $result = $result === null ? '' : $result;
- if (Yii::$app->user->identity->$main != 0) {
- $result .= " and $main = " . Yii::$app->user->identity->$main;
- }
- } else {
- $result->andFilterWhere([$main => Yii::$app->user->identity->$main]);
- }
-
- return $result;
- }
- }
|