DefaultController.php 8.53 KB
<?php

namespace app\wx\modules\order\controllers;

use Yii;
use yii\helpers\HtmlPurifier;
use yii\base\Exception;

use domain\order\RepairOrder;
use domain\order\RepairOrderImages;
use domain\order\RepairOrderRepository;
use domain\order\RepairOrderStatus;
use domain\order\RepairFinishImages;
use domain\order\RepairOrderRepairPlan;

use stdClass;

/**
 * 控制器
 */
class DefaultController extends BaseController
{
    /**
     * 首页
     */
    public function actionIndex()
    {
        return $this->render('index');
    }

    /**
     * @return string
     */
    public function actionSubmit()
    {
        $e = new stdClass();
        $e->success = false;
        $e->message = 'ok';
        $userId = $this->getUserId();
        $req            = Yii::$app->request;
        $carNo          =  HtmlPurifier::process($req->post('carNo'), ['HTML.Allowed' => '']);
        $carModel       =  HtmlPurifier::process($req->post('carModel'), ['HTML.Allowed' => '']);
        $customer       =  HtmlPurifier::process($req->post('customer'), ['HTML.Allowed' => '']);
        $phone          =  HtmlPurifier::process($req->post('phone'), ['HTML.Allowed' => '']);
        $preRepair      =  HtmlPurifier::process($req->post('preRepair'), ['HTML.Allowed' => '']);
        $predictPrice   =  HtmlPurifier::process($req->post('repairPrice'), ['HTML.Allowed' => '']);
        $finishDate     =  HtmlPurifier::process($req->post('finishDate'), ['HTML.Allowed' => '']);
        $images         =  $req->post('images');

        if (empty($carNo)) {
            $e->message = '车牌号码未填';
            return $this->renderJson($e);
        }

        if (empty($carModel)) {
            $e->message = '车辆型号未填';
            return $this->renderJson($e);
        }

        if (empty($phone)) {
            $e->message = '联系电话未填';
            return $this->renderJson($e);
        }

        $data = [
            'user_id'               => $userId,
            'car_no'                => $carNo,
            'car_model'             => $carModel,
            'customer'              => $customer,
            'contact_mobile'        => $phone,
            'predict_fault'         => $preRepair,
            'predict_price'         => $predictPrice,
            'predict_finish_time'   => strtotime($finishDate.':00:00'),
            'status'                => RepairOrderStatus::REPAIRING,
        ];
        $tran = Yii::$app->db->beginTransaction();
        try {
            $repairOrder = RepairOrder::create($data);
            $uuid = $repairOrder->uuid;
            RepairOrderImages::createBrokenImages($repairOrder->id, $uuid, $images);
            $tran->commit();
            RepairOrderImages::mvBrokenImages($uuid, $images);
            $e->orderId = $uuid;
            $e->success = true;
        } catch(Exception $ex) {
            $tran->rollBack();
            $e->orderId = '';
            $e->message = '提交失败';
        }

        return $this->renderJson($e);
    }

    /**
     * @return string
     */
    public function actionOrderList()
    {
        $e = new stdClass();
        $e->success = false;
        $e->page = null;
        $e->page_count = null;
        $e->items = [];

        $page = $this->request->get('page');
        $status = $this->request->get('status');

        if (empty($page)) {
            $page = 1;
        }
        $allItemList = [0, 0, 0];
        if ('working' == $status || '' == $status) {
            $allItemList = $this->getOrderList($page, 1);
        } else {
            $allItemList = $this->getOrderList($page, 0);
        }

        $e->page_count = $allItemList[1];
        $e->page = $page ;
        $e->items = $allItemList[2];
        $e->success = true;

        return $this->renderJson($e);
    }

    /**
     * @param $page
     * @param $status
     */
    private function getOrderList($page, $status)
    {
        $pageSize = 5 ;
        $pager_offset = ($page -1) * $pageSize;
        $limitPage = $pageSize;
        $count = 15;
        $items = [];
        $pageCount = ceil(($count*1) / $pageSize);
        $carModelList = ['宝马X3','宝马X4','宝马X5','宝马X6','奔驰c200l','奔驰E260','奔驰E260','奔驰E300'];
        $customerList = ['张先生','李先生','王先生','赵先生','刘先生','宝先生','好先生','费先生'];
        for ($i = $pager_offset; $i < ($pager_offset+$limitPage); $i++ ) {
            $carModel = $carModelList[mt_rand(0,7)];
            $customer = $customerList[mt_rand(0,7)];
            $items[] = ['id'=> $i ,'carNo' => 'XA'.mt_rand(10000,99999), 'carModel' => $carModel, 'status' => $status, 'customer' => $customer ,'createdTime' => date('Y-m-d H:i')];
        }

        return [$count, $pageCount, $items];
    }

    /**
     * @return string
     */
    public function actionOrderDetails()
    {
        $e = new stdClass();
        $e->success = false;
        $e->message = 'ok';
        $orderUUId = $this->request->post('id');
        $userId = $this->getUserId();
        $orderModel = RepairOrderRepository::findOne(['uuid' => $orderUUId, 'user_id' => $userId]);
        if (empty($orderModel)) {
            $e->message = '找不到该订单';
            return $this->renderJson($e);
        }
        $brokenImageModels = RepairOrderRepository::findOrderImageAll(['repair_order_id' => $orderModel->id]);
        $brokenImages = [];
        $baseURL = Yii::$app->request->getHostInfo();
        foreach($brokenImageModels as $k => $v) {
            $brokenImages[] = $baseURL.'/'.$v->image_path;
        }
        $totalPrice = $orderModel->order_price;
        $rPlans = RepairOrderRepository::findOrderPlansAll(['repair_order_id' => $orderModel->id]);
        $repairPlans = [];
        foreach($rPlans as $k => $plan) {
            $repairPlans[] = ['plan' => $plan['repair_plan'] ,'price' => $plan['price']];
        }
        $fImages = RepairOrderRepository::findOrderFinishImageAll(['repair_order_id' => $orderModel->id]);
        $finishImages = [];
        foreach($fImages as $k => $image) {
            $finishImages[] = $baseURL.'/'.$image['image_path'];
        }
        $hasComment = false;

        $e->success = true;
        $e->item = [
            'carNo'     => $orderModel->car_no,
            'carModel'  => $orderModel->car_model,
            'customer'  => $orderModel->customer,
            'mobile'    => $orderModel->contact_mobile,
            'preRepair' => $orderModel->predict_fault,
            'repairPrice'   => $orderModel->predict_price.'元',
            'preFinishDate' => date('Y-m-d H:00', $orderModel->predict_finish_time),
            'orderDateTime' => date('Y-m-d H:00', $orderModel->created_at),
            'status'        => RepairOrderStatus::getEnLabel($orderModel->status),
            'hasComment'    => $hasComment,
            'brokenImages'  => $brokenImages,
            'repairPlans'   => $repairPlans,
            'totalPrice'    => $totalPrice,
            'finishImages'  => $finishImages
        ];

        return $this->renderJson($e);
    }

    /**
     * @return string
     */
    public function actionSubmitRepairPlans()
    {
        $e = new stdClass();
        $e->success = false;
        $e->message = 'ok';
        $repairPlans = $this->request->post('plans');
        $images = $this->request->post('images');
        $orderUUId = $this->request->post('id');
        $userId = $this->getUserId();
        $orderModel = RepairOrderRepository::findOne(['uuid' => $orderUUId, 'user_id' => $userId]);

        if (empty($orderModel)) {
            $e->message = '找不到订单';
            return $this->renderJson($e);
        }

        if (RepairOrderStatus::FINISH == $orderModel->status) {
            $e->message = '订单已完成,不能编辑';
            return $this->renderJson($e);
        }
        $orderId = $orderModel->id;
        $tran = Yii::$app->db->beginTransaction();
        try {
            $totalPrice = RepairOrderRepairPlan::batchCreate($orderId, $repairPlans);
            RepairFinishImages::createFinishImages($orderId, $orderModel->uuid, $images);
            $orderModel->status = RepairOrderStatus::FINISH;
            $orderModel->finish_at = time();
            $orderModel->order_price = $totalPrice;
            $orderModel->save();
            $tran->commit();
            RepairFinishImages::mvFinishImages($orderModel->uuid, $images);
            $e->success = true;
        } catch (Exception $ex) {
            $tran->rollBack();
            $e->success = false;
            $e->message = '提交失败';
            $e->innerMEssage = $ex->getMessage();
        }

        return $this->renderJson($e);
    }

}