Order.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <?php
  2. namespace app\api\model\order;
  3. use app\api\model\BaseModel;
  4. use app\api\model\massager\Comment;
  5. use app\api\model\massager\Massager;
  6. use app\api\model\profit\Bill;
  7. use app\api\model\Store;
  8. use app\api\model\User;
  9. use app\api\model\user\Area;
  10. class Order extends BaseModel
  11. {
  12. // 表名
  13. protected $name = 'order';
  14. // 自动写入时间戳字段
  15. protected $autoWriteTimestamp = 'integer';
  16. // 定义时间戳字段名
  17. protected $createTime = 'createtime';
  18. protected $updateTime = 'updatetime';
  19. protected $deleteTime = false;
  20. static function OKey($o_id)
  21. {
  22. return "order:pay:$o_id";
  23. }
  24. public function genOrderNo(int $user_id, $store_id = 0): string
  25. {
  26. $no = null;
  27. $check = true;
  28. while ($check) {
  29. $no = $store_id > 0 ? 'OSP' . time() : 'OAP' . time();
  30. $order = $this->where('no', $no)->find();
  31. if (!$order)
  32. $check = false;
  33. }
  34. return $no;
  35. }
  36. public function getPaymentTypeList()
  37. {
  38. return ['ali' => __('Ali'), 'wechat' => __('Wechat'), 'balance' => __('Balance')];
  39. }
  40. public function getStatusList()
  41. {
  42. return ['proceed' => __('Proceed'), 'pending' => __('Pending'), 'finish' => __('Finish'), 'wait_feedback' => __('Wait_feedback'), 'cancel' => __('Cancel')];
  43. }
  44. public function user()
  45. {
  46. return $this->hasOne(User::class, "id", "user_id", [], "LEFT")->setEagerlyType(0);
  47. }
  48. public function area()
  49. {
  50. return $this->belongsTo(Area::class, 'user_area_id', 'id', [], 'LEFT')->setEagerlyType(0);
  51. }
  52. public function store()
  53. {
  54. return $this->belongsTo(Store::class, 'store_id', 'id', [], 'LEFT')->setEagerlyType(0);
  55. }
  56. public function massager()
  57. {
  58. return $this->belongsTo(Massager::class, 'massager_id', 'id', [], 'LEFT')->setEagerlyType(0);
  59. }
  60. public function services()
  61. {
  62. return $this->hasMany(Service::class, 'order_id', 'id');
  63. }
  64. public function progress()
  65. {
  66. return $this->hasMany(Progress::class, 'order_id', 'id')->order("index", "asc");
  67. }
  68. public function bill()
  69. {
  70. return $this->belongsTo(Bill::class, 'no', 'order_no', [], 'LEFT')->setEagerlyType(0);
  71. }
  72. public function comment()
  73. {
  74. return $this->belongsTo(Comment::class, 'id', 'order_id', [], 'LEFT')->setEagerlyType(0);
  75. }
  76. public function order_detail($order_id)
  77. {
  78. return $this->where([
  79. "order.id" => $order_id
  80. ])->with(["area", "store", "massager", "services", "progress"])
  81. ->find();
  82. }
  83. /**
  84. * @param $user_id
  85. * @param $status
  86. * @param $page
  87. * @param $size
  88. * @return \think\Paginator
  89. * @throws \think\exception\DbException
  90. */
  91. public function fetchOrder($user_id, array $status, $page, $size)
  92. {
  93. return $this->where(["order.user_id" => $user_id])
  94. ->where("order.status", "in", $status)
  95. ->where("order.is_delete", 0)
  96. ->with(["services", "massager"])
  97. ->order("order.updatetime", "desc")
  98. ->page($page)
  99. ->paginate($size);
  100. }
  101. public function fetchByJudgeMassagerWork($massager_id, $order_id)
  102. {
  103. return $this->where("massager_id", $massager_id)
  104. ->where("id", "<>", $order_id)
  105. ->where('service_start_date', '>=', date('Y-m-d H:00:00', time()))
  106. ->where('service_start_date', '<=', date('Y-m-d H:00:00', time() + 2 * 24 * 60 * 60))
  107. ->where('status', 'in', [
  108. \E_ORDER_STATUS::Purchase,
  109. \E_ORDER_STATUS::Proceed,
  110. ])
  111. ->select();
  112. }
  113. public function fetchMassagerOrder($m_id, array $status, $page, $size)
  114. {
  115. return $this->alias("o")->where(["o.massager_id" => $m_id])
  116. ->where("o.status", "in", $status)
  117. ->with("services")
  118. ->order("o.updatetime", "desc")
  119. ->page($page)
  120. ->paginate($size);
  121. }
  122. public function untakeOrderCount($m_id)
  123. {
  124. return $this->where([
  125. "massager_id" => $m_id,
  126. "status" => \E_ORDER_STATUS::Purchase
  127. ])->count();
  128. }
  129. public function sumTotalServiceAmount($m_id)
  130. {
  131. return $this->where("massager_id", $m_id)
  132. ->where("status", "in", [
  133. \E_ORDER_STATUS::Proceed,
  134. \E_ORDER_STATUS::Purchase,
  135. \E_ORDER_STATUS::WaitFeedback,
  136. \E_ORDER_STATUS::Finish
  137. ])->where("is_use_profit", 0)
  138. ->sum("total_service_amount");
  139. }
  140. public function fetchByTripAmountGTZero($m_id, $page, $size)
  141. {
  142. return $this->where([
  143. "massager_id" => $m_id,
  144. "status" => \E_ORDER_STATUS::Finish
  145. ])
  146. ->order("updatetime", "desc")
  147. ->page($page)
  148. ->paginate($size);
  149. }
  150. public function groupByMassager($id, $starttime, $endtime)
  151. {
  152. return $this->where("massager_id", $id)
  153. ->where('createtime', 'between time', [$starttime, $endtime])
  154. ->field('createtime, status, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(createtime), "%Y-%m-%d") AS create_date')
  155. ->group('create_date')
  156. ->select();
  157. }
  158. function sumPerformanceByIntraday($m_id, $city_code, $intraday)
  159. {
  160. if (is_null($intraday)) {
  161. $intraday = date("Y-m-d");
  162. } else {
  163. $intraday = date("Y-m-d", strtotime($intraday));
  164. }
  165. $total_real_amount = $this->where("massager_id", $m_id)
  166. ->where("city_code", $city_code)
  167. ->where("DATE_FORMAT(FROM_UNIXTIME(createtime),'%Y-%m-%d') = '{$intraday}'")
  168. ->where("status", \E_ORDER_STATUS::Finish)
  169. ->sum("total_real_amount");
  170. $total_trip_amount = $this->where("massager_id", $m_id)
  171. ->where("city_code", $city_code)
  172. ->where("DATE_FORMAT(FROM_UNIXTIME(createtime),'%Y-%m-%d') = '{$intraday}'")
  173. ->where("status", \E_ORDER_STATUS::Finish)
  174. ->sum("trip_amount");
  175. return $total_real_amount - $total_trip_amount;
  176. }
  177. function sumPerformanceByNowMonth($m_id, $city_code, $y = null, $m = null)
  178. {
  179. $ym_str = ym($y, $m);
  180. $total_real_amount = $this->where("massager_id", $m_id)
  181. ->where("city_code", $city_code)
  182. ->where("DATE_FORMAT(FROM_UNIXTIME(createtime),'%Y-%m') = '{$ym_str}'")
  183. ->where("status", \E_ORDER_STATUS::Finish)
  184. ->sum("total_real_amount");
  185. $total_trip_amount = $this->where("massager_id", $m_id)
  186. ->where("city_code", $city_code)
  187. ->where("DATE_FORMAT(FROM_UNIXTIME(createtime),'%Y-%m') = '{$ym_str}'")
  188. ->where("status", \E_ORDER_STATUS::Finish)
  189. ->sum("trip_amount");
  190. return $total_real_amount - $total_trip_amount;
  191. }
  192. function countByPraiseRate($m_id, $city_code, $year = null, $month = null)
  193. {
  194. $ym_str = ym($year, $month);
  195. $all = $this
  196. ->with("comment")
  197. ->where("order.city_code", $city_code)
  198. ->where("order.massager_id", $m_id)
  199. ->where("DATE_FORMAT(FROM_UNIXTIME(order.createtime),'%Y-%m') = '{$ym_str}'")
  200. ->where("order.status", \E_ORDER_STATUS::Finish)
  201. ->select();
  202. $count = count($all);
  203. if (0 === $count)
  204. return 0;
  205. $praiseCount = array_reduce($all, function ($p, $cur) {
  206. if ($cur["comment"]["negative"] == 0)
  207. $p += 1;
  208. return $p;
  209. }, 0);
  210. return $praiseCount > 0 ? fixed2Float(($praiseCount / $count) * 100) : 0;
  211. }
  212. function chargebackRate($m_id, $city_code, $year = null, $month = null)
  213. {
  214. $ym_str = ym($year, $month);
  215. $total = $this
  216. ->with("comment")
  217. ->where("order.city_code", $city_code)
  218. ->where("order.massager_id", $m_id)
  219. ->where("DATE_FORMAT(FROM_UNIXTIME(order.createtime),'%Y-%m') = '{$ym_str}'")
  220. ->where("order.status", "in", [
  221. \E_ORDER_STATUS::Proceed,
  222. \E_ORDER_STATUS::WaitFeedback,
  223. \E_ORDER_STATUS::Finish,
  224. \E_ORDER_STATUS::Reject,
  225. \E_ORDER_STATUS::Cancel,
  226. \E_ORDER_STATUS::AdminCancel,
  227. \E_ORDER_STATUS::AutoCancel
  228. ])
  229. ->count();
  230. $chargeback = $this
  231. ->with("comment")
  232. ->where("order.city_code", $city_code)
  233. ->where("order.massager_id", $m_id)
  234. ->where("DATE_FORMAT(FROM_UNIXTIME(order.createtime),'%Y-%m') = '{$ym_str}'")
  235. ->where("order.status", "in", [
  236. \E_ORDER_STATUS::Reject,
  237. \E_ORDER_STATUS::AutoCancel
  238. ])
  239. ->count();
  240. return $chargeback > 0 ? fixed2Float(($chargeback / $total) * 100) : 0;
  241. }
  242. function reorderRateByNowMonth($m_id, $city_code, $year = null, $month = null)
  243. {
  244. $ym_str = ym($year, $month);
  245. $all = $this
  246. ->where("city_code", $city_code)
  247. ->where("massager_id", $m_id)
  248. ->where("DATE_FORMAT(FROM_UNIXTIME(createtime),'%Y-%m') = '$ym_str'")
  249. ->where("status", \E_ORDER_STATUS::Finish)
  250. ->select();
  251. $count = count($all);
  252. if (0 === $count)
  253. return 0;
  254. $reorderCount = array_reduce($all, function ($p, $cur) {
  255. if ($cur["reorder"] == 1)
  256. $p += 1;
  257. return $p;
  258. }, 0);
  259. $count -= $reorderCount;
  260. if ($reorderCount > $count)
  261. return 100;
  262. return $reorderCount > 0 ? fixed2Float(($reorderCount / $count) * 100) : 0;
  263. }
  264. function monthFinishOrdersByYm($m_id, $city_code, $year = null, $month = null)
  265. {
  266. $ym_str = ym($year, $month);
  267. return $this->where("order.massager_id", $m_id)
  268. ->with("bill")
  269. ->where("order.city_code", $city_code)
  270. ->where("DATE_FORMAT(FROM_UNIXTIME(order.createtime),'%Y-%m') = '{$ym_str}'")
  271. ->where("order.status", \E_ORDER_STATUS::Finish)
  272. ->where([
  273. "bill.identity_type" => \E_IDENTITY_TYPE::Massager,
  274. "bill.target_id" => $m_id,
  275. "bill.change_type" => "profit",
  276. ])
  277. ->order("createtime", "DESC")
  278. ->select();
  279. }
  280. /**
  281. * @param $user_id
  282. * @return int|string
  283. * @throws \think\Exception
  284. */
  285. static function countByUser($user_id)
  286. {
  287. return self::where([
  288. "user_id" => $user_id,
  289. "status" => \E_ORDER_STATUS::Finish
  290. ])->count();
  291. }
  292. /**
  293. * @param $user_id
  294. * @return bool|float|int|string|null
  295. */
  296. static function sumByUser($user_id)
  297. {
  298. return self::where([
  299. "user_id" => $user_id,
  300. "status" => \E_ORDER_STATUS::Finish
  301. ])->sum("total_real_amount");
  302. }
  303. public function fetchByMassager($m_id, $y, $m)
  304. {
  305. $ym = ym($y, $m);
  306. return $this->where("DATE_FORMAT(FROM_UNIXTIME(pay_time),'%Y-%m') = '$ym'")
  307. ->where("massager_id", $m_id)
  308. ->where("status", \E_ORDER_STATUS::Finish)
  309. ->order("pay_time", "DESC")
  310. ->select();
  311. }
  312. public function fetchReorderByMassager($m_id, $y, $m)
  313. {
  314. $ym = ym($y, $m);
  315. return $this->where("DATE_FORMAT(FROM_UNIXTIME(pay_time),'%Y-%m') = '$ym'")
  316. ->where("massager_id", $m_id)
  317. ->where("reorder", 1)
  318. ->where("status", \E_ORDER_STATUS::Finish)
  319. ->order("pay_time", "desc")
  320. ->select();
  321. }
  322. public function byOrderCommentExpired($max_timeout)
  323. {
  324. return $this->where("updatetime", "<=", $max_timeout)
  325. ->where("status", \E_ORDER_STATUS::WaitFeedback)
  326. ->order("createtime", "ASC")
  327. ->select();
  328. }
  329. public function byOrderTakeExpired($max_timeout)
  330. {
  331. return $this->where("pay_time", "<=", $max_timeout)
  332. ->where("massager_id", ">", 0)
  333. ->where("status", \E_ORDER_STATUS::Purchase)
  334. ->order("createtime", "ASC")
  335. ->select();
  336. }
  337. }