<?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): array
    {
        $query = ModelWorks::where(
            Tool::getOptionalQuery(
                ['works.works_name', 'LIKE'],
                ['works.works_author', 'LIKE'],
                ['works.works_type_guid'],
                ['works.classes_guid'],
            )
        )->field([
            'works.works_id',
            'works.works_guid',
            'works.works_img',
            'works.works_name',
            'works.works_author',
            'works.classes_guid',
            'works.works_intro',
            'works.works_likes_count',
            'works.works_order',
            'works.works_type_guid',
            'works_type.works_type_name',
            'classes.classes_name',
        ])->leftJoin('works_type', 'works_type.works_type_guid = works.works_type_guid')
            ->leftJoin('classes', 'classes.classes_guid = works.classes_guid')
            ->order('works_order', 'asc');

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

    /**
     * 编辑作品接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-18
     * @author xjh
     * @since 1.0.0
     */
    public function editWorks(Request $request): array
    {
        Db::startTrans();
        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',
                'works_name|作品名称' => 'require',
                'works_author|作品作者' => 'require',
                'classes_guid|班型guid' => 'require',
            ]);
            $model = ModelWorks::where('works_guid', $params['works_guid'])->find();
            if (!$model) throwErrorMsg("该作品不存在", 1);
            $model->allowField([
                'works_update_user_guid',
                'works_img',
                'works_intro',
                'works_likes_count',
                'works_type_guid',
                'works_order',
                'works_name',
                'works_author',
                'classes_guid',
            ])->save($params);
            Db::commit();
            return msg('编辑成功!');
        } catch (\Throwable $th) {
            Db::rollback();
            throw  $th;
        }
    }

    /**
     * 添加作品接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-18
     * @author xjh
     * @since 1.0.0
     */
    public function addWorks(Request $request): array
    {
        Db::startTrans();
        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',
                'works_name|作品名称' => 'require',
                'works_author|作品作者' => 'require',
                'classes_guid|班型guid' => 'require',
            ]);
            ModelWorks::create($params, [
                'works_guid',
                'works_create_user_guid',
                'works_update_user_guid',
                'works_img',
                'works_intro',
                'works_likes_count',
                'works_type_guid',
                'works_order',
                'classes_guid',
                'works_author',
                'works_name',
            ]);
            Db::commit();
            return msg('添加成功!');
        } catch (\Throwable $th) {
            Db::rollback();
            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();
        try {
            $params = $request->param();
            $this->validate($params, [
                'works_guid' => 'require',
            ]);
            $works = ModelWorks::where([
                'works_guid' => explode(',', $params['works_guid'])
            ])->select();
            $works->delete();
            Db::commit();
            return msg('删除成功!');
        } catch (\Throwable $th) {
            Db::rollback();
            throw  $th;
        }
    }

    /**
     * 导出Excel接口
     *
     * @param Request request
     * @return array
     * @date 2023-04-18
     * @author xjh
     * @since 1.0.0
     */
    public function exportExcel(Request $request): void
    {
        $params = $request->param();
        $con = [
            ['works_type_delete_time', 'NULL', null],
            ['classes_delete_time', 'NULL', null],
        ];
        if (isset($params['works_guids']) && $params['works_guids']) {
            $con['works.works_guid'] = explode(',', $params['works_guids']);
        }
        $select = ModelWorks::where($con)
            ->leftJoin('works_type', 'works_type.works_type_guid = works.works_type_guid')
            ->leftJoin('classes', 'classes.classes_guid = works.classes_guid')
            ->order('works_order', 'asc')
            ->select()
            ->toArray();

        ModelWorks::exportExcel($select);
    }

    /**
     * 下载导入模板接口
     *
     * @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)
    {
        $file = new UploadFile('uploads', 'fileExt:xlsx');
        $file->putFile('works');

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