OwnerStoragePriceModelService.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Services;
  3. use App\OwnerReport;
  4. use App\OwnerStoragePriceModel;
  5. use Illuminate\Support\Facades\DB;
  6. Class OwnerStoragePriceModelService
  7. {
  8. public function getSelection(array $columns = ["counting_type","using_type"], array $withs = [])
  9. {
  10. return OwnerStoragePriceModel::query()->select($columns)->with($withs)->get();
  11. }
  12. public function paginate($id = null,array $withs = [])
  13. {
  14. $query = OwnerStoragePriceModel::query()->orderByDesc('id')->with($withs);
  15. if ($id)$query->where("id",$id);
  16. return $query->paginate($params["paginate"] ?? 50);
  17. }
  18. public function create(array $params)
  19. {
  20. return OwnerStoragePriceModel::query()->create($params);
  21. }
  22. public function update(array $params, array $values)
  23. {
  24. $query = OwnerStoragePriceModel::query();
  25. foreach ($params as $column => $value){
  26. $query->where($column,$value);
  27. }
  28. return $query->update($values);
  29. }
  30. public function find($id)
  31. {
  32. return OwnerStoragePriceModel::query()->find($id);
  33. }
  34. public function destroy($id)
  35. {
  36. DB::delete(DB::raw("DELETE FROM owner_storage_price_model_owner WHERE owner_storage_price_model_id = ?"),[$id]);
  37. return OwnerStoragePriceModel::destroy($id);
  38. }
  39. //暂时不考虑单位换算问题:后期可能存在多单位换算,此处仅视为单位为 m²
  40. public function calculationAmount(OwnerStoragePriceModel $model, $area, $owner_id = null, $month = null)
  41. {
  42. if (!$model || !$area) return 0;
  43. if ($area < $model->minimum_area) $area = $model->minimum_area;
  44. $money = $area*$model->price;
  45. switch ($model->discount_type){
  46. case "按单减免":
  47. if ($owner_id && $month){
  48. $report = OwnerReport::query()->select("id","total")
  49. ->where("owner_id",$owner_id)
  50. ->where("counting_month","like",$month."%")->first();
  51. $money -= $report ? ($report->total)*($model->discount_value) : 0;
  52. }
  53. break;
  54. case "固定减免":
  55. $money -= $model->discount_value;
  56. break;
  57. }
  58. return $money;
  59. }
  60. }