OwnerStoragePriceModelService.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. if (!$model || !$area) return 0;
  47. if ($area < $model->minimum_area) $area = $model->minimum_area;
  48. $money = $area*$model->price;
  49. switch ($model->discount_type){
  50. case "按单减免":
  51. if ($owner_id && $month){
  52. if ($model->timeUnit->name == '月'){
  53. $report = OwnerReport::query()->select("id","total")
  54. ->where("owner_id",$owner_id)
  55. ->where("counting_month","like",$month."%")->first();
  56. $money -= $report ? ($report->total)*($model->discount_value) : 0;
  57. }else{
  58. $days = date('t', strtotime($month."-01"));
  59. $money *= $days;
  60. for($i=1;$i<=$days;$i++){
  61. $d = $i<10 ? "0".$i : $i;
  62. $query = DB::raw("SELECT COUNT(1) c FROM orders WHERE wms_status = ? and updated_at between ? and ?");
  63. $count = DB::selectOne($query,['订单完成',$month."-".$d." 00:00:00",$month."-".$d." 23:59:59"]);
  64. $money -= $count ? ($count->c)*($model->discount_value) : 0;
  65. }
  66. }
  67. }
  68. break;
  69. case "固定减免":
  70. if ($model->timeUnit->name == '月'){
  71. $money -= $model->discount_value;
  72. }else{
  73. $days = date('t', strtotime($month."-01"));
  74. $money *= $days;
  75. $money -= $days*$model->discount_value;
  76. }
  77. break;
  78. }
  79. return $money;
  80. }
  81. }