123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- <?php
- namespace backend\controllers;
- use common\models\MaterType;
- use common\models\SendOrder;
- use common\models\SendOrderLog;
- use common\models\UserCompany;
- use yii;
- use yii\filters\AccessControl;
- /**
- * 派单管理
- * @package backend\controllers
- */
- class SendOrderController extends BaseController
- {
- public $layout = 'iframe';
- const PAGESIZE = 20;//分页数
- public function behaviors()
- {
- return [
- 'access' => [
- 'class' => AccessControl::className(),
- 'rules' => [
- [
- 'actions' => [],
- 'allow' => true,
- 'roles' => ['@'],
- ],
- ],
- ],
- ];
- }
- /**
- * 列表
- * @return string
- */
- public function actionIndex()
- {
- $search =['city'=>'','source'=>'','status'=>'','name'=>''];
- $search['city'] = Yii::$app->request->get('city');
- $search['source'] = Yii::$app->request->get('source');
- $search['status'] = Yii::$app->request->get('status');
- $search['name'] = Yii::$app->request->get('name');
- $typeList = MaterType::find()->select('id,name')->all();
- $models = SendOrder::find();
- if(!empty($search['city']))
- $models->andWhere("city = :city",[':city'=>$search['city']]);
- if(!empty($search['source']))
- $models->andWhere("source = :source",[':source'=>$search['source']]);
- if(!empty($search['status']))
- $models->andWhere("status = :status",[':status'=>$search['status']]);
- if(!empty($search['name']))
- $models->andWhere("name like :name OR house_name like :name ",[':name'=>"%".$search['name']."%"]);
- $pages = new yii\data\Pagination(["totalCount" => $models->count(), "pageSize" => self::PAGESIZE]);
- $models = $models->offset($pages->offset)->limit($pages->limit)->orderBy("id DESC")->all();
- return $this->render('index', ['models' => $models, 'pages' => $pages,'name'=>'派单管理','search'=>$search,'typeList'=>$typeList]);
- }
- /**
- * 新增修改
- */
- public function actionAdd()
- {
- if(Yii::$app->request->isPost)
- {
- $id = Yii::$app->request->post('id');
- if(!empty($id)){
- $model = SendOrder::find()->where(['id'=>$id])->one();
- }
- else{
- $model = new SendOrder();
- $model->status = SendOrder::STATUS_NO;
- }
- $model->load(Yii::$app->request->post());
- if($model->save()&&$model->validate())
- {
- Yii::$app->getSession()->setFlash('success', '操作成功');
- return $this->redirect(['send-order/index']);
- }else{
- Yii::$app->getSession()->setFlash('error', '操作失败');
- return $this->redirect(Yii::$app->request->referrer);
- }
- }
- $id = Yii::$app->request->get('id');
- if(!empty($id))
- {
- $model= SendOrder::find()->where(['id'=>$id])->one();
- $name = "编辑";
- }else{
- $model =new SendOrder();
- $name = "新增";
- }
- return $this->render('add',['model'=>$model,'name'=>$name]);
- }
- /**
- * 装修公司列表
- */
- public function actionCompanyList()
- {
- $id = Yii::$app->request->get('id');
- $city = SendOrder::findOne($id)->getAttribute("city");
- $search =['name'=>''];
- $search['name'] = Yii::$app->request->get('name');
- //已经派单的装修公司
- $logList = SendOrderLog::find()->where('send_id = :id AND status != :status ',[':id'=>$id,':status'=>SendOrderLog::STATUS_CANCEL])->select('uid')->asArray()->all();
- $uids = "";
- if(!empty($logList))
- {
- end($logList);
- $key_last = key($logList);
- foreach ($logList as $logKey=>$logVal)
- {
- $uids .= $logVal['uid'].",";
- if($logKey == $key_last)
- {
- $uids = rtrim($uids,",");
- }
- }
- }
- $models = UserCompany::find()->where(['city'=>$city,'member'=>UserCompany::ISMEMBER]);
- if(!empty($search['name']))
- $models->andWhere("company like :name ",[':name'=>"%".$search['name']."%"]);
- if(!empty($uids))
- $models->andWhere("uid not in ({$uids})");
- $pages = new yii\data\Pagination(["totalCount" => $models->count(), "pageSize" => self::PAGESIZE]);
- $models = $models->offset($pages->offset)->limit($pages->limit)->orderBy("id DESC")->all();
- return $this->render('companylist', ['models' => $models, 'pages' => $pages,'name'=>'派单','search'=>$search,'id'=>$id]);
- }
- /**
- * 派单操作
- * @return string
- */
- public function actionSend()
- {
- if(Yii::$app->request->isPost)
- {
- $result= ['sign'=>1,'msg'=>'派单成功'];
- $id = Yii::$app->request->post('id');
- $uids = Yii::$app->request->post('uids');
- if(empty($id) || empty($uids)){
- $result = ['sign'=>0,'msg'=>'缺少参数'];
- return json_encode($result);
- }
- $sendOrder = SendOrder::find()->where(['id'=>$id])->one();
- if(empty($sendOrder))
- {
- $result = ['sign'=>0,'msg'=>'找不到订单'];
- return json_encode($result);
- }
- $uidArr = explode(",",$uids);
- if(empty($uidArr) || !is_array($uidArr))
- {
- $result = ['sign'=>0,'msg'=>'非法操作'];
- return json_encode($result);
- }
- foreach ($uidArr as $value)
- {
- if(!empty($value))
- {
- $model = new SendOrderLog();
- $model->send_id = $sendOrder->id;
- $model->uid = $value;
- $model->c_time = time();
- $model->u_time = time();
- $model->handle = SendOrderLog::HANDLE_NO;
- $model->status = SendOrderLog::STATUS_SEND;
- if(!$model->save())
- {
- $result = ['sign'=>0,'msg'=>'派单失败'];
- return json_encode($result);
- }
- }
- }
- if($sendOrder->status == SendOrder::STATUS_NO)
- {
- $sendOrder->status = SendOrder::STATUS_YES;
- $sendOrder->save();
- $result['status']="已派单";
- }
- return json_encode($result);
- }
- }
- }
|