CatForm.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace app\modules\admin\models\q_and_a;
  3. use app\models\QAndACat;
  4. use yii\base\Model;
  5. class CatForm extends Model
  6. {
  7. public $id;
  8. public $name;
  9. public $sort;
  10. public $state;
  11. public $type;
  12. public $store_id;
  13. public $dateTime;
  14. /**
  15. * @return array
  16. */
  17. public function rules()
  18. {
  19. return [
  20. [['name', 'sort','state'], 'required'],
  21. [['id', 'type'], 'integer'],
  22. [['name', 'dateTime'], 'string', 'max' => 255],
  23. ];
  24. }
  25. public function attributeLabels()
  26. {
  27. return [
  28. 'name' => '名称'
  29. ];
  30. }
  31. public function search()
  32. {
  33. $query = QAndACat::find()->alias('ro')->where([
  34. 'ro.is_delete' => 0,
  35. 'ro.store_id' => $this->store_id,
  36. ])->orderBy('ro.id DESC');
  37. if ($this->name) {
  38. $query->andWhere(['like', 'ro.name', $this->name]);
  39. }
  40. if ($this->state >= 0) {
  41. $query->andWhere(['ro.state' => $this->state]);
  42. }
  43. if ($this->dateTime) {
  44. $query->andWhere(['>=', 'ro.created_at', strtotime($this->dateTime[0])]);
  45. $query->andWhere(['<=', 'ro.created_at', strtotime($this->dateTime[1])]);
  46. }
  47. if ($this->type) {
  48. $data = [
  49. 'list' => $query->asArray()->all(),
  50. 'pageNo' => 0,
  51. 'totalCount' => 0
  52. ];
  53. } else {
  54. $data = pagination_make($query);
  55. }
  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 = QAndACat::findOne($this->id) ?: new QAndACat();
  78. $cloud->store_id = get_store_id();
  79. $cloud->name = $this->name;
  80. $cloud->state = $this->state;
  81. $cloud->sort = $this->sort;
  82. if ($cloud->save()) {
  83. return [
  84. 'code' => 0,
  85. 'msg' => '保存成功'
  86. ];
  87. } else {
  88. return [
  89. 'code' => 1,
  90. 'msg' => '保存失败',
  91. 'err' => $cloud->errors
  92. ];
  93. }
  94. }
  95. }