<?php

namespace frontend\modules\api\controllers;

use common\models\BaseRole;
use common\models\Department;
use common\models\Job;
use frontend\modules\api\components\BaseAdminController;
use yii\web\Response;

class SystemController extends BaseAdminController
{
    /**
     * 部门列表
     * @param int $current
     * @param int $page
     * @param bool $call
     * @return Response
     */
    public function actionDepartmentList(int $current = 1, int $page = 10, bool $call = false)
    {
        //如果是调用
        if ($call) {
            $query = Department::find()
                ->select("id,department_name")
                ->where(['department_status' => 1])
                ->orderBy("id desc");
            $data['data'] = $query->asArray()->all();
            return $this->asJson($data);
        }
        //公共部分
        $query = Department::find()
            ->limit($page)
            ->offset(($current - 1) * $page);
        //排序
        $this->initSequence($query, "id desc", "sequenceDepartment");
        //主要筛选条件
        $query = $this->addConditionToQuery("department_name", $query, true);
        $query = $this->addConditionToQuery("department_status", $query);
        $data['data'] = $query->asArray()->all();
        $data['total'] = $query->count();
        return $this->asJson($data);
    }

    /**
     * 部门添加
     * @param null $department_name
     * @param null $department_status
     * @return Response
     */
    public function actionDepartmentAdd($department_name = null, $department_status = null)
    {
        // 基础校验
        $info = Department::findOne(['department_name' => $department_name]);
        if ($info) {
            return $this->asJson([], 1, "该部门：{$department_name} 已存在！"); //通知前端
        }
        $time = strtotime('now');
        //插入
        $params = [
            'department_name' => $department_name,
            'department_status' => $department_status,
            'create_time' => date("Y-m-d H:i:s", $time),
        ];
        $insertInfo = new Department($params);
        $insertInfo->save();
        //返回
        return $this->asJson();
    }

    /**
     * 部门修改
     * @param null $department_name
     * @param null $department_status
     * @return Response
     */
    public function actionDepartmentUpdate($id, $department_name = null, $department_status = null)
    {
        // 基础校验
        $info = Department::findOne(['id' => $id]);
        if (!$info) {
            return $this->asJson([], 1, "没有这条记录！"); //通知前端
        }
        $ifUpdateName = false;
        if ($info->department_name !== $department_name) {
            //意味着没有变更名称
            $ifUpdateName = true;
        }
        $infoName = Department::find()
            ->where(['department_name' => $department_name])
            ->andWhere("id <> $id")
            ->one();
        if ($infoName) {
            return $this->asJson([], 1, "该部门：{$department_name} 已存在！"); //通知前端
        }
        //修改
        if ($ifUpdateName) {
            $info->department_name = $department_name;
        }
        $info->department_status = $department_status;
        $info->save();
        //返回
        return $this->asJson();
    }

    /**
     * 岗位列表
     * @param int $current
     * @param int $page
     * @param bool $call
     * @return Response
     */
    public function actionJobList(int $current = 1, int $page = 10, bool $call = false)
    {
        //如果是调用
        if ($call) {
            $query = Job::find()
                ->select("id,job_name")
                ->where(['job_status' => 1])
                ->orderBy("id desc");
            $data['data'] = $query->asArray()->all();
            return $this->asJson($data);
        }
        //公共部分
        $query = Job::find()
            ->limit($page)
            ->offset(($current - 1) * $page);
        //排序
        $this->initSequence($query, "id desc", "sequenceJob");
        //主要筛选条件
        $query = $this->addConditionToQuery("job_name", $query, true);
        $query = $this->addConditionToQuery("job_status", $query);
        $data['data'] = $query->asArray()->all();
        $data['total'] = $query->count();
        return $this->asJson($data);
    }

    /**
     * 岗位添加
     * @param null $job_name
     * @param null $job_status
     * @return Response
     */
    public function actionJobAdd($job_name = null, $job_status = null)
    {
        // 基础校验
        $info = Job::findOne(['job_name' => $job_name]);
        if ($info) {
            return $this->asJson([], 1, "该岗位：{$job_name} 已存在！"); //通知前端
        }
        $time = strtotime('now');
        //插入
        $params = [
            'job_name' => $job_name,
            'job_status' => $job_status,
            'create_time' => date("Y-m-d H:i:s", $time),
        ];
        $insertInfo = new Job($params);
        $insertInfo->save();
        //返回
        return $this->asJson();
    }

    /**
     * 岗位修改
     * @param null $job_name
     * @param null $job_status
     * @return Response
     */
    public function actionJobUpdate($id, $job_name = null, $job_status = null)
    {
        // 基础校验
        $info = Job::findOne(['id' => $id]);
        if (!$info) {
            return $this->asJson([], 1, "没有这条记录！"); //通知前端
        }
        $ifUpdateName = false;
        if ($info->job_name !== $job_name) {
            //意味着没有变更名称
            $ifUpdateName = true;
        }
        $infoName = Job::find()
            ->where(['job_name' => $job_name])
            ->andWhere("id <> $id")
            ->one();
        if ($infoName) {
            return $this->asJson([], 1, "该部门：{$job_name} 已存在！"); //通知前端
        }
        //修改
        if ($ifUpdateName) {
            $info->job_name = $job_name;
        }
        $info->job_status = $job_status;
        $info->save();
        //返回
        return $this->asJson();
    }

    /**
     * 类型列表
     * @return Response
     */
    public function actionRolesList()
    {
        $query = BaseRole::find()
            ->select("id,name as role_name")
            ->where("id > 1")
            ->orderBy("id desc");
        $data['data'] = $query->asArray()->all();
        return $this->asJson($data);
    }
}