|
- <?php
- /**
- * Created by PhpStorm.
- * User: nizongfeng
- * Date: 2021/10/27
- * Time: 11:26
- */
-
- namespace app\admin\dao;
-
-
- use app\admin\command\Util;
- use app\admin\model\OrderHotel;
- use app\admin\model\OrderItem;
- use app\admin\model\OrderMain;
- use think\Db;
- use think\Exception;
-
- class OrderMainDao
- {
-
- /**
- * 添加、更新订单信息
- * @param $param
- * @return array
- */
- public function save($param) {
- try{
- //1、获取渠道
- $channelDao = new CfChannelInfoDao();
- $channelRe = $channelDao->getInfoById($param['channel_id']);
- if (!$channelRe['flag']) {
- return $channelRe;
- }
- //2.获取专员
- $adminDao = new AdminDao();
- $adminRe = $adminDao->getInfoById($param['commissioner_id']);
- if (!$adminRe['flag']) {
- return $adminRe;
- }
- $data = [
- "commissioner_id"=>$param['commissioner_id'],
- "commissioner"=>$adminRe["data"]['nickname'],
- "channel_id"=>$param["channel_id"],
- "channel_name"=>$channelRe['data']['channel_name'],
- "channel_order_no"=>$param["channel_order_no"],
- "user_name"=>$param["user_name"],
- "user_phone"=>$param["user_phone"],
- "order_memo"=>$param["order_memo"],
- "create_id"=>empty($param['create_id'])?0:$param['create_id'],
- "group_id"=>empty($param['group_id'])?0:$param['group_id']
- ];
- $orderMain = new OrderMain();
- if (empty($param['id'])) {
- $id = $orderMain->insertGetId($data);
- return Util::returnArrSu("", $id);
- } else {
- $orderMain->save($data,['id'=>$param['id']]);
- return Util::returnArrSu("", $param['id']);
- }
- }catch (Exception $e){
- return Util::returnArrEr("更新主订单失败:".$e->getMessage());
- }
-
- }
-
- /**
- * 设置主订单金额
- * @param int $orderId
- * @return array
- */
- public function setOrderAmount(int $orderId){
- try {
- $itemModel = new OrderItem();
- $hotelModel = new OrderHotel();
- $itemList = $itemModel->where(["order_id" => $orderId,"del_flag"=>0])->select()->toArray();
- $hotelList = $hotelModel->where(["order_id" => $orderId,"del_flag"=>0])->select()->toArray();
- $amount = 0;
- $cost = 0;
- //状态数量统计 用于统计当前订单的状态
- $statusList = [
- 1=>0,
- 2=>0,
- 3=>0,
- 4=>0,
- "isPayment"=>0
- ];
- $cnt = count($itemList)+count($hotelList);
- foreach ($itemList as $item) {
- $amount += $item['total_price'];
- $cost += $item['total_cost'];
- $statusList[$item['confirm_status']]++;
- if ($item['payment_order_status'] ==2) {
- $statusList['isPayment']++;
- }
- }
- foreach ($hotelList as $hotel) {
- $amount += $hotel['total_price'];
- $cost += $hotel["total_cost"];
- $statusList[$hotel['confirm_status']]++;
- if ($hotel['payment_order_status'] ==2) {
- $statusList['isPayment']++;
- }
- }
- $orderInfoRe = $this->getInfoById($orderId);
- if (!$orderInfoRe['flag']) {
- return $orderInfoRe;
- }
- $orderInfo = $orderInfoRe['data'];
- $orderStatus = $this->getStatus($cnt,$statusList,$orderInfo);
- $saveVal = [
- "total_amount" => $amount,
- "cost_amount" => $cost,
- "profit_amount"=>$amount-$cost,
- "order_status"=>$orderStatus,
- "success_time"=>null,
- "cancel_time"=>null
- ];
- if ($orderStatus != $orderInfo["order_status"]) {
- if ($orderStatus==10) {
- $saveVal["success_time"] = date("Y-m-d H:i:s");
- }
- if ($orderStatus == 11 ) {
- $saveVal['cancel_time'] = date("Y-m-d H:m:s");
- }
- }
- //更新金额
- $orderMain = new OrderMain();
- $orderMain->save($saveVal,["id" => $orderId]);
- return Util::returnArrSu();
- }catch (Exception $e){
- return Util::returnArrEr("更新主表订单金额失败:".$e->getMessage());
- }
- }
-
- /**
- * 获取订单状态
- * @param $cnt
- * @param $statusList
- * @param $orderInfo
- * @return int
- */
- public function getStatus($cnt,$statusList,$orderInfo){
- //资源单状态 1、未发单/ 2已发单、3已确认、4已取消
- //订单状态0待处理 1已确认 2部分取消 3处理中 10已完成 11已取消
- //已完成:订单已完成付款、已完成收款(无视子订单状态)
- if ($orderInfo['receipt_order_status'] ==2 && $statusList['isPayment'] == $cnt) {
- return 10;
- }
- //全部未发单 待处理:子订单全部未发单
- if ($statusList[1] == $cnt) {
- return 0;
- }
- //全部已确认 已确认:子订单全部已确认
- if ($statusList[3] == $cnt) {
- return 1;
- }
- //全部已取消 已取消:订单中所有子订单已取消
- if ($statusList[4] == $cnt) {
- return 11;
- }
- //部分取消:订单中有子订单取消,其他子订单已确认
- if (($statusList[3]+$statusList[4]) == $cnt ) {
- return 2;
- }
- //处理中:非以上状态,即部分子订单确认、或者部分子订单在已发单,或部分子订单在未发单。
- return 3;
-
- }
-
-
-
- /**
- * 根据ID获取详情
- * @param $id
- * @return array
- */
- public function getInfoById($id) {
- try {
- $orderMainModel = new OrderMain();
- $orderMain = $orderMainModel->where(["id" => $id])->find();
- if (null == $orderMain) {
- return Util::returnArrEr("订单查询失败:".$id);
- }
- return Util::returnArrSu("",$orderMain->toArray());
- }catch (Exception $e) {
- return Util::returnArrEr("订单查询失败:".$e->getMessage());
- }
- }
-
- /**
- * 子订单展示
- * @param $purchaseShow
- * @param $orderHotel
- * @param $orderItem
- * @return array
- */
- public function setSubOrderShow($purchaseShow,$orderHotel,$orderItem){
- $result = [];
-
- foreach ($orderItem as $item) {
- $item['prod_type'] = 'item';
- foreach ($purchaseShow as $key=> $purchase) {
- if ($item['id'] == $key) {
- $item = array_merge($item,$purchase);
- }
- }
- $result[] = $item;
- }
- foreach ($orderHotel as $hotel) {
- $hotel['prod_type'] = 'hotel';
- foreach ($purchaseShow as $key=> $purchase) {
- if ($hotel['id'] == $key) {
- $hotel = array_merge($hotel,$purchase);
- }
- }
- $result[] = $hotel;
- }
- return Util::returnArrSu("",$result);
- }
-
- /**
- * 更新收款单下的订单信息
- * @param $receiptOrderId
- * @param $status
- * @return array
- */
- public function setReceiptOrderStatus($receiptOrderId, $status){
- try{
- $model = new OrderMain();
- $model->save(['receipt_order_status'=>$status],['receipt_order_id'=>$receiptOrderId]);
- return Util::returnArrSu();
- }catch (Exception $e){
- return Util::returnArrEr("更新收款单子啊的主订单状态失败".$e->getMessage());
- }
- }
-
- /**
- * 添加主订单到收款单下
- * @param $receiptOrder
- * @param $orderIds
- * @return array
- */
- public function addOrderMainInReceipt($receiptOrder,$orderIds){
- try{
- $data = [
- "receipt_order_id"=>$receiptOrder['id'],
- "receipt_order_status"=>$receiptOrder['status'],
- "receipt_order_name"=>$receiptOrder['name']
- ];
- $model = new OrderMain();
- $model->save($data,["id"=>["in",$orderIds]]);
- return Util::returnArrSu();
- }catch (Exception $e){
- return Util::returnArrEr("添加主订单到收款单下失败".$e->getMessage());
- }
- }
-
- /**
- * 将主订单从收款单下移除
- * @param $orderIds
- * @return array
- */
- public function removeOrderMainFormReceipt($orderIds){
- try{
- $data = [
- "receipt_order_id"=>0,
- "receipt_order_status"=>0,
- "receipt_order_name"=>""
- ];
- $model = new OrderMain();
- $model->save($data,["id"=>["in",$orderIds]]);
- return Util::returnArrSu();
- }catch (Exception $e){
- return Util::returnArrEr("将主订单从收款单下移除失败".$e->getMessage());
- }
- }
-
- /**
- * 获取订单列表
- * @param $where
- * @param $param
- * @return array
- */
- public function getOrderListByWhere($where,$param){
- try {
- $offset = ($param['pageNum'] - 1) * $param['pageSize'];
- $model = new OrderMain();
- $count = $model->where($where)->count();
- $list = $model->where($where)->limit($offset,$param['pageSize'])->order("id","DESC")->select();
- return Util::returnArrSu("", ["total" => $count, "list" => $list->toArray()]);
- }catch (Exception $e){
- return Util::returnArrSu("", ["total" => 0, "list" => []]);
- }
- }
-
- /**
- * 删除收款单
- * @param $id
- * @return array
- */
- public function delReceiptOrder($id){
- try{
- $data = [
- "receipt_order_id"=>0,
- "receipt_order_status"=>0,
- "receipt_order_name"=>""
- ];
- $model = new OrderMain();
- $model->save($data,["receipt_order_id"=>$id]);
- return Util::returnArrSu();
- }catch (Exception $e){
- return Util::returnArrEr("将主订单从收款单下移除失败".$e->getMessage());
- }
- }
-
- /**
- * 获取收款单下所有主订单的订单ID
- * @param $receipt_order_id
- * @return array
- */
- public function getOrderMainIdsByReceipt($receipt_order_id){
- $orderModel = new OrderMain();
- try {
- $subOrderList = $orderModel->where(["receipt_order_id" => $receipt_order_id, "del_flag" => 0])->select()->toArray();
- if (null == $subOrderList) {
- return [];
- }
-
- $id = [];
- foreach ($subOrderList as $val){
- $id[] = $val['id'];
- }
- return array_unique($id);
- }catch (Exception $e) {
- return [];
- }
- }
-
- /**
- * 获取订单列表
- * @param $where
- * @param $param
- * @return array
- */
- public function getOrderListByWhereStr($where, $param) {
- $limit = $param['pageSize'];
- $offset = ($param['pageNum']-1)*$param['pageSize'];
- $sqlCnt="SELECT count(DISTINCT a.id) cnt
- from hbp_order_main a
- left join hbp_order_hotel b on b.order_id=a.id and b.del_flag=0
- left join hbp_order_item c on c.order_id = a.id and c.del_flag=0
- where {$where}
- ";
- $totalRe = Db::query($sqlCnt);
- $sqlList="SELECT a.*
- from hbp_order_main a
- left join hbp_order_hotel b on b.order_id=a.id and b.del_flag=0
- left join hbp_order_item c on c.order_id = a.id and c.del_flag=0
- where {$where}
- GROUP BY a.id
- order by a.id desc
- limit {$limit} offset {$offset}
- ";
- $list = Db::query($sqlList);
- $result = ["list"=>$list,"total"=>$totalRe[0]['cnt']];
- return Util::returnArrSu("",$result);
- }
- }
|