| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\LocalDeliveryFreight;
- use yii\base\Model;
- class FreightForm extends Model
- {
- public $store_id;
- public $id;
- public $name;
- public $detail;
- public $is_enable = 1;
- public $is_scan = 0;
- public $begin_time;
- public $end_time;
- public $level = 0;
- public $is_delete;
- public $day;
- public $price_type;
- public $order_price_type;
- public $is_have_page = 1;
- //搜索
- public $search_key;
- public $search_is_show;
- const SCENARIO_ADD = 'add';
- const SCENARIO_EDIT = 'edit';
- const SCENARIO_DEL = 'del';
- const SCENARIO_LIST = 'list';
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['id'], 'required', 'on' => [self::SCENARIO_DEL, self::SCENARIO_EDIT]],
- [['name'], 'required', 'on' => [self::SCENARIO_ADD, self::SCENARIO_EDIT]],
- ['is_delete', 'default', 'value'=> LocalDeliveryFreight::IS_DELETE_NO],
- ['detail', 'default', 'value' => '[{"distance":"0","distance_price":"0","second":"0","second_price":"0"}]'],
- ['search_key', 'string', 'on' => self::SCENARIO_LIST],
- ['id', 'string', 'on' => self::SCENARIO_DEL],
- [['is_delete', 'is_enable', 'level', 'day', 'price_type', 'order_price_type', 'is_scan'], 'integer'],
- [['name', 'begin_time', 'end_time'], 'string', 'max' => 255],
- [['detail'], 'string'],
- [['store_id'], 'safe'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'is_enable' => '是否启用:0=否,1=是',
- 'level' => '规则优先级,值越大优先级越高',
- 'name' => '名称',
- 'detail' => '规则详细',
- 'begin_time' => '开始时间段',
- 'end_time' => '结束时间段',
- 'created_at' => '创建时间',
- 'updated_at' => '最后一次修改时间',
- 'is_delete' => '是否删除:0=否;1=是;',
- 'day' => '可预约天数'
- ];
- }
- public function scenarios()
- {
- $scenarios = parent::scenarios();
- return $scenarios;
- }
- /* ************************************************* */
- public function saveFreight($type = 0)
- {
- if ($this->validate()) {
- $t = \Yii::$app->db->beginTransaction();
- $level_query = LocalDeliveryFreight::find()->where(['type' => $type, 'level' => $this->level, 'is_delete' => 0]);
- if ($this->scenario == self::SCENARIO_EDIT) {
- $model = LocalDeliveryFreight::findOne(['id' => $this->id]);
- $level_query->andWhere(['<>','id', $this->id]);
- } else {
- $model = new LocalDeliveryFreight();
- }
- if($this->store_id > 0){
- $level_query->andWhere(['store_id' => $this->store_id]);
- } else {
- $level_query->andWhere(['store_id' => 0]);
- }
- $level_exist = $level_query->exists();
- if($level_exist) {
- return [
- 'code' => 1,
- 'msg' => '规则优先级已存在,不能重复'
- ];
- }
- $model->attributes = $this->attributes;
- $model->type = $type;
- if($type == LocalDeliveryFreight::TYPE_MAIYATIAN && $this->store_id > 0){
- $model->store_id = $this->store_id;
- }
- if (!$model->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $model->getErrorSummary(false)[0]
- ];
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '保存成功'
- ];
- } else {
- // 验证失败:$errors 是一个包含错误信息的数组
- return [
- 'code' => 1,
- "msg" => $this->getErrorSummary(false)[0]
- ];
- }
- }
- /* ************************************************* */
- public function searchFreight($type = 0)
- {
- $query = LocalDeliveryFreight::find();
- if ($this->is_have_page) {
- $query->where(['type' => $type, 'is_delete' => LocalDeliveryFreight::IS_DELETE_NO])->orderBy("id desc");
- $query->andWhere(['store_id' => $this->store_id ?: 0]);
- // 搜索
- if ($this->search_key) {
- $query->andWhere(['like', 'name', $this->search_key]);
- }
- $list = pagination_make($query);
- foreach ($list['list'] as &$val) {
- $val['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
- $val['updated_at'] = date('Y-m-d H:i:s', $val['updated_at']);
- $val['price_type'] = (int)$val['price_type'];
- $val['order_price_type'] = (int)$val['order_price_type'];
- $val['is_scan'] = (int)$val['is_scan'];
- }
- $data = [
- 'data' => $list['list'],
- 'pageNo' => $list['pageNo'],
- 'totalCount' => $list['totalCount']
- ];
- } else {
- $query->where(['type' => $type, 'is_delete' => LocalDeliveryFreight::IS_DELETE_NO]);
- if($this->store_id > 0){
- $query->andWhere(['store_id' => $this->store_id]);
- }
- $data = $query->orderBy("id desc")->asArray()->all();
- foreach ($data['list'] as &$val) {
- $val['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
- $val['updated_at'] = date('Y-m-d H:i:s', $val['updated_at']);
- }
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $data
- ];
- }
- public function delFreight()
- {
- if ($this->validate()) {
- $t = \Yii::$app->db->beginTransaction();
- if ($this->scenario !== self::SCENARIO_DEL) {
- return [
- 'code' => 1,
- 'msg' => '删除失败'
- ];
- }
- $model = LocalDeliveryFreight::findOne(['id' => $this->id]);
- $model->is_delete = LocalDeliveryFreight::IS_DELETE_YES;
- if (!$model || !$model->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $model->getErrorSummary(false)[0]
- ];
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '删除成功'
- ];
- // 所有输入数据都有效 all inputs are valid
- } else {
- // 验证失败:$errors 是一个包含错误信息的数组
- return [
- 'code' => 1,
- "msg" => $this->getErrorSummary(false)[0]
- ];
- }
- }
- public function applyFreight()
- {
- if ($this->validate()) {
- $t = \Yii::$app->db->beginTransaction();
- $model = LocalDeliveryFreight::findOne(['id' => $this->id]);
- $field = 'is_enable';
- if (isset($this->is_scan)) {
- $field = 'is_scan';
- }
- if ($model->$field == $this->$field) {
- return [
- 'code' => 1,
- 'msg' => '规则状态以改变,请勿重复提交'
- ];
- }
- if ($model->is_delete != 0) {
- return [
- 'code' => 1,
- 'msg' => '记录不存在,请刷新页面'
- ];
- }
- $model->$field = $this->$field;
- if (!$model || !$model->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $model->getErrorSummary(false)[0]
- ];
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => $this->$field == 1 ? '启用成功' : '关闭成功'
- ];
- // 所有输入数据都有效 all inputs are valid
- } else {
- // 验证失败:$errors 是一个包含错误信息的数组
- return [
- 'code' => 1,
- "msg" => $this->getErrorSummary(false)[0]
- ];
- }
- }
- public static function getFreightList($type = 0)
- {
- return LocalDeliveryFreight::find()->where(['type' => $type, 'is_delete' => LocalDeliveryFreight::IS_DELETE_NO])->orderBy(['sort desc, id desc'])->select('*')->asArray()->all();
- }
- }
|