MdGroupDriverForm.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * MdGroupDriverForm.php
  4. * todo 文件描述
  5. * Created on 2024/3/22 13:40
  6. * @author: hankaige
  7. */
  8. namespace app\modules\admin\models;
  9. use app\models\MdGroupDriver;
  10. use yii\base\Model;
  11. class MdGroupDriverForm extends Model
  12. {
  13. public $id;
  14. public $store_id;
  15. public $code;
  16. public $name;
  17. public $mobile;
  18. public $status = -1;
  19. public $start_time;
  20. public $end_time;
  21. public $keywords;
  22. public function rules()
  23. {
  24. return [
  25. [
  26. [
  27. 'id',
  28. 'store_id',
  29. 'code',
  30. 'name',
  31. 'mobile'
  32. ],
  33. 'required',
  34. 'on' => ['save']
  35. ],
  36. [
  37. [
  38. 'code',
  39. 'name',
  40. 'mobile'
  41. ],
  42. 'string'
  43. ],
  44. [
  45. [
  46. 'status',
  47. 'store_id'
  48. ],
  49. 'integer'
  50. ],
  51. [
  52. ['id'],
  53. 'required',
  54. 'on' => [
  55. 'item',
  56. 'del'
  57. ]
  58. ],
  59. [
  60. ['id'],
  61. 'integer',
  62. 'on' => [
  63. 'item'
  64. ]
  65. ],
  66. [
  67. ['id'],
  68. 'isArray',
  69. 'on' => [
  70. 'del'
  71. ]
  72. ],
  73. [['start_time','end_time'],'string']
  74. ];
  75. }
  76. /**
  77. * 自定义数组验证规则
  78. * @param $attribute
  79. * @param $params
  80. * @author: hankaige
  81. * @Time: 2024/3/22 15:53
  82. */
  83. public function isArray($attribute, $params)
  84. {
  85. if(!is_array($this->$attribute) || count($this->$attribute) <= 0){
  86. $this->addError($attribute,'删除数据为空');
  87. }
  88. }
  89. public function getList()
  90. {
  91. $query = MdGroupDriver::find()->where([
  92. 'store_id' => $this->store_id,
  93. 'is_delete' => 0,
  94. ]);
  95. if($this->status > 0){
  96. $query->andWhere(['status'=>$this->status]);
  97. }
  98. if(!empty($this->keywords)) {
  99. $query->andWhere(['or',['like', 'name', $this->keywords],['like', 'code', $this->keywords],['like', 'mobile', $this->keywords]]);
  100. }
  101. if (!empty($this->start_time)) {
  102. $query->andWhere([
  103. '>=',
  104. 'created_at',
  105. strtotime($this->start_time)
  106. ]);
  107. }
  108. if (!empty($this->end_time)) {
  109. $query->andWhere([
  110. '<=',
  111. 'created_at',
  112. strtotime($this->end_time)
  113. ]);
  114. }
  115. $result = pagination_make($query, TRUE, 'created_at');
  116. foreach ($result['list'] as &$item) {
  117. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  118. $item['status'] = (int)$item['status'];
  119. }
  120. return ['code'=>0,'data'=>$result];
  121. }
  122. public function createItem()
  123. {
  124. if (!$this->validate()) {
  125. return [
  126. 'code' => 1,
  127. 'msg' => $this->getErrorSummary(FALSE)[0]
  128. ];
  129. }
  130. $model = MdGroupDriver::findOne($this->id);
  131. if (empty($model)) {
  132. $model = new MdGroupDriver();
  133. }
  134. if(MdGroupDriver::find()->where(['code'=>$this->code])->andWhere(['!=','id',$this->id])->count() > 0){
  135. return ['code'=>1,'msg'=>'司机编号已存在'];
  136. }
  137. $model->store_id = $this->store_id;
  138. $model->code = $this->code;
  139. $model->name = $this->name;
  140. $model->mobile = $this->mobile;
  141. $model->status = $this->status;
  142. if ($model->save()) {
  143. return [
  144. 'code' => 0,
  145. 'msg' => '操作成功'
  146. ];
  147. }
  148. return [
  149. 'code' => 1,
  150. 'msg' => $model->getErrors()
  151. ];
  152. }
  153. public function delItem()
  154. {
  155. if (!$this->validate()) {
  156. return [
  157. 'code' => 1,
  158. 'msg' => $this->getErrorSummary(FALSE)[0]
  159. ];
  160. }
  161. $res = MdGroupDriver::updateAll(['is_delete'=>1],['id'=>$this->id,'store_id'=>$this->store_id]);
  162. if ($res > 0) {
  163. return [
  164. 'code'=>0,
  165. 'msg' => '删除成功'
  166. ];
  167. }
  168. return [
  169. 'code' => 1,
  170. 'msg' => '操作失败'
  171. ];
  172. }
  173. }