<?php

namespace app\admin\controller\Banner;

use app\BaseController;
use app\common\model\Banner\Banner as ModelBanner;
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 Banner extends BaseController
{
    /**
     * 获取轮播图列表
     */
    public function getBannerList(Request $request): array
    {
        $con = Tool::getOptionalQuery();

        $query = ModelBanner::where($con)
            ->field([
                'banner_id',
                'banner_img',
                'banner_location',
                'banner_order',
                'banner_guid'
            ])
            ->order('banner_order', 'asc');

        return msg("获取轮播图列表成功!", $query);
    }

    /**
     * 编辑轮播图
     */
    public function editBanner(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'banner_img|轮播图图片' => 'require',
            'banner_order|排序' => 'require'
        ]);
        $model = ModelBanner::where('banner_guid', $params['banner_guid'])->find();
        if (!$model) throwErrorMsg("该轮播图不存在", 1);
        $model->allowField([
            'banner_update_user_guid',
            'banner_img',
            'banner_order'
        ])->save($params);
        return msg('编辑成功!');
    }

    /**
     * 添加轮播图
     */
    public function addBanner(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'banner_img|轮播图图片' => 'require',
            'banner_order|排序' => 'require'
        ]);
        ModelBanner::create($params, [
            'banner_guid',
            'banner_create_user_guid',
            'banner_update_user_guid',
            'banner_img',
            'banner_order'
        ]);
        return msg('添加成功!');
    }

    /**
     * 删除轮播图
     */
    public function deleteBanner(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'banner_guid' => 'require',
        ]);
        $banner = ModelBanner::where([
            'banner_guid' => explode(',', $params['banner_guid'])
        ])->select();
        $banner->delete();
        return msg('删除成功!');
    }
}
