WechatReplyDao.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace app\common\dao\wechat;
  3. use app\common\dao\BaseDao;
  4. use app\common\model\BaseModel;
  5. use app\common\model\wechat\WechatReply;
  6. use think\db\BaseQuery;
  7. use think\db\exception\DataNotFoundException;
  8. use think\db\exception\DbException;
  9. use think\db\exception\ModelNotFoundException;
  10. use think\Model;
  11. /**
  12. * Class WechatReplyDao
  13. * @package app\common\dao\wechat
  14. * @author xaboy
  15. * @day 2020-04-24
  16. */
  17. class WechatReplyDao extends BaseDao
  18. {
  19. /**
  20. * @return BaseModel
  21. * @author xaboy
  22. * @day 2020-03-30
  23. */
  24. protected function getModel(): string
  25. {
  26. return WechatReply::class;
  27. }
  28. /**
  29. * @param string $key
  30. * @return array|Model|null
  31. * @throws DataNotFoundException
  32. * @throws DbException
  33. * @throws ModelNotFoundException
  34. * @author xaboy
  35. * @day 2020-04-24
  36. */
  37. public function keyByReply(string $key)
  38. {
  39. return WechatReply::where('key', $key)->find();
  40. }
  41. /**
  42. * @param array $where
  43. * @return BaseQuery
  44. * @author xaboy
  45. * @day 2020-04-24
  46. */
  47. public function search(array $where)
  48. {
  49. $query = WechatReply::getDB()->where('hidden', 0);
  50. if (isset($where['keyword']) && $where['keyword'])
  51. $query->whereLike('key', "%{$where['keyword']}%");
  52. return $query;
  53. }
  54. /**
  55. * @param int $id
  56. * @return int
  57. * @throws DbException
  58. * @author xaboy
  59. * @day 2020-04-24
  60. */
  61. public function delete(int $id)
  62. {
  63. return ($this->getModel())::getDB()->where($this->getPk(), $id)->where('hidden', 0)->delete();
  64. }
  65. /**
  66. * @param $key
  67. * @return array|Model|null
  68. * @throws DataNotFoundException
  69. * @throws DbException
  70. * @throws ModelNotFoundException
  71. * @author xaboy
  72. * @day 2020-04-27
  73. */
  74. public function keyByValidData($key)
  75. {
  76. return WechatReply::getDB()->where(function ($query) use ($key) {
  77. $query->where('key', $key)->whereFieldRaw('CONCAT(\',\',`key`,\',\')', 'LIKE', '%,' . $key . ',%', 'OR');
  78. })->where('status', 1)->find();
  79. }
  80. }