StoreCouponProductDao.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\StoreCouponProduct;
  15. use think\Collection;
  16. use think\db\exception\DataNotFoundException;
  17. use think\db\exception\DbException;
  18. use think\db\exception\ModelNotFoundException;
  19. /**
  20. * Class StoreCouponProductDao
  21. * @package app\common\dao\store\coupon
  22. * @author xaboy
  23. * @day 2020-05-13
  24. */
  25. class StoreCouponProductDao extends BaseDao
  26. {
  27. /**
  28. * @return BaseModel
  29. * @author xaboy
  30. * @day 2020-03-30
  31. */
  32. protected function getModel(): string
  33. {
  34. return StoreCouponProduct::class;
  35. }
  36. /**
  37. * 新增数据
  38. * @param array $data
  39. * @return int
  40. * @author xaboy
  41. * @day 2020-05-13
  42. */
  43. public function insertAll(array $data)
  44. {
  45. return StoreCouponProduct::getDB()->insertAll($data);
  46. }
  47. /**
  48. * 根据优惠券id清除数据
  49. * @param $couponId
  50. * @return int
  51. * @throws DbException
  52. * @author xaboy
  53. * @day 2020-05-13
  54. */
  55. public function clear($couponId)
  56. {
  57. return StoreCouponProduct::getDB()->where('coupon_id', $couponId)->delete();
  58. }
  59. /**
  60. * 根据商品id查询优惠券id
  61. * @param $productId
  62. * @return array
  63. * @author xaboy
  64. * @day 2020/6/1
  65. */
  66. public function productByCouponId($productId)
  67. {
  68. return StoreCouponProduct::getDB()->whereIn('product_id', $productId)->column('coupon_id');
  69. }
  70. /**
  71. * 根据条件搜索优惠券产品信息。
  72. *
  73. * 本函数旨在根据传入的条件数组,查询优惠券产品数据库。条件数组可以包含coupon_id和type两个字段,
  74. * 函数将根据这两个字段的值进行查询条件的动态添加,实现灵活的数据库查询。
  75. *
  76. * @param array $where 包含查询条件的数组,可能包含coupon_id和type两个键。
  77. * @return \think\db\BaseQuery|\think\db\Query
  78. */
  79. public function search(array $where)
  80. {
  81. // 获取数据库操作对象
  82. return StoreCouponProduct::getDB()
  83. // 当传入的$where数组中包含coupon_id键且值不为空时,添加where条件查询coupon_id
  84. ->when(isset($where['coupon_id']) && $where['coupon_id'] !== '', function ($query) use ($where) {
  85. return $query->where('coupon_id', $where['coupon_id']);
  86. })
  87. // 当传入的$where数组中包含type键且值不为空时,添加where条件查询type
  88. ->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
  89. return $query->where('type', $where['type']);
  90. });
  91. }
  92. }