<?php

namespace app\admin\logic\user_label;


use app\common\model\user_label\UserLabel;
use app\common\logic\BaseLogic;
use support\exception\BusinessException;
use think\facade\Db;


/**
 * 用户标签逻辑
 * Class UserLabelLogic
 * @package app\admin\logic\user_label
 */
class UserLabelLogic extends BaseLogic
{


    /**
     * @notes 添加用户标签
     * @param array $params
     * @return bool
     * @author admin
     * @date 2024/06/17 17:02
     */
    public static function add(array $params): bool
    {
        Db::startTrans();
        try {
            UserLabel::create([
                'label_name' => $params['label_name']
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            throw new BusinessException($e->getMessage());
        }
    }


    /**
     * @notes 编辑用户标签
     * @param array $params
     * @return bool
     * @author admin
     * @date 2024/06/17 17:02
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {
            UserLabel::where('label_id', $params['label_id'])->update([
                'label_name' => $params['label_name']
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            throw new BusinessException($e->getMessage());

        }
    }


    /**
     * @notes 删除用户标签
     * @param array $params
     * @return bool
     * @author admin
     * @date 2024/06/17 17:02
     */
    public static function delete(array $params): bool
    {
        return UserLabel::destroy($params['label_id']);
    }


    /**
     * @notes 获取用户标签详情
     * @param $params
     * @return array
     * @author admin
     * @date 2024/06/17 17:02
     */
    public static function detail($params): array
    {
        return UserLabel::findOrEmpty($params['label_id'])->toArray();
    }
}