OwnerAreaReportService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 = ["storage_fee"=>$report->storage_fee - $diffAmount];
  61. $report->initial_fee -= $report->storage_fee - $diffAmount;
  62. if ($report->confirm_fee !== null)$up["difference"] = $report->initial_fee - $report->confirm_fee;
  63. $report->update($up);
  64. }
  65. }
  66. }
  67. $area->update($values);
  68. DB::commit();
  69. return true;
  70. }catch (\Exception $e){
  71. DB::rollBack();
  72. return false;
  73. }
  74. }
  75. public function get(array $params, array $withs = null)
  76. {
  77. $query = OwnerAreaReport::query()->orderByDesc('id');
  78. if ($withs)$query->with($withs);
  79. return $this->query($query,$params)->get();
  80. }
  81. //锁定面积
  82. public function lockArea($id = null, $ownerId = null, $countingMonth = null)
  83. {
  84. if (!$id && !$ownerId && !$countingMonth)return false;
  85. $query = OwnerAreaReport::query()->where("status","编辑中");
  86. if ($id)$query->where("id",$id);
  87. if ($ownerId)$query->where("owner_id",$ownerId);
  88. if ($countingMonth)$query->where("counting_month","like",$countingMonth."%");
  89. return $query->update(["status"=>"已完成"]);
  90. }
  91. //根据货主生成盘点面积记录
  92. public function notExistToInsert($owners)
  93. {
  94. if (!$owners)return;
  95. $reports = OwnerAreaReport::query()
  96. ->where("counting_month",">=",date("Y-m")."-01")
  97. ->whereIn("owner_id",array_column($owners->toArray(),"id"))->get();
  98. $sign = [];
  99. foreach ($reports as $report)$sign[$report->owner_id."_".$report->owner_storage_price_model_id] = true;
  100. $month = date('Y-m-d');
  101. $date = date('Y-m-d H:i:s');
  102. $createOwnerAreaReport = [];
  103. foreach ($owners as $owner){
  104. if (!$owner->ownerStoragePriceModels)continue;
  105. foreach ($owner->ownerStoragePriceModels as $model){
  106. $key = $owner->id."_".$model->id;
  107. if (!isset($sign[$key])) $createOwnerAreaReport[] = [
  108. "owner_id" => $owner->id,
  109. "counting_month" => $month,
  110. "user_owner_group_id" => $owner->user_owner_group_id,
  111. "created_at" => $date,
  112. "owner_storage_price_model_id" => $model->id,
  113. ];
  114. }
  115. }
  116. if ($createOwnerAreaReport){
  117. DB::table("owner_area_reports")->insert($createOwnerAreaReport);
  118. LogService::log(__METHOD__,"客户管理-生成盘点记录",json_encode($createOwnerAreaReport));
  119. }
  120. }
  121. }