| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- namespace app\modules\admin\models\q_and_a;
- use app\models\QAndACat;
- use app\models\QAndAQuestionTemplate;
- use yii\base\Model;
- class QuestionTemplateForm extends Model
- {
- public $id;
- public $name;
- public $cat_id;
- public $state;
- public $question;
- public $store_id;
- public $is_hot;
- public $dateTime;
- /**
- * @return array
- */
- public function rules()
- {
- return [
- [['name', 'cat_id', 'question', 'is_hot', 'state'], 'required'],
- [['id', 'cat_id', 'is_hot', 'state'], 'integer'],
- [['is_hot'], 'default', 'value' => 0],
- [['name', 'question', 'dateTime'], 'string', 'max' => 255],
- ];
- }
- public function attributeLabels()
- {
- return [
- 'name' => '名称'
- ];
- }
- public function search()
- {
- $query = QAndAQuestionTemplate::find()->alias('ro')->where([
- 'ro.is_delete' => 0,
- 'ro.store_id' => $this->store_id,
- ])->leftJoin(['c' => QAndACat::tableName()], 'ro.cat_id = c.id')
- ->orderBy('ro.id DESC');
- if ($this->state >= 0) {
- $query->andWhere(['ro.state' => $this->state]);
- }
- if ($this->cat_id > 0) {
- $query->andWhere(['ro.cat_id' => $this->cat_id]);
- }
- if ($this->name) {
- $query->andWhere(['like', 'ro.name', $this->name]);
- }
- if ($this->dateTime) {
- $query->andWhere(['>=', 'ro.created_at', strtotime($this->dateTime[0])]);
- $query->andWhere(['<=', 'ro.created_at', strtotime($this->dateTime[1])]);
- }
- $query->select('ro.*, c.name cat_name');
- $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 = QAndAQuestionTemplate::findOne($this->id) ?: new QAndAQuestionTemplate();
- $cloud->store_id = get_store_id();
- $cloud->cat_id = $this->cat_id;
- $cloud->name = $this->name;
- $cloud->question = $this->question;
- $cloud->is_hot = $this->is_hot;
- $cloud->state = $this->state;
- if ($cloud->save()) {
- return [
- 'code' => 0,
- 'msg' => '保存成功'
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => '保存失败',
- 'err' => $cloud->errors
- ];
- }
- }
- }
|