123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- <?php
- /**
- * 供应商服务类
- *
- * 使用示例:
- *
- * // 获取单例实例
- * $supplierService = \app\common\Service\Supplier::getInstance();
- *
- * // 或者直接实例化(普通方式)
- * $supplierService = new \app\common\Service\Supplier();
- *
- * // 获取供应商列表
- * $list = $supplierService->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;
- }
- }
|