<?php

namespace app\admin\controller\AboutUs\TeachingEnvir;

use app\BaseController;
use app\common\model\AboutUs\TeachingEnvir\TeachingEnvirType as ModelTeachingEnvirType;
use app\Request;
use think\Validate;
use think\exception\ValidateException;
use think\facade\Filesystem;
use app\common\arw\adjfut\src\Excel;
use app\common\arw\adjfut\src\UploadFile;
use app\common\exception\Tool;
use app\common\arw\adjfut\src\Traverse;
use think\facade\Db;
use think\facade\Env;


class TeachingEnvirType extends BaseController
{
    /**
     * 获取教学环境类型列表接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-22
     * @author xjh
     * @since 1.0.0
     */
    public function getTeachingEnvirTypeList(Request $request): array
    {
        $con = Tool::getOptionalQuery(['teaching_envir_type_name', 'LIKE']);
        $con[] = ['teaching_envir_type_parent_guid', '=', "0"];
        $type_guids  = self::pageWrapper(ModelTeachingEnvirType::where($con))
            ->order('teaching_envir_type_order')
            ->column('teaching_envir_type_guid');

        $type_tree = [];
        if ($type_guids) {
            $type_data  = ModelTeachingEnvirType::whereOr([
                [["a.teaching_envir_type_guid", 'in', $type_guids]],
                [["a.teaching_envir_type_ancestors_guid", 'REGEXP', implode('|', $type_guids)]],
            ])
                ->where([['b.teaching_envir_type_delete_time', 'NULL', null]])
                ->field([
                    'a.teaching_envir_type_id',
                    'a.teaching_envir_type_guid',
                    'a.teaching_envir_type_name',
                    'a.teaching_envir_type_order',
                    'a.teaching_envir_type_parent_guid',
                    'a.teaching_envir_type_ancestors_guid',
                    'b.teaching_envir_type_name' => "teaching_envir_type_parent_name",
                ])->alias('a')
                ->leftjoin('teaching_envir_type b', 'a.teaching_envir_type_parent_guid = b.teaching_envir_type_guid')
                ->order('teaching_envir_type_order')
                ->select()
                ->toArray();

            $Traverse = new Traverse('teaching_envir_type_guid', 'teaching_envir_type_parent_guid');
            $type_tree = $Traverse->tree($type_data, '0', function ($v) {
                return [
                    'teaching_envir_type_name' => $v['teaching_envir_type_name'],
                    'teaching_envir_type_parent_name' => $v['teaching_envir_type_parent_name'],
                    'teaching_envir_type_guid' => $v['teaching_envir_type_guid'],
                    'teaching_envir_type_parent_guid' => $v['teaching_envir_type_parent_guid'],
                    'teaching_envir_type_ancestors_guid' => $v['teaching_envir_type_ancestors_guid'],
                    'teaching_envir_type_order' => $v['teaching_envir_type_order'],
                ];
            });
        }
        return msg(0, "获取作品类型列表成功!", [
            'data' => $type_tree,
            'count' => ModelTeachingEnvirType::where('teaching_envir_type_parent_guid', '0')->count()
        ]);
    }

    /**
     * 编辑教学环境类型接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-22
     * @author xjh
     * @since 1.0.0
     */
    public function editTeachingEnvirType(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'teaching_envir_type_name|环境类型名称' => 'require',
            'teaching_envir_type_order|环境类型排序' => 'require',
            'teaching_envir_type_parent_guid|作品类型父级guid' => 'require',
        ]);
        $model = ModelTeachingEnvirType::where('teaching_envir_type_guid', $params['teaching_envir_type_guid'])->find();
        if (!$model) throwErrorMsg("该教学环境类型不存在", 1);
        $model->allowField([
            'teaching_envir_type_update_user_guid',
            'teaching_envir_type_name',
            'teaching_envir_type_order',
            'teaching_envir_type_ancestors_guid',
            'teaching_envir_type_parent_guid'
        ])->save($params);
        return msg('编辑成功!');
    }

    /**
     * 添加教学环境类型接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-22
     * @author xjh
     * @since 1.0.0
     */
    public function addTeachingEnvirType(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'teaching_envir_type_name|环境类型名称' => 'require',
            'teaching_envir_type_order|环境类型排序' => 'require',
            'teaching_envir_type_parent_guid|作品类型父级guid' => 'require',
        ]);
        ModelTeachingEnvirType::create($params, [
            'teaching_envir_type_guid',
            'teaching_envir_type_create_user_guid',
            'teaching_envir_type_update_user_guid',
            'teaching_envir_type_name',
            'teaching_envir_type_order',
            'teaching_envir_type_ancestors_guid',
            'teaching_envir_type_parent_guid'
        ]);
        return msg('添加成功!');
    }

    /**
     * 删除教学环境类型接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-22
     * @author xjh
     * @since 1.0.0
     */
    public function deleteTeachingEnvirType(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'teaching_envir_type_guid' => 'require',
        ]);
        $teaching_envir_type = ModelTeachingEnvirType::where([
            'teaching_envir_type_guid' => explode(',', $params['teaching_envir_type_guid'])
        ])->select();
        $teaching_envir_type->delete();
        return msg('删除成功!');
    }

    /**
     * 导出Excel接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-22
     * @author xjh
     * @since 1.0.0
     */
    public function exportExcel(Request $request)
    {
        $params = $request->param();
        $con = [];
        if (isset($params['teaching_envir_type_guids']) && $params['teaching_envir_type_guids']) {
            $con['a.teaching_envir_type_guid'] = explode(',', $params['teaching_envir_type_guids']);
        }
        $select = ModelTeachingEnvirType::field([
            'a.teaching_envir_type_name',
            'a.teaching_envir_type_order',
            // 'a.teaching_envir_type_parent_guid',
            // 'b.teaching_envir_type_name' => 'teaching_envir_type_parent_name',
        ])
            ->where($con)
            ->alias('a')
            // ->leftjoin('teaching_envir_type b', 'a.teaching_envir_type_parent_guid = b.teaching_envir_type_guid')
            ->order('teaching_envir_type_order', 'asc')
            ->select();
        return ModelTeachingEnvirType::exportExcel($select);
    }

    /**
     * 下载导入模板接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-22
     * @author xjh
     * @since 1.0.0
     */
    public function downloadTemplate(Request $request)
    {
        $data = [
            array_values(ModelTeachingEnvirType::EXCELFIELD),
            // ['', '素描', '1']
            ['校园', '1']
        ];
        $excel = (new Excel())->exporTsheet($data);
        $excel->save('教学环境类型导入模板.xlsx');
    }

    /**
     * 导入excel接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-22
     * @author xjh
     * @since 1.0.0
     */
    public function importExcel(Request $request): array
    {
        $file = new UploadFile('uploads', 'fileExt:xlsx');
        $file->putFile('teaching_envir_type');

        $msg = ModelTeachingEnvirType::importExcel($file);
        return [
            'code' => 0,
            'msg' => $msg
        ];
    }
}
