OwnerStoragePriceModelService.php 2.1 KB

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