<?php

namespace app\controller\manager;

use app\model\{Article as MArticle, ArticleTags, Category, SpecialRoute, System, Log};
use app\validate\Article as VArticle;
use think\exception\ValidateException;
use app\service\Tool;
use think\facade\Db;

/**
 * 内容管理 - 文章管理
 */
class Article extends Base
{
    // 允许设置组图的模版列表
    protected $allowImgTemplate = [
        Category::TEMPLATE_CASES,
    ];


    //批量修改属性
    public function attribute()
    {
        if ($this->request->isPost()) {
            $ids = input('post.id/a');
            if (empty($ids) || !is_array($ids)) {
                return $this->json(2, '参数错误,请核对之后再操作!');
            }
            $data = [];
            foreach (['top', 'hot', 'recommend'] as $key) {
                $val = input('post.'.$key, 0);
                if (in_array($val, [1, 2])) {
                    if ($val == 1) {
                        $data[$key] = 1;
                    } else {
                        $data[$key] = 0;
                    }
                }
            }
            if (!empty($data)) {
                MArticle::whereIn('id', $ids)->update($data);
                Log::write('article', 'attribute', '批量修改了文章属性，涉及到的文章ID为：'.implode(',', $ids));
            }
            return $this->json();
        }
        return $this->json(1, '非法请求!');
    }

    //批量删除
    public function batchDel()
    {
        if ($this->request->isPost()) {
            $ids = input('post.ids/a');
            if (empty($ids) || !is_array($ids)) {
                return $this->json(2, '参数错误,请核对之后再操作!');
            }
            $items = MArticle::getListByIds($ids);
            if (empty($items)) {
                return $this->json(3, '待删除文章列表为空');
            }

            Db::startTrans();
            try {
                $delIds = [];
                $cateId = $items[0]['category_id'];
                foreach ($items as $item) {
                    $delIds[] = $item['id'];
                }

                MArticle::destroy($delIds);
                SpecialRoute::deleteByTypeIds($delIds ,SpecialRoute::type_archives);
                Log::write('article', 'betchDel', '批量删除了文章，涉及到的文章ID为：'.implode(',', $delIds));
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return $this->json(5000, '文章删除失败!');
            }

            return $this->json();
        }
        return $this->json(1, '非法请求!');
    }

    //删除
    public function del()
    {
        if ($this->request->isPost()) {
            $id = input('post.id/d');
            if (is_numeric($id) && $id > 0) {
                $item = MArticle::getById($id);
                if (empty($item)) {
                    return $this->json(3, '待删除文章不存在');
                }

                Db::startTrans();
                try {
                    MArticle::destroy($id);
                    Log::write('article', 'del', '删除文章，ID：'.$id.'，标题：'.$item['title']);
                    SpecialRoute::deleteByTypeIds([$id] ,SpecialRoute::type_archives);
                    Db::commit();
                } catch (\Exception $e) {
                    Db::rollback();
                    return $this->json(5000, '文章删除失败！');
                }
                return $this->json();
            }

            return $this->json(2, '参数错误,请核对之后再操作!');
        }
        return $this->json(1, '非法请求!');
    }

    //排序
    public function sort()
    {
        if ($this->request->isPost()) {
            $id         = input('post.id/d');
            $sort       = input('post.sort');
            $num        = input('post.num/d', 1);
            $categoryId = $this->request->param('category_id/d', 0); // get | post
            if ($num <= 0) {
                $num = 1;
            }
            if (!in_array($sort, ['up', 'down'], true)) {
                return $this->json(2, '参数错误');
            }
            $item = MArticle::getById($id);
            if (empty($item)) {
                return $this->json(3, '该文章信息不存在');
            }
            $whereMap = [];
            if (is_numeric($categoryId) && $categoryId > 0) {
                $children = Category::getChildrenByParentId($categoryId);
                if (!empty($children)) {
                    $childrenIds = [];
                    foreach ($children as $child) {
                        if ($child['model_id'] == 31) {
                            $childrenIds[] = $child['id'];
                        }
                    }
                    $whereMap[] = ['category_id', 'in', $childrenIds];
                } else {
                    $whereMap[] = ['category_id', '=', $categoryId];
                }
            } else {
                $whereMap[] = ['category_id', '=', $item['category_id']];
            }

            if ($sort == 'up') {
                $whereMap[] = ['sort', '>', $item['sort']];
                $order      = "sort asc";
            } else {
                $whereMap[] = ['sort', '<', $item['sort']];
                $order      = "sort desc";
            }
            $forSortItems = MArticle::getListByWhereAndOrder($whereMap, $order, $num);
            if (!empty($forSortItems)) {
                $updateData   = [];
                $forSortCount = count($forSortItems);
                for ($i = 0; $i < $forSortCount; $i++) {
                    if ($i == 0) {
                        $updateData[] = [
                            'id'   => $forSortItems[$i]['id'],
                            'sort' => $item['sort']
                        ];
                    } else {
                        $updateData[] = [
                            'id'   => $forSortItems[$i]['id'],
                            'sort' => $forSortItems[$i - 1]['sort']
                        ];
                    }
                }
                $updateData[] = [
                    'id'   => $item['id'],
                    'sort' => $forSortItems[$i - 1]['sort']
                ];
                if (!empty($updateData)) {
                    $model = new MArticle();
                    $model->saveAll($updateData);
                    $sortStr = $sort == 'up' ? '上移' : '下调';
                    Log::write('article', 'sort', "文章排序，ID：{$id} ，标题：{$item['title']}，{$sortStr}了{$num}位");
                    return $this->json();
                }
            }
            return $this->json(4, '无须调整排序!');
        }
        return $this->json(1, '非法请求!');
    }

    //编辑
    public function edit()
    {
        if ($this->request->isPost()) {
            $item    = input('post.item/a');
            $img     = input('post.img', '');
            $images  = input('post.img_imgs/a', []);
            $logo    = input('post.img_logo', '');
            $banner  = input('post.img_banner', '');
            $video   = input('post.video', '');
            $id      = input('post.id/d');
            $article = MArticle::getById($id);
            if (empty($article)) {
                return $this->json(1, '该文章不存在！');
            }

            $item['imgs'] = json_encode(array_values($images), JSON_UNESCAPED_UNICODE);
            $item['src']  = $img;
            if (!empty($logo)) {
                $item['logo'] = $logo;
            }
            if (!empty($banner)) {
                $item['banner'] = $banner;
            }
            $item['video'] = $video;

            try {
                validate(VArticle::class)->scene("edit")->check($item);
                $auth                = session('auth');
                $item['update_time'] = time();
                $item['updated']     = $auth['userName'];

                cache('articlesForRoute', null);    //清理缓存

                // 默认属性
                $defaultAttributes = ['recommend', 'top', 'hot'];
                $item['recommend'] = $item['recommend'] ?? 0;
                $item['top']       = $item['top'] ?? 0;
                $item['hot']       = $item['hot'] ?? 0;

                // 自定义属性配置（包含默认属性）
                $recommendOtherNameStr = input('post.recommend_other_str', '');
                $recommendOtherList    = [];
                if (!empty($recommendOtherNameStr)) {
                    $recommendOtherNameList = explode(',', $recommendOtherNameStr);
                    $attributeList          = MArticle::getAttributeList([$article['category_id']]);
                    $attributeKeyList       = array_flip($attributeList);

                    foreach ($recommendOtherNameList as $recommendOtherName) {
                        $attributeKey = $attributeKeyList[$recommendOtherName] ?? '';
                        if (!empty($attributeKey)) {
                            $recommendOtherList[] = $attributeKey;

                            // 默认属性配置，适用于排序
                            if (in_array($attributeKey, $defaultAttributes)) {
                                $item[$attributeKey] = 1;
                            }
                        }
                    }
                }
                $item['recommend_other'] = empty($recommendOtherList) ? '' : implode(',', $recommendOtherList);

                // 标签
                $item['tag'] = trim($item['tag'] ?? '');
                $item['tag'] = str_replace('，', ',', $item['tag']);
                if (!empty($tagSaveCheck) && ($article['status'] == MArticle::STATUS_NORMAL || $item['status'] == MArticle::STATUS_NORMAL)) {
                    $oldTagList = empty($article['tag']) ? [] : explode(',', $article['tag']);
                    $curTagList = empty($item['tag']) ? [] : explode(',', $item['tag']);
                    /**
                     * $oldTagList 差集：total - 1
                     * $curTagList 差集：不存在则新增，存在则total + 1
                     * [normal->disable] total - 1
                     * [disable->normal] total + 1
                     */
                    if ($article['status'] != $item['status']) {
                        if ($article['status'] == MArticle::STATUS_NORMAL) {
                            $curTagList = [];
                        } else {
                            $oldTagList = [];
                        }
                    }

                    $oldTagsDiff = array_diff($oldTagList, $curTagList);
                    $curTagsDiff = array_diff($curTagList, $oldTagList);
                    if (count($oldTagsDiff) > 0) {
                        ArticleTags::whereIn('name', $oldTagsDiff)
                            ->where($tagSaveCheck, '>', 0)
                            ->dec($tagSaveCheck, 1)
                            ->update();
                    }
                    if (count($curTagsDiff) > 0) {
                        $hadTagItems = ArticleTags::findByNames($curTagsDiff);
                        $hadTags     = $hadTagItems->column('name');
                        $newTags     = array_diff($curTagsDiff, $hadTags);

                        $tagsInsert = [];
                        $tagsUpdate = [];
                        foreach ($newTags as $tagName) {
                            $tagsInsert[] = [
                                'name'        => $tagName,
                                $tagSaveCheck => 1
                            ];
                        }
                        foreach ($hadTagItems as $tagItem) {
                            $tagsUpdate[] = [
                                'id'          => $tagItem['id'],
                                $tagSaveCheck => $tagItem[$tagSaveCheck] + 1
                            ];
                        }

                        if (count($tagsInsert) > 0) {
                            ArticleTags::insertAll($tagsInsert);
                        }
                        if (count($tagsUpdate) > 0) {
                            (new ArticleTags())->saveAll($tagsUpdate);
                        }
                    }
                }

                MArticle::updateById($id, $item);

                //处理特殊路由
                if (array_key_exists("route", $item) && !empty($item['route'])) {
                    $specialRoute = SpecialRoute::findByTypeRelaTioneId($id,SpecialRoute::type_archives);
                    if(empty($specialRoute)){
                        $specialRouteData = [
                            "route"         =>$item["route"]??'',
                            "type"          =>SpecialRoute::type_archives,
                            "relation_id"   =>$id,
                        ];
                        SpecialRoute::create($specialRouteData);
                    }else{
                        $specialRoute->save(["route"=>$item["route"]??'']);
                    }
                }else{
                    SpecialRoute::deleteByTypeIds([$id],SpecialRoute::type_archives);
                }

                Db::commit();
                Log::write('article', 'edit', "文章编辑，ID：{$id} ，标题：{$item['title']}");
                return $this->json();
            }
            catch (ValidateException $e) {
                Db::rollback();
                return $this->json(2, $e->getError());
            }catch (\Exception $e) {
                Db::rollback();
                return $this->json(2, $e->getMessage());
            }
        } else {
            $id            = input('param.id');
            $article       = MArticle::getById($id);
            $category      = [];
            $categoryId    = $article['category_id'] ?? 0;
            $imgSize       = '';
            $attributeList = [];

            if ($article) {
                $article       = MArticle::convertRecommendOther([$categoryId], $article, false);
                $attributeList = MArticle::getAttributeList([$categoryId]);

                $category = Category::getById($categoryId);
                if ($category['img_width'] && $category['img_height']) {
                    $imgSize = $category['img_width'].'像素 X '.$category['img_height'].'像素';
                } else {
                    $imgSize = System::getArticleImageSize();
                }
            }

            $this->data['item']          = $article;
            $this->data['category']      = $category;
            $this->data['imgSize']       = $imgSize;
            $this->data['attributeList'] = $attributeList;
            $this->data['contentRaw']    = in_array($categoryId, Category::$contentRawCategoryList);
            $this->data['allowTag']      = in_array($categoryId, Category::$allowTagCategoryList);
            $this->data['allowImgs']     = in_array($category['template_list'], $this->allowImgTemplate);

            return $this->view();
        }
    }

    //添加
    public function add()
    {
        if ($this->request->isPost()) {
            $item   = input('post.item/a');
            $img    = input('post.img', '');
            $images = input('post.img_imgs/a', []);
            $logo   = input('post.img_logo', '');
            $banner = input('post.img_banner', '');
            $video  = input('post.video', '');

            $item['imgs'] = json_encode($images, JSON_UNESCAPED_UNICODE);
            $item['src']  = $img;
            if (!empty($logo)) {
                $item['logo'] = $logo;
            }

            if (!empty($banner)) {
                $item['banner'] = $banner;
            }
            $item['video'] = $video;
            Db::startTrans();
            try {
                validate(VArticle::class)->scene("add")->check($item);
                $content = $item['content'] ?? '';
                if (isset($item['content'])) {
                    unset($item['content']);
                }
                $item['content'] = $content;
                if (!empty($item['alias'])) {
                    if (Tool::hasAlias($item['alias'])) {
                        throw new ValidateException('别名已存在');
                    }
                    cache('articlesForRoute', null);    //有别名时，清理缓存
                }

                // 默认属性
                $defaultAttributes = ['recommend', 'top', 'hot'];
                $item['recommend'] = $item['recommend'] ?? 0;
                $item['top']       = $item['top'] ?? 0;
                $item['hot']       = $item['hot'] ?? 0;

                // 自定义属性配置
                $recommendOtherNameStr = input('post.recommend_other_str', '');
                $recommendOtherList    = [];
                if (!empty($recommendOtherNameStr)) {
                    $recommendOtherNameList = explode(',', $recommendOtherNameStr);
                    $attributeList          = MArticle::getAttributeList([$item['category_id']]);
                    $attributeKeyList       = array_flip($attributeList);

                    foreach ($recommendOtherNameList as $recommendOtherName) {
                        $attributeKey = $attributeKeyList[$recommendOtherName] ?? '';
                        if (!empty($attributeKey)) {
                            $recommendOtherList[] = $attributeKey;

                            // 默认属性配置，适用于排序
                            if (in_array($attributeKey, $defaultAttributes)) {
                                $item[$attributeKey] = 1;
                            }
                        }
                    }
                }
                $item['recommend_other'] = empty($recommendOtherList) ? '' : implode(',', $recommendOtherList);

                // 标签
                $item['tag'] = trim($item['tag'] ?? '');
                $item['tag'] = str_replace('，', ',', $item['tag']);
                if (!empty($item['tag']) && !empty($tagSaveCheck) && $item['status'] == MArticle::STATUS_NORMAL) {
                    $curTags  = explode(',', $item['tag']);
                    $tagItems = ArticleTags::findByNames($curTags);
                    $hadTags  = $tagItems->column('name');
                    $newTags  = array_diff($curTags, $hadTags);
                    if (count($newTags) > 0) {
                        $tagsInsert = [];
                        $tagsUpdate = [];
                        foreach ($newTags as $tag) {
                            $tagsInsert[] = [
                                'name'        => $tag,
                                $tagSaveCheck => 1
                            ];
                        }
                        foreach ($tagItems as $tagItem) {
                            $tagsUpdate[] = [
                                'id'          => $tagItem['id'],
                                $tagSaveCheck => $tagItem[$tagSaveCheck] + 1,
                            ];
                        }

                        if (count($tagsInsert) > 0) {
                            ArticleTags::insertAll($tagsInsert);
                        }
                        if (count($tagsUpdate)) {
                            (new ArticleTags())->saveAll($tagsUpdate);
                        }
                    }
                }


                $article = MArticle::create($item);
                //处理特殊路由
                if (array_key_exists("route", $item) && !empty($item['route'])) {
                    $specialRouteData = [
                        "route"         =>$item["route"]??'',
                        "type"          =>SpecialRoute::type_archives,
                        "relation_id"   =>$article->id,
                    ];
                    SpecialRoute::create($specialRouteData);
                }
                Log::write('article', 'add', "文章新增，ID：{$article->id} ，标题：{$item['title']}");
                Db::commit();
                return $this->json();
            } catch (ValidateException $e) {
                Db::rollback();
                return $this->json(2, $e->getError());
            } catch (\Exception $e) {
                Db::rollback();
                return $this->json(2, 'request fail! '.$e->getMessage());
            }
        } else {
            $categoryId = input('param.category_id');
            $category   = Category::getById($categoryId);
            if (count($category) > 0 && $category['img_width'] && $category['img_height']) {
                $imgSize = $category['img_width'].'像素 X '.$category['img_height'].'像素';
            } else {
                $imgSize = System::getArticleImageSize();
            }

            $this->data['category']      = $category;
            $this->data['imgSize']       = $imgSize;
            $this->data['attributeList'] = MArticle::getAttributeList([$categoryId]);
            $this->data['contentRaw']    = in_array($categoryId, Category::$contentRawCategoryList);
            $this->data['allowTag']      = in_array($categoryId, Category::$allowTagCategoryList);
            $this->data['allowImgs']     = in_array($category['template_list'], $this->allowImgTemplate);

            return $this->view();
        }
    }
}
