<?php

namespace common\models\forms;

use common\components\Model;
use common\helpers\Date;
use common\libs\logging\operate\AdminOperate;
use common\models\User;
use common\models\UserInfo;
use yii\base\Exception;

/**
 * Login form
 */
class UserDecForm extends Model {
    public $userIds;
    public $isDec;
    public $isStudio;
    public $isAtlas;
    public $isRecharge;

    public function init() {
        parent::init();
        $this->adminOperateLogger = new AdminOperate([
            'fetchClass' => User::class,
        ]);
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['userIds', 'isDec','isStudio', 'isAtlas', 'isRecharge'], 'trim'],
            [['userIds'], 'required'],
        ];
    }

    /**
     * 指定校验场景
     * @return array
     */
    public function scenarios() {
        $parentScenarios = parent::scenarios();
        $customScenarios = [
            'isDec' => ['userIds', 'isDec'],
            'isAtlas' => ['userIds', 'isAtlas'],
            'isStudio' => ['userIds', 'isStudio'],
            'isRecharge' => ['userIds', 'isRecharge'],
        ];
        return array_merge($parentScenarios, $customScenarios);
    }

    public function attributeLabels() {
        return [
            'userIds' => '会员ID',
            'isDec' => '是否报单中心',
            'isStudio' => '是否工作室',
            'isAtlas' => '是否显示图谱',
            'isRecharge' => '是否显示充值',
        ];
    }

    public function beforeValidate() {
        return parent::beforeValidate();
    }

    /**
     * 是否报单中心
     * @return null
     * @throws \yii\db\Exception
     */
    public function isDec() {
        if (!$this->validate()) {
            return null;
        }
        $this->adminOperateLogger->setIsBatch(true)->beforeUpdate($this->userIds, 'ID',['select'=>'ID,IS_DEC']);
        $db = \Yii::$app->db;
        $transaction = $db->beginTransaction();
        try {
            $allUidIn = implode("','", $this->userIds);
            if (!User::updateAll(['IS_DEC' => $this->isDec ? $this->isDec : 0], "ID IN ('" . $allUidIn . "')")) {
                throw new Exception('设置报单中心更新失败');
            }
            $transaction->commit();
        } catch (Exception $e) {
            $transaction->rollBack();
            return null;
        }
        $this->adminOperateLogger->setIsBatch(true)->afterUpdate($this->userIds, 'ID',['select'=>'ID,IS_DEC']);
        $this->adminOperateLogger->setBatchField('USER_ID')->clean()->save([
            'optType' => $this->isDec ? '设置报单中心' : '取消报单中心',
        ]);



        return $this->userIds;
    }

    /**
     * 是否工作室
     * @return null
     * @throws \yii\db\Exception
     */
    public function isStudio() {
        if (!$this->validate()) {
            return null;
        }
        $this->adminOperateLogger->setIsBatch(true)->beforeUpdate($this->userIds, 'ID',['select'=>'ID,IS_STUDIO']);
        $db = \Yii::$app->db;
        $transaction = $db->beginTransaction();
        try {
            $allUidIn = implode("','", $this->userIds);
            if (!User::updateAll(['IS_STUDIO' => $this->isStudio ? $this->isStudio : 0], "ID IN ('" . $allUidIn . "')")) {
                throw new Exception('设置工作室更新失败');
            }
            $transaction->commit();
        } catch (Exception $e) {
            $transaction->rollBack();
            return null;
        }
        $this->adminOperateLogger->setIsBatch(true)->afterUpdate($this->userIds, 'ID',['select'=>'ID,IS_STUDIO']);
        $this->adminOperateLogger->setBatchField('USER_ID')->clean()->save([
            'optType' => $this->isStudio ? '设置工作室' : '取消工作室',
        ]);
        return $this->userIds;
    }

    /**
     * 是否显示图谱
     * @return null
     * @throws \yii\db\Exception
     */
    public function isAtlas() {
        if (!$this->validate()) {
            return null;
        }
        $this->adminOperateLogger->setIsBatch(true)->beforeUpdate($this->userIds, 'ID',['select'=>'ID,IS_ATLAS']);
        $db = \Yii::$app->db;
        $transaction = $db->beginTransaction();
        try {
            $allUidIn = implode("','", $this->userIds);
            if (!User::updateAll(['IS_ATLAS' => $this->isAtlas ? $this->isAtlas : 0], "ID IN ('" . $allUidIn . "')")) {
                throw new Exception('设置图谱更新失败');
            }
            $transaction->commit();
        } catch (Exception $e) {
            $transaction->rollBack();
            return null;
        }
        $this->adminOperateLogger->setIsBatch(true)->afterUpdate($this->userIds, 'ID',['select'=>'ID,IS_ATLAS']);
        $this->adminOperateLogger->setBatchField('USER_ID')->clean()->save([
            'optType' => $this->isAtlas ? '显示图谱' : '隐藏图谱',
        ]);



        return $this->userIds;
    }

    /**
     * 是否显示充值
     * @return null
     * @throws \yii\db\Exception
     */
    public function isRecharge() {
        if (!$this->validate()) {
            return null;
        }
        $this->adminOperateLogger->setIsBatch(true)->beforeUpdate($this->userIds, 'ID',['select'=>'ID,IS_RECHARGE']);
        $db = \Yii::$app->db;
        $transaction = $db->beginTransaction();
        try {
            $allUidIn = implode("','", $this->userIds);
            if (!User::updateAll(['IS_RECHARGE' => $this->isRecharge ? $this->isRecharge : 0], "ID IN ('" . $allUidIn . "')")) {
                throw new Exception('充值管理设置更新失败');
            }
            $transaction->commit();
        } catch (Exception $e) {
            $transaction->rollBack();
            return null;
        }
        $this->adminOperateLogger->setIsBatch(true)->afterUpdate($this->userIds, 'ID',['select'=>'ID,IS_RECHARGE']);
        $this->adminOperateLogger->setBatchField('USER_ID')->clean()->save([
            'optType' => $this->isRecharge ? '显示充值' : '隐藏充值',
        ]);
        return $this->userIds;
    }

}
