WechatReplyKeyDao.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\dao\wechat;
  13. use think\model;
  14. use app\dao\BaseDao;
  15. use app\model\wechat\WechatReply;
  16. use app\model\wechat\WechatKey;
  17. /**
  18. *
  19. * Class UserWechatUserDao
  20. * @package app\dao\user
  21. */
  22. class WechatReplyKeyDao extends BaseDao
  23. {
  24. /**
  25. * 主表别名
  26. * @var string
  27. */
  28. protected $alias = 'r';
  29. /**
  30. * 附表别名
  31. * @var string
  32. */
  33. protected $joinAlis = 'k';
  34. /**
  35. * 设置模型
  36. * @return string
  37. */
  38. protected function setModel(): string
  39. {
  40. return WechatReply::class;
  41. }
  42. /**
  43. * 设置join连表模型
  44. * @return string
  45. */
  46. protected function setJoinModel(): string
  47. {
  48. return WechatKey::class;
  49. }
  50. /**
  51. * 关联模型
  52. * @param string $alias
  53. * @param string $join_alias
  54. * @return \crmeb\basic\BaseModel
  55. */
  56. protected function getModel(string $key = 'id', string $join = 'LEFT')
  57. {
  58. /** @var WechatKey $keys */
  59. $keys = app()->make($this->setJoinModel());
  60. $name = $keys->getName();
  61. return parent::getModel()->join($name . ' ' . $this->joinAlis, $this->alias . '.' . $key . ' = ' . $this->joinAlis . '.reply_id', $join)->alias($this->alias);
  62. }
  63. /**
  64. * 获取所有关键字
  65. * @param array $where
  66. * @param bool $group
  67. * @return \crmeb\basic\BaseModel|mixed|Model
  68. */
  69. public function search(array $where = [], bool $search = false)
  70. {
  71. return $this->getModel()->when(isset($where['key']) && $where['key'], function ($query) use ($where) {
  72. $query->where($this->joinAlis . '.keys', 'LIKE', "%$where[key]%");
  73. })->when(isset($where['type']) && $where['type'], function ($query) use ($where) {
  74. $query->where($this->alias . '.type', $where['type']);
  75. })->when(isset($where['key_type']) && $where['key_type'] !== '', function ($query) use ($where) {
  76. $query->where($this->joinAlis . '.key_type', $where['key_type']);
  77. })->where($this->joinAlis . '.keys', '<>', 'subscribe')
  78. ->where($this->joinAlis . '.keys', '<>', 'default');
  79. }
  80. /**
  81. * 获取关键字回复列表
  82. * @param array $where
  83. * @param bool $group
  84. * @param int $page
  85. * @param int $limit
  86. * @return array
  87. * @throws \think\db\exception\DataNotFoundException
  88. * @throws \think\db\exception\DbException
  89. * @throws \think\db\exception\ModelNotFoundException
  90. */
  91. public function getReplyKeyList(array $where, int $page, int $limit)
  92. {
  93. return $this->search($where)->page($page, $limit)->group($this->alias . '.id')->field($this->alias . '.*,' . $this->joinAlis . '.keys')->select()->toArray();
  94. }
  95. /**
  96. * 获取条件下的条数
  97. * @param array $where
  98. * @param bool $search
  99. * @return int
  100. */
  101. public function count(array $where = [], bool $search = true)
  102. {
  103. return $this->search($where, $search)->group($this->alias . '.id')->count();
  104. }
  105. }