getFactoryList(['name' => '测试', 'page' => 1, 'limit' => 10]); * * // 获取供应商详情 * $detail = $supplierService->getFactoryById(1); * * // 获取启用状态的供应商 * $activeList = $supplierService->getActiveFactoryList(); * * // 获取启用状态的供应商(带缓存) * $activeListCached = $supplierService->getActiveFactoryListWithCache(7200); * * // 搜索供应商 * $searchResult = $supplierService->searchFactory('关键词', 20); * * // 获取统计信息 * $stats = $supplierService->getFactoryStats(); * * // 获取下拉选项 * $options = $supplierService->getFactoryOptions(); * * // 清除缓存 * $supplierService->clearCache(); */ namespace app\common\Service; use app\common\model\supplier\Index as SupplierModel; use think\Db; use think\Cache; class SupplierService { /** * 单例实例 * @var self */ private static $instance = null; /** * 获取单例实例 * @return self */ public static function getInstance() { if (self::$instance === null) { self::$instance = new self(); } return self::$instance; } /** * 构造函数 */ public function __construct() { // 初始化代码 } /** * 查询工厂/供应商列表 * @param array $params 查询参数 * @return array */ public function getFactoryList($params = []) { $where = []; $page = $params['page'] ?? 1; $limit = $params['limit'] ?? 10; // 搜索条件 if (!empty($params['name'])) { $where[] = ['name', 'like', '%' . $params['name'] . '%']; } if (!empty($params['category_id'])) { $where[] = ['category_id', '=', $params['category_id']]; } if (!empty($params['status'])) { $where[] = ['status', '=', $params['status']]; } // 查询数据 $list = SupplierModel::with(['category']) ->where($where) ->order('weigh desc, id desc') ->paginate($limit, false, ['page' => $page]); return [ 'list' => $list->items(), 'total' => $list->total(), 'page' => $page, 'limit' => $limit ]; } /** * 根据ID获取工厂/供应商详情 * @param int $id 供应商ID * @return array|null */ public static function getFactoryById($id) { return SupplierModel::where('id', $id)->find(); } /** * 获取启用状态的工厂/供应商列表 * @return array */ public function getActiveFactoryList() { return SupplierModel::where('status', 'normal') ->order('weigh desc, id desc') ->select(); } /** * 获取启用状态的工厂/供应商列表(带缓存) * @param int $expire 缓存时间(秒),默认3600秒 * @return array */ public function getActiveFactoryListWithCache($expire = 3600) { $cacheKey = 'supplier:active_list'; $list = Cache::get($cacheKey); if ($list === null) { $list = SupplierModel::where('status', 'normal') ->order('weigh desc, id desc') ->select(); Cache::set($cacheKey, $list, $expire); } return $list; } /** * 根据分类ID获取工厂/供应商列表 * @param int $categoryId 分类ID * @return array */ public function getFactoryByCategoryId($categoryId) { return SupplierModel::where('category_id', $categoryId) ->where('status', 'normal') ->order('weigh desc, id desc') ->select(); } /** * 搜索工厂/供应商 * @param string $keyword 关键词 * @param int $limit 限制数量 * @return array */ public function searchFactory($keyword, $limit = 10) { return SupplierModel::where('name', 'like', '%' . $keyword . '%') ->where('status', 'normal') ->limit($limit) ->order('weigh desc, id desc') ->select(); } /** * 获取供应商统计信息 * @return array */ public function getFactoryStats() { $total = SupplierModel::count(); $active = SupplierModel::where('status', 'normal')->count(); $hidden = SupplierModel::where('status', 'hidden')->count(); return [ 'total' => $total, 'active' => $active, 'hidden' => $hidden ]; } /** * 获取供应商选项列表(用于下拉选择) * @return array */ public function getFactoryOptions() { $options = SupplierModel::getOptions(); return $options; } /** * 批量更新供应商状态 * @param array $ids 供应商ID数组 * @param string $status 状态 * @return bool */ public function batchUpdateStatus($ids, $status) { if (empty($ids) || !in_array($status, ['normal', 'hidden'])) { return false; } return SupplierModel::where('id', 'in', $ids)->update(['status' => $status]); } /** * 根据联系方式查找供应商 * @param string $contact 联系方式(手机号或座机) * @return mixed */ public function getFactoryByContact($contact) { return SupplierModel::where('mobile', $contact) ->whereOr('landline', $contact) ->find(); } /** * 清除供应商相关缓存 * @return bool */ public function clearCache() { $cacheKeys = [ 'supplier:active_list', 'supplier:options', 'supplier:stats' ]; foreach ($cacheKeys as $key) { Cache::rm($key); } return true; } }