| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models\pt;
- use app\models\Cat;
- use app\models\DeliveryRules;
- use app\models\Goods;
- use app\models\GoodsCat;
- use app\models\PtActivity;
- use app\models\PtActivityGoodsCat;
- use app\models\SeckillActivity;
- use app\models\SeckillActivityGoods;
- use app\models\SeckillActivityOrderLog;
- use yii\base\Model;
- class PtActivityGoodsCatForm extends Model
- {
- public $id;
- public $ids;
- public $name;
- public $is_use_coupon;
- public $start_time;
- public $end_time;
- public $goods;
- public $status;
- public $self_limit_num;
- public $order_limit_num;
- public $cat_id;
- public function rules()
- {
- return [
- [['status', 'is_use_coupon', 'id', 'goods_id', 'cat_id'], 'integer'],
- [['start_time', 'end_time', 'ids', 'name'], 'string'],
- [['goods'], 'safe']
- ];
- }
- public function search ()
- {
- try {
- $query = PtActivityGoodsCat::find()->where(['store_id' => get_store_id(), 'is_delete' => 0]);
- if ($this->name) {
- $query->andWhere(['LIKE', 'name', $this->name]);
- }
- if ((int)$this->status !== -1 && $this->status !== null) {
- $query->andWhere(['status' => $this->status]);
- }
- if ($this->start_time) {
- $start_time = strtotime($this->start_time);
- $query->andWhere(['>=', 'created_at', $start_time]);
- }
- if ($this->end_time) {
- $end_time = strtotime($this->end_time);
- $query->andWhere(['<=', 'created_at', $end_time]);
- }
- $query->select('id, status, created_at, updated_at, name')->orderBy('created_at desc');
- $pagination = pagination_make($query);
- foreach ($pagination['list'] as &$item) {
- $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
- $item['updated_at'] = $item['updated_at'] ? date("Y-m-d H:i:s", $item['updated_at']) : '';
- $item['status'] *= 1;
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $pagination['list'],
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount'],
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function save ()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (empty($this->name)) {
- throw new \Exception('分类名称不可为空');
- }
- $pt_goods_cat = new PtActivityGoodsCat();
- if ($this->id) {
- $pt_goods_cat = PtActivityGoodsCat::findOne($this->id);
- if (empty($pt_goods_cat)) {
- throw new \Exception('查找失败');
- }
- }
- $pt_goods_cat->name = $this->name;
- $pt_goods_cat->status = $this->status ?: 1;
- $pt_goods_cat->store_id = get_store_id();
- if (!$pt_goods_cat->save()) {
- throw new \Exception(json_encode($pt_goods_cat->errors));
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '操作成功!'
- ];
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function setStatus ()
- {
- try {
- if ($this->ids) {
- $ids = explode(',', $this->ids);
- if (in_array($this->status, [0, 1])) {
- PtActivityGoodsCat::updateAll(['status' => $this->status], ['id' => $ids, 'store_id' => get_store_id(), 'is_delete' => 0]);
- }
- if ((int)$this->status === 2) {
- PtActivityGoodsCat::updateAll(['is_delete' => 1], ['id' => $ids, 'store_id' => get_store_id(), 'is_delete' => 0]);
- }
- } else {
- $rules = PtActivityGoodsCat::findOne(['id' => $this->id, 'is_delete' => 0, 'store_id' => get_store_id()]);
- if (empty($rules)) {
- throw new \Exception("分类未找到");
- }
- if (in_array($this->status, [0, 1])) {
- $rules->status = $this->status;
- }
- if ((int)$this->status === 2) {
- $rules->is_delete = 1;
- }
- if (!$rules->save()) {
- throw new \Exception(json_encode($rules->errors));
- }
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功!'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|