OwnerStoragePriceModelService.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. public function getSelection(array $columns = ["counting_type","using_type"], array $withs = [])
  12. {
  13. return OwnerStoragePriceModel::query()->select($columns)->with($withs)->get();
  14. }
  15. public function paginate($id = null,array $withs = [])
  16. {
  17. $query = OwnerStoragePriceModel::query()->orderByDesc('id')->with($withs);
  18. if ($id)$query->where("id",$id);
  19. return $query->paginate($params["paginate"] ?? 50);
  20. }
  21. public function create(array $params)
  22. {
  23. return OwnerStoragePriceModel::query()->create($params);
  24. }
  25. public function update(array $params, array $values)
  26. {
  27. $query = OwnerStoragePriceModel::query();
  28. foreach ($params as $column => $value){
  29. $query->where($column,$value);
  30. }
  31. return $query->update($values);
  32. }
  33. public function find($id, $withs = [])
  34. {
  35. return OwnerStoragePriceModel::query()->with($withs)->find($id);
  36. }
  37. public function destroy($id)
  38. {
  39. DB::delete(DB::raw("DELETE FROM owner_storage_price_model_owner WHERE owner_storage_price_model_id = ?"),[$id]);
  40. return OwnerStoragePriceModel::destroy($id);
  41. }
  42. //暂时不考虑单位换算问题:后期可能存在多单位换算,此处仅视为单位为 m²,m³
  43. public function calculationAmount(OwnerStoragePriceModel $model, $area, $owner_id = null, $month = null)
  44. {
  45. if (!$model || !$area) return 0;
  46. if ($area < $model->minimum_area) $area = $model->minimum_area;
  47. $money = $area*$model->price;
  48. switch ($model->discount_type){
  49. case "按单减免":
  50. if ($owner_id && $month){
  51. if ($model->timeUnit->name == '月'){
  52. $report = OwnerReport::query()->select("id","total")
  53. ->where("owner_id",$owner_id)
  54. ->where("counting_month","like",$month."%")->first();
  55. $money -= $report ? ($report->total)*($model->discount_value) : 0;
  56. }
  57. }
  58. break;
  59. case "固定减免":
  60. $money -= $model->discount_value;
  61. break;
  62. }
  63. if ($model->timeUnit->name == '日'){
  64. $days = date('t', strtotime($month."-01"));
  65. $money *= $days;
  66. for($i=1;$i<=$days;$i++){
  67. $d = $i<10 ? "0".$i : $i;
  68. $query = DB::raw("SELECT COUNT(1) c FROM orders WHERE wms_status = ? and updated_at between ? and ?");
  69. $count = DB::selectOne($query,['订单完成',$month."-".$d." 00:00:00",$month."-".$d." 23:59:59"]);
  70. $money -= $count ? ($count->c)*($model->discount_value) : 0;
  71. }
  72. }
  73. return $money;
  74. }
  75. }