| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- <?php
- namespace app\modules\admin\models;
- use app\models\GoodsBrandCat;
- use yii\base\Model;
- class GoodsBrandCatForm extends Model
- {
- public $id;
- public $store_id;
- public $status;
- public $sort;
- public $brand_cat_name;
- public $start_time;
- public $end_time;
- public $ids;
- public function rules()
- {
- return [
- [['id', 'store_id', 'status', 'sort'], 'integer'],
- [['brand_cat_name'], 'trim'],
- [['brand_cat_name', 'start_time', 'end_time', 'ids'], 'string'],
- [['sort'], 'integer', 'min' => 1],
- ['status', 'in', 'range' => [GoodsBrandCat::STATUS_CLOSE, GoodsBrandCat::STATUS_OPEN]],
- ];
- }
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'store_id' => 'Store ID',
- 'status' => '状态',
- 'sort' => '排序',
- 'brand_cat_name' => '品牌分类名称',
- 'start_time' => '开始时间',
- 'end_time' => '结束时间',
- ];
- }
- //品牌分类列表
- public function list() {
- try {
- $store_id = $this->store_id;
- $status = $this->status;
- $brand_cat_name = $this->brand_cat_name;
- $start_time = $this->start_time;
- $end_time = $this->end_time;
- $query = GoodsBrandCat::find()->where(['store_id' => $store_id, 'is_delete' => 0]);
- if (isset($status) && in_array($status, [GoodsBrandCat::STATUS_CLOSE, GoodsBrandCat::STATUS_OPEN])) {
- $query->andWhere(['status' => $status]);
- }
- if (!empty($brand_cat_name)) {
- $query->andWhere(['LIKE', 'brand_cat_name', $brand_cat_name]);
- }
- if (!empty($start_time)) {
- $start_time = strtotime($start_time);
- $query->andWhere(['>=', 'created_at', $start_time]);
- }
- if (!empty($end_time)) {
- $end_time = strtotime($end_time);
- $query->andWhere(['<=', 'created_at', $end_time]);
- }
- $query->select('id, brand_cat_name, sort, status, created_at')->orderBy('sort DESC');
- $list = pagination_make($query);
- foreach ($list['list'] as &$item) {
- $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
- $item['status'] = intval($item['status']);
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $list
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //保存品牌分类
- public function save() {
- try {
- if (!$this->validate()) {
- throw new \Exception($this->getErrorSummary(false)[0]);
- }
- $id = $this->id;
- $brand_cat_name = $this->brand_cat_name;
- $sort = $this->sort;
- $status = intval($this->status);
- if (empty($brand_cat_name)) {
- throw new \Exception('品牌分类名称不能为空');
- }
- $GoodsBrandCat = GoodsBrandCat::findOne(['id' => $id, 'is_delete' => 0]) ?: new GoodsBrandCat();
- $GoodsBrandCat->brand_cat_name = $brand_cat_name;
- $GoodsBrandCat->sort = $sort;
- $GoodsBrandCat->status = $status;
- $GoodsBrandCat->store_id = $this->store_id;
- if (!$GoodsBrandCat->save()) {
- throw new \Exception(implode(';', array_values($GoodsBrandCat->firstErrors)));
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //修改状态
- public function setStatus() {
- try {
- if (!$this->validate()) {
- throw new \Exception($this->getErrorSummary(false)[0]);
- }
- $ids = explode(',', $this->ids);
- $status = intval($this->status);
- if (empty($ids)) {
- throw new \Exception('品牌分类不存在');
- }
- foreach ($ids as $id) {
- $GoodsBrandCat = GoodsBrandCat::findOne(['id' => $id, 'is_delete' => 0]);
- if (!$GoodsBrandCat) {
- throw new \Exception('品牌分类不存在');
- }
- $GoodsBrandCat->status = $status;
- if (!$GoodsBrandCat->save()) {
- throw new \Exception(implode(';', array_values($GoodsBrandCat->firstErrors)));
- }
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //删除品牌分类
- public function delete() {
- try {
- $ids = explode(',', $this->ids);
- if (empty($ids)) {
- throw new \Exception('品牌分类不存在');
- }
- foreach ($ids as $id) {
- $GoodsBrandCat = GoodsBrandCat::findOne(['id' => $id, 'is_delete' => 0]);
- if (!$GoodsBrandCat) {
- throw new \Exception('品牌分类不存在');
- }
- $GoodsBrandCat->is_delete = 1;
- if (!$GoodsBrandCat->save()) {
- throw new \Exception(implode(';', array_values($GoodsBrandCat->firstErrors)));
- }
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|