OwnerAreaReportService.php 5.3 KB

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