select($columns)->with($withs)->get(); } public function paginate($id = null,array $withs = []) { $query = OwnerStoragePriceModel::query()->orderByDesc('id')->with($withs); if ($id)$query->where("id",$id); return $query->paginate($params["paginate"] ?? 50); } public function create(array $params) { return OwnerStoragePriceModel::query()->create($params); } public function update(array $params, array $values) { $query = OwnerStoragePriceModel::query(); foreach ($params as $column => $value){ $query->where($column,$value); } return $query->update($values); } public function find($id, $withs = []) { return OwnerStoragePriceModel::query()->with($withs)->find($id); } public function destroy($id) { DB::delete(DB::raw("DELETE FROM owner_storage_price_model_owner WHERE owner_storage_price_model_id = ?"),[$id]); return OwnerStoragePriceModel::destroy($id); } //暂时不考虑单位换算问题:后期可能存在多单位换算,此处仅视为单位为 m²,m³ public function calculationAmount(OwnerStoragePriceModel $model, $area, $owner_id = null, $month = null) { if (!$model || !$area) return 0; if ($area < $model->minimum_area) $area = $model->minimum_area; $money = $area*$model->price; switch ($model->discount_type){ case "按单减免": if ($owner_id && $month){ if ($model->timeUnit->name == '月'){ $report = OwnerReport::query()->select("id","total") ->where("owner_id",$owner_id) ->where("counting_month","like",$month."%")->first(); $money -= $report ? ($report->total)*($model->discount_value) : 0; }else{ $days = date('t', strtotime($month."-01")); $money *= $days; for($i=1;$i<=$days;$i++){ $d = $i<10 ? "0".$i : $i; $query = DB::raw("SELECT COUNT(1) c FROM orders WHERE wms_status = ? and updated_at between ? and ?"); $count = DB::selectOne($query,['订单完成',$month."-".$d." 00:00:00",$month."-".$d." 23:59:59"]); $money -= $count ? ($count->c)*($model->discount_value) : 0; } } } break; case "固定减免": if ($model->timeUnit->name == '月'){ $money -= $model->discount_value; }else{ $days = date('t', strtotime($month."-01")); $money *= $days; $money -= $days*$model->discount_value; } break; } return $money; } }