OwnerStoragePriceModelService.php 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace App\Services;
  3. use App\Order;
  4. use App\OwnerReport;
  5. use App\OwnerStoragePriceModel;
  6. use Illuminate\Support\Facades\DB;
  7. use App\Traits\ServiceAppAop;
  8. class OwnerStoragePriceModelService
  9. {
  10. use ServiceAppAop;
  11. protected $modelClass=OwnerStoragePriceModel::class;
  12. public function getSelection(array $columns = ["counting_type","using_type"], array $withs = [])
  13. {
  14. return OwnerStoragePriceModel::query()->select($columns)->with($withs)->get();
  15. }
  16. public function paginate($id = null,array $withs = [])
  17. {
  18. $query = OwnerStoragePriceModel::query()->orderByDesc('id')->with($withs);
  19. if ($id)$query->where("id",$id);
  20. return $query->paginate($params["paginate"] ?? 50);
  21. }
  22. public function create(array $params)
  23. {
  24. return OwnerStoragePriceModel::query()->create($params);
  25. }
  26. public function update(array $params, array $values)
  27. {
  28. $query = OwnerStoragePriceModel::query();
  29. foreach ($params as $column => $value){
  30. $query->where($column,$value);
  31. }
  32. return $query->update($values);
  33. }
  34. public function find($id, $withs = [])
  35. {
  36. return OwnerStoragePriceModel::query()->with($withs)->find($id);
  37. }
  38. public function destroy($id)
  39. {
  40. DB::delete(DB::raw("DELETE FROM owner_storage_price_model_owner WHERE owner_storage_price_model_id = ?"),[$id]);
  41. return OwnerStoragePriceModel::destroy($id);
  42. }
  43. //暂时不考虑单位换算问题:后期可能存在多单位换算,此处仅视为单位为 m²,m³
  44. public function calculationAmount(OwnerStoragePriceModel $model, $area, $owner_id = null, $month = null)
  45. {
  46. /** @var \stdClass $model */
  47. if (!$model || !$area) return 0;
  48. if ($area < $model->minimum_area) $area = $model->minimum_area;
  49. switch ($model->discount_type){
  50. case "按单减免":
  51. if ($owner_id && $month){
  52. if ($model->timeUnit->name == '月'){
  53. $report = OwnerReport::query()->select("id",DB::raw("(to_business_quantity+to_customer_quantity) AS total"))
  54. ->where("owner_id",$owner_id)
  55. ->where("counting_month","like",$month."%")->first();
  56. if ($report && $report->total>=0)$area -= floor($report->total/$model->discount_value);
  57. }else{
  58. $days = date('t', strtotime($month."-01"));
  59. $area *= $days;
  60. $logistics = app("OwnerPriceExpressService")->getBuildLogistic($owner_id);
  61. $logisticSql = "(''";
  62. foreach ($logistics as $logistic)$logisticSql.=",".$logistic;
  63. $logisticSql .= ")";
  64. for($i=1;$i<=$days;$i++){
  65. $d = $i<10 ? "0".$i : $i;
  66. $query = DB::raw("SELECT COUNT(1) c FROM orders WHERE logistic_id IN {$logisticSql} AND wms_status = ? AND wms_edittime BETWEEN ? AND ?");
  67. $count = DB::selectOne($query,['订单完成',$month."-".$d." 00:00:00",$month."-".$d." 23:59:59"]);
  68. if ($count && $count->c>=0)$area -= floor($count->c/$model->discount_value);
  69. }
  70. }
  71. }
  72. break;
  73. case "固定减免":
  74. if ($model->timeUnit->name == '月'){
  75. $area -= $model->discount_value;
  76. }else{
  77. $days = date('t', strtotime($month."-01"));
  78. $area *= $days;
  79. $area -= $days*$model->discount_value;
  80. }
  81. break;
  82. }
  83. return $area*$model->price;
  84. }
  85. }