OrderProductModel.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. class OrderProductModel extends BaseModel
  5. {
  6. protected $table = 'erp_order_product';
  7. protected function genSchema(array $schema)
  8. {
  9. // TODO: Implement genSchema() method.
  10. }
  11. public function product()
  12. {
  13. return self::hasOne(ProductModel::class, 'id', 'product_id')->where('is_delete', 0);
  14. }
  15. public function payments()
  16. {
  17. return self::hasMany(OrderPaymentModel::class, 'order_id', 'order_id')->where('is_delete', 0);
  18. }
  19. /**
  20. * @param $o_id
  21. * @param int $is_pay
  22. * @return OrderProductModel[]|array|\think\Collection
  23. * @throws \think\db\exception\DataNotFoundException
  24. * @throws \think\db\exception\DbException
  25. * @throws \think\db\exception\ModelNotFoundException
  26. */
  27. public function findByOrderId($o_id, $is_pay = 1)
  28. {
  29. return $this->where('is_delete', 0)
  30. ->where('order_id', $o_id)
  31. ->where('is_pay', $is_pay)
  32. ->select();
  33. }
  34. /**
  35. * @param $o_id
  36. * @param int $page
  37. * @param int $size
  38. * @return \think\Paginator
  39. * @throws \think\db\exception\DbException
  40. */
  41. public function fetchByOrderId($o_id, $page = 1, $size = 10)
  42. {
  43. return $this->where('is_delete', 0)
  44. ->where('order_id', $o_id)
  45. ->page($page)
  46. ->paginate($size);
  47. }
  48. /**
  49. * @param $c_id
  50. * @param int $is_upload_numerology
  51. * @param int $is_upload
  52. * @return int
  53. * @throws \think\db\exception\DbException
  54. */
  55. public function countByCustomerId($c_id, $is_upload_numerology = 1, $is_upload = null)
  56. {
  57. $where = [
  58. ['is_delete', '=', 0],
  59. ['customer_id', '=', $c_id],
  60. ['is_upload_numerology', '=', $is_upload_numerology]
  61. ];
  62. if ($is_upload) {
  63. array_push($where, ['is_upload', '=', $is_upload]);
  64. }
  65. return $this->where($where)
  66. ->count();
  67. }
  68. public function countByAdviser($adviser_1_id, $start_time = null, $end_time = null)
  69. {
  70. $where = [
  71. ['is_delete', '=', 0],
  72. ['adviser_1_id', '=', $adviser_1_id],
  73. ['is_pay', '=', 1]
  74. ];
  75. if ($start_time) array_push($where, ['create_time', '>=', $start_time]);
  76. if ($end_time) array_push($where, ['create_time', '<=', $end_time]);
  77. return [
  78. $this->where($where)->sum('real_price'),
  79. $this->where(array_merge($where, [
  80. ['is_upload_numerology', '=', 1],
  81. ['is_upload', '=', 1]
  82. ]))->count(),
  83. $this->where(array_merge($where, [['is_upload_numerology', '=', 1]]))->count(),
  84. $this->where(array_merge($where, [['is_serve', '=', 0]]))->sum('real_price'),
  85. $this->where(array_merge($where, [['teacher_1_id', '>', 0]]))->sum('real_price'),
  86. $this->where(array_merge($where, [['is_serve', '=', 1]]))->sum('real_price'),
  87. ];
  88. }
  89. public function countByAdviserProductCategoryIds($adviser_1_id, $category_ids = [], $start_time = null, $end_time = null)
  90. {
  91. if (count($category_ids) == 0) return [0, 0];
  92. $where = [
  93. ['is_delete', '=', 0],
  94. ['adviser_1_id', '=', $adviser_1_id],
  95. ['is_pay', '=', 1],
  96. ['product_category_id', 'in', $category_ids]
  97. ];
  98. if ($start_time) array_push($where, ['create_time', '>=', $start_time]);
  99. if ($end_time) array_push($where, ['create_time', '<=', $end_time]);
  100. return [$this->where($where)->sum('real_price'), $this->where($where)->count()];
  101. }
  102. public function findByPaginate(array $params)
  103. {
  104. return $this->genWhere($params)
  105. ->paginate([
  106. 'list_rows' => 10,
  107. "query" => $params
  108. ]);
  109. }
  110. public function report($params)
  111. {
  112. return $this->genWhere($params)
  113. ->select();
  114. }
  115. public function genWhere($params)
  116. {
  117. $self_where = [
  118. ['erp_order_product.is_delete', '=', 0],
  119. ['erp_order_product.is_pay', '=', 1],
  120. ];
  121. $product_where = [];
  122. if ($params['order_no']) {
  123. array_push($self_where, ["{$this->table}.order_no", "like", "%{$params['order_no']}%"]);
  124. }
  125. if ($params['product_name']) {
  126. array_push($self_where, ["{$this->table}.product_name", "like", "%{$params['product_name']}%"]);
  127. }
  128. if ($params['adviser_id'] > 0) {
  129. array_push($self_where, ["{$this->table}.adviser_1_id|{$this->table}.adviser_2_id", "=", $params['adviser_id']]);
  130. }
  131. if ($params['teacher_id'] > 0) {
  132. array_push($self_where, ["{$this->table}.teacher_1_id|{$this->table}.teacher_2_id", "=", $params['teacher_id']]);
  133. }
  134. if ($params['date_range']) {
  135. $date_range = explode('/', preg_replace("/\s| /", "", $params['date_range']));
  136. $start_time = strtotime($date_range[0]);
  137. $end_time = strtotime($date_range[1]);
  138. if ($start_time > 0) {
  139. array_push($self_where, ["{$this->table}.create_time", ">=", $start_time]);
  140. }
  141. if ($end_time > 0) {
  142. array_push($self_where, ["{$this->table}.create_time", "<", $end_time]);
  143. }
  144. }
  145. if ($params['bar_code']) {
  146. array_push($product_where, ['bar_code', "like", "%{$params['bar_code']}%"]);
  147. }
  148. if ($params['company_id'] && $params['company_id'] > 0) {
  149. array_push($product_where, ['company_id', "=", $params['company_id']]);
  150. }
  151. $query = $this->where($self_where)
  152. ->with(['product', 'product.company', 'payments'])
  153. ->hasWhere('product', $product_where);
  154. if ($params['channel_id'] > 0) {
  155. $query->where("FIND_IN_SET('{$params['channel_id']}', OrderProductModel.channel_ids)");
  156. }
  157. return $query;
  158. }
  159. }