OwnerStoragePriceModelService.php 2.3 KB

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