| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <?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\PtActivityBanner;
- use app\models\PtActivityGoodsCat;
- use app\models\SeckillActivity;
- use app\models\SeckillActivityGoods;
- use app\models\SeckillActivityOrderLog;
- use yii\base\Model;
- class PtActivityBannerForm extends Model
- {
- public $id;
- public $ids;
- public $name;
- public $start_time;
- public $end_time;
- public $status;
- public $url;
- public $sort;
- public $cover_pic;
- public function rules()
- {
- return [
- [['status', 'id', 'goods_id', 'sort'], 'integer'],
- [['start_time', 'ids', 'name', 'cover_pic'], 'string'],
- [['url'], 'safe']
- ];
- }
- public function search ()
- {
- try {
- $query = PtActivityBanner::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, created_at, updated_at, name, cover_pic, url, status, sort')->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;
- $item['url'] = !empty($item['url']) ? json_decode($item['url'], true) : [
- 'name' => '',
- 'link' => '',
- 'open_type' => ''
- ];
- }
- 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->cover_pic)) {
- throw new \Exception('图片地址不可为空');
- }
- $banner_form = new PtActivityBanner();
- if ($this->id) {
- $banner_form = PtActivityBanner::findOne($this->id);
- if (empty($banner_form)) {
- throw new \Exception('查找失败');
- }
- }
- if (!empty($this->url)) {
- $url = $this->url;
- } else {
- $url = [
- 'name' => '',
- 'link' => '',
- 'open_type' => ''
- ];
- }
- $banner_form->name = $this->name;
- $banner_form->cover_pic = $this->cover_pic;
- $banner_form->url = json_encode($url);
- $banner_form->sort = $this->sort;
- $banner_form->status = $this->status ?: 0;
- $banner_form->store_id = get_store_id();
- if (!$banner_form->save()) {
- throw new \Exception(json_encode($banner_form->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])) {
- PtActivityBanner::updateAll(['status' => $this->status], ['id' => $ids, 'store_id' => get_store_id(), 'is_delete' => 0]);
- }
- if ((int)$this->status === 2) {
- PtActivityBanner::updateAll(['is_delete' => 1], ['id' => $ids, 'store_id' => get_store_id(), 'is_delete' => 0]);
- }
- } else {
- $rules = PtActivityBanner::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()
- ];
- }
- }
- }
|