<?php
/**
 * Tag
 * Class Tag
 * @package App\Models\marketing\mysql
 */

namespace App\Services\marketing;

use App\Exception\custom\MarketingException;
use App\Models\marketing\mysql\Tag;
use App\Models\marketing\mysql\TakePlace;

class TagService
{
    /**
     * 分类标签详情
     * @param array $params
     * @return \Api\PhpUtils\Mysql\MysqlBase
     */
    public static function tagDetail($params = [])
    {
        return Tag::getRecord(["tag_id" => $params["tag_id"]]);
    }

    /**
     * 分类标签编辑
     * @param array $params
     * @return \Api\PhpUtils\Mysql\MysqlBase
     * @throws MarketingException
     */
    public static function tagEdit($params = [])
    {
        $tagId = empty($params["tag_id"]) ? 0 : $params["tag_id"];

        $data = [];
        $data["tag_name"] = empty($params["tag_name"]) ? "" : $params["tag_name"];
        $data["type"] = empty($params["type"]) ? 1 : $params["type"];
        $data["is_delete"] = empty($params["is_delete"]) ? Tag::STATUS_IS_DEL_NO : $params["is_delete"];

        if (!empty($tagId)) {
            $tag = Tag::getRecordMaster(["tag_name" => $data["tag_name"], "tag_id[!]" => $tagId]);
            if (!empty($tag)) {
                throw new MarketingException(['cus' => MarketingException::TAG_IS_EXIST]);
            }

            $result = Tag::save($data, ["tag_id" => $tagId]);
        } else {
            $tag = Tag::getRecordMaster(["tag_name" => $data["tag_name"]]);
            if (!empty($tag)) {
                throw new MarketingException(['cus' => MarketingException::TAG_IS_EXIST]);
            }

            $result = Tag::save($data);
        }
        return $result;
    }

    /**
     * 逻辑删除分类标签
     * @param array $params
     * @return \Api\PhpUtils\Mysql\MysqlBase
     */
    public static function tagDelete($params = [])
    {
        $tagId = $params["tag_id"];
        $data = [];
        $data["is_delete"] = empty($params["is_delete"]) ? 0 : $params["is_delete"];

        $deleteRes = Tag::save($data, ["tag_id" => $tagId]);
        if (!empty($data["is_delete"])) {
            $takePlaceIds = TakePlace::getRecords(["tag_id" => $tagId], ["take_place_id"]);
            if (!empty($takePlaceIds)) {
                $ids = array_column($takePlaceIds, "take_place_id");
                TakePlace::save(["tag_id" => 0], ["take_place_id" => $ids]);
            }
        }
        return $deleteRes;
    }

    /**
     * 分类标签列表
     * @param array $params
     * @return array
     */
    public static function getList($params = [])
    {
        $params['page'] = !empty($params['page']) ? $params['page'] : 1;
        $limit = !empty($params['page_size']) ? $params['page_size'] : 20;
        $page = ($params['page'] - 1) * $limit;

        $where = [];
        if (!empty($params["tag_name"])) {
            $where["tag_name[~]"] = $params["tag_name"];
        }
        $where['is_delete'] = Tag::STATUS_IS_DEL_NO;
        $where['ORDER'] = ["tag_id" => "DESC"];
        $where['LIMIT'] = [$page, $limit];
        $list = Tag::getRecords($where);

        unset($where['LIMIT']);
        unset($where['ORDER']);
        $count = Tag::count("*", $where);
        return ['result' => $list, 'count' => $count];
    }
}