OwnerAreaReportService.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. //根据货主生成盘点面积记录
  90. public function notExistToInsert($owners)
  91. {
  92. if (!$owners)return;
  93. $reports = OwnerAreaReport::query()
  94. ->where("counting_month",">=",date("Y-m")."-01")
  95. ->whereIn("owner_id",array_column($owners->toArray(),"id"))->get();
  96. $sign = [];
  97. foreach ($reports as $report)$sign[$report->owner_id."_".$report->owner_storage_price_model_id] = true;
  98. $month = date('Y-m-d');
  99. $date = date('Y-m-d H:i:s');
  100. $createOwnerAreaReport = [];
  101. foreach ($owners as $owner){
  102. if (!$owner->ownerStoragePriceModels)continue;
  103. foreach ($owner->ownerStoragePriceModels as $model){
  104. $key = $owner->id."_".$model->id;
  105. if (!isset($sign[$key])) $createOwnerAreaReport[] = [
  106. "owner_id" => $owner->id,
  107. "counting_month" => $month,
  108. "user_owner_group_id" => $owner->user_owner_group_id,
  109. "created_at" => $date,
  110. "owner_storage_price_model_id" => $model->id,
  111. ];
  112. }
  113. }
  114. if ($createOwnerAreaReport){
  115. DB::table("owner_area_reports")->insert($createOwnerAreaReport);
  116. LogService::log(__METHOD__,"客户管理-生成盘点记录",json_encode($createOwnerAreaReport));
  117. }
  118. }
  119. }