<?php

namespace app\admin\controller\Works;

use app\BaseController;
use app\common\model\Works\Works as ModelWorks;
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 Works extends BaseController
{
    /**
     * 获取作品列表接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-18
     * @author xjh
     * @since 1.0.0
     */
    public function getWorksList(Request $request, bool $isExport = false): array
    {
        $query = ModelWorks::where(
            Tool::getOptionalQuery(
                ['works.works_author_type'],
                ['works.works_type_guid'],
            )
        )->field([
            'works.works_id',
            'works.works_guid',
            'works.works_author_type',
            'works.works_img',
            'works.works_intro',
            'works.works_likes_count',
            'works.works_order',
            'works.works_type_guid',
            'works_type.works_type_name',
        ])->leftJoin('works_type', 'works_type.works_type_guid = works.works_type_guid')
            ->order('works_order', 'asc');

        return $isExport ? $query->select()->toArray() :  msg("获取作品列表成功!", $query);
    }

    /**
     * 添加作品接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-18
     * @author xjh
     * @since 1.0.0
     */
    public function addWorks(Request $request): array
    {
        Db::startTrans();
        Tool::adminLockTableWrite('works');
        try {
            $params = $request->param();
            $this->validate($params, [
                'works_img|作品图片' => 'require',
                'works_intro|作品介绍' => 'require',
                'works_likes_count|作品点赞数' => 'require',
                'works_order|排序' => 'require',
                'works_type_guid|作品类型guid' => 'require',
            ]);
            ModelWorks::create($params, [
                'works_guid',
                'works_create_user_guid',
                'works_update_user_guid',
                'works_author_type',
                'works_img',
                'works_intro',
                'works_likes_count',
                'works_type_guid',
                'works_order',
            ]);
            Db::commit();
            Tool::unlockTable();
            return msg('添加成功!');
        } catch (\Throwable $th) {
            Db::rollback();
            Tool::unlockTable();
            throw  $th;
        }
    }

    /**
     * 编辑作品接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-18
     * @author xjh
     * @since 1.0.0
     */
    public function editWorks(Request $request): array
    {
        Db::startTrans();
        Tool::adminLockTableWrite('works');
        try {
            $params = $request->param();
            $this->validate($params, [
                'works_img|作品图片' => 'require',
                'works_guid|作品guid' => 'require',
                'works_intro|作品介绍' => 'require',
                'works_likes_count|作品点赞数' => 'require',
                'works_order|排序' => 'require',
                'works_type_guid|作品类型guid' => 'require',
            ]);
            $model = ModelWorks::where('works_guid', $params['works_guid'])->find();
            if (!$model) throwErrorMsg("该作品不存在", 1);
            $model->allowField([
                'works_update_user_guid',
                'works_author_type',
                'works_img',
                'works_intro',
                'works_likes_count',
                'works_type_guid',
                'works_order',
            ])->save($params);
            Db::commit();
            Tool::unlockTable();
            return msg('编辑成功!');
        } catch (\Throwable $th) {
            Db::rollback();
            Tool::unlockTable();
            throw  $th;
        }
    }

    /**
     * 删除作品接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-18
     * @author xjh
     * @since 1.0.0
     */
    public function deleteWorks(Request $request): array
    {
        Db::startTrans();
        Tool::adminLockTableWrite('works');
        try {
            $params = $request->param();
            $this->validate($params, [
                'works_guid' => 'require',
            ]);
            $works = ModelWorks::where([
                'works_guid' => explode(',', $params['works_guid'])
            ])->select();
            $works->delete();
            Db::commit();
            Tool::unlockTable();
            return msg('删除成功!');
        } catch (\Throwable $th) {
            Db::rollback();
            Tool::unlockTable();
            throw  $th;
        }
    }

    /**
     * 导出Excel接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-18
     * @author xjh
     * @since 1.0.0
     */
    public function exportExcel(Request $request): void
    {
        ModelWorks::exportExcel($this->getWorksList($request, true));
    }

    /**
     * 下载导入模板接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-18
     * @author xjh
     * @since 1.0.0
     */
    public function downloadTemplate(Request $request)
    {
        $data = [
            array_values(ModelWorks::IMPORT_EXCEL_FIELD),
            ['素描', '龙与虎', '张三', '色彩班', "0", "0"]
        ];
        $excel = (new Excel())->exporTsheet($data);
        $excel->save('作品导入模板.xlsx');
    }

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

            $msg = ModelWorks::importExcel($file);
            Db::commit();
            Tool::unlockTable();
            return [
                'code' => 0,
                'msg' => $msg
            ];
        } catch (\Throwable $th) {
            Db::rollback();
            Tool::unlockTable();
            throw  $th;
        }
    }
}
