CouponCondition.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. class CouponCondition extends Model
  5. {
  6. // 表名
  7. protected $name = 'shop_coupon_condition';
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = 'int';
  10. // 定义时间戳字段名
  11. protected $createTime = 'createtime';
  12. protected $updateTime = 'updatetime';
  13. protected $deleteTime = false;
  14. // 追加属性
  15. protected $append = [
  16. 'type_text'
  17. ];
  18. public function getTypeList()
  19. {
  20. return ['1' => __('Type 1'), '2' => __('Type 2'), '3' => __('Type 3'), '4' => __('Type 4')];
  21. }
  22. public function getTypeTextAttr($value, $data)
  23. {
  24. $value = $value ?: ($data['type'] ?? '');
  25. $list = $this->getTypeList();
  26. return $list[$value] ?? '';
  27. }
  28. /**
  29. * @ 获取指定商品的优惠券条件id
  30. * @param $goods_ids
  31. * @param $category_ids
  32. * @param $brand_ids
  33. * @param $type
  34. * @return bool|\PDOStatement|string|\think\Collection
  35. */
  36. public static function getGoodsCondition($goods_ids, $category_ids = [], $brand_ids = [], $type = 2)
  37. {
  38. return self::where('type', $type)
  39. ->whereOr(function ($query) use ($goods_ids) {
  40. $sql = '1=2';
  41. if (is_array($goods_ids)) {
  42. foreach ($goods_ids as $id) {
  43. $sql .= " OR FIND_IN_SET('{$id}',content)";
  44. }
  45. } else {
  46. $sql .= " OR FIND_IN_SET('{$goods_ids}',content)";
  47. }
  48. $query->where('type', 1)->where($sql);
  49. })
  50. ->whereOr(function ($query) use ($category_ids) {
  51. $sql = '1=2';
  52. if (is_array($category_ids)) {
  53. foreach ($category_ids as $id) {
  54. $sql .= " OR FIND_IN_SET('{$id}',content)";
  55. }
  56. } else {
  57. $sql .= " OR FIND_IN_SET('{$category_ids}',content)";
  58. }
  59. $query->where('type', 4)->where($sql);
  60. })
  61. ->whereOr(function ($query) use ($brand_ids) {
  62. $sql = '1=2';
  63. if (is_array($brand_ids)) {
  64. foreach ($brand_ids as $id) {
  65. $sql .= " OR FIND_IN_SET('{$id}',content)";
  66. }
  67. } else {
  68. $sql .= " OR FIND_IN_SET('{$brand_ids}',content)";
  69. }
  70. $query->where('type', 5)->where($sql);
  71. })
  72. ->select();
  73. }
  74. }