<?php

namespace app\admin\controller\Home;

use app\BaseController;
use app\common\model\Home\HomeWorks as ModelHomeWorks;
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 think\facade\Db;
use think\facade\Env;


class HomeWorks extends BaseController
{
    /**
     * 获取首页作品列表
     */
    public function getHomeWorksList(Request $request): array
    {
        $params = $request->param();
        $con = [];

        $con = Tool::getOptionalQuery(['home_works_author_type', '='],);

        $query = ModelHomeWorks::where($con)
            ->field([
                'home_works_id',
                'home_works_guid',
                'home_works_author_type',
                'home_works_img',
                'home_works_sort',
                'home_works_intro'
            ])
            ->order('home_works_sort', 'asc');

        return msg("获取首页作品列表成功!", $query);
    }

    /**
     * 编辑首页作品
     */
    public function editHomeWorks(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'home_works_author_type|作者类型' => 'require',
            'home_works_img|图片' => 'require',
            'home_works_sort|排序' => 'require',
        ]);
        $model = ModelHomeWorks::where('home_works_guid', $params['home_works_guid'])->find();
        if (!$model) throwErrorMsg("该首页作品不存在", 1);
        $model->allowField([
            'home_works_update_user_guid',
            'home_works_author_type',
            'home_works_img',
            'home_works_sort',
            'home_works_intro'
        ])->save($params);
        return msg('编辑成功!');
    }

    /**
     * 添加首页作品
     */
    public function addHomeWorks(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'home_works_author_type|作者类型' => 'require',
            'home_works_img|图片' => 'require',
            'home_works_sort|排序' => 'require',
        ]);
        $model = ModelHomeWorks::create($params, [
            'home_works_guid',
            'home_works_create_user_guid',
            'home_works_update_user_guid',
            'home_works_author_type',
            'home_works_img',
            'home_works_sort',
            'home_works_intro'
        ]);
        return msg('添加成功!');
    }

    /**
     * 删除首页作品
     */
    public function deleteHomeWorks(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'home_works_guid' => 'require',
        ]);
        $home_works = ModelHomeWorks::where([
            'home_works_guid' => explode(',', $params['home_works_guid'])
        ])->select();
        $home_works->delete();
        return msg('删除成功!');
    }

    /**
     * 导出Excel
     */
    public function exportExcel(Request $request)
    {
        $params = $request->param();
        $select = ModelHomeWorks::field([
            'home_works_author_type',
            'home_works_img',
            'home_works_sort',
            'home_works_intro'
        ])
            ->order('home_works_sort', 'asc')
            ->select()->toArray();
        return ModelHomeWorks::exportExcel($select);
    }
}
