where('is_delete', 0); } public function payments() { return self::hasMany(OrderPaymentModel::class, 'order_id', 'order_id')->where('is_delete', 0); } /** * @param $o_id * @param int $is_pay * @return OrderProductModel[]|array|\think\Collection * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function findByOrderId($o_id, $is_pay = 1) { return $this->where('is_delete', 0) ->where('order_id', $o_id) ->where('is_pay', $is_pay) ->select(); } /** * @param $o_id * @param int $page * @param int $size * @return \think\Paginator * @throws \think\db\exception\DbException */ public function fetchByOrderId($o_id, $page = 1, $size = 10) { return $this->where('is_delete', 0) ->where('order_id', $o_id) ->page($page) ->paginate($size); } /** * @param $c_id * @param int $is_upload_numerology * @param int $is_upload * @return int * @throws \think\db\exception\DbException */ public function countByCustomerId($c_id, $is_upload_numerology = 1, $is_upload = null) { $where = [ ['is_delete', '=', 0], ['customer_id', '=', $c_id], ['is_upload_numerology', '=', $is_upload_numerology] ]; if ($is_upload) { array_push($where, ['is_upload', '=', $is_upload]); } return $this->where($where) ->count(); } public function countByAdviser($adviser_1_id, $start_time = null, $end_time = null) { $where = [ ['is_delete', '=', 0], ['adviser_1_id', '=', $adviser_1_id], ['is_pay', '=', 1] ]; if ($start_time) array_push($where, ['create_time', '>=', $start_time]); if ($end_time) array_push($where, ['create_time', '<=', $end_time]); return [ $this->where($where)->sum('real_price'), $this->where(array_merge($where, [ ['is_upload_numerology', '=', 1], ['is_upload', '=', 1] ]))->count(), $this->where(array_merge($where, [['is_upload_numerology', '=', 1]]))->count(), $this->where(array_merge($where, [['is_serve', '=', 0]]))->sum('real_price'), $this->where(array_merge($where, [['teacher_1_id', '>', 0]]))->sum('real_price'), $this->where(array_merge($where, [['is_serve', '=', 1]]))->sum('real_price'), ]; } public function countByAdviserProductCategoryIds($adviser_1_id, $category_ids = [], $start_time = null, $end_time = null) { if (count($category_ids) == 0) return [0, 0]; $where = [ ['is_delete', '=', 0], ['adviser_1_id', '=', $adviser_1_id], ['is_pay', '=', 1], ['product_category_id', 'in', $category_ids] ]; if ($start_time) array_push($where, ['create_time', '>=', $start_time]); if ($end_time) array_push($where, ['create_time', '<=', $end_time]); return [$this->where($where)->sum('real_price'), $this->where($where)->count()]; } public function findByPaginate(array $params) { return $this->genWhere($params) ->paginate([ 'list_rows' => 10, "query" => $params ]); } public function report($params) { return $this->genWhere($params) ->select(); } public function genWhere($params) { $self_where = [ ['erp_order_product.is_delete', '=', 0], ['erp_order_product.is_pay', '=', 1], ]; $product_where = []; if ($params['order_no']) { array_push($self_where, ["{$this->table}.order_no", "like", "%{$params['order_no']}%"]); } if ($params['product_name']) { array_push($self_where, ["{$this->table}.product_name", "like", "%{$params['product_name']}%"]); } if ($params['adviser_id'] > 0) { array_push($self_where, ["{$this->table}.adviser_1_id|{$this->table}.adviser_2_id", "=", $params['adviser_id']]); } if ($params['teacher_id'] > 0) { array_push($self_where, ["{$this->table}.teacher_1_id|{$this->table}.teacher_2_id", "=", $params['teacher_id']]); } if ($params['date_range']) { $date_range = explode('/', preg_replace("/\s| /", "", $params['date_range'])); $start_time = strtotime($date_range[0]); $end_time = strtotime($date_range[1]); if ($start_time > 0) { array_push($self_where, ["{$this->table}.create_time", ">=", $start_time]); } if ($end_time > 0) { array_push($self_where, ["{$this->table}.create_time", "<", $end_time]); } } if ($params['bar_code']) { array_push($product_where, ['bar_code', "like", "%{$params['bar_code']}%"]); } if ($params['company_id'] && $params['company_id'] > 0) { array_push($product_where, ['company_id', "=", $params['company_id']]); } $query = $this->where($self_where) ->with(['product', 'product.company', 'payments']) ->hasWhere('product', $product_where); if ($params['channel_id'] > 0) { $query->where("FIND_IN_SET('{$params['channel_id']}', OrderProductModel.channel_ids)"); } return $query; } }