StoreCouponSendDao.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\dao\store\coupon;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\BaseModel;
  14. use app\common\model\store\coupon\StoreCouponSend;
  15. class StoreCouponSendDao extends BaseDao
  16. {
  17. /**
  18. * 获取模型类名
  19. *
  20. * 本方法返回一个字符串,代表了StoreCouponSend类的完全限定名。
  21. * 它被设计为一个受保护的方法,意味着它只能在当前类或其子类中被调用。
  22. * 这种设计模式常用于框架中,通过这种方式,子类可以重写此方法以返回不同的模型类名,
  23. * 从而实现不同的行为或逻辑,而不需要修改父类的其他部分。
  24. *
  25. * @return string 返回StoreCouponSend类的完全限定名
  26. */
  27. protected function getModel(): string
  28. {
  29. return StoreCouponSend::class;
  30. }
  31. /**
  32. * 根据条件搜索优惠券发送记录
  33. *
  34. * 本函数用于根据提供的条件从数据库中检索优惠券发送记录。它支持搜索优惠券名称、
  35. * 发送日期范围、优惠券类型、状态和商家ID。搜索结果基于这些条件的组合。
  36. *
  37. * @param array $where 搜索条件数组,包含可能的键:coupon_name(优惠券名称)、
  38. * date(日期范围)、coupon_type(优惠券类型)、status(状态)、mer_id(商家ID)。
  39. * @return \think\db\Query 返回一个数据库查询对象,该对象可用于进一步的查询操作或数据检索。
  40. */
  41. public function search(array $where)
  42. {
  43. // 初始化查询,指定别名为A,并左连接到StoreCoupon表(别名为B) on B.coupon_id = A.coupon_id
  44. return StoreCouponSend::getDB()->alias('A')->leftJoin('StoreCoupon B', 'B.coupon_id = A.coupon_id')
  45. // 当coupon_name条件存在且不为空时,添加LIKE条件到查询中
  46. ->when(isset($where['coupon_name']) && $where['coupon_name'] !== '', function ($query) use ($where) {
  47. $query->whereLike('B.title', "%{$where['coupon_name']}%");
  48. })
  49. // 当date条件存在且不为空时,调用getModelTime函数来处理日期范围条件
  50. ->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  51. getModelTime($query, $where['date'], 'A.create_time');
  52. })
  53. // 当coupon_type条件存在且不为空时,添加类型条件到查询中
  54. ->when(isset($where['coupon_type']) && $where['coupon_type'] !== '', function ($query) use ($where) {
  55. $query->where('B.type', $where['coupon_type']);
  56. })
  57. // 当status条件存在且不为空时,添加状态条件到查询中
  58. ->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
  59. $query->where('A.status', $where['status']);
  60. })
  61. // 当mer_id条件存在且不为空时,添加商家ID条件到查询中
  62. ->when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
  63. $query->where('A.mer_id', $where['mer_id']);
  64. });
  65. }
  66. }