<?php

namespace app\admin\controller\Poster;

use app\BaseController;
use app\common\model\Poster\Poster as ModelPoster;
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 Poster extends BaseController
{
    /**
     * 获取海报列表
     */
    public function getPosterList(Request $request): array
    {
        $params = $request->param();
        $con = Tool::getOptionalQuery(['poster_location', '='],);

        $query = ModelPoster::where($con)
            ->field([
                'poster_id',
                'poster_guid',
                'poster_location',
                'poster_img',
                'poster_title',
                'poster_describe',
                'poster_sort'
            ])
            ->order('poster_sort', 'asc');

        return msg("获取海报列表成功!", $query);
    }

    /**
     * 编辑海报
     */
    public function editPoster(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'poster_location|位置' => 'require',
            'poster_img|图片' => 'require',
            'poster_title|标题' => 'require',
            'poster_describe|描述' => 'require',
            'poster_sort|排序' => 'require'
        ]);
        $model = ModelPoster::where('poster_guid', $params['poster_guid'])->find();
        if (!$model) throwErrorMsg("该海报不存在", 1);
        $model->allowField([
            'poster_update_user_guid',
            'poster_location',
            'poster_img',
            'poster_title',
            'poster_describe',
            'poster_sort'
        ])->save($params);
        return msg('编辑成功!');
    }

    /**
     * 添加海报
     */
    public function addPoster(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'poster_location|位置' => 'require',
            'poster_img|图片' => 'require',
            'poster_title|标题' => 'require',
            'poster_describe|描述' => 'require',
            'poster_sort|排序' => 'require'
        ]);
        $model = ModelPoster::create($params, [
            'poster_guid',
            'poster_create_user_guid',
            'poster_update_user_guid',
            'poster_location',
            'poster_img',
            'poster_title',
            'poster_describe',
            'poster_sort'
        ]);
        return msg('添加成功!');
    }

    /**
     * 删除海报
     */
    public function deletePoster(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'poster_guid' => 'require',
        ]);
        $poster = ModelPoster::where([
            'poster_guid' => explode(',', $params['poster_guid'])
        ])->select();
        $poster->delete();
        return msg('删除成功!');
    }
}
