| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?php
- namespace App\Services;
- use App\Order;
- use App\OwnerReport;
- use App\OwnerStoragePriceModel;
- use Illuminate\Support\Facades\DB;
- use App\Traits\ServiceAppAop;
- class OwnerStoragePriceModelService
- {
- use ServiceAppAop;
- protected $modelClass=OwnerStoragePriceModel::class;
- public function getSelection(array $columns = ["counting_type","using_type"], array $withs = [])
- {
- return OwnerStoragePriceModel::query()->select($columns)->with($withs)->get();
- }
- public function paginate($id = null,array $withs = [])
- {
- $query = OwnerStoragePriceModel::query()->orderByDesc('id')->with($withs);
- if ($id)$query->where("id",$id);
- return $query->paginate($params["paginate"] ?? 50);
- }
- public function create(array $params)
- {
- return OwnerStoragePriceModel::query()->create($params);
- }
- public function update(array $params, array $values)
- {
- $query = OwnerStoragePriceModel::query();
- foreach ($params as $column => $value){
- $query->where($column,$value);
- }
- return $query->update($values);
- }
- public function find($id, $withs = [])
- {
- return OwnerStoragePriceModel::query()->with($withs)->find($id);
- }
- public function destroy($id)
- {
- DB::delete(DB::raw("DELETE FROM owner_storage_price_model_owner WHERE owner_storage_price_model_id = ?"),[$id]);
- return OwnerStoragePriceModel::destroy($id);
- }
- //暂时不考虑单位换算问题:后期可能存在多单位换算,此处仅视为单位为 m²,m³
- public function calculationAmount(OwnerStoragePriceModel $model, $area, $owner_id = null, $month = null)
- {
- /** @var \stdClass $model */
- if (!$model || !$area) return 0;
- if ($area < $model->minimum_area) $area = $model->minimum_area;
- switch ($model->discount_type){
- case "按单减免":
- if ($owner_id && $month){
- if ($model->timeUnit->name == '月'){
- $report = OwnerReport::query()->select("id","total")
- ->where("owner_id",$owner_id)
- ->where("counting_month","like",$month."%")->first();
- if ($report && $report->total>=0)$area -= floor($report->total/$model->discount_value);
- }else{
- $days = date('t', strtotime($month."-01"));
- $area *= $days;
- for($i=1;$i<=$days;$i++){
- $d = $i<10 ? "0".$i : $i;
- $query = DB::raw("SELECT COUNT(1) c FROM orders WHERE wms_status = ? and updated_at between ? and ?");
- $count = DB::selectOne($query,['订单完成',$month."-".$d." 00:00:00",$month."-".$d." 23:59:59"]);
- if ($count && $count->c>=0)$area -= floor($count->c/$model->discount_value);
- }
- }
- }
- break;
- case "固定减免":
- if ($model->timeUnit->name == '月'){
- $area -= $model->discount_value;
- }else{
- $days = date('t', strtotime($month."-01"));
- $area *= $days;
- $area -= $days*$model->discount_value;
- }
- break;
- }
- return $area*$model->price;
- }
- }
|