OwnerAreaReportService.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace App\Services;
  3. use App\OwnerAreaReport;
  4. use App\OwnerBillReport;
  5. use App\Services\common\QueryService;
  6. use Illuminate\Database\Eloquent\Builder;
  7. use Illuminate\Support\Facades\DB;
  8. Class OwnerAreaReportService
  9. {
  10. /**
  11. * @param Builder $builder
  12. * @param array $params
  13. * @return Builder
  14. */
  15. private function query(Builder $builder, array $params)
  16. {
  17. $columnQueryRules = [
  18. 'counting_month_start' => ['alias' => 'counting_month', 'startDate' => '-01'],
  19. 'counting_month_end' => ['alias' => 'counting_month', 'endDate' => '-31'],
  20. 'owner_id' => ['multi' => ','],
  21. ];
  22. if ($params["customer_id"] ?? false){
  23. $builder->whereHas('owner',function ($query)use(&$params){
  24. /** @var Builder $query*/
  25. $query->where("customer_id",$params["customer_id"]);
  26. unset($params["customer_id"]);
  27. });
  28. }
  29. return app(QueryService::class)->query($params, $builder, $columnQueryRules);
  30. }
  31. public function paginate(array $params, array $withs = null)
  32. {
  33. $areas = OwnerAreaReport::query()->orderByDesc('id');
  34. if ($withs)$areas->with($withs);
  35. return $this->query($areas,$params)->paginate($params["paginate"] ?? 50);
  36. }
  37. /**
  38. * 面积变更会去对比账单更新账单 考虑到后续账单变更有多种渠道此处加排他锁
  39. *
  40. * @param array $params
  41. * @param array $values
  42. * @return bool
  43. */
  44. public function update(array $params, array $values):bool
  45. {
  46. DB::beginTransaction();
  47. $area = $this->query(OwnerAreaReport::query(),$params)->with("ownerStoragePriceModel")->lockForUpdate()->first();
  48. try{
  49. if ($values["accounting_area"] ?? null && $area->accounting_area != $values["accounting_area"]){
  50. $report = OwnerBillReport::query()->lockForUpdate()->where("owner_id",$area->owner_id)
  51. ->where("counting_month",'like',$area->counting_month."%")->first();
  52. if ($report){
  53. if (!$area->ownerStoragePriceModel)return false;
  54. $diff = $area->accounting_area - $values["accounting_area"];
  55. $diffAmount = app("OwnerStoragePriceModelService")->calculationAmount($area->ownerStoragePriceModel, $diff, $area->owner_id, $area->counting_month);
  56. if ($diffAmount != 0){
  57. $up = ["initial_fee"=>$report->initial_fee - $diffAmount];
  58. if ($report->confirm_fee !== null)$up["difference"] = $up["initial_fee"] - $report->confirm_fee;
  59. $report->update($up);
  60. }
  61. }
  62. }
  63. $area->update($values);
  64. DB::commit();
  65. return true;
  66. }catch (\Exception $e){
  67. DB::rollBack();
  68. return false;
  69. }
  70. }
  71. public function get(array $params, array $withs = null)
  72. {
  73. $query = OwnerAreaReport::query()->orderByDesc('id');
  74. if ($withs)$query->with($withs);
  75. return $this->query($query,$params)->get();
  76. }
  77. //锁定面积
  78. public function lockArea($id = null, $ownerId = null, $countingMonth = null)
  79. {
  80. if (!$id && !$ownerId && !$countingMonth)return false;
  81. $query = OwnerAreaReport::query()->where("status","编辑中");
  82. if ($id)$query->where("id",$id);
  83. if ($ownerId)$query->where("owner_id",$ownerId);
  84. if ($countingMonth)$query->where("counting_month","like",$countingMonth."%");
  85. return $query->update(["status","已完成"]);
  86. }
  87. }