OwnerStoragePriceModelService.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace App\Services;
  3. use App\Order;
  4. use App\OwnerReport;
  5. use App\OwnerStoragePriceModel;
  6. use App\Services\common\QueryService;
  7. use Illuminate\Database\Eloquent\Builder;
  8. use Illuminate\Support\Facades\DB;
  9. use App\Traits\ServiceAppAop;
  10. class OwnerStoragePriceModelService
  11. {
  12. use ServiceAppAop;
  13. protected $modelClass=OwnerStoragePriceModel::class;
  14. public function getSelection(array $columns = ["counting_type","using_type"], array $withs = [])
  15. {
  16. return OwnerStoragePriceModel::query()->select($columns)->with($withs)->get();
  17. }
  18. public function paginate($id = null,array $withs = [])
  19. {
  20. $query = OwnerStoragePriceModel::query()->orderByDesc('id')->with($withs);
  21. if ($id)$query->where("id",$id);
  22. return $query->paginate($params["paginate"] ?? 50);
  23. }
  24. public function create(array $params)
  25. {
  26. $params["operation"] = "C";
  27. return OwnerStoragePriceModel::query()->create($params);
  28. }
  29. /**
  30. * 拷贝目标数据
  31. *
  32. * @param object|int $model
  33. * @param array $values
  34. * @param array|null $owners
  35. *
  36. * @return object|null
  37. */
  38. public function copy($model, $values = [], $owners = null)
  39. {
  40. if (is_integer($model))$model = OwnerStoragePriceModel::query()->find($model);
  41. if (!$model)return null;
  42. $values["operation"] = "U";
  43. $values["target_id"] = $model->id;
  44. foreach ($model->getFillable() as $column){
  45. if (!array_key_exists($column,$values))$values[$column] = $model[$column];
  46. }
  47. if ($owners===null){
  48. $query = DB::raw("SELECT * FROM owner_storage_price_model_owner WHERE owner_storage_price_model_id = {$model->id}");
  49. $owners = array_column(DB::select($query),"owner_id");
  50. }
  51. /** @var OwnerStoragePriceModel $model */
  52. $model = OwnerStoragePriceModel::query()->create($values);
  53. $model->owners()->sync($owners);
  54. return $model;
  55. }
  56. /**
  57. * 审核或恢复目标集
  58. *
  59. * @param bool $isAudit
  60. * @param integer|null|array $ownerId
  61. * @param integer|null|array $ids
  62. */
  63. public function auditOrRecover($isAudit = true, $ownerId = null, $ids = null)
  64. {
  65. if (!$ownerId && !$ids)return;
  66. $result = app(QueryService::class)->priceModelAuditOrRecoverQuery($isAudit,OwnerStoragePriceModel::query(),$ownerId,$ids);
  67. if ($result["delete"])$this->destroy($result["delete"]);
  68. if ($result["update"])OwnerStoragePriceModel::query()->whereIn("id",$result["update"])->update(["operation"=>null,"target_id"=>null]);
  69. }
  70. public function update(array $params, array $values)
  71. {
  72. $query = OwnerStoragePriceModel::query();
  73. foreach ($params as $column => $value){
  74. $query->where($column,$value);
  75. }
  76. return $query->update($values);
  77. }
  78. public function find($id, $withs = [])
  79. {
  80. return OwnerStoragePriceModel::query()->with($withs)->find($id);
  81. }
  82. public function destroy($id)
  83. {
  84. $sql = "SELECT * FROM owner_storage_price_model_owner WHERE owner_storage_price_model_id ";
  85. if (is_array($id)){
  86. $query = "IN (";
  87. for ($i=0;$i<count($id)-1;$i++)$query .= "?,";
  88. $query .= "?)";
  89. }else{
  90. $query = "= ?";
  91. $id = [$id];
  92. }
  93. $owners = array_column(DB::select(DB::raw($sql.$query),$id),"owner_id");
  94. DB::delete(DB::raw("DELETE FROM owner_storage_price_model_owner WHERE owner_storage_price_model_id ".$query),$id);
  95. app("OwnerService")->refreshRelevance($owners,0,true);
  96. return OwnerStoragePriceModel::destroy($id);
  97. }
  98. //暂时不考虑单位换算问题:后期可能存在多单位换算,此处仅视为单位为 m²,m³
  99. public function calculationAmount(OwnerStoragePriceModel $model, $area, $owner_id = null, $month = null)
  100. {
  101. /** @var \stdClass $model */
  102. if (!$model || !$area || $model->operation) return 0;
  103. if ($area < $model->minimum_area) $area = $model->minimum_area;
  104. switch ($model->discount_type){
  105. case "按单减免":
  106. if ($owner_id && $month){
  107. if ($model->timeUnit->name == '月'){
  108. $report = OwnerReport::query()->select("id",DB::raw("(to_business_quantity+to_customer_quantity) AS total"))
  109. ->where("owner_id",$owner_id)
  110. ->where("counting_month","like",$month."%")->first();
  111. if ($report && $report->total>=0)$area -= floor($report->total/$model->discount_value);
  112. }else{
  113. $days = date('t', strtotime($month."-01"));
  114. $area *= $days;
  115. $logistics = app("OwnerPriceExpressService")->getBuildLogistic($owner_id);
  116. $logisticSql = "(''";
  117. foreach ($logistics as $logistic)$logisticSql.=",".$logistic;
  118. $logisticSql .= ")";
  119. for($i=1;$i<=$days;$i++){
  120. $d = $i<10 ? "0".$i : $i;
  121. $query = DB::raw("SELECT COUNT(1) c FROM orders WHERE logistic_id IN {$logisticSql} AND wms_status = ? AND wms_edittime BETWEEN ? AND ?");
  122. $count = DB::selectOne($query,['订单完成',$month."-".$d." 00:00:00",$month."-".$d." 23:59:59"]);
  123. if ($count && $count->c>=0)$area -= floor($count->c/$model->discount_value);
  124. }
  125. }
  126. }
  127. break;
  128. case "固定减免":
  129. if ($model->timeUnit->name == '月'){
  130. $area -= $model->discount_value;
  131. }else{
  132. $days = date('t', strtotime($month."-01"));
  133. $area *= $days;
  134. $area -= $days*$model->discount_value;
  135. }
  136. break;
  137. }
  138. $index = 0;
  139. if ($model->amount_interval){
  140. $total = app("OrderService")->getOrderQuantity($owner_id);
  141. for ($i=count($model->amount_interval);$i<0;$i--){
  142. if ($total>=$model->amount_interval[$i])$model->price = $index;
  143. }
  144. }
  145. return $area*$model->price[$index];
  146. }
  147. }