酒店预订平台
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.
 
 
 
 
 
 

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