StoreOrderInvoiceDao.php 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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\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. public function search(array $where = [])
  31. {
  32. $realName = $where['real_name'] ?? '';
  33. $fieldKey = $where['field_key'] ?? '';
  34. $fieldKey = $fieldKey == 'all' ? '' : $fieldKey;
  35. $type = $where['type'] ?? '';
  36. unset($where['type']);
  37. return parent::search($where)->when($type, function ($query) use ($type) {
  38. switch ($type) {
  39. case 1://待开
  40. $query->where('is_invoice', 0)->where('invoice_time', 0)->where('is_refund', 0);
  41. break;
  42. case 2://已开
  43. $query->where('is_invoice', 1)->where('is_refund', 0);
  44. break;
  45. case 3://退款
  46. $query->where('is_refund', 1);
  47. break;
  48. case 4://未开
  49. $query->where('is_invoice', 0)->where('invoice_time', 0)->where('is_refund', 0);
  50. break;
  51. }
  52. })->when($realName && $fieldKey && in_array($fieldKey, $this->withField), function ($query) use ($realName, $fieldKey) {
  53. $query->where('order_id', 'IN', function ($que) use ($fieldKey, $realName) {
  54. $que->name('store_order')->where(trim($fieldKey), trim($realName))->field(['id'])->select();
  55. });
  56. })->when($realName && !$fieldKey, function ($query) use ($where) {
  57. $query->where(function ($que) use ($where) {
  58. $que->whereLike('order_id|invoice_id|name|drawer_phone|email|tell|address|bank', "%{$where['real_name']}%")
  59. ->whereOr('order_id', 'IN', function ($order) use ($where) {
  60. $order->name('store_order')->where('uid|real_name|order_id', 'LIKE', "%{$where['real_name']}%")->field('id')->select();
  61. })
  62. ->whereOr('uid', 'in', function ($q) use ($where) {
  63. $q->name('user')->whereLike('nickname|uid|phone', '%' . $where['real_name'] . '%')->field(['uid'])->select();
  64. });
  65. });
  66. });
  67. }
  68. /**
  69. * @param array $where
  70. * @param string $field
  71. * @param array|string[] $with
  72. * @param string $order
  73. * @param int $page
  74. * @param int $limit
  75. * @return array
  76. * @throws \think\db\exception\DataNotFoundException
  77. * @throws \think\db\exception\DbException
  78. * @throws \think\db\exception\ModelNotFoundException
  79. */
  80. public function getList(array $where, string $field = '*', array $with = ['order'], string $order = '', int $page = 0, int $limit = 0)
  81. {
  82. return $this->search($where)->field($field)->when(count($with), function ($query) use ($with) {
  83. $query->with($with);
  84. })->when($order != '', function ($query) use ($order) {
  85. $query->order($order);
  86. })->when($page && $limit, function ($query) use ($page, $limit) {
  87. $query->page($page, $limit);
  88. })->select()->toArray();
  89. }
  90. }