SharedProsperityRewardRecordDao.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace app\common\dao\shared;
  3. use app\common\dao\BaseDao;
  4. use app\common\model\shared\SharedProsperityRewardRecord;
  5. use app\entity\data\shared\SharedProsperityRewardRecordEntity;
  6. class SharedProsperityRewardRecordDao extends BaseDao
  7. {
  8. protected function getModel(): string
  9. {
  10. return SharedProsperityRewardRecord::class;
  11. }
  12. /**
  13. * @param array $idList
  14. * @return SharedProsperityRewardRecordEntity[]
  15. */
  16. public function getSharedProsperityRewardRecordEntityListByIdList(array $idList): array
  17. {
  18. $entityList = [];
  19. try {
  20. /** @var SharedProsperityRewardRecord $model */
  21. $model = $this->getModel();
  22. $dataRes = $model::getDB()
  23. ->whereIn('id', $idList)
  24. ->select()
  25. ->toArray();
  26. if (!empty($dataRes)) {
  27. $entityList = array_map(function ($data) {
  28. return SharedProsperityRewardRecordEntity::newInstance($data);
  29. }, $dataRes);
  30. }
  31. } catch (\Exception $e) {
  32. }
  33. return $entityList;
  34. }
  35. /**
  36. * 获取奖励记录分页列表
  37. * @param int|null $userId 用户ID
  38. * @param int|null $type 奖励类型
  39. * @param int|null $status 状态:0=待确定 1=有效 -1=无效
  40. * @param int|null $typeShop 类型店铺:0平台1多多进宝2唯品会3苏宁易购4网易考拉5淘宝客6京东联盟7饿了么8线上 9话费
  41. * @param int|null $pm 0=支出 1=获得
  42. * @param int $page 页码
  43. * @param int $pageSize 每页数量
  44. * @return array
  45. */
  46. public function getRewardList(?int $userId = null, ?int $type = null, ?int $status = null, ?int $typeShop = null, ?int $pm = null, int $page = 1, int $pageSize = 10): array
  47. {
  48. $list = [];
  49. try {
  50. /** @var SharedProsperityRewardRecord $model */
  51. $model = $this->getModel();
  52. $query = $model::getDB();
  53. // 构建 where 条件
  54. if (!is_null($userId) && $userId > 0) {
  55. $query->where('user_id', $userId);
  56. }
  57. if (!is_null($type)) {
  58. $query->where('type', $type);
  59. }
  60. if (!is_null($status)) {
  61. $query->where('status', $status);
  62. }
  63. if (!is_null($typeShop)) {
  64. $query->where('type_shop', $typeShop);
  65. }
  66. if (!is_null($pm)) {
  67. $query->where('pm', $pm);
  68. }
  69. // 按创建时间倒序排列
  70. $query->order('create_time', 'desc');
  71. $list = $query->paginate(['page' => $page, 'list_rows' => $pageSize])
  72. ->toArray();
  73. } catch (\Exception $e) {
  74. // 记录日志或忽略
  75. }
  76. return $list;
  77. }
  78. /**
  79. * 根据条件求和奖励记录中的 number 字段
  80. * @param int|array|null $userIds 单个用户ID或用户ID数组,为null时忽略此条件
  81. * @param int|null $type
  82. * @param int|null $status
  83. * @param int|null $typeShop
  84. * @param int|null $pm
  85. * @return float
  86. */
  87. public function getRewardSum($userIds = null, ?int $type = null, ?int $status = null, ?int $typeShop = null, ?int $pm = null): float
  88. {
  89. try {
  90. /** @var SharedProsperityRewardRecord $model */
  91. $model = $this->getModel();
  92. $query = $model::getDB();
  93. if (!is_null($userIds)) {
  94. if (is_array($userIds)) {
  95. if (empty($userIds)) {
  96. return 0.0;
  97. }
  98. $query->whereIn('user_id', $userIds);
  99. } else {
  100. $query->where('user_id', (int) $userIds);
  101. }
  102. }
  103. if (!is_null($type)) {
  104. $query->where('type', $type);
  105. }
  106. if (!is_null($status)) {
  107. $query->where('status', $status);
  108. }
  109. if (!is_null($typeShop)) {
  110. $query->where('type_shop', $typeShop);
  111. }
  112. if (!is_null($pm)) {
  113. $query->where('pm', $pm);
  114. }
  115. $sum = $query->sum('number');
  116. return (float) $sum;
  117. } catch (\Exception $e) {
  118. return 0.0;
  119. }
  120. }
  121. }