酒店预订平台
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

321 wiersze
10 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. $orderStatus = $this->getStatus($cnt,$statusList,$orderInfoRe['data']);
  102. //更新金额
  103. $orderMain = new OrderMain();
  104. $orderMain->save(["total_amount" => $amount, "cost_amount" => $cost,"profit_amount"=>$amount-$cost,"order_status"=>$orderStatus],["id" => $orderId]);
  105. return Util::returnArrSu();
  106. }catch (Exception $e){
  107. return Util::returnArrEr("更新主表订单金额失败:".$e->getMessage());
  108. }
  109. }
  110. /**
  111. * 获取订单状态
  112. * @param $cnt
  113. * @param $statusList
  114. * @param $orderInfo
  115. * @return int
  116. */
  117. public function getStatus($cnt,$statusList,$orderInfo){
  118. //资源单状态 1、未发单/ 2已发单、3已确认、4已取消
  119. //订单状态0待处理 1已确认 2部分取消 3处理中 10已完成 11已取消
  120. //已完成:订单已完成付款、已完成收款(无视子订单状态)
  121. if ($orderInfo['receipt_order_status'] ==2 && $statusList['isPayment'] == $cnt) {
  122. return 10;
  123. }
  124. //全部未发单 待处理:子订单全部未发单
  125. if ($statusList[1] == $cnt) {
  126. return 0;
  127. }
  128. //全部已确认 已确认:子订单全部已确认
  129. if ($statusList[3] == $cnt) {
  130. return 1;
  131. }
  132. //全部已取消 已取消:订单中所有子订单已取消
  133. if ($statusList[4] == $cnt) {
  134. return 11;
  135. }
  136. //部分取消:订单中有子订单取消,其他子订单已确认
  137. if (($statusList[3]+$statusList[4]) == $cnt ) {
  138. return 2;
  139. }
  140. //处理中:非以上状态,即部分子订单确认、或者部分子订单在已发单,或部分子订单在未发单。
  141. return 3;
  142. }
  143. /**
  144. * 根据ID获取详情
  145. * @param $id
  146. * @return array
  147. */
  148. public function getInfoById($id) {
  149. try {
  150. $orderMainModel = new OrderMain();
  151. $orderMain = $orderMainModel->where(["id" => $id])->find();
  152. if (null == $orderMain) {
  153. return Util::returnArrEr("订单查询失败:".$id);
  154. }
  155. return Util::returnArrSu("",$orderMain->toArray());
  156. }catch (Exception $e) {
  157. return Util::returnArrEr("订单查询失败:".$e->getMessage());
  158. }
  159. }
  160. /**
  161. * 子订单展示
  162. * @param $purchaseShow
  163. * @param $orderHotel
  164. * @param $orderItem
  165. * @return array
  166. */
  167. public function setSubOrderShow($purchaseShow,$orderHotel,$orderItem){
  168. $result = [];
  169. foreach ($orderItem as $item) {
  170. $item['prod_type'] = 'item';
  171. foreach ($purchaseShow as $key=> $purchase) {
  172. if ($item['id'] == $key) {
  173. $item = array_merge($item,$purchase);
  174. }
  175. }
  176. $result[] = $item;
  177. }
  178. foreach ($orderHotel as $hotel) {
  179. $hotel['prod_type'] = 'hotel';
  180. foreach ($purchaseShow as $key=> $purchase) {
  181. if ($hotel['id'] == $key) {
  182. $hotel = array_merge($hotel,$purchase);
  183. }
  184. }
  185. $result[] = $hotel;
  186. }
  187. return Util::returnArrSu("",$result);
  188. }
  189. /**
  190. * 更新收款单下的订单信息
  191. * @param $receiptOrderId
  192. * @param $status
  193. * @return array
  194. */
  195. public function setReceiptOrderStatus($receiptOrderId, $status){
  196. try{
  197. $model = new OrderMain();
  198. $model->save(['receipt_order_status'=>$status],['receipt_order_id'=>$receiptOrderId]);
  199. return Util::returnArrSu();
  200. }catch (Exception $e){
  201. return Util::returnArrEr("更新收款单子啊的主订单状态失败".$e->getMessage());
  202. }
  203. }
  204. /**
  205. * 添加主订单到收款单下
  206. * @param $receiptOrder
  207. * @param $orderIds
  208. * @return array
  209. */
  210. public function addOrderMainInReceipt($receiptOrder,$orderIds){
  211. try{
  212. $data = [
  213. "receipt_order_id"=>$receiptOrder['id'],
  214. "receipt_order_status"=>$receiptOrder['status'],
  215. "receipt_order_name"=>$receiptOrder['name']
  216. ];
  217. $model = new OrderMain();
  218. $model->save($data,["id"=>["in",$orderIds]]);
  219. return Util::returnArrSu();
  220. }catch (Exception $e){
  221. return Util::returnArrEr("添加主订单到收款单下失败".$e->getMessage());
  222. }
  223. }
  224. /**
  225. * 将主订单从收款单下移除
  226. * @param $orderIds
  227. * @return array
  228. */
  229. public function removeOrderMainFormReceipt($orderIds){
  230. try{
  231. $data = [
  232. "receipt_order_id"=>0,
  233. "receipt_order_status"=>0,
  234. "receipt_order_name"=>""
  235. ];
  236. $model = new OrderMain();
  237. $model->save($data,["id"=>["in",$orderIds]]);
  238. return Util::returnArrSu();
  239. }catch (Exception $e){
  240. return Util::returnArrEr("将主订单从收款单下移除失败".$e->getMessage());
  241. }
  242. }
  243. /**
  244. * 获取订单列表
  245. * @param $where
  246. * @param $param
  247. * @return array
  248. */
  249. public function getOrderListByWhere($where,$param){
  250. try {
  251. $offset = ($param['pageNum'] - 1) * $param['pageSize'];
  252. $model = new OrderMain();
  253. $count = $model->where($where)->count();
  254. $list = $model->where($where)->limit($offset,$param['pageSize'])->order("id","DESC")->select();
  255. return Util::returnArrSu("", ["total" => $count, "list" => $list->toArray()]);
  256. }catch (Exception $e){
  257. return Util::returnArrSu("", ["total" => 0, "list" => []]);
  258. }
  259. }
  260. /**
  261. * 删除收款单
  262. * @param $id
  263. * @return array
  264. */
  265. public function delReceiptOrder($id){
  266. try{
  267. $data = [
  268. "receipt_order_id"=>0,
  269. "receipt_order_status"=>0,
  270. "receipt_order_name"=>""
  271. ];
  272. $model = new OrderMain();
  273. $model->save($data,["receipt_order_id"=>$id]);
  274. return Util::returnArrSu();
  275. }catch (Exception $e){
  276. return Util::returnArrEr("将主订单从收款单下移除失败".$e->getMessage());
  277. }
  278. }
  279. /**
  280. * 获取收款单下所有主订单的订单ID
  281. * @param $receipt_order_id
  282. * @return array
  283. */
  284. public function getOrderMainIdsByReceipt($receipt_order_id){
  285. $orderModel = new OrderMain();
  286. try {
  287. $subOrderList = $orderModel->where(["receipt_order_id" => $receipt_order_id, "del_flag" => 0])->select()->toArray();
  288. if (null == $subOrderList) {
  289. return [];
  290. }
  291. $id = [];
  292. foreach ($subOrderList as $val){
  293. $id[] = $val['id'];
  294. }
  295. return array_unique($id);
  296. }catch (Exception $e) {
  297. return [];
  298. }
  299. }
  300. }