SharedProsperityRewardRecordDao.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace app\common\dao\shared;
  3. use app\common\dao\BaseDao;
  4. use app\common\enum\shared\SharedProsperityRewardRecordEnum;
  5. use app\common\model\BaseModel;
  6. use app\common\model\shared\SharedProsperityRewardRecord;
  7. use app\entity\data\shared\SharedProsperityRewardRecordEntity;
  8. use think\db\exception\DataNotFoundException;
  9. use think\db\exception\DbException;
  10. use think\db\exception\ModelNotFoundException;
  11. class SharedProsperityRewardRecordDao extends BaseDao
  12. {
  13. /**
  14. * @return BaseModel
  15. */
  16. protected function getModel(): string
  17. {
  18. return SharedProsperityRewardRecord::class;
  19. }
  20. /**
  21. * @param array $idList
  22. * @return SharedProsperityRewardRecordEntity[]
  23. */
  24. public function getSharedProsperityRewardRecordEntityListByIdList(array $idList): array
  25. {
  26. $entityList = [];
  27. try {
  28. /** @var SharedProsperityRewardRecord $model */
  29. $model = $this->getModel();
  30. $dataRes = $model::getDB()
  31. ->whereIn('id', $idList)
  32. ->select()
  33. ->toArray();
  34. if (!empty($dataRes)) {
  35. $entityList = array_map(function ($data) {
  36. return SharedProsperityRewardRecordEntity::newInstance($data);
  37. }, $dataRes);
  38. }
  39. } catch (\Exception $e) {
  40. }
  41. return $entityList;
  42. }
  43. /**
  44. * 批量更新
  45. * @param $dataList
  46. * @return array
  47. */
  48. public function saveAll($dataList): array
  49. {
  50. try {
  51. return $this->getModel()::getInstance()->allowField(array_keys(reset($dataList)))->saveAll($dataList)->toArray();
  52. } catch (\Exception $e) {
  53. return [];
  54. }
  55. }
  56. /**
  57. * @param string $typeShop
  58. * @param array $orderSnList
  59. * @return SharedProsperityRewardRecordEntity[]
  60. */
  61. public function getSharedProsperityRewardRecordEntityListByOrderSnListAndPending(string $typeShop, array $orderSnList): array
  62. {
  63. $entityList = [];
  64. try {
  65. /** @var SharedProsperityRewardRecord $model */
  66. $model = $this->getModel();
  67. $dataList = $model::getDB()
  68. ->where('type_shop', '=', $typeShop)
  69. ->whereIn('order_sn', $orderSnList)
  70. ->where('status', '=', SharedProsperityRewardRecordEnum::STATUS['PENDING']['code'])
  71. ->select()
  72. ->toArray();
  73. if (!empty($dataList)) {
  74. $entityList = array_map(function ($data) {
  75. return SharedProsperityRewardRecordEntity::newInstance($data);
  76. }, $dataList);
  77. }
  78. } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
  79. }
  80. return $entityList;
  81. }
  82. /**
  83. * 获取奖励记录分页列表
  84. * @param int|null $userId 用户ID
  85. * @param int|null $type 奖励类型
  86. * @param int|null $status 状态:0=待确定 1=有效 -1=无效
  87. * @param int|null $typeShop 类型店铺:0平台1多多进宝2唯品会3苏宁易购4网易考拉5淘宝客6京东联盟7饿了么8线上 9话费
  88. * @param int|null $pm 0=支出 1=获得
  89. * @param int $page 页码
  90. * @param int $pageSize 每页数量
  91. * @return array
  92. */
  93. 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
  94. {
  95. $list = [];
  96. try {
  97. /** @var SharedProsperityRewardRecord $model */
  98. $model = $this->getModel();
  99. $query = $model::getDB();
  100. // 构建 where 条件
  101. if (!is_null($userId) && $userId > 0) {
  102. $query->where('user_id', $userId);
  103. }
  104. if (!is_null($type)) {
  105. $query->where('type', $type);
  106. }
  107. if (!is_null($status)) {
  108. $query->where('status', $status);
  109. }
  110. if (!is_null($typeShop)) {
  111. $query->where('type_shop', $typeShop);
  112. }
  113. if (!is_null($pm)) {
  114. $query->where('pm', $pm);
  115. }
  116. // 按创建时间倒序排列
  117. $query->order('create_time', 'desc');
  118. $list = $query->paginate(['page' => $page, 'list_rows' => $pageSize])
  119. ->toArray();
  120. } catch (\Exception $e) {
  121. // 记录日志或忽略
  122. }
  123. return $list;
  124. }
  125. /**
  126. * 根据条件求和奖励记录中的 number 字段
  127. * @param int|array|null $userIds 单个用户ID或用户ID数组,为null时忽略此条件
  128. * @param int|null $type
  129. * @param int|null $status
  130. * @param int|null $typeShop
  131. * @param int|null $pm
  132. * @return float
  133. */
  134. public function getRewardSum($userIds = null, ?int $type = null, ?int $status = null, ?int $typeShop = null, ?int $pm = null): float
  135. {
  136. try {
  137. /** @var SharedProsperityRewardRecord $model */
  138. $model = $this->getModel();
  139. $query = $model::getDB();
  140. if (!is_null($userIds)) {
  141. if (is_array($userIds)) {
  142. if (empty($userIds)) {
  143. return 0.0;
  144. }
  145. $query->whereIn('user_id', $userIds);
  146. } else {
  147. $query->where('user_id', (int) $userIds);
  148. }
  149. }
  150. if (!is_null($type)) {
  151. $query->where('type', $type);
  152. }
  153. if (!is_null($status)) {
  154. $query->where('status', $status);
  155. }
  156. if (!is_null($typeShop)) {
  157. $query->where('type_shop', $typeShop);
  158. }
  159. if (!is_null($pm)) {
  160. $query->where('pm', $pm);
  161. }
  162. $sum = $query->sum('number');
  163. return (float) $sum;
  164. } catch (\Exception $e) {
  165. return 0.0;
  166. }
  167. }
  168. /**
  169. * @param $start
  170. * @param $end
  171. * @return array
  172. * @throws DataNotFoundException
  173. * @throws DbException
  174. * @throws ModelNotFoundException
  175. * @author 史晨
  176. * @date 2026/2/10 13:56
  177. * 时间范围搜索
  178. */
  179. public function getListByTime($start, $end, $type)
  180. {
  181. $model = $this->getModel();
  182. return $model::getDB()
  183. ->where('type',$type)
  184. ->where('take_time', '>', $start)
  185. ->where('take_time', '<', $end)
  186. ->select()
  187. ->toArray();
  188. }
  189. }