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

365 lines
13 KiB

  1. <?php
  2. namespace app\admin\service;
  3. use app\admin\command\Util;
  4. use app\admin\dao\OrderHotelDao;
  5. use app\admin\dao\OrderItemDao;
  6. use app\admin\dao\OrderMainDao;
  7. use app\admin\dao\PurchaseDao;
  8. use app\admin\dao\PurchasePriceDao;
  9. /**
  10. * Created by PhpStorm.
  11. * User: nizongfeng
  12. * Date: 2021/10/24
  13. * Time: 15:55
  14. */
  15. class OrderMainService
  16. {
  17. /**
  18. * 保存主订单
  19. * @param $param
  20. * @return array
  21. */
  22. public function saveOrder($param)
  23. {
  24. /**
  25. * 1.添加主订单
  26. */
  27. $orderMainDao = new OrderMainDao();
  28. $addOrderMain = $orderMainDao->save($param);
  29. if (!$addOrderMain["flag"]) {
  30. return $addOrderMain;
  31. }
  32. if (!isset($param['subOrderList']) || count($param['subOrderList'])==0) {
  33. return Util::returnArrSu("成功",$addOrderMain['data']);
  34. }
  35. $orderId = $addOrderMain['data'];
  36. //所有子订单设置为删除
  37. $orderHotelDao = new OrderHotelDao();
  38. $orderHotelDao->delete($orderId);
  39. $orderItemDao = new OrderItemDao();
  40. $orderItemDao->delete($orderId);
  41. $purchasePriceDao = new PurchasePriceDao();
  42. $purchasePriceDao->delete($orderId);
  43. //循环子订单
  44. foreach ($param['subOrderList'] as $subOrderParam) {
  45. $subOrderParam['group_id'] = $param['group_id'];
  46. /**
  47. * 2.添加子订单 有则激活更新、无则添加
  48. */
  49. if ($subOrderParam['prod_type'] == 'hotel') {
  50. $subOrderDao = $orderHotelDao;
  51. } else {
  52. $subOrderDao = $orderItemDao;
  53. }
  54. $addSubOrder = $subOrderDao->modify($subOrderParam, $orderId);
  55. if (!$addSubOrder['flag']) {
  56. return $addSubOrder;
  57. }
  58. $subOrderId = $addSubOrder['data'];
  59. $subOrderInfo = $subOrderDao->getInfoById($subOrderId);
  60. if (!$subOrderInfo['flag']) {
  61. return $subOrderInfo;
  62. }
  63. /**
  64. * 2.1添加子订单下的采购单 有则更新激活、无则添加
  65. */
  66. $purchaseDao = new PurchaseDao();
  67. if ($subOrderParam['prod_type'] == 'hotel') {
  68. $addPurchase = $purchaseDao->saveHotelPurchase($subOrderParam, $subOrderInfo['data']);
  69. } else {
  70. $addPurchase = $purchaseDao->saveItemPurchase($subOrderParam, $subOrderInfo['data']);
  71. }
  72. if (!$addPurchase['flag']) {
  73. return $addPurchase;
  74. }
  75. $purchaseId = $addPurchase['data'];
  76. /**
  77. * 2.1.1添加采购单的每日价格 先删除、后添加激活
  78. */
  79. $purchasePriceDao = new PurchasePriceDao();
  80. $addPurchasePrice = $purchasePriceDao->saveList($subOrderParam['purchasePriceList'], $orderId, $subOrderParam['prod_type'], $subOrderId, $purchaseId);
  81. if (!$addPurchasePrice['flag']) {
  82. return $addPurchasePrice;
  83. }
  84. /**
  85. * 2.1.2 计算更新 采购单总金额、成本、产品数量
  86. */
  87. $setPurchaseRe = $purchaseDao->setPurchaseAmount($purchaseId);
  88. if (!$setPurchaseRe['flag']) {
  89. return $setPurchaseRe;
  90. }
  91. /**
  92. * 2.2 计算更新 子订单成本、金额、产品数量
  93. */
  94. $setSubOrderRe = $subOrderDao->setSubOrderAmount($subOrderId);
  95. if (!$setSubOrderRe['flag']) {
  96. return $setSubOrderRe;
  97. }
  98. }
  99. /**
  100. * 3 计算更新 主订单成本、金额、产品数量
  101. */
  102. $setOrderMainRe = $orderMainDao->setOrderAmount($orderId);
  103. if (!$setOrderMainRe['flag']) {
  104. return $setOrderMainRe;
  105. }
  106. return Util::returnArrSu("成功",$orderId);
  107. }
  108. /**
  109. * 保存子订单
  110. * @param $subOrderParam
  111. * @return array
  112. */
  113. public function subOrderSave($subOrderParam)
  114. {
  115. $orderId = $subOrderParam['order_id'];
  116. $orderMainDao = new OrderMainDao();
  117. /**
  118. * 2.添加子订单
  119. */
  120. if ($subOrderParam['prod_type'] == 'hotel') {
  121. $subOrderDao = new OrderHotelDao();
  122. } else {
  123. $subOrderDao = new OrderItemDao();
  124. }
  125. $addSubOrder = $subOrderDao->modify($subOrderParam, $orderId);
  126. if (!$addSubOrder['flag']) {
  127. return $addSubOrder;
  128. }
  129. $subOrderId = $addSubOrder['data'];
  130. $subOrderInfo = $subOrderDao->getInfoById($subOrderId);
  131. if (!$subOrderInfo['flag']) {
  132. return $subOrderInfo;
  133. }
  134. /**
  135. * 2.1添加子订单下的采购单
  136. */
  137. $purchaseDao = new PurchaseDao();
  138. if ($subOrderParam['prod_type'] == 'hotel') {
  139. $addPurchase = $purchaseDao->saveHotelPurchase($subOrderParam, $subOrderInfo['data']);
  140. } else {
  141. $addPurchase = $purchaseDao->saveItemPurchase($subOrderParam, $subOrderInfo['data']);
  142. }
  143. if (!$addPurchase['flag']) {
  144. return $addPurchase;
  145. }
  146. $purchaseId = $addPurchase['data'];
  147. /**
  148. * 2.1.1添加采购单的每日价格
  149. */
  150. $purchasePriceDao = new PurchasePriceDao();
  151. $addPurchasePrice = $purchasePriceDao->saveList($subOrderParam['purchasePriceList'], $orderId, $subOrderParam['prod_type'], $subOrderId, $purchaseId);
  152. if (!$addPurchasePrice['flag']) {
  153. return $addPurchasePrice;
  154. }
  155. /**
  156. * 2.1.2 计算更新 采购单总金额、成本、产品数量
  157. */
  158. $setPurchaseRe = $purchaseDao->setPurchaseAmount($purchaseId);
  159. if (!$setPurchaseRe['flag']) {
  160. return $setPurchaseRe;
  161. }
  162. /**
  163. * 2.2 计算更新 子订单成本、金额、产品数量
  164. */
  165. $setSubOrderRe = $subOrderDao->setSubOrderAmount($subOrderId);
  166. if (!$setSubOrderRe['flag']) {
  167. return $setSubOrderRe;
  168. }
  169. /**
  170. * 3 计算更新 主订单成本、金额、产品数量
  171. */
  172. $setOrderMainRe = $orderMainDao->setOrderAmount($orderId);
  173. if (!$setOrderMainRe['flag']) {
  174. return $setOrderMainRe;
  175. }
  176. return Util::returnArrSu("保存成功",$subOrderId);
  177. }
  178. /**
  179. * 删除子订单
  180. * @param $param
  181. * @return array
  182. */
  183. public function delSubOrder($param){
  184. try {
  185. if ($param['prod_type'] == 'hotel') {
  186. $subOrderDao = new OrderHotelDao();
  187. } else {
  188. $subOrderDao = new OrderItemDao();
  189. }
  190. $subOrderRe = $subOrderDao->getInfoById($param['id']);
  191. if (!$subOrderRe['flag']) {
  192. return $subOrderRe;
  193. }
  194. $subOrderDao->delById($param['id']);
  195. //删除采购单
  196. $purchaseDao = new PurchaseDao();
  197. $purchaseDao->deleteBySubOrderId($param['id']);
  198. //删除每日采购单价格
  199. $purchasePriceDao = new PurchasePriceDao();
  200. $purchasePriceDao->deleteBySubOrderId($param['id']);
  201. //重新计算订单总金额
  202. $orderMainDao = new OrderMainDao();
  203. $orderMainDao->setOrderAmount($subOrderRe['data']['order_id']);
  204. return Util::returnArrSu("成功");
  205. }catch (\Exception $e){
  206. return Util::returnArrEr("删除子订单失败:".$e->getMessage());
  207. }
  208. }
  209. /**
  210. * 获取订单详情
  211. * @param $id
  212. * @return array
  213. */
  214. public function getOrderInfo($id){
  215. $orderMainDao = new OrderMainDao();
  216. $orderMainRe = $orderMainDao->getInfoById($id);
  217. if (!$orderMainRe['flag']) {
  218. return $orderMainRe;
  219. }
  220. $orderMain = $orderMainRe['data'];
  221. //获取采购单金额列表
  222. $purchasePriceDao = new PurchasePriceDao();
  223. $purchasePriceRe = $purchasePriceDao->getPurchasePriceListByOrderId($id);
  224. if (!$purchasePriceRe['flag']) {
  225. return $purchasePriceRe;
  226. }
  227. //获取采购单列表
  228. $purchaseDao = new PurchaseDao();
  229. $purchaseRe = $purchaseDao->getListByOrderId($id);
  230. if (!$purchaseRe['flag']) {
  231. return $purchaseRe;
  232. }
  233. //设置采购单展示数据
  234. $purchaseShow = $purchaseDao->setPurchaseShow($purchaseRe['data'],$purchasePriceRe['data']);
  235. if (!$purchaseShow['flag']) {
  236. return $purchaseShow;
  237. }
  238. //获取子订单列表
  239. $orderHotelDao = new OrderHotelDao();
  240. $orderHotelRe = $orderHotelDao->getListByOrderId($id);
  241. if (!$orderHotelRe['flag']) {
  242. return $orderHotelRe;
  243. }
  244. $orderItemDao = new OrderItemDao();
  245. $orderItemRe = $orderItemDao->getListByOrderId($id);
  246. if (!$orderItemRe['flag']) {
  247. return $orderItemRe;
  248. }
  249. //设置子订单列表
  250. $subOrderList = $orderMainDao->setSubOrderShow($purchaseShow['data'],$orderHotelRe['data'],$orderItemRe['data']);
  251. if (!$subOrderList['flag']) {
  252. return $subOrderList;
  253. }
  254. $orderMain["subOrderList"]=$subOrderList['data'];
  255. return Util::returnArrSu("成功",$orderMain);
  256. }
  257. /**
  258. * 获取订单列表
  259. * @param $param
  260. * @return array
  261. */
  262. public function getOrderList($param){
  263. $orderMainDao = new OrderMainDao();
  264. $where = ["id"=>["neq",""]];
  265. if (!empty($param['order_id'])) {
  266. $where["id"]=$param['order_id'];
  267. }
  268. if (!empty($param['channel_id'])) {
  269. $where['channel_id'] = $param['channel_id'];
  270. }
  271. if ($param['order_status'] != '') {
  272. $where['order_status'] = $param['order_status'];
  273. }
  274. if (!empty($param['commissioner_id'])) {
  275. $where['commissioner_id'] = $param['commissioner_id'];
  276. }
  277. if (!empty($param['user_name'])) {
  278. $where['user_name'] = ["like","'%".$param['user_name']."%'"];
  279. }
  280. if (!empty($param['user_phone'])) {
  281. $where['user_phone'] = $param['user_phone'];
  282. }
  283. if (!empty($param['create_id'])) {
  284. $where['create_id'] = $param['create_id'];
  285. }
  286. //金额区间查询
  287. if (!empty($param['startMoney'])&& empty($param['endMoney'])) {
  288. $where['total_amount'] = [">=",$param['startMoney']];
  289. }
  290. if (!empty($param['endMoney']) && empty($param['startMoney'])) {
  291. $where['total_amount'] = ["<=",$param['endMoney']];
  292. }
  293. if (!empty($param['endMoney']) && !empty($param['startMoney'])) {
  294. $where['total_amount'] = ["between",[$param['startMoney'],$param['endMoney']]];
  295. }
  296. //时间区间查询
  297. if (!empty($param['startTime']) && empty($param['endTime'])) {
  298. $where['create_time'] = [">=",$param['startTime']." 00:00:00"];
  299. }
  300. if (!empty($param['endTime'])&& empty($param['startTime'])) {
  301. $where['create_time'] = ["<=",$param['endTime']." 23:59:59"];
  302. }
  303. if(!empty($param['endTime'])&& !empty($param['startTime'])){
  304. $where['create_time'] = ["between",[$param['startTime']." 00:00:00",$param['endTime']." 23:59:59"]];
  305. }
  306. if ($param['receipt_order_status'] !== '') {
  307. $where['receipt_order_status'] = $param['receipt_order_status'];
  308. }
  309. if ($param['receipt_order_id'] !== '') {
  310. $where['receipt_order_id'] = $param['receipt_order_id'];
  311. }
  312. if ($param['channel_order_no'] != '') {
  313. $where['channel_order_no'] = $param['channel_order_no'];
  314. }
  315. $result = $orderMainDao->getOrderListByWhere($where,$param);
  316. if (!$result['flag'] || $result['data']['total']==0) {
  317. return $result;
  318. }
  319. //获取主订单ID
  320. $ids = [];
  321. foreach ($result['data']['list'] as $info){
  322. $ids[] = $info['id'];
  323. }
  324. //获取子订单列表
  325. $orderHotelDao = new OrderHotelDao();
  326. $orderItemDao = new OrderItemDao();
  327. $orderHotelList = $orderHotelDao->getOrderListByOrderIds($ids);
  328. $orderItemList = $orderItemDao->getOrderListByOrderIds($ids);
  329. //设置主订单列表
  330. $orderMainList = [];
  331. foreach ($result['data']['list'] as $order) {
  332. $order['subOrder']=[];
  333. foreach ($orderHotelList as $hotel){
  334. if ($hotel['order_id'] == $order['id']) {
  335. $hotel["type"]="酒店";
  336. $order['subOrder'][] = $hotel;
  337. }
  338. }
  339. foreach ($orderItemList as $item){
  340. if ($item['order_id'] == $order['id']) {
  341. $item["type"]="附加项目";
  342. $order['subOrder'][] = $item;
  343. }
  344. }
  345. $orderMainList[] = $order;
  346. }
  347. $result['data']['list'] = $orderMainList;
  348. return $result;
  349. }
  350. }