OrderModel.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace app\common\model;
  3. class OrderModel extends BaseModel
  4. {
  5. protected $table = 'erp_order';
  6. protected function genSchema(array $schema)
  7. {
  8. // TODO: Implement genSchema() method.
  9. }
  10. public function genOrderNo($store_id, $store_abbr) {
  11. $order = $this->where('store_id', $store_id)->order('id','desc')->find();
  12. if(!$order)
  13. return "{$store_abbr}10000001";
  14. $code = (int)str_ireplace($store_abbr, '', $order->no) + 1;
  15. return "{$store_abbr}{$code}";
  16. }
  17. public function products() {
  18. return $this->hasMany(OrderProductModel::class, 'order_id','id')->where('is_delete',0);
  19. }
  20. public function payments() {
  21. return $this->hasMany(OrderPaymentModel::class, 'order_id','id')->where('is_delete',0);
  22. }
  23. public function annuals() {
  24. return $this->hasMany(OrderAnnualFeeModel::class, 'order_id','id')->where('is_delete',0);
  25. }
  26. public function proceeds() {
  27. return $this->hasMany(OrderProceedsModel::class, 'order_id','id')->where('is_delete',0);
  28. }
  29. public function store() {
  30. return $this->hasOne(StoreModel::class, 'id', 'store_id');
  31. }
  32. public function findByPaginate(array $params) {
  33. $where = [
  34. ['is_delete', '=', 0]
  35. ];
  36. if($params['store_id'] > 0)
  37. array_push($where,['store_id', '=', $params['store_id']]);
  38. if($params['type'] > 0)
  39. array_push($where,['type', '=', $params['type']]);
  40. return $this->where($where)
  41. ->with(['products','store'])
  42. ->order('update_time','desc')
  43. ->paginate(['list_rows'=>10, "query" => $params]);
  44. }
  45. public function report(array $params) {
  46. $where = [
  47. ['is_delete', '=', 0]
  48. ];
  49. if($params['store_id'] > 0)
  50. array_push($where,['store_id', '=', $params['store_id']]);
  51. if($params['type'] > 0)
  52. array_push($where,['type', '=', $params['type']]);
  53. return $this->where($where)
  54. ->with(['products','store'])
  55. ->order('create_time','desc')
  56. ->select();
  57. }
  58. public function findById($id)
  59. {
  60. return $this->where('is_delete',0)
  61. ->with([
  62. 'products',
  63. 'payments',
  64. 'payments.payment_channel',
  65. // 'payments.card_config',
  66. 'annuals',
  67. 'proceeds'
  68. ])->find($id);
  69. }
  70. /**
  71. * @param $params
  72. * @param bool $is_look_all
  73. * @return array
  74. * @throws \think\db\exception\DbException
  75. */
  76. public function search($params, $is_look_all = false) {
  77. $where = [
  78. ['is_delete', '=', 0],
  79. ['type', '=', $params['type']],
  80. ['store_id', '=', $params['store_id']],
  81. ];
  82. if (isset($params['order_no']) && strlen($params['order_no']) > 0)
  83. array_push($where, ['no', 'like', '%'.$params['order_no'].'%']);
  84. if (isset($params['start_time']) && $params['start_time'] > 0)
  85. array_push($where, ['create_time', '>=', $params['start_time']]);
  86. if (isset($params['end_time']) && $params['end_time'] > 0)
  87. array_push($where, ['create_time', '<=', $params['end_time']]);
  88. if (isset($params['obj_name']) && strlen($params['obj_name']) > 0)
  89. array_push($where, ['obj_names', 'like', '%'.$params['obj_name'].'%']);
  90. $query = $this->where($where)
  91. ->with(['products']);
  92. if (isset($params['advisor_id']))
  93. $query->where("FIND_IN_SET('{$params['advisor_id']}', advisor_ids)");
  94. if (!$is_look_all) {
  95. $query->where("FIND_IN_SET('{$params['admin_id']}', advisor_ids)");
  96. }
  97. return $query->order('update_time', 'desc')
  98. ->page($params['page'] ?? 1)
  99. ->paginate($params['size'] ?? 10)->toArray();
  100. }
  101. public function fetchOrderByCustomerId($c_id) {
  102. return $this->where([
  103. ['is_delete', '=', 0],
  104. ['customer_id', '=', $c_id]
  105. ])->select()->toArray();
  106. }
  107. /**
  108. * @param $customer_id
  109. * @param int $page
  110. * @param int $size
  111. * @return array
  112. * @throws \think\db\exception\DbException
  113. */
  114. public function fetchByCustomerId($customer_id, $page = 1, $size = 10) {
  115. return $this->where([
  116. ['is_delete', '=', 0],
  117. ['customer_id', '=', $customer_id]
  118. ])->with(['products'])->order('update_time', 'desc')->page($page)->paginate($size)->toArray();
  119. }
  120. }