WechatReplyKeyDao.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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 $key
  53. * @param string $join
  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. protected function search(array $where = [])
  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. })->where($this->joinAlis . '.keys', '<>', 'subscribe')
  76. ->where($this->joinAlis . '.keys', '<>', 'default');
  77. }
  78. /**
  79. * 获取关键字回复列表
  80. * @param array $where
  81. * @param bool $group
  82. * @param int $page
  83. * @param int $limit
  84. * @return array
  85. * @throws \think\db\exception\DataNotFoundException
  86. * @throws \think\db\exception\DbException
  87. * @throws \think\db\exception\ModelNotFoundException
  88. */
  89. public function getReplyKeyList(array $where, int $page, int $limit)
  90. {
  91. return $this->search($where)->page($page, $limit)->group($this->alias . '.id')->field($this->alias . '.*,' . $this->joinAlis . '.keys')->select()->toArray();
  92. }
  93. /**
  94. * 获取条件下的条数
  95. * @param array $where
  96. * @param string $field
  97. * @return int
  98. */
  99. public function count(array $where = [], string $field = '*'): int
  100. {
  101. return $this->search($where)->group($this->alias . '.id')->count($field);
  102. }
  103. }