OwnerAreaReportService.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. protected $modelClass=OwnerAreaReport::class;
  13. /**
  14. * @param Builder $builder
  15. * @param array $params
  16. * @return Builder
  17. */
  18. private function query(Builder $builder, array $params)
  19. {
  20. $columnQueryRules = [
  21. 'counting_month_start' => ['alias' => 'counting_month', 'startDate' => '-01'],
  22. 'counting_month_end' => ['alias' => 'counting_month', 'endDate' => '-31'],
  23. 'owner_id' => ['multi' => ','],
  24. ];
  25. if ($params["customer_id"] ?? false){
  26. $builder->whereHas('owner',function ($query)use(&$params){
  27. /** @var Builder $query*/
  28. $query->where("customer_id",$params["customer_id"]);
  29. unset($params["customer_id"]);
  30. });
  31. }
  32. return app(QueryService::class)->query($params, $builder, $columnQueryRules);
  33. }
  34. public function paginate(array $params, array $withs = null)
  35. {
  36. $areas = OwnerAreaReport::query()->orderByDesc('id');
  37. if ($withs)$areas->with($withs);
  38. return $this->query($areas,$params)->paginate($params["paginate"] ?? 50);
  39. }
  40. /**
  41. * 面积变更会去对比账单更新账单 考虑到后续账单变更有多种渠道此处加排他锁
  42. *
  43. * @param array $params
  44. * @param array $values
  45. * @return bool
  46. */
  47. public function update(array $params, array $values):bool
  48. {
  49. DB::beginTransaction();
  50. $area = $this->query(OwnerAreaReport::query(),$params)->with("ownerStoragePriceModel")->lockForUpdate()->first();
  51. try{
  52. if ($values["accounting_area"] ?? null && $area->accounting_area != $values["accounting_area"]){
  53. $report = OwnerBillReport::query()->lockForUpdate()->where("owner_id",$area->owner_id)
  54. ->where("counting_month",'like',$area->counting_month."%")->first();
  55. if ($report){
  56. if (!$area->ownerStoragePriceModel)return false;
  57. $diff = $area->accounting_area - $values["accounting_area"];
  58. $diffAmount = app("OwnerStoragePriceModelService")->calculationAmount($area->ownerStoragePriceModel, $diff, $area->owner_id, $area->counting_month);
  59. if ($diffAmount != 0){
  60. $up = ["initial_fee"=>$report->initial_fee - $diffAmount];
  61. if ($report->confirm_fee !== null)$up["difference"] = $up["initial_fee"] - $report->confirm_fee;
  62. $report->update($up);
  63. }
  64. }
  65. }
  66. $area->update($values);
  67. DB::commit();
  68. return true;
  69. }catch (\Exception $e){
  70. DB::rollBack();
  71. return false;
  72. }
  73. }
  74. public function get(array $params, array $withs = null)
  75. {
  76. $query = OwnerAreaReport::query()->orderByDesc('id');
  77. if ($withs)$query->with($withs);
  78. return $this->query($query,$params)->get();
  79. }
  80. //锁定面积
  81. public function lockArea($id = null, $ownerId = null, $countingMonth = null)
  82. {
  83. if (!$id && !$ownerId && !$countingMonth)return false;
  84. $query = OwnerAreaReport::query()->where("status","编辑中");
  85. if ($id)$query->where("id",$id);
  86. if ($ownerId)$query->where("owner_id",$ownerId);
  87. if ($countingMonth)$query->where("counting_month","like",$countingMonth."%");
  88. return $query->update(["status"=>"已完成"]);
  89. }
  90. //根据货主生成盘点面积记录
  91. public function notExistToInsert($owners)
  92. {
  93. if (!$owners)return;
  94. $reports = OwnerAreaReport::query()
  95. ->where("counting_month",">=",date("Y-m")."-01")
  96. ->whereIn("owner_id",array_column($owners->toArray(),"id"))->get();
  97. $sign = [];
  98. foreach ($reports as $report)$sign[$report->owner_id."_".$report->owner_storage_price_model_id] = true;
  99. $month = date('Y-m-d');
  100. $date = date('Y-m-d H:i:s');
  101. $createOwnerAreaReport = [];
  102. foreach ($owners as $owner){
  103. if (!$owner->ownerStoragePriceModels)continue;
  104. foreach ($owner->ownerStoragePriceModels as $model){
  105. $key = $owner->id."_".$model->id;
  106. if (!isset($sign[$key])) $createOwnerAreaReport[] = [
  107. "owner_id" => $owner->id,
  108. "counting_month" => $month,
  109. "user_owner_group_id" => $owner->user_owner_group_id,
  110. "created_at" => $date,
  111. "owner_storage_price_model_id" => $model->id,
  112. ];
  113. }
  114. }
  115. if ($createOwnerAreaReport){
  116. DB::table("owner_area_reports")->insert($createOwnerAreaReport);
  117. LogService::log(__METHOD__,"客户管理-生成盘点记录",json_encode($createOwnerAreaReport));
  118. }
  119. }
  120. }