<?php
/**
 * ${CARET}
 * @file:NoitceTypeService.php
 * @Created by gnitif
 * @Date: 2023/3/27
 * @Time: 11:54
 */


namespace Modules\System\Services\Notice;

use Modules\System\Models\NoticeTypes;

class NoitceTypeService
{

    /**
     *  添加分类
     * name: store
     * @param array $param
     * $param [
     *      'name' => "平台通知"
     * ];
     * date 2023/03/27 18:14
     */
    public static function store(array $param)
    {
        return self::getModel()->storeBy($param);
    }

    protected static function getModel(){
         return new NoticeTypes();
    }

    /**
     *  获取通知分类列表分类
     * name: list
     * @param mixed $param
     *  $param = [
     *      'name' => '系统通知', // 分类名称模糊搜索
     *      'page' => 1, //  页码
     *      'limit' => 15, //  每页条数
     * ]
     * @param mixed $isAll // 是否获取所有数据 默认否
     * date 2023/03/28 17:05
     */
    public static function list($param = [], $isAll = false)
    {
        $where = self::getCondition($param);
        if ($isAll){
            return   NoticeTypes::where($where)->select('id','name')->get();
        }else {
            $pageSize = $param['limit'] ?? 15;
            return   NoticeTypes::where($where)->select('id', 'name', 'created_at')->paginate($pageSize);
        }

    }

    /**
     *  拼接查询条件
     * name: getCondition
     * @param mixed $param
     * @return \string[][]
     * date 2023/03/28 17:19
     */
    private static function getCondition(mixed $param)
    {
        $where = [['is_deleted', '=', '0']];
        if (isset($param['name']) && !empty($param['name'])) {
            $where[] = ['name', 'like', "%" . $param['name'] . "%"];
        }
        return $where;
    }

    /**
     *  删除分类，软删除
     * name: del
     * @param $id
     * @return mixed
     * date 2023/03/29 11:05
     */
    public static function del($id)
    {
        return self::getModel()->updateBy($id,['is_deleted' => 1,'deleted_at' => date("Y-m-d H:i:s")]);
    }
}
