| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\DeliveryRules;
- use yii\base\Model;
- class DeliveryRulesForm extends Model
- {
- public $id;
- public $ids;
- public $name;
- public $type;
- public $times;
- public $status;
- public $days;
- public function rules()
- {
- return [
- [['status', 'type', 'id', 'days'], 'integer'],
- [['times', 'ids', 'name'], 'string']
- ];
- }
- public function search ()
- {
- try {
- $query = DeliveryRules::find()->where(['is_delete' => 0, 'store_id' => get_store_id()]);
- if ($this->status !== null && $this->status !== '' && in_array($this->status, [0, 1])) {
- $query->andWhere(['status' => $this->status]);
- }
- $pagination = pagination_make($query);
- foreach ($pagination['list'] as &$item) {
- if ((int)$item['type'] === 0) {
- $item['times'] = date("Y-m-d H:i:s", $item['times']);
- }
- $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
- $item['updated_at'] = date("Y-m-d H:i:s", $item['updated_at']);
- }
- 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 ()
- {
- try {
- if (!$this->name || (!$this->times && !$this->days)) {
- throw new \Exception("请将参数填充完整");
- }
- $rules = DeliveryRules::findOne(['id' => $this->id, 'is_delete' => 0, 'store_id' => get_store_id()]);
- if (empty($rules)) {
- $rules = new DeliveryRules();
- $rules->store_id = get_store_id();
- }
- $rules->name = $this->name;
- $rules->type = (int)$this->type;
- // $rules->times = (int)$this->times;
- if ((int)$this->type === 0) {
- $rules->times = strtotime($this->times);
- } else {
- $rules->days = (int)$this->days;
- }
- if (!$rules->save()) {
- throw new \Exception(json_encode($rules->errors));
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功!'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function setStatus ()
- {
- try {
- if ($this->ids) {
- $ids = explode(',', $this->ids);
- if (in_array($this->status, [0, 1])) {
- DeliveryRules::updateAll(['status' => $this->status], ['id' => $ids, 'store_id' => get_store_id(), 'is_delete' => 0]);
- }
- if ((int)$this->status === 2) {
- DeliveryRules::updateAll(['is_delete' => 1], ['id' => $ids, 'store_id' => get_store_id(), 'is_delete' => 0]);
- }
- } else {
- $rules = DeliveryRules::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()
- ];
- }
- }
- public function getSelectList()
- {
- try {
- $list = DeliveryRules::find()->where(['is_delete' => 0, 'store_id' => get_store_id()])->asArray()->all();
- foreach ($list as &$item) {
- if ((int)$item['type'] === 0) {
- $item['times'] = date("Y-m-d H:i:s", $item['times']);
- }
- $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
- $item['updated_at'] = date("Y-m-d H:i:s", $item['updated_at']);
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list
- ]
- ];
- } catch (\Exception $e){
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|