StoreOrderOrderInvoiceDao.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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\StoreOrder;
  15. use app\model\order\StoreOrderInvoice;
  16. /**
  17. * Class StoreOrderOrderInvoiceDao
  18. * @package app\dao\order
  19. */
  20. class StoreOrderOrderInvoiceDao extends BaseDao
  21. {
  22. /**
  23. * 限制精确查询字段
  24. * @var string[]
  25. */
  26. protected $withField = ['uid', 'order_id', 'real_name', 'user_phone', 'title'];
  27. /**
  28. * @var string
  29. */
  30. protected $alias = '';
  31. /**
  32. * @var string
  33. */
  34. protected $join_alis = '';
  35. protected function setModel(): string
  36. {
  37. return StoreOrderInvoice::class;
  38. }
  39. public function joinModel(): string
  40. {
  41. return StoreOrder::class;
  42. }
  43. /**
  44. * 关联模型
  45. * @param string $alias
  46. * @param string $join_alias
  47. * @return \crmeb\basic\BaseModel
  48. */
  49. public function getModel(string $alias = 'i', string $join_alias = 'o', $join = 'left')
  50. {
  51. $this->alias = $alias;
  52. $this->join_alis = $join_alias;
  53. /** @var StoreOrder $storeOrder */
  54. $storeOrder = app()->make($this->joinModel());
  55. $table = $storeOrder->getName();
  56. return parent::getModel()->alias($alias)->join($table . ' ' . $join_alias, $alias . '.order_id = ' . $join_alias . '.id', $join);
  57. }
  58. public function getSearch($where)
  59. {
  60. $realName = $where['real_name'] ?? '';
  61. $fieldKey = $where['field_key'] ?? '';
  62. $fieldKey = $fieldKey == 'all' ? '' : $fieldKey;
  63. return $this->getModel()->when(isset($where['data']) && $where['data'], function ($query) use ($where) {
  64. if (strstr($where['data'], '-') !== false) {
  65. [$startTime, $endTime] = explode('-', $where['data']);
  66. $query->whereBetween($this->alias . '.add_time', [strtotime($startTime), strtotime($endTime) + 86400]);
  67. }
  68. })->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
  69. switch ((int)$where['status']) {
  70. case 0://未支付
  71. $query->where($this->join_alis . '.paid', 0)->where($this->join_alis . '.status', 0)->where($this->join_alis . '.refund_status', 0)->where($this->join_alis . '.is_del', 0);
  72. break;
  73. case 1://已支付 未发货
  74. $query->where($this->join_alis . '.paid', 1)->where($this->join_alis . '.status', 0)->where($this->join_alis . '.refund_status', 0)->when(isset($where['shipping_type']), function ($query) {
  75. $query->where($this->join_alis . '.shipping_type', 1);
  76. })->where($this->join_alis . '.is_del', 0);
  77. break;
  78. case 2://已支付 待收货
  79. $query->where($this->join_alis . '.paid', 1)->where($this->join_alis . '.status', 1)->where($this->join_alis . '.refund_status', 0)->where($this->join_alis . '.is_del', 0);
  80. break;
  81. case 3:// 已支付 已收货 待评价
  82. $query->where($this->join_alis . '.paid', 1)->where($this->join_alis . '.status', 2)->where($this->join_alis . '.refund_status', 0)->where($this->join_alis . '.is_del', 0);
  83. break;
  84. case 4:// 交易完成
  85. $query->where($this->join_alis . '.paid', 1)->where($this->join_alis . '.status', 3)->where($this->join_alis . '.refund_status', 0)->where($this->join_alis . '.is_del', 0);
  86. break;
  87. case 5://已支付 待核销
  88. $query->where($this->join_alis . '.paid', 1)->where($this->join_alis . '.status', 0)->where($this->join_alis . '.refund_status', 0)->where($this->join_alis . '.shipping_type', 2)->where($this->join_alis . '.is_del', 0);
  89. break;
  90. case 6://已支付 已核销 没有退款
  91. $query->where($this->join_alis . '.paid', 1)->where($this->join_alis . '.status', 2)->where($this->join_alis . '.refund_status', 0)->where($this->join_alis . '.shipping_type', 2)->where($this->join_alis . '.is_del', 0);
  92. break;
  93. case -1://退款中
  94. $query->where($this->join_alis . '.paid', 1)->where($this->join_alis . '.refund_status', 1)->where($this->join_alis . '.is_del', 0);
  95. break;
  96. case -2://已退款
  97. $query->where($this->join_alis . '.paid', 1)->where($this->join_alis . '.refund_status', 2)->where($this->join_alis . '.is_del', 0);
  98. break;
  99. case -3://退款
  100. $query->where($this->join_alis . '.paid', 1)->where($this->join_alis . '.refund_status', 'in', '1,2')->where($this->join_alis . '.is_del', 0);
  101. break;
  102. case -4://已删除
  103. $query->where($this->join_alis . '.is_del', 1);
  104. break;
  105. }
  106. })->when(isset($where['type']), function ($query) use ($where) {
  107. switch ($where['type']) {
  108. case 1:
  109. $query->where($this->join_alis . '.type', 0);
  110. break;
  111. case 2:
  112. $query->where($this->join_alis . '.type', 3);
  113. break;
  114. case 3:
  115. $query->where($this->join_alis . '.type', 1);
  116. break;
  117. case 4:
  118. $query->where($this->join_alis . '.type', 2);
  119. break;
  120. }
  121. })->when($realName && $fieldKey && in_array($fieldKey, $this->withField), function ($query) use ($where, $realName, $fieldKey) {
  122. if ($fieldKey !== 'title') {
  123. $query->where($this->join_alis . '.' . trim($fieldKey), trim($realName));
  124. } else {
  125. $query->where($this->join_alis . '.id', 'in', function ($que) use ($where) {
  126. $que->name('store_order_cart_info')->whereIn('product_id', function ($q) use ($where) {
  127. $q->name('store_product')->whereLike('store_name|keyword', '%' . $where['real_name'] . '%')->field(['id'])->select();
  128. })->field(['oid'])->select();
  129. });
  130. }
  131. })->when($realName && !$fieldKey, function ($query) use ($where) {
  132. $query->where(function ($que) use ($where) {
  133. $que->whereLike($this->alias . '.order_id|' . $this->alias . '.invoice_id|' . $this->alias . '.name|' . $this->alias . '.drawer_phone|' . $this->alias . '.email|' . $this->alias . '.tell|' . $this->alias . '.address|' . $this->alias . '.bank', "%{$where['real_name']}%")
  134. ->whereOr($this->join_alis . '.uid|' . $this->join_alis . '.real_name|' . $this->join_alis . '.order_id', 'LIKE', "%{$where['real_name']}%")
  135. ->whereOr($this->join_alis . '.uid', 'in', function ($q) use ($where) {
  136. $q->name('user')->whereLike('nickname|uid|phone', '%' . $where['real_name'] . '%')->field(['uid'])->select();
  137. })->whereOr($this->join_alis . '.id', 'in', function ($que) use ($where) {
  138. $que->name('store_order_cart_info')->whereIn('product_id', function ($q) use ($where) {
  139. $q->name('store_product')->whereLike('store_name|keyword', '%' . $where['real_name'] . '%')->field(['id'])->select();
  140. })->field(['oid'])->select();
  141. });
  142. });
  143. });
  144. }
  145. public function getCount(array $where)
  146. {
  147. return $this->getSearch($where)->count();
  148. }
  149. /**
  150. * @param array $where
  151. * @param string $field
  152. * @param array|string[] $with
  153. * @param string $order
  154. * @param int $page
  155. * @param int $limit
  156. * @return array
  157. * @throws \think\db\exception\DataNotFoundException
  158. * @throws \think\db\exception\DbException
  159. * @throws \think\db\exception\ModelNotFoundException
  160. */
  161. public function getList(array $where, string $field = '*', string $order = '', int $page = 0, int $limit = 0)
  162. {
  163. return $this->getSearch($where)->field($field)->when($order != '', function ($query) use ($order) {
  164. $query->order($order);
  165. })->when($page && $limit, function ($query) use ($page, $limit) {
  166. $query->page($page, $limit);
  167. })->select()->toArray();
  168. }
  169. }