| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?php
- namespace app\modules\admin\models\q_and_a;
- use app\models\QAndACat;
- use yii\base\Model;
- class CatForm extends Model
- {
- public $id;
- public $name;
- public $sort;
- public $state;
- public $type;
- public $store_id;
- public $dateTime;
- /**
- * @return array
- */
- public function rules()
- {
- return [
- [['name', 'sort','state'], 'required'],
- [['id', 'type'], 'integer'],
- [['name', 'dateTime'], 'string', 'max' => 255],
- ];
- }
- public function attributeLabels()
- {
- return [
- 'name' => '名称'
- ];
- }
- public function search()
- {
- $query = QAndACat::find()->alias('ro')->where([
- 'ro.is_delete' => 0,
- 'ro.store_id' => $this->store_id,
- ])->orderBy('ro.id DESC');
- if ($this->name) {
- $query->andWhere(['like', 'ro.name', $this->name]);
- }
- if ($this->state >= 0) {
- $query->andWhere(['ro.state' => $this->state]);
- }
- if ($this->dateTime) {
- $query->andWhere(['>=', 'ro.created_at', strtotime($this->dateTime[0])]);
- $query->andWhere(['<=', 'ro.created_at', strtotime($this->dateTime[1])]);
- }
- if ($this->type) {
- $data = [
- 'list' => $query->asArray()->all(),
- 'pageNo' => 0,
- 'totalCount' => 0
- ];
- } else {
- $data = pagination_make($query);
- }
- foreach ($data['list'] as &$item) {
- $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
- }
- return [
- 'code' => 0,
- 'data' => [
- 'data' => $data['list'],
- 'pageNo' => $data['pageNo'],
- 'totalCount' => $data['totalCount']
- ],
- 'sql' => $query->createCommand()->getRawSql(),
- ];
- }
- public function save()
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- $cloud = QAndACat::findOne($this->id) ?: new QAndACat();
- $cloud->store_id = get_store_id();
- $cloud->name = $this->name;
- $cloud->state = $this->state;
- $cloud->sort = $this->sort;
- if ($cloud->save()) {
- return [
- 'code' => 0,
- 'msg' => '保存成功'
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => '保存失败',
- 'err' => $cloud->errors
- ];
- }
- }
- }
|