UserPvLogDao.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace app\common\dao\user;
  3. use app\common\dao\BaseDao;
  4. use app\common\enum\user\UserPvLogEnum;
  5. use app\common\model\BaseModel;
  6. use app\common\model\user\UserPvLog;
  7. use app\entity\CommonEntity;
  8. use app\entity\data\user\UserPvLogEntity;
  9. use think\db\exception\DataNotFoundException;
  10. use think\db\exception\DbException;
  11. use think\db\exception\ModelNotFoundException;
  12. class UserPvLogDao extends BaseDao
  13. {
  14. /**
  15. * @return BaseModel
  16. */
  17. protected function getModel(): string
  18. {
  19. return UserPvLog::class;
  20. }
  21. /**
  22. * @param array $idList
  23. * @return CommonEntity[]
  24. */
  25. public function getUserPvLogEntityListByIdList(array $idList): array
  26. {
  27. $entityList = [];
  28. try {
  29. /** @var UserPvLog $model */
  30. $model = $this->getModel();
  31. $dataRes = $model::getDB()
  32. ->whereIn('id', $idList)
  33. ->select()
  34. ->toArray();
  35. if (!empty($dataRes)) {
  36. $entityList = array_map(function ($data) {
  37. return UserPvLogEntity::newInstance($data);
  38. }, $dataRes);
  39. }
  40. } catch (\Exception $e) {
  41. }
  42. return $entityList;
  43. }
  44. /**
  45. * @param string $orderType
  46. * @param array $orderIdList
  47. * @return UserPvLogEntity[]
  48. */
  49. public function getUserPvLogEntityListByOrderIdListAndPending(string $orderType, array $orderIdList): array
  50. {
  51. $entityList = [];
  52. try {
  53. $dataList = UserPvLog::getDB()
  54. ->where('order_type', '=', $orderType)
  55. ->whereIn('order_id', $orderIdList)
  56. ->where('status', '=', UserPvLogEnum::STATUS['PendingConfirmation']['code'])
  57. ->select()
  58. ->toArray();
  59. if (!empty($dataList)) {
  60. $entityList = array_map(function ($data) {
  61. return UserPvLogEntity::newInstance($data);
  62. }, $dataList);
  63. }
  64. } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
  65. }
  66. return $entityList;
  67. }
  68. /**
  69. * 批量更新
  70. * @param $dataList
  71. * @return array
  72. */
  73. public function saveAll($dataList): array
  74. {
  75. try {
  76. return $this->getModel()::getInstance()->allowField(array_keys(reset($dataList)))->saveAll($dataList)->toArray();
  77. } catch (\Exception $e) {
  78. return [];
  79. }
  80. }
  81. /**
  82. * 根据order_id和status查找pv值
  83. */
  84. public function getPVFromStatusOrderId($orderId)
  85. {
  86. try {
  87. return $this::getModel()::getDB()->where('order_id', '=', $orderId)->where('status','=',1)->value('pv');
  88. } catch (\Exception $e) {
  89. return [];
  90. }
  91. }
  92. /**
  93. * 根据order_id获取pv值(用于备份用户变更记录)
  94. */
  95. public function getPvForBackupFromOrderId($orderId)
  96. {
  97. try {
  98. $value = $this::getModel()::getDB()
  99. ->where('order_id', '=', $orderId)
  100. ->where('status', '=', 1)
  101. ->value('pv');
  102. return $value ?: 0;
  103. } catch (\Exception $e) {
  104. return 0;
  105. }
  106. }
  107. }