QuestionTemplateForm.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace app\modules\admin\models\q_and_a;
  3. use app\models\QAndACat;
  4. use app\models\QAndAQuestionTemplate;
  5. use yii\base\Model;
  6. class QuestionTemplateForm extends Model
  7. {
  8. public $id;
  9. public $name;
  10. public $cat_id;
  11. public $state;
  12. public $question;
  13. public $store_id;
  14. public $is_hot;
  15. public $dateTime;
  16. /**
  17. * @return array
  18. */
  19. public function rules()
  20. {
  21. return [
  22. [['name', 'cat_id', 'question', 'is_hot', 'state'], 'required'],
  23. [['id', 'cat_id', 'is_hot', 'state'], 'integer'],
  24. [['is_hot'], 'default', 'value' => 0],
  25. [['name', 'question', 'dateTime'], 'string', 'max' => 255],
  26. ];
  27. }
  28. public function attributeLabels()
  29. {
  30. return [
  31. 'name' => '名称'
  32. ];
  33. }
  34. public function search()
  35. {
  36. $query = QAndAQuestionTemplate::find()->alias('ro')->where([
  37. 'ro.is_delete' => 0,
  38. 'ro.store_id' => $this->store_id,
  39. ])->leftJoin(['c' => QAndACat::tableName()], 'ro.cat_id = c.id')
  40. ->orderBy('ro.id DESC');
  41. if ($this->state >= 0) {
  42. $query->andWhere(['ro.state' => $this->state]);
  43. }
  44. if ($this->cat_id > 0) {
  45. $query->andWhere(['ro.cat_id' => $this->cat_id]);
  46. }
  47. if ($this->name) {
  48. $query->andWhere(['like', 'ro.name', $this->name]);
  49. }
  50. if ($this->dateTime) {
  51. $query->andWhere(['>=', 'ro.created_at', strtotime($this->dateTime[0])]);
  52. $query->andWhere(['<=', 'ro.created_at', strtotime($this->dateTime[1])]);
  53. }
  54. $query->select('ro.*, c.name cat_name');
  55. $data = pagination_make($query);
  56. foreach ($data['list'] as &$item) {
  57. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  58. }
  59. return [
  60. 'code' => 0,
  61. 'data' => [
  62. 'data' => $data['list'],
  63. 'pageNo' => $data['pageNo'],
  64. 'totalCount' => $data['totalCount']
  65. ],
  66. 'sql' => $query->createCommand()->getRawSql(),
  67. ];
  68. }
  69. public function save()
  70. {
  71. if (!$this->validate()) {
  72. return [
  73. 'code' => 1,
  74. 'msg' => $this->getErrorSummary(false)[0],
  75. ];
  76. }
  77. $cloud = QAndAQuestionTemplate::findOne($this->id) ?: new QAndAQuestionTemplate();
  78. $cloud->store_id = get_store_id();
  79. $cloud->cat_id = $this->cat_id;
  80. $cloud->name = $this->name;
  81. $cloud->question = $this->question;
  82. $cloud->is_hot = $this->is_hot;
  83. $cloud->state = $this->state;
  84. if ($cloud->save()) {
  85. return [
  86. 'code' => 0,
  87. 'msg' => '保存成功'
  88. ];
  89. } else {
  90. return [
  91. 'code' => 1,
  92. 'msg' => '保存失败',
  93. 'err' => $cloud->errors
  94. ];
  95. }
  96. }
  97. }