Order.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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")
  116. ->where(["o.massager_id" => $m_id])
  117. ->where("o.status", "in", $status)
  118. ->with("services")
  119. ->order("o.updatetime", "desc")
  120. ->page($page)
  121. ->paginate($size);
  122. }
  123. public function untakeOrderCount($m_id)
  124. {
  125. return $this->where([
  126. "massager_id" => $m_id,
  127. "status" => \E_ORDER_STATUS::Purchase
  128. ])->count();
  129. }
  130. public function sumTotalServiceAmount($m_id)
  131. {
  132. return $this->where("massager_id", $m_id)
  133. ->where("status", "in", [
  134. \E_ORDER_STATUS::Proceed,
  135. \E_ORDER_STATUS::Purchase,
  136. \E_ORDER_STATUS::WaitFeedback,
  137. \E_ORDER_STATUS::Finish
  138. ])->where("is_use_profit", 0)
  139. ->sum("total_service_amount");
  140. }
  141. public function fetchByTripAmountGTZero($m_id, $page, $size)
  142. {
  143. return $this->where([
  144. "massager_id" => $m_id,
  145. "status" => \E_ORDER_STATUS::Finish
  146. ])
  147. ->order("updatetime", "desc")
  148. ->page($page)
  149. ->paginate($size);
  150. }
  151. public function groupByMassager($id, $starttime, $endtime)
  152. {
  153. return $this->where("massager_id", $id)
  154. ->where('createtime', 'between time', [$starttime, $endtime])
  155. ->field('createtime, status, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(createtime), "%Y-%m-%d") AS create_date')
  156. ->group('create_date')
  157. ->select();
  158. }
  159. function sumPerformanceByIntraday($m_id, $city_code, $intraday)
  160. {
  161. if (is_null($intraday)) {
  162. $intraday = date("Y-m-d");
  163. } else {
  164. $intraday = date("Y-m-d", strtotime($intraday));
  165. }
  166. $total_real_amount = $this->where("massager_id", $m_id)
  167. ->where("city_code", $city_code)
  168. ->where("DATE_FORMAT(FROM_UNIXTIME(createtime),'%Y-%m-%d') = '{$intraday}'")
  169. ->where("status", \E_ORDER_STATUS::Finish)
  170. ->sum("total_real_amount");
  171. $total_trip_amount = $this->where("massager_id", $m_id)
  172. ->where("city_code", $city_code)
  173. ->where("DATE_FORMAT(FROM_UNIXTIME(createtime),'%Y-%m-%d') = '{$intraday}'")
  174. ->where("status", \E_ORDER_STATUS::Finish)
  175. ->sum("trip_amount");
  176. return $total_real_amount - $total_trip_amount;
  177. }
  178. function sumPerformanceByNowMonth($m_id, $city_code, $y = null, $m = null)
  179. {
  180. $ym_str = ym($y, $m);
  181. $total_real_amount = $this->where("massager_id", $m_id)
  182. ->where("city_code", $city_code)
  183. ->where("DATE_FORMAT(FROM_UNIXTIME(createtime),'%Y-%m') = '{$ym_str}'")
  184. ->where("status", \E_ORDER_STATUS::Finish)
  185. ->sum("total_real_amount");
  186. $total_trip_amount = $this->where("massager_id", $m_id)
  187. ->where("city_code", $city_code)
  188. ->where("DATE_FORMAT(FROM_UNIXTIME(createtime),'%Y-%m') = '{$ym_str}'")
  189. ->where("status", \E_ORDER_STATUS::Finish)
  190. ->sum("trip_amount");
  191. return $total_real_amount - $total_trip_amount;
  192. }
  193. function countByPraiseRate($m_id, $city_code, $year = null, $month = null)
  194. {
  195. $ym_str = ym($year, $month);
  196. $all = $this
  197. ->with("comment")
  198. ->where("order.city_code", $city_code)
  199. ->where("order.massager_id", $m_id)
  200. ->where("DATE_FORMAT(FROM_UNIXTIME(order.createtime),'%Y-%m') = '{$ym_str}'")
  201. ->where("order.status", \E_ORDER_STATUS::Finish)
  202. ->select();
  203. $count = count($all);
  204. if (0 === $count)
  205. return 0;
  206. $praiseCount = array_reduce($all, function ($p, $cur) {
  207. if ($cur["comment"]["negative"] == 0)
  208. $p += 1;
  209. return $p;
  210. }, 0);
  211. return $praiseCount > 0 ? fixed2Float(($praiseCount / $count) * 100) : 0;
  212. }
  213. function chargebackRate($m_id, $city_code, $year = null, $month = null)
  214. {
  215. $ym_str = ym($year, $month);
  216. $total = $this
  217. ->with("comment")
  218. ->where("order.city_code", $city_code)
  219. ->where("order.massager_id", $m_id)
  220. ->where("DATE_FORMAT(FROM_UNIXTIME(order.createtime),'%Y-%m') = '{$ym_str}'")
  221. ->where("order.status", "in", [
  222. \E_ORDER_STATUS::Proceed,
  223. \E_ORDER_STATUS::WaitFeedback,
  224. \E_ORDER_STATUS::Finish,
  225. \E_ORDER_STATUS::Reject,
  226. \E_ORDER_STATUS::Cancel,
  227. \E_ORDER_STATUS::AdminCancel,
  228. \E_ORDER_STATUS::AutoCancel
  229. ])
  230. ->count();
  231. $chargeback = $this
  232. ->with("comment")
  233. ->where("order.city_code", $city_code)
  234. ->where("order.massager_id", $m_id)
  235. ->where("DATE_FORMAT(FROM_UNIXTIME(order.createtime),'%Y-%m') = '{$ym_str}'")
  236. ->where("order.status", "in", [
  237. \E_ORDER_STATUS::Reject,
  238. \E_ORDER_STATUS::AutoCancel
  239. ])
  240. ->count();
  241. return $chargeback > 0 ? fixed2Float(($chargeback / $total) * 100) : 0;
  242. }
  243. function reorderRateByNowMonth($m_id, $city_code, $year = null, $month = null)
  244. {
  245. $ym_str = ym($year, $month);
  246. $all = $this
  247. ->where("city_code", $city_code)
  248. ->where("massager_id", $m_id)
  249. ->where("DATE_FORMAT(FROM_UNIXTIME(createtime),'%Y-%m') = '$ym_str'")
  250. ->where("status", \E_ORDER_STATUS::Finish)
  251. ->select();
  252. $count = count($all);
  253. if (0 === $count)
  254. return 0;
  255. $reorderCount = array_reduce($all, function ($p, $cur) {
  256. if ($cur["reorder"] == 1)
  257. $p += 1;
  258. return $p;
  259. }, 0);
  260. $count -= $reorderCount;
  261. if ($reorderCount > $count)
  262. return 100;
  263. return $reorderCount > 0 ? fixed2Float(($reorderCount / $count) * 100) : 0;
  264. }
  265. function monthFinishOrdersByYm($m_id, $city_code, $year = null, $month = null)
  266. {
  267. $ym_str = ym($year, $month);
  268. return $this->where("order.massager_id", $m_id)
  269. ->with("bill")
  270. ->where("order.city_code", $city_code)
  271. ->where("DATE_FORMAT(FROM_UNIXTIME(order.createtime),'%Y-%m') = '{$ym_str}'")
  272. ->where("order.status", \E_ORDER_STATUS::Finish)
  273. ->where([
  274. "bill.identity_type" => \E_IDENTITY_TYPE::Massager,
  275. "bill.target_id" => $m_id,
  276. "bill.change_type" => "profit",
  277. ])
  278. ->order("createtime", "DESC")
  279. ->select();
  280. }
  281. /**
  282. * @param $user_id
  283. * @return int|string
  284. * @throws \think\Exception
  285. */
  286. static function countByUser($user_id)
  287. {
  288. return self::where([
  289. "user_id" => $user_id,
  290. "status" => \E_ORDER_STATUS::Finish
  291. ])->count();
  292. }
  293. /**
  294. * @param $user_id
  295. * @return bool|float|int|string|null
  296. */
  297. static function sumByUser($user_id)
  298. {
  299. return self::where([
  300. "user_id" => $user_id,
  301. "status" => \E_ORDER_STATUS::Finish
  302. ])->sum("total_real_amount");
  303. }
  304. public function fetchByMassager($m_id, $y, $m)
  305. {
  306. $ym = ym($y, $m);
  307. return $this->where("DATE_FORMAT(FROM_UNIXTIME(pay_time),'%Y-%m') = '$ym'")
  308. ->where("massager_id", $m_id)
  309. ->where("status", \E_ORDER_STATUS::Finish)
  310. ->order("pay_time", "DESC")
  311. ->select();
  312. }
  313. public function fetchReorderByMassager($m_id, $y, $m)
  314. {
  315. $ym = ym($y, $m);
  316. return $this->where("DATE_FORMAT(FROM_UNIXTIME(pay_time),'%Y-%m') = '$ym'")
  317. ->where("massager_id", $m_id)
  318. ->where("reorder", 1)
  319. ->where("status", \E_ORDER_STATUS::Finish)
  320. ->order("pay_time", "desc")
  321. ->select();
  322. }
  323. public function byOrderCommentExpired($max_timeout)
  324. {
  325. return $this->where("updatetime", "<=", $max_timeout)
  326. ->where("status", \E_ORDER_STATUS::WaitFeedback)
  327. ->order("createtime", "ASC")
  328. ->select();
  329. }
  330. public function byOrderTakeExpired($max_timeout)
  331. {
  332. return $this->where("pay_time", "<=", $max_timeout)
  333. ->where("massager_id", ">", 0)
  334. ->where("status", \E_ORDER_STATUS::Purchase)
  335. ->order("createtime", "ASC")
  336. ->select();
  337. }
  338. }