<?php
namespace common\models\forms;

use common\components\Model;
use common\helpers\Form;
use common\libs\logging\operate\AdminOperate;
use common\models\Period;
use common\models\UserPerf;
use yii\base\Exception;

class PerfAdjustmentForm extends Model
{
    public $USER_ID;
    public $USER_NAME;

    public $SURPLUS_1L;
    public $SURPLUS_1L_ZC;
    public $SURPLUS_1L_FX;

    public $SURPLUS_2L;
    public $SURPLUS_2L_ZC;
    public $SURPLUS_2L_FX;

    public $SURPLUS_3L;
    public $SURPLUS_3L_ZC;
    public $SURPLUS_3L_FX;

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

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['USER_ID', 'SURPLUS_1L', 'SURPLUS_1L_ZC', 'SURPLUS_1L_FX', 'SURPLUS_2L', 'SURPLUS_2L_ZC', 'SURPLUS_2L_FX', 'SURPLUS_3L', 'SURPLUS_3L_ZC', 'SURPLUS_3L_FX'], 'required'],
        ];
    }

    /**
     * 指定场景
     * @return array
     */
    public function scenarios() {
        $parentScenarios = parent::scenarios();
        $customScenarios = [
            'perfAdjustment' => ['USER_ID', 'USER_NAME', 'SURPLUS_1L', 'SURPLUS_1L_ZC', 'SURPLUS_1L_FX', 'SURPLUS_2L', 'SURPLUS_2L_ZC', 'SURPLUS_2L_FX', 'SURPLUS_3L', 'SURPLUS_3L_ZC', 'SURPLUS_3L_FX']
        ];
        return array_merge($parentScenarios, $customScenarios);
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'USER_ID'       => '会员ID',
            'SURPLUS_1L'    => '一市场综合结余业绩',
            'SURPLUS_1L_ZC' => '一市场综合报单业绩',
            'SURPLUS_1L_FX' => '一市场复消结余业绩',
            'SURPLUS_2L'    => '二市场综合结余业绩',
            'SURPLUS_2L_ZC' => '二市场综合报单业绩',
            'SURPLUS_2L_FX' => '二市场复消结余业绩',
            'SURPLUS_3L'    => '三市场综合结余业绩',
            'SURPLUS_3L_ZC' => '三市场综合报单业绩',
            'SURPLUS_3L_FX' => '三市场复消结余业绩',
        ];
    }

    /**
     * 调整会员业绩
     * @return bool|null
     */
    public function perfAdjustment(): ?bool
    {
        if (!$this->validate()) {
            return false;
        }

        $transaction = \Yii::$app->db->beginTransaction();
        try {
            // 修改前业绩
            $userPerf = UserPerf::find()
                ->where('USER_ID=:USER_ID', [':USER_ID' => $this->USER_ID])
                ->select('USER_ID,SURPLUS_1L,SURPLUS_1L_ZC,SURPLUS_1L_FX,SURPLUS_2L,SURPLUS_2L_ZC,SURPLUS_2L_FX,SURPLUS_3L,SURPLUS_3L_ZC,SURPLUS_3L_FX')
                ->one();
            if (!$userPerf->toArray()) {
                throw new Exception('Member performance does not exist'); // 会员业绩不存在
            }

            $this->adminOperateLogger->beforeUpdate($userPerf);

            $modernPerf = [
                'SURPLUS_1L'    => $this->SURPLUS_1L,
                'SURPLUS_1L_ZC' => $this->SURPLUS_1L_ZC,
                'SURPLUS_1L_FX' => $this->SURPLUS_1L_FX,
                'SURPLUS_2L'    => $this->SURPLUS_2L,
                'SURPLUS_2L_ZC' => $this->SURPLUS_2L_ZC,
                'SURPLUS_2L_FX' => $this->SURPLUS_2L_FX,
                'SURPLUS_3L'    => $this->SURPLUS_3L,
                'SURPLUS_3L_ZC' => $this->SURPLUS_3L_ZC,
                'SURPLUS_3L_FX' => $this->SURPLUS_3L_FX,
            ];
            if (!UserPerf::updateAll($modernPerf, 'USER_ID=:USER_ID', [':USER_ID' => $this->USER_ID])) {
                throw new Exception(Form::formatErrorsForApi($userPerf->getErrors()));
            }

            $transaction->commit();

            $afterUpdate = UserPerf::find()
                ->where('USER_ID=:USER_ID', [':USER_ID' => $this->USER_ID])
                ->select('USER_ID,SURPLUS_1L,SURPLUS_1L_ZC,SURPLUS_1L_FX,SURPLUS_2L,SURPLUS_2L_ZC,SURPLUS_2L_FX,SURPLUS_3L,SURPLUS_3L_ZC,SURPLUS_3L_FX')
                ->one();
            $this->adminOperateLogger->afterUpdate($afterUpdate)->clean()->save([
                'optType' => '调整业绩',
                'userId' => $this->USER_ID,
                'userName' => $this->USER_NAME,
                'periodNum' => Period::instance()->getNowPeriodNum(),
            ]);
        } catch (Exception $e) {
            $transaction->rollBack();
            $this->addError('perfAdjustment', $e->getMessage());
            return false;
        }

        return true;
    }

}
