MoneyLogService.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace app\common\service;
  3. use app\common\model\MoneyLogModel;
  4. use utils\RedisCache;
  5. /**
  6. * 余额流水服务 by wes
  7. * Class MoneyLogService
  8. * @package app\common\service
  9. */
  10. class MoneyLogService
  11. {
  12. protected static $instance = null;
  13. protected $model = null;
  14. public function __construct()
  15. {
  16. $this->model = new MoneyLogModel();
  17. }
  18. /**
  19. * 静态化入口
  20. * @return static|null
  21. */
  22. public static function make()
  23. {
  24. if (!self::$instance) {
  25. self::$instance = new static();
  26. }
  27. return self::$instance;
  28. }
  29. /**
  30. * 获取列表
  31. * @param $params 查询参数
  32. * @param int $pageSize
  33. * @param string $field
  34. * @param string $cache
  35. * @return array|mixed
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. */
  40. public function getList($params, $pageSize = 10, $field = '', $cache = true)
  41. {
  42. $page = request()->post('page', 1);
  43. $cacheKey = "caches:temp:profitLogs:{$page}_{$pageSize}_" . md5(json_encode($params));
  44. $data = RedisCache::get($cacheKey);
  45. if ($data && $cache) {
  46. return $data;
  47. }
  48. $typeArr = config('type.profit');
  49. $where = ['status' => 1];
  50. $type = isset($params['type']) ? intval($params['type']) : 0;
  51. if ($type) {
  52. $where['type'] = $type;
  53. }
  54. $field = $field ? $field : 'id,uid,money,status,state,type,create_at,remark';
  55. $data = $this->model->where($where)
  56. ->field($field)
  57. ->withAttr('type', function ($value, $data) use ($typeArr) {
  58. return isset($typeArr[$value]) ? $typeArr[$value] : '未知类型';
  59. })
  60. ->order('id desc')
  61. ->page($page, $pageSize)->select();
  62. $data = $data ? $data->toArray() : [];
  63. if ($data && $cache) {
  64. RedisCache::set($cacheKey, $data, rand(3, 5));
  65. }
  66. return $data;
  67. }
  68. }