BoardController.php 12 KB
<?php

namespace app\ht\modules\home\controllers;

use yii\helpers\Url;
use app\ht\controllers\BaseController;
use common\exts\Invoice\baiwang\InvoiceApi;
use common\helpers\Distance;
use common\models\BindDeviceApply   as BindDeviceApplyModel;
use common\models\Engineer          as EngineerModel;
use common\models\EngineerSkills    as EngineerSkillsModel;
use common\models\NoticeInfo        as NoticeInfoModel;
use common\models\RepairOrder       as RepairOrderModel;
use common\models\RepairOrderAppeal as RepairOrderAppealModel;
use domain\finance\models\Invoice   as InvoiceModel;
use domain\engineer\EngineerStatus;
use domain\trade\RepairOrderStatus;
use domain\finance\EngineerWithdrawRepository;
use stdClass;
use function time;
use function strtotime;
use function date;
use function intval;
use function explode;

/**
 * 管理->帐号管理
 */
class BoardController extends BaseController
{
    /**
     * 帐号管理显示页面
     */
    public function actionIndex()
    {
        $params = array();
        return $this->render("index", $params);
    }

    // 获取仪表盘信息
    public function actionGetData()
    {
        $act = $this->request->get('pickOrder');
        $e= new stdClass();

        if (empty($act)) {
            $e->engineerAuth = $this->getEngineerAuth(EngineerStatus::STATUS_REALNAME_REVIEW);
            $e->engineerAvaliable  = $this->getEngineerAuth();
            $e->engineerSkillAuth = $this->getEngineerSkillAuth();
            $e->deviceReview = $this->getDeviceReview();
            $e->appeal = $this->getAppeal();
            $e->errorInvoice = $this->getErrorInvoice();
            $e->withdraw = $this->getWithdraw();
        } else {
            $queryPickOrder = RepairOrderModel::find();
            $queryPickOrder->where(['status' => RepairOrderStatus::STATUS_WAIT_PICK]);
            $count = $queryPickOrder->count();
            if (empty($count)) {
                $count = 0;
            }
            $e->pickOrderCount = $count;
        }

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

    // 获取订单数据
    public function actionGetOrderData()
    {
        $e = new stdClass();
        $e->success = true;
        $end = time();
        $endT = strtotime(date('Y-m-d')) + 1;
        $start = $endT - 6 * 24 * 3600;
        $status = array(
            RepairOrderStatus::STATUS_FINISH,
            RepairOrderStatus::STATUS_WAIT_RATE,
            RepairOrderStatus::STATUS_USER_CANCEL,
            RepairOrderStatus::STATUS_ENGINEER_CANCEL,
            RepairOrderStatus::STATUS_USER_CLOSE,
            RepairOrderStatus::STATUS_ENGINEER_CLOSE
        );

        $type = 0;
        $orderListTmp = $this->getOrder($start, $end, $type, $status);
        $orderList = array();
        if ($orderListTmp) {
            foreach ($orderListTmp as $k => $v) {
                $orderList[$v['mydate']] = $v;
            }
        }
        $dateList = $this->genDate($start);
        $data =array();
        foreach ($dateList as $kk => $vv) {
            $myArr = array();
            $myArr['date'] = $kk;
            if ($type == 0) {
                $myArr['pay_price'] = isset($orderList[$kk]['pay_price']) ? $orderList[$kk]['pay_price'] : 0;
                $myArr['repair_price'] = isset($orderList[$kk]['repair_price']) ? $orderList[$kk]['repair_price'] : 0;
            }

            $data[] = $myArr;
        }
        $e->data = $data;
        return $this->renderJson($e);
    }

    // 热力图
    public function actionGetHeatData()
    {
        $e=  new stdClass();
        $e->success = true;
        $north_east= $this->request->get('north_east');
        $south_west = $this->request->get('south_west');
        $zoom = $this->request->get('zoom');
        $distance = 25000; //25公里内的点
        $earthRadius = Distance::EARTH_RADIUS;
        $table1 = 'address';
        $end = time();
        $endT =strtotime(date('Y-m-d')) + 1;
        $start = $endT - 6 * 24 * 3600;
        $status =array(
            RepairOrderStatus::STATUS_FINISH,
            RepairOrderStatus::STATUS_WAIT_RATE,
            RepairOrderStatus::STATUS_USER_CANCEL,
            RepairOrderStatus::STATUS_ENGINEER_CANCEL,
            RepairOrderStatus::STATUS_USER_CLOSE,
            RepairOrderStatus::STATUS_ENGINEER_CLOSE
        );

        /*
        $vRadian= "$earthRadius * acos( cos( radians('$lat') ) * cos( radians( $table1.latitude ) ) * cos( radians( $table1.longitude ) - radians('$lng') ) +
			sin( radians('$lat') ) * sin( radians( $table1.latitude ) ) )";
        */

        $repairOrderModel = RepairOrderModel::find();
        $repairOrderModel->leftJoin("address","address.id = repair_order.address_id");
        $repairOrderModel->select(["concat(address.latitude,'_',address.longitude) as latlng", "count(*) as mycount"]);
        $repairOrderModel->where(['repair_order.status' => $status]);
        $repairOrderModel->andFilterWhere(['>', 'repair_order.created_at', $start]);
        $repairOrderModel->andFilterWhere(['<', 'repair_order.created_at', $end]);
        
        $repairOrderModel->andFilterWhere(['<', "address.latitude", $north_east[0]]);
        $repairOrderModel->andFilterWhere(['>', "address.latitude", $south_west[0]]);

        $repairOrderModel->andFilterWhere(['<', "address.longitude", $north_east[1]]);
        $repairOrderModel->andFilterWhere(['>', "address.longitude", $south_west[1]]);
        $repairOrderModel->groupBy('latlng');
        $repairOrderModel->asArray();
        $olist = $repairOrderModel->all();


        $item = array('max' => 100, 'data' => array());
        if ($olist) {
            foreach ($olist as $k => $v) {
                $latlngTemp = explode('_', $v['latlng']);
                $item['data'][] = array('lat' => $latlngTemp[0], 'lng' => $latlngTemp[1], 'count' => $v['mycount']);
            }
        }
        $e->item =$item;
        return $this->renderJson($e);
    }

    // 获取通知
    public function actionGetNotice()
    {
        $act = $this->request->post('act');
        if ($act == 'remove') {
            $id = $this->request->post('id');
            return $this->removeNotice($id);
        }
        $e = new stdClass();
        $e->success = false;

        $e->count = 0;

        $noticeInfoModel = NoticeInfoModel::find();
        $noticeInfoModel->where(['status' => NoticeInfoModel::STATUS_UNREAD]);

        $noticeInfoModel->asArray();
        $count = $noticeInfoModel->count();
        $noticeInfoModel->limit(6);
        $nList = $noticeInfoModel->all();
        $e->msgList = array();
        if ($nList) {
            $tt = time();
            foreach ($nList as $k => $v) {
                $v['created_date_time'] = date('Y-m-d H:i:s', $v['created_at']);
                $v['created_time'] = $this->getTime($v['created_at'], $tt);
                $nList[$k] = $v;
            }
            $e->count = $count;
            $e->msgList = $nList;
            $e->success = true;
        }

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

    // 删除
    protected function removeNotice($id)
    {
        $e= new stdClass();
        $e->success = false;
        $e->error = 'Fail';
        $noticeInfo = NoticeInfoModel::findOne($id);
        if ($noticeInfo && $noticeInfo->delete()) {
            $e->success = true;
        }

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

    // 判断时间
    protected function getTime($t, $curT)
    {
        $timeDistance = $curT - $t;
        if ($timeDistance < 60) {
            $str = '刚刚';
        } elseif ($timeDistance >= 60 && $timeDistance < 3600) {
            $str = intval($timeDistance / 60) . '分钟前';
        } elseif ($timeDistance >= 3600 && $timeDistance < 3600 * 24) {
            $str = intval($timeDistance /(60 * 60)) . '小时前';
        } else {
            $str = intval($timeDistance /(60 * 60 * 24)) . '天前';
        }

        return $str;
    }

    // 组装日期列表
    private function genDate($start)
    {
        $dataArr = array();
        $days = 7;
        for ($i = 0; $i < $days; $i++) {
            $dataArr[date('Y-m-d', $start)] = array();
            $start = $start + (3600 * 24);
        }

        return $dataArr;
    }

    // 贴码设备
    protected function getDeviceReview()
    {
        $bindDeviceModel = BindDeviceApplyModel::find();
        $wh = array('status' => BindDeviceApplyModel::STATUS_APPLYING);
        $bindDeviceModel->where($wh);
        $count = $bindDeviceModel->count();

        $url = Url::toRoute('/device/userdevice/apply?status=0');
        $returnArr = array(0, $url);
        if ($count > 0) {
            $returnArr = array($count, $url);
        }

        return $returnArr;
    }

    // 质保申请
    protected function getAppeal()
    {
        $repairOrderAppealModel = RepairOrderAppealModel::find();
        $wh = array('status' => RepairOrderAppealModel::STATUS_APPEALING);
        $repairOrderAppealModel->where($wh);
        $count = $repairOrderAppealModel->count();
        $url = Url::toRoute('/trade/appeal/index');
        $returnArr = array(0, $url);
        if ($count > 0) {
            $returnArr = array($count, $url);
        }

        return $returnArr;
    }

    // 工程师认证
    protected function getEngineerAuth($status = EngineerStatus::STATUS_SKILL_AUTH)
    {
        $engineerModel = EngineerModel::find();
        $wh = array('status' => $status, 'is_fake_id' => 0);
        $engineerModel->where($wh);
        $count = $engineerModel->count();
        $url = Url::toRoute('/engineer/engineer/index?status=2');
        $returnArr = array(0, $url);
        if ($count > 0) {
            $returnArr = array($count, $url);
        }

        return $returnArr;
    }

    // 技能审核,这里不能以人为单位,应该以技能为单位,一个人可以多项技能认证
    protected function getEngineerSkillAuth()
    {
        $engineerSkillModel = EngineerSkillsModel::find();
        $wh = array('engineer_skills.status' => EngineerSkillsModel::STATUS_REVIEWING);
        $engineerSkillModel->select(["engineer_skills.engineer_id"]);
        $engineerSkillModel->distinct();
        $engineerSkillModel->where($wh);

        $count = $engineerSkillModel->count();
        $url = Url::toRoute('/engineer/engineer/index?status=6');
        $returnArr = array(0, $url);
        if ($count > 0) {
            $returnArr = array($count, $url);
        }

        return $returnArr;
    }

    // 提现审核
    protected function getWithdraw()
    {
        $count = EngineerWithdrawRepository::getReviewingWithdrawCount();
        $url = Url::toRoute('/engineer/withdraw/index');
        $returnArr = array(0, $url);
        if ($count > 0) {
            $returnArr = array($count, $url);
        }

        return $returnArr;
    }

    // 发票出错
    protected function getErrorInvoice($kplx = InvoiceApi::KPLX_NORMAL)
    {
        $invoiceModel = InvoiceModel::find();
        $wh = array('status' => InvoiceModel::STATUS_ERROR, 'kplx' => $kplx);
        $invoiceModel->where($wh);
        $count = $invoiceModel->count();
        $url = Url::toRoute(['/invoice/invoice/index', 'invoice_status' => InvoiceModel::STATUS_ERROR]);
        $returnArr = array(0, $url);
        if ($count > 0) {
            $returnArr = array($count, $url);
        }

        return $returnArr;
    }

    /*  订单数据  */
    //获取订单数量(当日和七天,已支付,未支付)
    protected function getOrder($start, $end, $selectType = 0, $status = 0)
    {
        $orderModel = RepairOrderModel::find();
        if ($status != 0) {
            $orderModel->where(['status' => $status]);
        }

        if ($selectType == 0) {
            $orderModel->select(['FROM_UNIXTIME(`created_at`, "%Y-%m-%d") as mydate','sum(pay_price) as pay_price','sum(repair_price) as repair_price']);
        } else {
            $orderModel->select(['FROM_UNIXTIME(`created_at`, "%Y-%m-%d") as mydate','count(*) as cc']);
        }

        $orderModel->andFilterWhere(['>', 'created_at', $start]);
        $orderModel->andFilterWhere(['<', 'created_at', $end]);
        $orderModel->groupBy('mydate');
        $orderModel->asArray();
        $oList = $orderModel->all();

        return $oList;
    }
}