SendOrderController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace backend\controllers;
  3. use common\models\MaterType;
  4. use common\models\SendOrder;
  5. use common\models\SendOrderLog;
  6. use common\models\UserCompany;
  7. use yii;
  8. use yii\filters\AccessControl;
  9. /**
  10. * 派单管理
  11. * @package backend\controllers
  12. */
  13. class SendOrderController extends BaseController
  14. {
  15. public $layout = 'iframe';
  16. const PAGESIZE = 20;//分页数
  17. public function behaviors()
  18. {
  19. return [
  20. 'access' => [
  21. 'class' => AccessControl::className(),
  22. 'rules' => [
  23. [
  24. 'actions' => [],
  25. 'allow' => true,
  26. 'roles' => ['@'],
  27. ],
  28. ],
  29. ],
  30. ];
  31. }
  32. /**
  33. * 列表
  34. * @return string
  35. */
  36. public function actionIndex()
  37. {
  38. $search =['city'=>'','source'=>'','status'=>'','name'=>''];
  39. $search['city'] = Yii::$app->request->get('city');
  40. $search['source'] = Yii::$app->request->get('source');
  41. $search['status'] = Yii::$app->request->get('status');
  42. $search['name'] = Yii::$app->request->get('name');
  43. $typeList = MaterType::find()->select('id,name')->all();
  44. $models = SendOrder::find();
  45. if(!empty($search['city']))
  46. $models->andWhere("city = :city",[':city'=>$search['city']]);
  47. if(!empty($search['source']))
  48. $models->andWhere("source = :source",[':source'=>$search['source']]);
  49. if(!empty($search['status']))
  50. $models->andWhere("status = :status",[':status'=>$search['status']]);
  51. if(!empty($search['name']))
  52. $models->andWhere("name like :name OR house_name like :name ",[':name'=>"%".$search['name']."%"]);
  53. $pages = new yii\data\Pagination(["totalCount" => $models->count(), "pageSize" => self::PAGESIZE]);
  54. $models = $models->offset($pages->offset)->limit($pages->limit)->orderBy("id DESC")->all();
  55. return $this->render('index', ['models' => $models, 'pages' => $pages,'name'=>'派单管理','search'=>$search,'typeList'=>$typeList]);
  56. }
  57. /**
  58. * 新增修改
  59. */
  60. public function actionAdd()
  61. {
  62. if(Yii::$app->request->isPost)
  63. {
  64. $id = Yii::$app->request->post('id');
  65. if(!empty($id)){
  66. $model = SendOrder::find()->where(['id'=>$id])->one();
  67. }
  68. else{
  69. $model = new SendOrder();
  70. $model->status = SendOrder::STATUS_NO;
  71. }
  72. $model->load(Yii::$app->request->post());
  73. if($model->save()&&$model->validate())
  74. {
  75. Yii::$app->getSession()->setFlash('success', '操作成功');
  76. return $this->redirect(['send-order/index']);
  77. }else{
  78. Yii::$app->getSession()->setFlash('error', '操作失败');
  79. return $this->redirect(Yii::$app->request->referrer);
  80. }
  81. }
  82. $id = Yii::$app->request->get('id');
  83. if(!empty($id))
  84. {
  85. $model= SendOrder::find()->where(['id'=>$id])->one();
  86. $name = "编辑";
  87. }else{
  88. $model =new SendOrder();
  89. $name = "新增";
  90. }
  91. return $this->render('add',['model'=>$model,'name'=>$name]);
  92. }
  93. /**
  94. * 装修公司列表
  95. */
  96. public function actionCompanyList()
  97. {
  98. $id = Yii::$app->request->get('id');
  99. $city = SendOrder::findOne($id)->getAttribute("city");
  100. $search =['name'=>''];
  101. $search['name'] = Yii::$app->request->get('name');
  102. //已经派单的装修公司
  103. $logList = SendOrderLog::find()->where('send_id = :id AND status != :status ',[':id'=>$id,':status'=>SendOrderLog::STATUS_CANCEL])->select('uid')->asArray()->all();
  104. $uids = "";
  105. if(!empty($logList))
  106. {
  107. end($logList);
  108. $key_last = key($logList);
  109. foreach ($logList as $logKey=>$logVal)
  110. {
  111. $uids .= $logVal['uid'].",";
  112. if($logKey == $key_last)
  113. {
  114. $uids = rtrim($uids,",");
  115. }
  116. }
  117. }
  118. $models = UserCompany::find()->where(['city'=>$city,'member'=>UserCompany::ISMEMBER]);
  119. if(!empty($search['name']))
  120. $models->andWhere("company like :name ",[':name'=>"%".$search['name']."%"]);
  121. if(!empty($uids))
  122. $models->andWhere("uid not in ({$uids})");
  123. $pages = new yii\data\Pagination(["totalCount" => $models->count(), "pageSize" => self::PAGESIZE]);
  124. $models = $models->offset($pages->offset)->limit($pages->limit)->orderBy("id DESC")->all();
  125. return $this->render('companylist', ['models' => $models, 'pages' => $pages,'name'=>'派单','search'=>$search,'id'=>$id]);
  126. }
  127. /**
  128. * 派单操作
  129. * @return string
  130. */
  131. public function actionSend()
  132. {
  133. if(Yii::$app->request->isPost)
  134. {
  135. $result= ['sign'=>1,'msg'=>'派单成功'];
  136. $id = Yii::$app->request->post('id');
  137. $uids = Yii::$app->request->post('uids');
  138. if(empty($id) || empty($uids)){
  139. $result = ['sign'=>0,'msg'=>'缺少参数'];
  140. return json_encode($result);
  141. }
  142. $sendOrder = SendOrder::find()->where(['id'=>$id])->one();
  143. if(empty($sendOrder))
  144. {
  145. $result = ['sign'=>0,'msg'=>'找不到订单'];
  146. return json_encode($result);
  147. }
  148. $uidArr = explode(",",$uids);
  149. if(empty($uidArr) || !is_array($uidArr))
  150. {
  151. $result = ['sign'=>0,'msg'=>'非法操作'];
  152. return json_encode($result);
  153. }
  154. foreach ($uidArr as $value)
  155. {
  156. if(!empty($value))
  157. {
  158. $model = new SendOrderLog();
  159. $model->send_id = $sendOrder->id;
  160. $model->uid = $value;
  161. $model->c_time = time();
  162. $model->u_time = time();
  163. $model->handle = SendOrderLog::HANDLE_NO;
  164. $model->status = SendOrderLog::STATUS_SEND;
  165. if(!$model->save())
  166. {
  167. $result = ['sign'=>0,'msg'=>'派单失败'];
  168. return json_encode($result);
  169. }
  170. }
  171. }
  172. if($sendOrder->status == SendOrder::STATUS_NO)
  173. {
  174. $sendOrder->status = SendOrder::STATUS_YES;
  175. $sendOrder->save();
  176. $result['status']="已派单";
  177. }
  178. return json_encode($result);
  179. }
  180. }
  181. }