<?php

namespace app\admin\controller\Dictionary;

use app\common\arw\adjfut\src\Excel;
use app\common\arw\adjfut\src\UploadFile;
use app\common\arw\adjfut\src\Traverse;
use app\BaseController;
use app\exception\ErrorMsg;
use app\common\model\Dictionary\Dictionary as ModelDictionary;
use app\Request;

class Dictionary extends BaseController
{
    /**
     * 获取字典列表
     *
     * @param Request $request
     * @date 2022-03-14
     * @example
     * @author admin
     * @since 1.0.0
     */
    public function getDictionaryList(Request $request): array
    {
        $select = ModelDictionary::where('dictionary_parent_guid', 0)->order([
            'dictionary_index',
            'dictionary_order' => 'desc',
        ])->select();
        return [
            'code' => 0,
            'msg' => 'ok',
            'data' => $select
        ];
    }

    /**
     * 获取菜单树
     *
     * @param Request $request
     * @return array
     * @date 2022-03-05
     * @example
     * @author admin
     * @since 1.0.0
     */
    public function getDictionaryTree(Request $request)
    {
        $dictionary = ModelDictionary::withoutGlobalScope(['status'])->order([
            'dictionary_index',
            'dictionary_order' => 'desc',
        ])->field([
            'dictionary_parent_guid',
            'dictionary_name',
            'dictionary_index',
            'dictionary_value',
            'dictionary_order',
            'dictionary_status',
            'dictionary_allow_update',
            'dictionary_list_class',
            'dictionary_guid',
        ])->select()->toArray();

        array_unshift($dictionary, [
            'dictionary_guid' => '0',
            'dictionary_parent_guid' => '-1',
            'dictionary_name' => '系统字典',
            'dictionary_index' => 0,
            'dictionary_value' => '',
            'dictionary_order' => 0,
            'dictionary_status' => 0,
            'dictionary_allow_update' => 2,
            'dictionary_list_class' => '',
        ]);

        $Traverse = new Traverse('dictionary_guid', 'dictionary_parent_guid');
        $dictionaryTree = $Traverse->tree($dictionary, '-1', function ($v) {
            return [
                'label' => $v['dictionary_name'],
                'id' => $v['dictionary_guid'],
                'parent_id' => $v['dictionary_parent_guid'],
                'index' => $v['dictionary_index'],
                'order' => $v['dictionary_order'],
                'status' => $v['dictionary_status'],
                'allow' => $v['dictionary_allow_update'],
                'value' => $v['dictionary_value'],
                'list_class' => $v['dictionary_list_class']
            ];
        });

        return [
            'code' => 0,
            'msg' => 'ok',
            'data' => $dictionaryTree
        ];
    }

    /**
     * 添加字典
     *
     * @param Request $request
     * @return array
     * @date 2022-03-05
     * @example
     * @author admin
     * @since 1.0.0
     */
    public function addDictionary(Request $request)
    {
        $params = $request->param();
        $this->validate($params, [
            'dictionary_name' => 'require',
            'dictionary_index' => 'require',
            'dictionary_order' => 'require',
            'dictionary_parent_guid' => 'require',
        ]);
        if (isset($params['dictionary_guid'])) {
            unset($params['dictionary_guid']);
        }
        ModelDictionary::create($params);
        return [
            'code' => 0,
            'msg' => '添加成功'
        ];
    }

    /**
     * 更新部门
     *
     * @param Request $request
     * @return array
     * @date 2022-03-05
     * @example
     * @author admin
     * @since 1.0.0
     */
    public function updateDictionary(Request $request)
    {
        $params = $request->param();
        $this->validate($params, [
            'dictionary_guid' => 'require'
        ]);
        ModelDictionary::withoutGlobalScope(['status'])->update($params, [
            'dictionary_guid' => $params['dictionary_guid']
        ]);
        return [
            'code' => 0,
            'msg' => '更新成功'
        ];
    }

    /**
     * 删除菜单
     *
     * @param Request $request
     * @return array
     * @date 2022-03-05
     * @example
     * @author admin
     * @since 1.0.0
     */
    public function deleteDictionary(Request $request)
    {
        $params = $request->param();
        $this->validate($params, [
            'dictionary_guid' => 'require'
        ]);
        ModelDictionary::find($params['dictionary_guid'])->delete();
        return [
            'code' => 0,
            'msg' => '删除成功'
        ];
    }

    // 获取字典
    public function getDictionary(Request $request)
    {
        $dictionary_value = $request->param('dictionary_value');

        $dictionary_guid = ModelDictionary::where('dictionary_value', $dictionary_value)->find()['dictionary_guid'];
        $res = ModelDictionary::field([
            'dictionary_guid',
            'dictionary_parent_guid',
            'dictionary_name',
            'dictionary_value',
            'dictionary_list_class',
        ])->where('dictionary_parent_guid', $dictionary_guid)
            ->where('dictionary_status', 1)->select();
        if (!$res) throwErrorMsg("字典不存在");
        else return $res;
    }


    /**
     * 下载导入模板
     */
    public function downloadTemplate(Request $request)
    {
        $params = $request->param();
        $data = [
            [
                '上级字典',
                '名称',
                '值',
                '层级',
                '排序',
                '回显',
            ]
        ];

        $data[] = ['','状态','status','1','0','',];
        $data[] = ['状态','启用','1','2','1','primary'];
        $data[] = ['状态','禁用','2','2','2','danger',];

        $excel = (new Excel())->exporTsheet($data);
        $excel->save('字典导入模板.xlsx');
    }

    /**
     * 导入excel
     */
    public function importExcel(Request $request)
    {
        $file = new UploadFile('uploads', 'fileExt:xlsx');
        $file->putFile('Dictionary');

        $msg = ModelDictionary::importExcel($file);

        return [
            'code' => 0,
            'msg' => $msg
        ];
    }
}
