OrderProductModel.php 6.0 KB

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