OwnerStoragePriceModelService.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace App\Services;
  3. use App\OwnerReport;
  4. use App\OwnerStoragePriceModel;
  5. use App\Traits\ServiceAppAop;
  6. Class OwnerStoragePriceModelService
  7. {
  8. use ServiceAppAop;
  9. public function getSelection(array $columns = ["counting_type","using_type"], array $withs = [])
  10. {
  11. return OwnerStoragePriceModel::query()->select($columns)->with($withs)->get();
  12. }
  13. public function paginate($id = null,array $withs = [])
  14. {
  15. $query = OwnerStoragePriceModel::query()->orderByDesc('id')->with($withs);
  16. if ($id)$query->where("id",$id);
  17. return $query->paginate($params["paginate"] ?? 50);
  18. }
  19. public function create(array $params)
  20. {
  21. return OwnerStoragePriceModel::query()->create($params);
  22. }
  23. public function update(array $params, array $values)
  24. {
  25. $query = OwnerStoragePriceModel::query();
  26. foreach ($params as $column => $value){
  27. $query->where($column,$value);
  28. }
  29. return $query->update($values);
  30. }
  31. public function find($id)
  32. {
  33. return OwnerStoragePriceModel::query()->find($id);
  34. }
  35. public function destroy($id)
  36. {
  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. }