SupplierService.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * 供应商服务类
  4. *
  5. * 使用示例:
  6. *
  7. * // 获取单例实例
  8. * $supplierService = \app\common\Service\Supplier::getInstance();
  9. *
  10. * // 或者直接实例化(普通方式)
  11. * $supplierService = new \app\common\Service\Supplier();
  12. *
  13. * // 获取供应商列表
  14. * $list = $supplierService->getFactoryList(['name' => '测试', 'page' => 1, 'limit' => 10]);
  15. *
  16. * // 获取供应商详情
  17. * $detail = $supplierService->getFactoryById(1);
  18. *
  19. * // 获取启用状态的供应商
  20. * $activeList = $supplierService->getActiveFactoryList();
  21. *
  22. * // 获取启用状态的供应商(带缓存)
  23. * $activeListCached = $supplierService->getActiveFactoryListWithCache(7200);
  24. *
  25. * // 搜索供应商
  26. * $searchResult = $supplierService->searchFactory('关键词', 20);
  27. *
  28. * // 获取统计信息
  29. * $stats = $supplierService->getFactoryStats();
  30. *
  31. * // 获取下拉选项
  32. * $options = $supplierService->getFactoryOptions();
  33. *
  34. * // 清除缓存
  35. * $supplierService->clearCache();
  36. */
  37. namespace app\common\Service;
  38. use app\common\model\supplier\Index as SupplierModel;
  39. use think\Db;
  40. use think\Cache;
  41. class SupplierService
  42. {
  43. /**
  44. * 单例实例
  45. * @var self
  46. */
  47. private static $instance = null;
  48. /**
  49. * 获取单例实例
  50. * @return self
  51. */
  52. public static function getInstance()
  53. {
  54. if (self::$instance === null) {
  55. self::$instance = new self();
  56. }
  57. return self::$instance;
  58. }
  59. /**
  60. * 构造函数
  61. */
  62. public function __construct()
  63. {
  64. // 初始化代码
  65. }
  66. /**
  67. * 查询工厂/供应商列表
  68. * @param array $params 查询参数
  69. * @return array
  70. */
  71. public function getFactoryList($params = [])
  72. {
  73. $where = [];
  74. $page = $params['page'] ?? 1;
  75. $limit = $params['limit'] ?? 10;
  76. // 搜索条件
  77. if (!empty($params['name'])) {
  78. $where[] = ['name', 'like', '%' . $params['name'] . '%'];
  79. }
  80. if (!empty($params['category_id'])) {
  81. $where[] = ['category_id', '=', $params['category_id']];
  82. }
  83. if (!empty($params['status'])) {
  84. $where[] = ['status', '=', $params['status']];
  85. }
  86. // 查询数据
  87. $list = SupplierModel::with(['category'])
  88. ->where($where)
  89. ->order('weigh desc, id desc')
  90. ->paginate($limit, false, ['page' => $page]);
  91. return [
  92. 'list' => $list->items(),
  93. 'total' => $list->total(),
  94. 'page' => $page,
  95. 'limit' => $limit
  96. ];
  97. }
  98. /**
  99. * 根据ID获取工厂/供应商详情
  100. * @param int $id 供应商ID
  101. * @return array|null
  102. */
  103. public static function getFactoryById($id)
  104. {
  105. return SupplierModel::where('id', $id)->find();
  106. }
  107. /**
  108. * 获取启用状态的工厂/供应商列表
  109. * @return array
  110. */
  111. public function getActiveFactoryList()
  112. {
  113. return SupplierModel::where('status', 'normal')
  114. ->order('weigh desc, id desc')
  115. ->select();
  116. }
  117. /**
  118. * 获取启用状态的工厂/供应商列表(带缓存)
  119. * @param int $expire 缓存时间(秒),默认3600秒
  120. * @return array
  121. */
  122. public function getActiveFactoryListWithCache($expire = 3600)
  123. {
  124. $cacheKey = 'supplier:active_list';
  125. $list = Cache::get($cacheKey);
  126. if ($list === null) {
  127. $list = SupplierModel::where('status', 'normal')
  128. ->order('weigh desc, id desc')
  129. ->select();
  130. Cache::set($cacheKey, $list, $expire);
  131. }
  132. return $list;
  133. }
  134. /**
  135. * 根据分类ID获取工厂/供应商列表
  136. * @param int $categoryId 分类ID
  137. * @return array
  138. */
  139. public function getFactoryByCategoryId($categoryId)
  140. {
  141. return SupplierModel::where('category_id', $categoryId)
  142. ->where('status', 'normal')
  143. ->order('weigh desc, id desc')
  144. ->select();
  145. }
  146. /**
  147. * 搜索工厂/供应商
  148. * @param string $keyword 关键词
  149. * @param int $limit 限制数量
  150. * @return array
  151. */
  152. public function searchFactory($keyword, $limit = 10)
  153. {
  154. return SupplierModel::where('name', 'like', '%' . $keyword . '%')
  155. ->where('status', 'normal')
  156. ->limit($limit)
  157. ->order('weigh desc, id desc')
  158. ->select();
  159. }
  160. /**
  161. * 获取供应商统计信息
  162. * @return array
  163. */
  164. public function getFactoryStats()
  165. {
  166. $total = SupplierModel::count();
  167. $active = SupplierModel::where('status', 'normal')->count();
  168. $hidden = SupplierModel::where('status', 'hidden')->count();
  169. return [
  170. 'total' => $total,
  171. 'active' => $active,
  172. 'hidden' => $hidden
  173. ];
  174. }
  175. /**
  176. * 获取供应商选项列表(用于下拉选择)
  177. * @return array
  178. */
  179. public function getFactoryOptions()
  180. {
  181. $options = SupplierModel::getOptions();
  182. return $options;
  183. }
  184. /**
  185. * 批量更新供应商状态
  186. * @param array $ids 供应商ID数组
  187. * @param string $status 状态
  188. * @return bool
  189. */
  190. public function batchUpdateStatus($ids, $status)
  191. {
  192. if (empty($ids) || !in_array($status, ['normal', 'hidden'])) {
  193. return false;
  194. }
  195. return SupplierModel::where('id', 'in', $ids)->update(['status' => $status]);
  196. }
  197. /**
  198. * 根据联系方式查找供应商
  199. * @param string $contact 联系方式(手机号或座机)
  200. * @return mixed
  201. */
  202. public function getFactoryByContact($contact)
  203. {
  204. return SupplierModel::where('mobile', $contact)
  205. ->whereOr('landline', $contact)
  206. ->find();
  207. }
  208. /**
  209. * 清除供应商相关缓存
  210. * @return bool
  211. */
  212. public function clearCache()
  213. {
  214. $cacheKeys = [
  215. 'supplier:active_list',
  216. 'supplier:options',
  217. 'supplier:stats'
  218. ];
  219. foreach ($cacheKeys as $key) {
  220. Cache::rm($key);
  221. }
  222. return true;
  223. }
  224. }