StoreOrderInvoiceDao.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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\order;
  13. use app\dao\BaseDao;
  14. use app\model\order\StoreOrderInvoice;
  15. /**
  16. * Class StoreOrderInvoiceDao
  17. * @package app\dao\order
  18. */
  19. class StoreOrderInvoiceDao extends BaseDao
  20. {
  21. /**
  22. * 限制精确查询字段
  23. * @var string[]
  24. */
  25. protected $withField = ['uid', 'order_id', 'real_name', 'user_phone'];
  26. protected function setModel(): string
  27. {
  28. return StoreOrderInvoice::class;
  29. }
  30. /**
  31. * 发票搜索
  32. * @param array $where
  33. * @param bool $search
  34. * @return \crmeb\basic\BaseModel
  35. * @throws \ReflectionException
  36. * @author 吴汐
  37. * @email 442384644@qq.com
  38. * @date 2023/03/20
  39. */
  40. public function search(array $where = [], bool $search = false)
  41. {
  42. $realName = $where['real_name'] ?? '';
  43. $fieldKey = $where['field_key'] ?? '';
  44. $fieldKey = $fieldKey == 'all' ? '' : $fieldKey;
  45. $type = $where['type'] ?? '';
  46. unset($where['type']);
  47. return parent::search($where, $search)->when($type, function ($query) use ($type) {
  48. switch ($type) {
  49. case 1://待开
  50. $query->where('is_invoice', 0)->where('is_refund', 0);
  51. break;
  52. case 2://已开
  53. $query->where('is_invoice', 1);
  54. break;
  55. case 3://退款
  56. $query->where('is_refund', 1);
  57. break;
  58. case 4://未开
  59. $query->where('is_invoice', 0)->where('invoice_time', 0)->where('is_refund', 0);
  60. break;
  61. }
  62. })->when($realName && $fieldKey && in_array($fieldKey, $this->withField), function ($query) use ($realName, $fieldKey) {
  63. $query->where('order_id', 'IN', function ($que) use ($fieldKey, $realName) {
  64. $que->name('store_order')->where(trim($fieldKey), trim($realName))->field(['id'])->select();
  65. });
  66. })->when($realName && !$fieldKey, function ($query) use ($where) {
  67. $query->where(function ($que) use ($where) {
  68. $que->whereLike('order_id|invoice_id|name|drawer_phone|email|tell|address|bank', "%{$where['real_name']}%")
  69. ->whereOr('order_id', 'IN', function ($order) use ($where) {
  70. $order->name('store_order')->where('uid|real_name|order_id', 'LIKE', "%{$where['real_name']}%")->field('id')->select();
  71. })
  72. ->whereOr('uid', 'in', function ($q) use ($where) {
  73. $q->name('user')->whereLike('nickname|uid|phone', '%' . $where['real_name'] . '%')->field(['uid'])->select();
  74. });
  75. });
  76. });
  77. }
  78. /**
  79. * @param array $where
  80. * @param string $field
  81. * @param array|string[] $with
  82. * @param string $order
  83. * @param int $page
  84. * @param int $limit
  85. * @return array
  86. * @throws \think\db\exception\DataNotFoundException
  87. * @throws \think\db\exception\DbException
  88. * @throws \think\db\exception\ModelNotFoundException
  89. */
  90. public function getList(array $where, string $field = '*', array $with = ['order'], string $order = '', int $page = 0, int $limit = 0)
  91. {
  92. return $this->search($where)->field($field)->when(count($with), function ($query) use ($with) {
  93. $query->with($with);
  94. })->when($order != '', function ($query) use ($order) {
  95. $query->order($order);
  96. })->when($page && $limit, function ($query) use ($page, $limit) {
  97. $query->page($page, $limit);
  98. })->select()->toArray();
  99. }
  100. }