<?php


namespace common\services;


use common\components\AjaxException;
use common\models\EmergencyResponse;
use common\models\OperationalAdjustments;
use common\util\EmergencyResponseDimensionObj;
use Exception;
use yii\db\ActiveQuery;

class OperationalAdjustmentsService
{
    /**
     * @param $id
     * @return OperationalAdjustments
     * @throws AjaxException
     */
    public static function getOperationalAdjustmentsById($id)
    {
        /** @var OperationalAdjustments $OperationalAdjustments */
        $OperationalAdjustments = OperationalAdjustments::find()->where(["delete_time" => 0, "id" => $id])->one();
        if (!$OperationalAdjustments) {
            throw new AjaxException("该运营调整不存在！");
        }
        return $OperationalAdjustments;
    }

    /**
     * @return ActiveQuery
     */
    public static function getQuery(): ActiveQuery
    {
        return OperationalAdjustments::find()->where(["delete_time" => 0]);
    }

    /**
     * 通过案例ID删除应急处置
     * @param $accident_id
     */
    public static function deleteByAccidentCasesId($accident_id): void
    {
        OperationalAdjustments::updateAll(["delete_time" => time()], ["accident_id" => $accident_id]);
    }

    /**
     * @param $data
     * @param $accident_id
     * @return array
     */
    public static function dealWithList($data, $accident_id): array
    {
        $sourceDataList = array_column(EventSourceDataService::getSourceDataListByAccidentId($accident_id), null, "id");
        /** @var EmergencyResponse[]|OperationalAdjustments[] $data */
        foreach ($data as $value) {
            //将数组从json转化出来
            self::dealWithRules($value);
            self::dealWithSourceData($value, $sourceDataList);
            if ($value->end_time == 0) {
                $value->end_time = "";
            }
        }
        return $data;
    }

    public static function listAddDimension($data): void
    {
        $OperationalAdjustmentsIdList = [];
        foreach ($data as $value) {
            $OperationalAdjustmentsIdList[] = $value->id;
        }
        $newOperationalAdjustmentsDimensionList = [];
        $OperationalAdjustmentsDimensionList = OperationalAdjustmentsDimensionService::getListByOperationalAdjustmentsId($OperationalAdjustmentsIdList);
        foreach ($OperationalAdjustmentsDimensionList as $OperationalAdjustmentsDimension) {
            $newOperationalAdjustmentsDimensionList[$OperationalAdjustmentsDimension->operational_adjustments_id][] = new EmergencyResponseDimensionObj($OperationalAdjustmentsDimension);
        }
        foreach ($data as $value) {
            $value->dimension = $newOperationalAdjustmentsDimensionList[$value->id];
        }
    }

    public static function dealWithRules($model)
    {
        $model->rules = json_decode($model->rules, true);
    }

    public static function dealWithSourceData($model, $sourceDataList)
    {
        $sourceDataIdList = json_decode($model->source_data, true);
        $sourceDataObj = [];
        foreach ($sourceDataIdList as $sourceDataId) {
            if (isset($sourceDataList[$sourceDataId])) {
                $sourceDataObj[] = $sourceDataList[$sourceDataId];
            }
        }
        $model->source_data = $sourceDataObj;
    }
}