UserHistoryDao.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace app\common\dao\user;
  3. use app\common\dao\BaseDao;
  4. use app\common\model\user\UserHistory;
  5. class UserHistoryDao extends BaseDao
  6. {
  7. protected function getModel(): string
  8. {
  9. return UserHistory::class;
  10. }
  11. public function createOrUpdate(array $data)
  12. {
  13. $ret = $this->getModel()::getDB()->where($data)->find();
  14. if($ret){
  15. $ret->update_time = time();
  16. $ret->save();
  17. }else{
  18. $data['update_time'] = time();
  19. $this->create($data);
  20. }
  21. }
  22. public function search(?int $uid, int $type)
  23. {
  24. $query = ($this->getModel()::getDB())->when($uid, function ($query) use ($uid) {
  25. $query->where('uid', $uid);
  26. })->when($type, function ($query) use ($type) {
  27. $query->where('res_type', $type);
  28. });
  29. return $query->order('update_time DESC');
  30. }
  31. public function deleteBatch($uid,$data)
  32. {
  33. if(is_array($data)){
  34. $this->getModel()::getDB()->where($this->getPk(),'in',$data)->delete();
  35. }else if($data == 1){
  36. $this->getModel()::getDB()->where('uid',$uid)->delete();
  37. }
  38. }
  39. public function userTotalHistory($uid)
  40. {
  41. return $this->getModel()::getDB()->where('uid',$uid)->count();
  42. }
  43. }