<?php


namespace frontend\modules\api\controllers;

use common\models\OperationalAdjustments;
use common\services\AccidentCasesScoreService;
use common\services\AdminLogService;
use common\services\CaseService;
use common\services\EmergencyResponseService;
use common\services\OperationalAdjustmentsDimensionService;
use common\services\OperationalAdjustmentsRelateToRulesPointsService;
use common\services\OperationalAdjustmentsService;
use common\util\DealWithPostData;
use frontend\modules\api\components\BaseAdminController;
use Throwable;
use Yii;
use yii\base\InvalidConfigException;
use yii\db\Exception;
use yii\web\Response;

class OperationalAdjustmentsController extends BaseAdminController
{
    /**
     * 运营调整列表
     * @param $accident_id
     * @return Response
     */
    public function actionList($accident_id)
    {
        $query = OperationalAdjustmentsService::getQuery()->andWhere(["accident_id" => $accident_id]);
        //排序
        $this->initSequence($query, "start_time desc");
        //主要筛选条件
        $query = $this->addConditionToQuery("type_first", $query);
        //组合数据
        $list = OperationalAdjustmentsService::dealWithList($query->all(), $accident_id);
        OperationalAdjustmentsService::listAddDimension($list);
        $list = EmergencyResponseService::getEmergencyResponseObjList($list);
        $data['data'] = $list;
        list($data['scoreList'], $data['totalScore']) = AccidentCasesScoreService::getOperationalAdjustmentsScore($accident_id);
        return $this->asJson($data);
    }

    /**
     * 添加运营调整
     * @return Response
     * @throws Exception
     * @throws Throwable
     */
    public function actionAdd()
    {
        $post = Yii::$app->request->post();
        $post = DealWithPostData::changePostDataArrayToJson($post);
        $case = CaseService::checkIsPublish($post["accident_id"], $this->userInfo);
        $param = [
            "accident_id" => $post["accident_id"],
            "assess" => $post["assess"],
            "assess_two" => $post["assess_two"],
            "content" => $post["content"],
            "important_content" => $post["important_content"],
            "is_important" => $post["is_important"] ? 1 : 0,
            "is_time_period" => isset($post["is_time_period"]) && $post["is_time_period"] ? 1 : 0,
            "end_time" => isset($post["end_time"]) && $post["end_time"] !== "" ? $post["end_time"] : 0,
            "rules" => $post["rules"],
            "source_data" => $post["source_data"],
            "start_time" => $post["start_time"],
            "type_first" => $post["type_first"],
            "type_second" => $post["type_second"],
            "type_third" => $post["type_third"],
            "type_fourth" => $post["type_fourth"],
        ];
        $model = new OperationalAdjustments($param);
        $origin = AdminLogService::getOrigin($model, $this->getAllParams(), true);
        Yii::$app->db->transaction(function () use ($model) {
            if (!$model->save()) {
                throw new Exception($model->getErrorSummary(true)[0]);
            }
            //将运营调整与规章表的关系添加
            OperationalAdjustmentsRelateToRulesPointsService::add($model);
            //评价维度表添加
            OperationalAdjustmentsDimensionService::add($model);
        });
        AdminLogService::saveLogWithUpdateHistory($origin, $model, $case->id, $case->title, $this->getAllParams());
        return $this->asJson();
    }

    /**
     * 运营调整修改
     * @param $id
     * @return Response
     * @throws InvalidConfigException
     * @throws Exception
     * @throws Throwable
     */
    public function actionUpdate($id)
    {
        $model = OperationalAdjustmentsService::getOperationalAdjustmentsById($id);
        $origin = AdminLogService::getOrigin($model, $this->getAllParams());
        $case = CaseService::checkIsPublish($model->accident_id, $this->userInfo);
        Yii::$app->request->setBodyParams(DealWithPostData::changePostDataArrayToJson(Yii::$app->request->post()));
        $this->setAttributeFromGetAndPost($model);
        Yii::$app->db->transaction(function () use ($model) {
            if (!$model->save()) {
                throw new Exception($model->getErrorSummary(true)[0]);
            }
            //将运营调整与规章表的关系修改
            OperationalAdjustmentsRelateToRulesPointsService::update($model);
        });
        AdminLogService::saveLogWithUpdateHistory($origin, $model, $case->id, $case->title, $this->getAllParams());
        return $this->asJson();
    }

    /**
     * 运营调整删除
     * @param $id
     * @return Response
     * @throws Exception
     * @throws Throwable
     */
    public function actionDelete($id)
    {
        $model = OperationalAdjustmentsService::getOperationalAdjustmentsById($id);
        $case = CaseService::checkIsPublish($model->accident_id, $this->userInfo);
        $model->delete_time = time();
        Yii::$app->db->transaction(function () use ($model) {
            if (!$model->save()) {
                throw new Exception($model->getErrorSummary(true)[0]);
            }
            //将运营调整与规章表的关系删除
            OperationalAdjustmentsRelateToRulesPointsService::deleteByOperationalAdjustmentsId($model->id);
            //删除评价维度
            OperationalAdjustmentsDimensionService::deleteByOperationalAdjustmentsId($model->id);
        });
        //返回数据
        $origin = AdminLogService::getOrigin($model, $this->getAllParams());
        AdminLogService::saveLogWithUpdateHistory($origin, $model, $case->id, $case->title, $this->getAllParams());
        return $this->asJson();
    }
}