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

294 lines
11 KiB

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: nizongfeng
  5. * Date: 2021/11/17
  6. * Time: 17:29
  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\PaymentOrder;
  13. use think\Db;
  14. use think\Exception;
  15. class PaymentOrderDao
  16. {
  17. /**
  18. * 获取详情
  19. * @param $id
  20. * @return array
  21. */
  22. public function getInfoById($id){
  23. try{
  24. $model = new PaymentOrder();
  25. $info = $model->where(["id"=>$id])->find();
  26. if ($info == null) {
  27. return Util::returnArrEr("获取付款单信息失败:".$id);
  28. }
  29. return Util::returnArrSu("",$info->toArray());
  30. }catch (Exception $e){
  31. return Util::returnArrEr("获取付款单信息失败:".$e->getMessage());
  32. }
  33. }
  34. /**
  35. * 添加记录
  36. * @param $param
  37. * @return array
  38. */
  39. public function save($param)
  40. {
  41. if ($this->checkName($param)) {
  42. return Util::returnArrEr("名称已经存在");
  43. }
  44. try {
  45. $data = [
  46. 'name' => $param['name'],
  47. "create_id"=>$param['create_id'],
  48. "group_id"=>$param['group_id']
  49. ];
  50. $receiptOrder = new PaymentOrder();
  51. if (empty($param['id'])) {
  52. $id = $receiptOrder->insertGetId($data);
  53. return Util::returnArrSu("", $id);
  54. } else {
  55. $receiptOrder->save($data, ['id' => $param['id']]);
  56. return Util::returnArrSu("", $param['id']);
  57. }
  58. } catch (Exception $e) {
  59. return Util::returnArrEr("更新主订单失败:" . $e->getMessage());
  60. }
  61. }
  62. /**
  63. * 校验名称
  64. * @param $param
  65. * @return bool
  66. */
  67. public function checkName($param):bool {
  68. try {
  69. $model = new PaymentOrder();
  70. $infoRe = $model->where(["name" => $param['name'],"group_id"=>$param['group_id']])->find();
  71. if ($infoRe == null) {
  72. return false;
  73. }
  74. $info = $infoRe->toArray();
  75. if (isset($param['id'])) {
  76. if ($param['id'] != $info['id']) {
  77. return true;
  78. }
  79. }else{
  80. return true;
  81. }
  82. return false;
  83. } catch (Exception $e) {
  84. return false;
  85. }
  86. }
  87. /**
  88. * 修改状态
  89. * @param $id
  90. * @param $status
  91. * @return array
  92. */
  93. public function setStatus($id, $status)
  94. {
  95. try {
  96. //设置收购单状态
  97. $receiptOrder = new PaymentOrder();
  98. $receiptOrder->save(['status' => $status], ['id' => $id]);
  99. return Util::returnArrSu();
  100. } catch (Exception $e) {
  101. return Util::returnArrEr("修改状态失败" . $e->getMessage());
  102. }
  103. }
  104. /**
  105. * 获取列表
  106. * @param $param
  107. * @return array
  108. */
  109. public function getList($param)
  110. {
  111. try {
  112. $where = ["a.del_flag"=>0,"a.group_id"=>$param['group_id']];
  113. if (!empty($param['order_id']) || !empty($param['sub_order_id'])) {
  114. $subWhere = [];
  115. if (!empty($param['order_id'])) {
  116. $subWhere["order_id"]=$param['order_id'];
  117. }
  118. if (!empty($param['sub_order_id'])) {
  119. $subWhere["id"]=$param['sub_order_id'];
  120. }
  121. $idList = [];
  122. $orderItemModel = new OrderItem();
  123. $orderItemList = $orderItemModel->where($subWhere)->select()->toArray();
  124. foreach ($orderItemList as $orderItem) {
  125. if ($orderItem != null && !empty($orderItem['payment_order_id'])) {
  126. $idList[] = $orderItem['payment_order_id'];
  127. }
  128. }
  129. $orderHotelModel = new OrderHotel();
  130. $orderHotelList = $orderHotelModel->where($subWhere)->select()->toArray();
  131. foreach ($orderHotelList as $orderHotel) {
  132. if ($orderHotel != null && !empty($orderHotel['payment_order_id'])) {
  133. $idList[] = $orderHotel['payment_order_id'];
  134. }
  135. }
  136. if (count($idList) == 0) {
  137. $where["a.id"]=0;
  138. }else{
  139. $where["a.id"]=["in",$idList];
  140. }
  141. }
  142. if (!empty($param['name'])) {
  143. $where['a.name'] = ["like","%".$param['name']."%"];
  144. }
  145. if ($param['status']."" != 'all' && $param['status']!=="") {
  146. $where["a.status"] = $param['status'];
  147. }
  148. if (!empty($param["id"])) {
  149. $where["a.id"] = $param["id"];
  150. }
  151. if (!empty($param['create_id'])) {
  152. $where["a.create_id"] = $param["create_id"];
  153. }
  154. $having = [];
  155. if(!empty($param['startCost']) && !empty($param['endCost'])) {
  156. $having[] = " (itemMoney >= {$param['startCost']} and itemMoney <= {$param['endCost']} ) or (hotelMoney >= {$param['startCost']} and hotelMoney <= {$param['endCost']} ) ";
  157. }
  158. if(!empty($param['startCost']) && empty($param['endCost'])) {
  159. $having[] = " (itemMoney >= {$param['startCost']} ) or (hotelMoney >= {$param['startCost']} ) ";
  160. }
  161. if(empty($param['startCost']) && !empty($param['endCost'])) {
  162. $having[] = " (itemMoney <= {$param['endCost']} ) or (hotelMoney <= {$param['endCost']} ) ";
  163. }
  164. $offset = ($param['pageNum'] - 1) * $param['pageSize'];
  165. $receiptOrder = new PaymentOrder();
  166. $total = $receiptOrder
  167. ->alias("a")
  168. ->field("a.*,
  169. (SELECT ifnull( sum( total_cost ), 0 ) from hbp_order_item where payment_order_id = a.id) 'itemMoney',
  170. (SELECT ifnull( sum( total_cost ), 0 ) from hbp_order_hotel where payment_order_id = a.id) 'hotelMoney'
  171. ")
  172. ->group("a.id")
  173. ->where($where)
  174. ->having(join("and", $having))
  175. ->count();
  176. $list = $receiptOrder
  177. ->alias("a")
  178. ->field("a.*,
  179. (SELECT concat('订单:',ifnull( GROUP_CONCAT( id ORDER BY id DESC ), '-' ), '</br>成本:',ifnull( sum( total_cost ), '-' )) from hbp_order_item where payment_order_id = a.id) 'item',
  180. (SELECT concat('订单:',ifnull( GROUP_CONCAT( id ORDER BY id DESC ), '-' ), '</br>成本:',ifnull( sum( total_cost ), '-' )) from hbp_order_hotel where payment_order_id = a.id) 'hotel',
  181. (SELECT ifnull( sum( total_cost ), 0 ) from hbp_order_item where payment_order_id = a.id) 'itemMoney',
  182. (SELECT ifnull( sum( total_cost ), 0 ) from hbp_order_hotel where payment_order_id = a.id) 'hotelMoney'
  183. ")
  184. ->group("a.id")
  185. ->where($where)
  186. ->having(join("and", $having))
  187. ->limit($offset, $param['pageSize'])
  188. ->order("a.id","DESC")->select();
  189. $data = ["total" => $total, "list" => $list->toArray()];
  190. return Util::returnArrSu("", $data);
  191. } catch (Exception $e) {
  192. return Util::returnArrSu("", ["total" => 0, "list" => []]);
  193. }
  194. }
  195. /**
  196. * 删除
  197. * @param $id
  198. * @return array
  199. */
  200. public function del($id){
  201. try {
  202. //设置收购单状态
  203. $receiptOrder = new PaymentOrder();
  204. $receiptOrder->save(['del_flag' => 1], ['id' => $id]);
  205. return Util::returnArrSu();
  206. } catch (Exception $e) {
  207. return Util::returnArrEr("修改状态失败" . $e->getMessage());
  208. }
  209. }
  210. /**
  211. * 获取子订单列表
  212. * @param $hotel_where
  213. * @param $item_where
  214. * @param $where
  215. * @param $param
  216. * @return array
  217. */
  218. public function getSubOrderListByWhere($hotel_where,$item_where,$where,$param):array {
  219. $limit = $param['pageSize'];
  220. $offset = ($param['pageNum']-1)*$param['pageSize'];
  221. $sqlCnt = "SELECT count(1) cnt
  222. from (
  223. (
  224. SELECT a.order_id,a.id,d.supplier_name,a.trade_order_number,a.item_type 'hotel_name',a.item_name 'room_name',
  225. a.item_unit 'plan_name',
  226. a.check_in_date,a.check_in_date 'check_out_date',d.count,a.create_time,'item' as 'prod_type'
  227. ,a.payment_order_name,a.payment_order_status,a.payment_order_id,a.customer_name,a.total_cost,a.total_price,a.confirm_status,a.finance_memo
  228. from hbp_order_item a
  229. INNER JOIN hbp_purchase d on a.id = d.order_detail_id and d.prod_type='item'
  230. where
  231. $item_where
  232. order by a.create_time desc
  233. )
  234. UNION
  235. (
  236. SELECT b.order_id,b.id,c.supplier_name,b.trade_order_number,b.hotel_name ,b.room_name ,b.plan_name,
  237. b.check_in_date,b.check_out_date,c.count,b.create_time,'hotel' as 'prod_type'
  238. ,b.payment_order_name,b.payment_order_status,b.payment_order_id,b.customer_name,b.total_cost,b.total_price,b.confirm_status,b.finance_memo
  239. from hbp_order_hotel b
  240. INNER JOIN hbp_purchase c on b.id=c.order_detail_id and c.prod_type='hotel'
  241. where
  242. $hotel_where
  243. order by b.create_time desc
  244. )
  245. ) x
  246. $where
  247. ";
  248. $totalRe = Db::query($sqlCnt);
  249. $sql = "SELECT x.*
  250. from (
  251. (
  252. SELECT a.order_id,a.id,d.supplier_name,a.trade_order_number,a.item_type 'hotel_name',a.item_name 'room_name',
  253. a.item_unit 'plan_name',
  254. a.check_in_date,a.check_in_date 'check_out_date',d.count,a.create_time,'item' as 'prod_type'
  255. ,a.payment_order_name,a.payment_order_status,a.payment_order_id,a.customer_name,a.total_cost,a.total_price,a.confirm_status,a.finance_memo
  256. from hbp_order_item a
  257. INNER JOIN hbp_purchase d on a.id = d.order_detail_id and d.prod_type='item'
  258. where
  259. $item_where
  260. order by a.create_time desc
  261. )
  262. UNION
  263. (
  264. SELECT b.order_id,b.id,c.supplier_name,b.trade_order_number,b.hotel_name ,b.room_name ,b.plan_name,
  265. b.check_in_date,b.check_out_date,c.count,b.create_time,'hotel' as 'prod_type'
  266. ,b.payment_order_name,b.payment_order_status,b.payment_order_id,b.customer_name,b.total_cost,b.total_price,b.confirm_status,b.finance_memo
  267. from hbp_order_hotel b
  268. INNER JOIN hbp_purchase c on b.id=c.order_detail_id and c.prod_type='hotel'
  269. where
  270. $hotel_where
  271. order by b.create_time desc
  272. )
  273. ) x
  274. ORDER BY x.create_time desc
  275. limit $limit offset $offset";
  276. $list = Db::query($sql);
  277. $result = ["list"=>$list,"total"=>$totalRe[0]['cnt']];
  278. return Util::returnArrSu("",$result);
  279. }
  280. }