OwnerAreaReportService.php 3.6 KB

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