| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- <?php
- namespace app\api\model\order;
- use app\api\model\BaseModel;
- use app\api\model\massager\Comment;
- use app\api\model\massager\Massager;
- use app\api\model\profit\Bill;
- use app\api\model\Store;
- use app\api\model\User;
- use app\api\model\user\Area;
- class Order extends BaseModel
- {
- // 表名
- protected $name = 'order';
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'integer';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- protected $deleteTime = false;
- static function OKey($o_id)
- {
- return "order:pay:$o_id";
- }
- public function genOrderNo(int $user_id, $store_id = 0): string
- {
- $no = null;
- $check = true;
- while ($check) {
- $no = $store_id > 0 ? 'OSP' . time() : 'OAP' . time();
- $order = $this->where('no', $no)->find();
- if (!$order)
- $check = false;
- }
- return $no;
- }
- public function getPaymentTypeList()
- {
- return ['ali' => __('Ali'), 'wechat' => __('Wechat'), 'balance' => __('Balance')];
- }
- public function getStatusList()
- {
- return ['proceed' => __('Proceed'), 'pending' => __('Pending'), 'finish' => __('Finish'), 'wait_feedback' => __('Wait_feedback'), 'cancel' => __('Cancel')];
- }
- public function user()
- {
- return $this->hasOne(User::class, "id", "user_id", [], "LEFT")->setEagerlyType(0);
- }
- public function area()
- {
- return $this->belongsTo(Area::class, 'user_area_id', 'id', [], 'LEFT')->setEagerlyType(0);
- }
- public function store()
- {
- return $this->belongsTo(Store::class, 'store_id', 'id', [], 'LEFT')->setEagerlyType(0);
- }
- public function massager()
- {
- return $this->belongsTo(Massager::class, 'massager_id', 'id', [], 'LEFT')->setEagerlyType(0);
- }
- public function services()
- {
- return $this->hasMany(Service::class, 'order_id', 'id');
- }
- public function progress()
- {
- return $this->hasMany(Progress::class, 'order_id', 'id')->order("index", "asc");
- }
- public function bill()
- {
- return $this->belongsTo(Bill::class, 'no', 'order_no', [], 'LEFT')->setEagerlyType(0);
- }
- public function comment()
- {
- return $this->belongsTo(Comment::class, 'id', 'order_id', [], 'LEFT')->setEagerlyType(0);
- }
- public function order_detail($order_id)
- {
- return $this->where([
- "order.id" => $order_id
- ])->with(["area", "store", "massager", "services", "progress"])
- ->find();
- }
- /**
- * @param $user_id
- * @param $status
- * @param $page
- * @param $size
- * @return \think\Paginator
- * @throws \think\exception\DbException
- */
- public function fetchOrder($user_id, array $status, $page, $size)
- {
- return $this->where(["order.user_id" => $user_id])
- ->where("order.status", "in", $status)
- ->where("order.is_delete", 0)
- ->with(["services", "massager"])
- ->order("order.updatetime", "desc")
- ->page($page)
- ->paginate($size);
- }
- public function fetchByJudgeMassagerWork($massager_id, $order_id)
- {
- return $this->where("massager_id", $massager_id)
- ->where("id", "<>", $order_id)
- ->where('service_start_date', '>=', date('Y-m-d H:00:00', time()))
- ->where('service_start_date', '<=', date('Y-m-d H:00:00', time() + 2 * 24 * 60 * 60))
- ->where('status', 'in', [
- \E_ORDER_STATUS::Purchase,
- \E_ORDER_STATUS::Proceed,
- ])
- ->select();
- }
- public function fetchMassagerOrder($m_id, array $status, $page, $size)
- {
- return $this->alias("o")
- ->where(["o.massager_id" => $m_id])
- ->where("o.status", "in", $status)
- ->with("services")
- ->order("o.updatetime", "desc")
- ->page($page)
- ->paginate($size);
- }
- public function untakeOrderCount($m_id)
- {
- return $this->where([
- "massager_id" => $m_id,
- "status" => \E_ORDER_STATUS::Purchase
- ])->count();
- }
- public function sumTotalServiceAmount($m_id)
- {
- return $this->where("massager_id", $m_id)
- ->where("status", "in", [
- \E_ORDER_STATUS::Proceed,
- \E_ORDER_STATUS::Purchase,
- \E_ORDER_STATUS::WaitFeedback,
- \E_ORDER_STATUS::Finish
- ])->where("is_use_profit", 0)
- ->sum("total_service_amount");
- }
- public function fetchByTripAmountGTZero($m_id, $page, $size)
- {
- return $this->where([
- "massager_id" => $m_id,
- "status" => \E_ORDER_STATUS::Finish
- ])
- ->order("updatetime", "desc")
- ->page($page)
- ->paginate($size);
- }
- public function groupByMassager($id, $starttime, $endtime)
- {
- return $this->where("massager_id", $id)
- ->where('createtime', 'between time', [$starttime, $endtime])
- ->field('createtime, status, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(createtime), "%Y-%m-%d") AS create_date')
- ->group('create_date')
- ->select();
- }
- function sumPerformanceByIntraday($m_id, $city_code, $intraday)
- {
- if (is_null($intraday)) {
- $intraday = date("Y-m-d");
- } else {
- $intraday = date("Y-m-d", strtotime($intraday));
- }
- $total_real_amount = $this->where("massager_id", $m_id)
- ->where("city_code", $city_code)
- ->where("DATE_FORMAT(FROM_UNIXTIME(createtime),'%Y-%m-%d') = '{$intraday}'")
- ->where("status", \E_ORDER_STATUS::Finish)
- ->sum("total_real_amount");
- $total_trip_amount = $this->where("massager_id", $m_id)
- ->where("city_code", $city_code)
- ->where("DATE_FORMAT(FROM_UNIXTIME(createtime),'%Y-%m-%d') = '{$intraday}'")
- ->where("status", \E_ORDER_STATUS::Finish)
- ->sum("trip_amount");
- return $total_real_amount - $total_trip_amount;
- }
- function sumPerformanceByNowMonth($m_id, $city_code, $y = null, $m = null)
- {
- $ym_str = ym($y, $m);
- $total_real_amount = $this->where("massager_id", $m_id)
- ->where("city_code", $city_code)
- ->where("DATE_FORMAT(FROM_UNIXTIME(createtime),'%Y-%m') = '{$ym_str}'")
- ->where("status", \E_ORDER_STATUS::Finish)
- ->sum("total_real_amount");
- $total_trip_amount = $this->where("massager_id", $m_id)
- ->where("city_code", $city_code)
- ->where("DATE_FORMAT(FROM_UNIXTIME(createtime),'%Y-%m') = '{$ym_str}'")
- ->where("status", \E_ORDER_STATUS::Finish)
- ->sum("trip_amount");
- return $total_real_amount - $total_trip_amount;
- }
- function countByPraiseRate($m_id, $city_code, $year = null, $month = null)
- {
- $ym_str = ym($year, $month);
- $all = $this
- ->with("comment")
- ->where("order.city_code", $city_code)
- ->where("order.massager_id", $m_id)
- ->where("DATE_FORMAT(FROM_UNIXTIME(order.createtime),'%Y-%m') = '{$ym_str}'")
- ->where("order.status", \E_ORDER_STATUS::Finish)
- ->select();
- $count = count($all);
- if (0 === $count)
- return 0;
- $praiseCount = array_reduce($all, function ($p, $cur) {
- if ($cur["comment"]["negative"] == 0)
- $p += 1;
- return $p;
- }, 0);
- return $praiseCount > 0 ? fixed2Float(($praiseCount / $count) * 100) : 0;
- }
- function chargebackRate($m_id, $city_code, $year = null, $month = null)
- {
- $ym_str = ym($year, $month);
- $total = $this
- ->with("comment")
- ->where("order.city_code", $city_code)
- ->where("order.massager_id", $m_id)
- ->where("DATE_FORMAT(FROM_UNIXTIME(order.createtime),'%Y-%m') = '{$ym_str}'")
- ->where("order.status", "in", [
- \E_ORDER_STATUS::Proceed,
- \E_ORDER_STATUS::WaitFeedback,
- \E_ORDER_STATUS::Finish,
- \E_ORDER_STATUS::Reject,
- \E_ORDER_STATUS::Cancel,
- \E_ORDER_STATUS::AdminCancel,
- \E_ORDER_STATUS::AutoCancel
- ])
- ->count();
- $chargeback = $this
- ->with("comment")
- ->where("order.city_code", $city_code)
- ->where("order.massager_id", $m_id)
- ->where("DATE_FORMAT(FROM_UNIXTIME(order.createtime),'%Y-%m') = '{$ym_str}'")
- ->where("order.status", "in", [
- \E_ORDER_STATUS::Reject,
- \E_ORDER_STATUS::AutoCancel
- ])
- ->count();
- return $chargeback > 0 ? fixed2Float(($chargeback / $total) * 100) : 0;
- }
- function reorderRateByNowMonth($m_id, $city_code, $year = null, $month = null)
- {
- $ym_str = ym($year, $month);
- $all = $this
- ->where("city_code", $city_code)
- ->where("massager_id", $m_id)
- ->where("DATE_FORMAT(FROM_UNIXTIME(createtime),'%Y-%m') = '$ym_str'")
- ->where("status", \E_ORDER_STATUS::Finish)
- ->select();
- $count = count($all);
- if (0 === $count)
- return 0;
- $reorderCount = array_reduce($all, function ($p, $cur) {
- if ($cur["reorder"] == 1)
- $p += 1;
- return $p;
- }, 0);
- $count -= $reorderCount;
- if ($reorderCount > $count)
- return 100;
- return $reorderCount > 0 ? fixed2Float(($reorderCount / $count) * 100) : 0;
- }
- function monthFinishOrdersByYm($m_id, $city_code, $year = null, $month = null)
- {
- $ym_str = ym($year, $month);
- return $this->where("order.massager_id", $m_id)
- ->with("bill")
- ->where("order.city_code", $city_code)
- ->where("DATE_FORMAT(FROM_UNIXTIME(order.createtime),'%Y-%m') = '{$ym_str}'")
- ->where("order.status", \E_ORDER_STATUS::Finish)
- ->where([
- "bill.identity_type" => \E_IDENTITY_TYPE::Massager,
- "bill.target_id" => $m_id,
- "bill.change_type" => "profit",
- ])
- ->order("createtime", "DESC")
- ->select();
- }
- /**
- * @param $user_id
- * @return int|string
- * @throws \think\Exception
- */
- static function countByUser($user_id)
- {
- return self::where([
- "user_id" => $user_id,
- "status" => \E_ORDER_STATUS::Finish
- ])->count();
- }
- /**
- * @param $user_id
- * @return bool|float|int|string|null
- */
- static function sumByUser($user_id)
- {
- return self::where([
- "user_id" => $user_id,
- "status" => \E_ORDER_STATUS::Finish
- ])->sum("total_real_amount");
- }
- public function fetchByMassager($m_id, $y, $m)
- {
- $ym = ym($y, $m);
- return $this->where("DATE_FORMAT(FROM_UNIXTIME(pay_time),'%Y-%m') = '$ym'")
- ->where("massager_id", $m_id)
- ->where("status", \E_ORDER_STATUS::Finish)
- ->order("pay_time", "DESC")
- ->select();
- }
- public function fetchReorderByMassager($m_id, $y, $m)
- {
- $ym = ym($y, $m);
- return $this->where("DATE_FORMAT(FROM_UNIXTIME(pay_time),'%Y-%m') = '$ym'")
- ->where("massager_id", $m_id)
- ->where("reorder", 1)
- ->where("status", \E_ORDER_STATUS::Finish)
- ->order("pay_time", "desc")
- ->select();
- }
- public function byOrderCommentExpired($max_timeout)
- {
- return $this->where("updatetime", "<=", $max_timeout)
- ->where("status", \E_ORDER_STATUS::WaitFeedback)
- ->order("createtime", "ASC")
- ->select();
- }
- public function byOrderTakeExpired($max_timeout)
- {
- return $this->where("pay_time", "<=", $max_timeout)
- ->where("massager_id", ">", 0)
- ->where("status", \E_ORDER_STATUS::Purchase)
- ->order("createtime", "ASC")
- ->select();
- }
- }
|