<?php

namespace app\api\controller\Works;

use app\BaseController;
use app\common\model\Works\Works as ModelWorks;
use app\common\model\Works\WorksType as ModelWorksType;
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-25
     * @author xjh
     * @since 1.0.0
     */
    public function getWorksList(Request $request)
    {
        $params = $request->param();
        $this->validate($params, [
            'idx|索引值' => 'require',
        ]);

        $work_type =  ModelWorksType::field(['works_type_name' => 'name', 'works_type_id'])
            ->where('works_type_parent_guid', "0")
            ->order('works_type_order')
            ->select();

        $params['works_type_id'] = null;
        foreach ($work_type as $key => $value) {
            if ($key == $params['idx']) {
                $params['works_type_id'] = $value['works_type_id'];
            }
        }

        $works_type =  ModelWorksType::where('works_type_id', $params['works_type_id'])->find();
        if (!$works_type) throwErrorMsg('该作品类型不存在!');

        $con = [
            ['works_type.works_type_delete_time', 'NULL', null],
            ['classes.classes_delete_time', 'NULL', null],
            ['works.works_type_guid', '=', $works_type->works_type_guid],
        ];
        $data = ModelWorks::field([
            'works.works_id',
            'works_type.works_type_id',
            'works.works_img',
            'works.works_name',
            'works.works_author',
            'works.works_intro',
            'works.works_likes_count',
            'works.works_order',
            'works_type.works_type_name',
            'classes.classes_name',
        ])
            ->where($con)
            ->leftJoin('works_type', 'works_type.works_type_guid = works.works_type_guid')
            ->leftJoin('classes', 'classes.classes_guid = works.classes_guid')
            ->append(['isLike', 'likeSrc'])
            ->order('works_order', 'asc')
            ->select();

        return msg(0, "获取作品列表成功!", [
            'count' => count($data),
            'data' => $data,
        ]);
    }

    /**
     * 点赞接口
     */
    public function WorksLike(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'works_id|作品id' => 'require',
        ]);
        $model = ModelWorks::where('works_id', $params['works_id'])->find();
        if (!$model) throwErrorMsg("该作品不存在", 1);

        $rmsg = "";
        try {
            $rmsg = $params['isLike'] == true ? "成功" : "取消";
            $model->allowField([
                'works_likes_count',
            ])->save($params);
            return msg('点赞' .  $rmsg . "!");
        } catch (\Throwable $th) {
            return throwErrorMsg("点赞失败" . $th, 1);
        }
    }
}
