| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
- /**
- * MdGroupDriverForm.php
- * todo 文件描述
- * Created on 2024/3/22 13:40
- * @author: hankaige
- */
- namespace app\modules\admin\models;
- use app\models\MdGroupDriver;
- use yii\base\Model;
- class MdGroupDriverForm extends Model
- {
- public $id;
- public $store_id;
- public $code;
- public $name;
- public $mobile;
- public $status = -1;
- public $start_time;
- public $end_time;
- public $keywords;
- public function rules()
- {
- return [
- [
- [
- 'id',
- 'store_id',
- 'code',
- 'name',
- 'mobile'
- ],
- 'required',
- 'on' => ['save']
- ],
- [
- [
- 'code',
- 'name',
- 'mobile'
- ],
- 'string'
- ],
- [
- [
- 'status',
- 'store_id'
- ],
- 'integer'
- ],
- [
- ['id'],
- 'required',
- 'on' => [
- 'item',
- 'del'
- ]
- ],
- [
- ['id'],
- 'integer',
- 'on' => [
- 'item'
- ]
- ],
- [
- ['id'],
- 'isArray',
- 'on' => [
- 'del'
- ]
- ],
- [['start_time','end_time'],'string']
- ];
- }
- /**
- * 自定义数组验证规则
- * @param $attribute
- * @param $params
- * @author: hankaige
- * @Time: 2024/3/22 15:53
- */
- public function isArray($attribute, $params)
- {
- if(!is_array($this->$attribute) || count($this->$attribute) <= 0){
- $this->addError($attribute,'删除数据为空');
- }
- }
- public function getList()
- {
- $query = MdGroupDriver::find()->where([
- 'store_id' => $this->store_id,
- 'is_delete' => 0,
- ]);
- if($this->status > 0){
- $query->andWhere(['status'=>$this->status]);
- }
- if(!empty($this->keywords)) {
- $query->andWhere(['or',['like', 'name', $this->keywords],['like', 'code', $this->keywords],['like', 'mobile', $this->keywords]]);
- }
- if (!empty($this->start_time)) {
- $query->andWhere([
- '>=',
- 'created_at',
- strtotime($this->start_time)
- ]);
- }
- if (!empty($this->end_time)) {
- $query->andWhere([
- '<=',
- 'created_at',
- strtotime($this->end_time)
- ]);
- }
- $result = pagination_make($query, TRUE, 'created_at');
- foreach ($result['list'] as &$item) {
- $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
- $item['status'] = (int)$item['status'];
- }
- return ['code'=>0,'data'=>$result];
- }
- public function createItem()
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(FALSE)[0]
- ];
- }
- $model = MdGroupDriver::findOne($this->id);
- if (empty($model)) {
- $model = new MdGroupDriver();
- }
- if(MdGroupDriver::find()->where(['code'=>$this->code])->andWhere(['!=','id',$this->id])->count() > 0){
- return ['code'=>1,'msg'=>'司机编号已存在'];
- }
- $model->store_id = $this->store_id;
- $model->code = $this->code;
- $model->name = $this->name;
- $model->mobile = $this->mobile;
- $model->status = $this->status;
- if ($model->save()) {
- return [
- 'code' => 0,
- 'msg' => '操作成功'
- ];
- }
- return [
- 'code' => 1,
- 'msg' => $model->getErrors()
- ];
- }
- public function delItem()
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(FALSE)[0]
- ];
- }
- $res = MdGroupDriver::updateAll(['is_delete'=>1],['id'=>$this->id,'store_id'=>$this->store_id]);
- if ($res > 0) {
- return [
- 'code'=>0,
- 'msg' => '删除成功'
- ];
- }
- return [
- 'code' => 1,
- 'msg' => '操作失败'
- ];
- }
- }
|