酒店预订平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

369 lines
12 KiB

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: nizongfeng
  5. * Date: 2021/10/27
  6. * Time: 11:26
  7. */
  8. namespace app\admin\dao;
  9. use app\admin\command\Util;
  10. use app\admin\model\OrderHotel;
  11. use app\admin\model\OrderItem;
  12. use app\admin\model\OrderMain;
  13. use think\Db;
  14. use think\Exception;
  15. class OrderMainDao
  16. {
  17. /**
  18. * 添加、更新订单信息
  19. * @param $param
  20. * @return array
  21. */
  22. public function save($param) {
  23. try{
  24. //1、获取渠道
  25. $channelDao = new CfChannelInfoDao();
  26. $channelRe = $channelDao->getInfoById($param['channel_id']);
  27. if (!$channelRe['flag']) {
  28. return $channelRe;
  29. }
  30. //2.获取专员
  31. $adminDao = new AdminDao();
  32. $adminRe = $adminDao->getInfoById($param['commissioner_id']);
  33. if (!$adminRe['flag']) {
  34. return $adminRe;
  35. }
  36. $data = [
  37. "commissioner_id"=>$param['commissioner_id'],
  38. "commissioner"=>$adminRe["data"]['nickname'],
  39. "channel_id"=>$param["channel_id"],
  40. "channel_name"=>$channelRe['data']['channel_name'],
  41. "channel_order_no"=>$param["channel_order_no"],
  42. "user_name"=>$param["user_name"],
  43. "user_phone"=>$param["user_phone"],
  44. "order_memo"=>$param["order_memo"],
  45. "create_id"=>empty($param['create_id'])?0:$param['create_id'],
  46. "group_id"=>empty($param['group_id'])?0:$param['group_id']
  47. ];
  48. $orderMain = new OrderMain();
  49. if (empty($param['id'])) {
  50. $id = $orderMain->insertGetId($data);
  51. return Util::returnArrSu("", $id);
  52. } else {
  53. $orderMain->save($data,['id'=>$param['id']]);
  54. return Util::returnArrSu("", $param['id']);
  55. }
  56. }catch (Exception $e){
  57. return Util::returnArrEr("更新主订单失败:".$e->getMessage());
  58. }
  59. }
  60. /**
  61. * 设置主订单金额
  62. * @param int $orderId
  63. * @return array
  64. */
  65. public function setOrderAmount(int $orderId){
  66. try {
  67. $itemModel = new OrderItem();
  68. $hotelModel = new OrderHotel();
  69. $itemList = $itemModel->where(["order_id" => $orderId,"del_flag"=>0])->select()->toArray();
  70. $hotelList = $hotelModel->where(["order_id" => $orderId,"del_flag"=>0])->select()->toArray();
  71. $amount = 0;
  72. $cost = 0;
  73. //状态数量统计 用于统计当前订单的状态
  74. $statusList = [
  75. 1=>0,
  76. 2=>0,
  77. 3=>0,
  78. 4=>0,
  79. "isPayment"=>0
  80. ];
  81. $cnt = count($itemList)+count($hotelList);
  82. foreach ($itemList as $item) {
  83. $amount += $item['total_price'];
  84. $cost += $item['total_cost'];
  85. $statusList[$item['confirm_status']]++;
  86. if ($item['payment_order_status'] ==2) {
  87. $statusList['isPayment']++;
  88. }
  89. }
  90. foreach ($hotelList as $hotel) {
  91. $amount += $hotel['total_price'];
  92. $cost += $hotel["total_cost"];
  93. $statusList[$hotel['confirm_status']]++;
  94. if ($hotel['payment_order_status'] ==2) {
  95. $statusList['isPayment']++;
  96. }
  97. }
  98. $orderInfoRe = $this->getInfoById($orderId);
  99. if (!$orderInfoRe['flag']) {
  100. return $orderInfoRe;
  101. }
  102. $orderInfo = $orderInfoRe['data'];
  103. $orderStatus = $this->getStatus($cnt,$statusList,$orderInfo);
  104. $saveVal = [
  105. "total_amount" => $amount,
  106. "cost_amount" => $cost,
  107. "profit_amount"=>$amount-$cost,
  108. "order_status"=>$orderStatus,
  109. "success_time"=>null,
  110. "cancel_time"=>null
  111. ];
  112. if ($orderStatus != $orderInfo["order_status"]) {
  113. if ($orderStatus==10) {
  114. $saveVal["success_time"] = date("Y-m-d H:i:s");
  115. }
  116. if ($orderStatus == 11 ) {
  117. $saveVal['cancel_time'] = date("Y-m-d H:m:s");
  118. }
  119. }
  120. //更新金额
  121. $orderMain = new OrderMain();
  122. $orderMain->save($saveVal,["id" => $orderId]);
  123. return Util::returnArrSu();
  124. }catch (Exception $e){
  125. return Util::returnArrEr("更新主表订单金额失败:".$e->getMessage());
  126. }
  127. }
  128. /**
  129. * 获取订单状态
  130. * @param $cnt
  131. * @param $statusList
  132. * @param $orderInfo
  133. * @return int
  134. */
  135. public function getStatus($cnt,$statusList,$orderInfo){
  136. //资源单状态 1、未发单/ 2已发单、3已确认、4已取消
  137. //订单状态0待处理 1已确认 2部分取消 3处理中 10已完成 11已取消
  138. //已完成:订单已完成付款、已完成收款(无视子订单状态)
  139. if ($orderInfo['receipt_order_status'] ==2 && $statusList['isPayment'] == $cnt) {
  140. return 10;
  141. }
  142. //全部未发单 待处理:子订单全部未发单
  143. if ($statusList[1] == $cnt) {
  144. return 0;
  145. }
  146. //全部已确认 已确认:子订单全部已确认
  147. if ($statusList[3] == $cnt) {
  148. return 1;
  149. }
  150. //全部已取消 已取消:订单中所有子订单已取消
  151. if ($statusList[4] == $cnt) {
  152. return 11;
  153. }
  154. //部分取消:订单中有子订单取消,其他子订单已确认
  155. if (($statusList[3]+$statusList[4]) == $cnt ) {
  156. return 2;
  157. }
  158. //处理中:非以上状态,即部分子订单确认、或者部分子订单在已发单,或部分子订单在未发单。
  159. return 3;
  160. }
  161. /**
  162. * 根据ID获取详情
  163. * @param $id
  164. * @return array
  165. */
  166. public function getInfoById($id) {
  167. try {
  168. $orderMainModel = new OrderMain();
  169. $orderMain = $orderMainModel->where(["id" => $id])->find();
  170. if (null == $orderMain) {
  171. return Util::returnArrEr("订单查询失败:".$id);
  172. }
  173. return Util::returnArrSu("",$orderMain->toArray());
  174. }catch (Exception $e) {
  175. return Util::returnArrEr("订单查询失败:".$e->getMessage());
  176. }
  177. }
  178. /**
  179. * 子订单展示
  180. * @param $purchaseShow
  181. * @param $orderHotel
  182. * @param $orderItem
  183. * @return array
  184. */
  185. public function setSubOrderShow($purchaseShow,$orderHotel,$orderItem){
  186. $result = [];
  187. foreach ($orderItem as $item) {
  188. $item['prod_type'] = 'item';
  189. foreach ($purchaseShow as $key=> $purchase) {
  190. if ($item['id'] == $key) {
  191. $item = array_merge($item,$purchase);
  192. }
  193. }
  194. $result[] = $item;
  195. }
  196. foreach ($orderHotel as $hotel) {
  197. $hotel['prod_type'] = 'hotel';
  198. foreach ($purchaseShow as $key=> $purchase) {
  199. if ($hotel['id'] == $key) {
  200. $hotel = array_merge($hotel,$purchase);
  201. }
  202. }
  203. $result[] = $hotel;
  204. }
  205. return Util::returnArrSu("",$result);
  206. }
  207. /**
  208. * 更新收款单下的订单信息
  209. * @param $receiptOrderId
  210. * @param $status
  211. * @return array
  212. */
  213. public function setReceiptOrderStatus($receiptOrderId, $status){
  214. try{
  215. $model = new OrderMain();
  216. $model->save(['receipt_order_status'=>$status],['receipt_order_id'=>$receiptOrderId]);
  217. return Util::returnArrSu();
  218. }catch (Exception $e){
  219. return Util::returnArrEr("更新收款单子啊的主订单状态失败".$e->getMessage());
  220. }
  221. }
  222. /**
  223. * 添加主订单到收款单下
  224. * @param $receiptOrder
  225. * @param $orderIds
  226. * @return array
  227. */
  228. public function addOrderMainInReceipt($receiptOrder,$orderIds){
  229. try{
  230. $data = [
  231. "receipt_order_id"=>$receiptOrder['id'],
  232. "receipt_order_status"=>$receiptOrder['status'],
  233. "receipt_order_name"=>$receiptOrder['name']
  234. ];
  235. $model = new OrderMain();
  236. $model->save($data,["id"=>["in",$orderIds]]);
  237. return Util::returnArrSu();
  238. }catch (Exception $e){
  239. return Util::returnArrEr("添加主订单到收款单下失败".$e->getMessage());
  240. }
  241. }
  242. /**
  243. * 将主订单从收款单下移除
  244. * @param $orderIds
  245. * @return array
  246. */
  247. public function removeOrderMainFormReceipt($orderIds){
  248. try{
  249. $data = [
  250. "receipt_order_id"=>0,
  251. "receipt_order_status"=>0,
  252. "receipt_order_name"=>""
  253. ];
  254. $model = new OrderMain();
  255. $model->save($data,["id"=>["in",$orderIds]]);
  256. return Util::returnArrSu();
  257. }catch (Exception $e){
  258. return Util::returnArrEr("将主订单从收款单下移除失败".$e->getMessage());
  259. }
  260. }
  261. /**
  262. * 获取订单列表
  263. * @param $where
  264. * @param $param
  265. * @return array
  266. */
  267. public function getOrderListByWhere($where,$param){
  268. try {
  269. $offset = ($param['pageNum'] - 1) * $param['pageSize'];
  270. $model = new OrderMain();
  271. $count = $model->where($where)->count();
  272. $list = $model->where($where)->limit($offset,$param['pageSize'])->order("id","DESC")->select();
  273. return Util::returnArrSu("", ["total" => $count, "list" => $list->toArray()]);
  274. }catch (Exception $e){
  275. return Util::returnArrSu("", ["total" => 0, "list" => []]);
  276. }
  277. }
  278. /**
  279. * 删除收款单
  280. * @param $id
  281. * @return array
  282. */
  283. public function delReceiptOrder($id){
  284. try{
  285. $data = [
  286. "receipt_order_id"=>0,
  287. "receipt_order_status"=>0,
  288. "receipt_order_name"=>""
  289. ];
  290. $model = new OrderMain();
  291. $model->save($data,["receipt_order_id"=>$id]);
  292. return Util::returnArrSu();
  293. }catch (Exception $e){
  294. return Util::returnArrEr("将主订单从收款单下移除失败".$e->getMessage());
  295. }
  296. }
  297. /**
  298. * 获取收款单下所有主订单的订单ID
  299. * @param $receipt_order_id
  300. * @return array
  301. */
  302. public function getOrderMainIdsByReceipt($receipt_order_id){
  303. $orderModel = new OrderMain();
  304. try {
  305. $subOrderList = $orderModel->where(["receipt_order_id" => $receipt_order_id, "del_flag" => 0])->select()->toArray();
  306. if (null == $subOrderList) {
  307. return [];
  308. }
  309. $id = [];
  310. foreach ($subOrderList as $val){
  311. $id[] = $val['id'];
  312. }
  313. return array_unique($id);
  314. }catch (Exception $e) {
  315. return [];
  316. }
  317. }
  318. /**
  319. * 获取订单列表
  320. * @param $where
  321. * @param $param
  322. * @return array
  323. */
  324. public function getOrderListByWhereStr($where, $param) {
  325. $limit = $param['pageSize'];
  326. $offset = ($param['pageNum']-1)*$param['pageSize'];
  327. $sqlCnt="SELECT count(DISTINCT a.id) cnt
  328. from hbp_order_main a
  329. left join hbp_order_hotel b on b.order_id=a.id and b.del_flag=0
  330. left join hbp_order_item c on c.order_id = a.id and c.del_flag=0
  331. where {$where}
  332. ";
  333. $totalRe = Db::query($sqlCnt);
  334. $sqlList="SELECT a.*
  335. from hbp_order_main a
  336. left join hbp_order_hotel b on b.order_id=a.id and b.del_flag=0
  337. left join hbp_order_item c on c.order_id = a.id and c.del_flag=0
  338. where {$where}
  339. GROUP BY a.id
  340. order by a.id desc
  341. limit {$limit} offset {$offset}
  342. ";
  343. $list = Db::query($sqlList);
  344. $result = ["list"=>$list,"total"=>$totalRe[0]['cnt']];
  345. return Util::returnArrSu("",$result);
  346. }
  347. }