<?php

namespace common\services;

use common\components\AjaxException;
use common\models\AccidentCasesDimension;
use Yii;
use yii\db\Exception;

class AccidentCasesDimensionService
{
    public static $typeList = [
        1, 2, 3, 4
    ];

    public static function initContent($accidentId, $dictionaryIdList)
    {
        $param = [
            "accident_id",
            "dictionary_id",
            "content",
            "type",
            "update_time",
            "delete_time",
        ];
        $time = time();
        $paramData = [];
        foreach ($dictionaryIdList as $dictionaryId) {
            foreach (self::$typeList as $type) {
                $paramData[] = [
                    "accident_id" => $accidentId,
                    "dictionary_id" => $dictionaryId,
                    "content" => "",
                    "type" => $type,
                    "update_time" => $time,
                    "delete_time" => 0,
                ];
            }
        }
        Yii::$app->db->createCommand()->batchInsert(AccidentCasesDimension::tableName(), $param, $paramData)->execute();
    }

    public static function getQuery()
    {
        return AccidentCasesDimension::find()->where(["delete_time" => 0]);
    }

    /**
     * @param $accidentId
     * @return AccidentCasesDimension[]
     */
    public static function getList($accidentId)
    {
        return self::getQuery()->andWhere(["accident_id" => $accidentId])->all();
    }

    /**
     * @throws AjaxException
     */
    public static function getById($id): AccidentCasesDimension
    {
        /** @var  AccidentCasesDimension $AccidentCasesScoreContent */
        $AccidentCasesScoreContent = self::getQuery()->andWhere(["id" => $id])->one();
        if (!$AccidentCasesScoreContent) {
            throw new AjaxException("该评价维度不存在！");
        }
        return $AccidentCasesScoreContent;
    }

    /**
     * @param $allParams
     * @param $userInfo
     * @return void
     * @throws AjaxException
     * @throws Exception
     */
    public static function update($allParams, $userInfo)
    {
        /** @var AccidentCasesDimension $model */
        $model = self::getQuery()->andWhere(["accident_id" => $allParams["accident_id"], "dictionary_id" => $allParams["dictionary_id"], "type" => $allParams["type"]])->one();
        if (!$model) {
            throw new AjaxException("该评价维度不存在！");
        }
        $case = CaseService::checkIsPublish($model->accident_id, $userInfo);
        $origin = AdminLogService::getOrigin($model, $allParams);
        $model->content = $allParams["content"];
        $model->update_time = time();
        if (!$model->save()) {
            throw new Exception($model->getErrorSummary(true)[0]);
        }
        AdminLogService::saveLogWithUpdateHistory($origin, $model, $case->id, $case->title, $allParams);
    }

    public static function deleteByAccidentCasesId($accident_id): void
    {
        AccidentCasesDimension::updateAll(["delete_time" => time()], ["accident_id" => $accident_id]);
    }
}