| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php
- namespace addons\Store\common\models;
- use common\enums\StatusEnum;
- use common\enums\WhetherEnum;
- use common\models\base\BaseActiveRecord;
- use Yii;
- use yii\helpers\Json;
- /**
- * This is the model class for table "qimall_addons_store_freight_rules".
- *
- * @property int $id
- * @property int $mall_id 商城ID
- * @property int $store_id 门店id
- * @property string $name 规则名称
- * @property int $select_com 选择地区组件[0=多选地区,1=单选地区]
- * @property string $rule 规则详情
- * @property int $is_default 是否默认
- * @property int $status 状态【1启用 0禁用 -1删除】
- * @property int $type 计费方式【1=>按重计费、2=>按件计费】
- * @property int $created_at 创建时间
- * @property int $updated_at
- */
- class StoreFreightRules extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_store_freight_rules}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'store_id', 'select_com', 'is_default', 'status', 'type', 'created_at', 'updated_at'], 'integer'],
- [['rule'], 'required'],
- [['rule'], 'string'],
- [['name'], 'string', 'max' => 65],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'store_id' => '门店id',
- 'name' => '规则名称',
- 'select_com' => '选择地区组件[0=多选地区,1=单选地区]',
- 'rule' => '规则详情',
- 'is_default' => '是否默认',
- 'status' => '状态【1启用 0禁用 -1删除】',
- 'type' => '计费方式【1=>按重计费、2=>按件计费】',
- 'created_at' => '创建时间',
- 'updated_at' => 'Updated At',
- ];
- }
- public static function setData(array $params){
- try{
- if(!empty($params['id'])){
- $model = self::findOne(['and',['id' => $params['id'],'store_id'=>$params['store_id'],'mall_id' => $params['mall_id']],['<>','status',StatusEnum::DELETE]]);
- if(empty($model)) throw new \Exception('规则存在或已删除');
- }else{
- $model = new self();
- $model->loadDefaultValues();
- $model->mall_id = $params['mall_id'];
- $model->store_id = $params['store_id'];
- }
- isset($params['name']) && $model->name = $params['name'];
- isset($params['select_com']) && $model->select_com = $params['select_com'];
- isset($params['rule']) && $model->rule = json_encode($params['rule'],JSON_UNESCAPED_UNICODE);
- if(isset($params['is_default']) && $params['is_default'] == WhetherEnum::ENABLED){
- self::updateAll(['is_default' => WhetherEnum::DISABLED], [
- 'mall_id' => $params['mall_id'],
- 'store_id' => $params['store_id']
- ]);
- $model->is_default = $params['is_default'];
- }
- isset($params['status']) && $model->status = $params['status'];
- isset($params['type']) && $model->type = $params['type'];
- $res = $model->save();
- if($res === false) throw new \Exception($model->getErrorMessage());
- return $model;
- }catch(\Exception $e){
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- public static function getFreightRules(array $cond=[],array $with=[],bool $is_array=true,string $select='*'){
- $default_cond = [['<>','status',StatusEnum::DELETE]];
- $cond = array_merge($default_cond,$cond);
- $query = self::find()->select($select);
- self::paramPack(['where'=>$cond,'with'=>$with],$query);
- $cates = $query->asArray($is_array)->all();
- return $cates;
- }
- /**
- * 获取包邮规则的包邮地区id
- * @Author: lun
- * @DateTime: 2021/9/29 0029 9:26
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- * @param $store_id
- * @return array
- */
- public static function getFreightRulesArea($mall_id, $store_id)
- {
- $list = self::getFreightRules([['mall_id' => $mall_id, 'store_id' => $store_id ?? 0]]);
- if (empty($list)) return [];
- $districtArr = [];
- foreach ($list as $item) {
- $districtArr[$item['id']] = [
- 'type' => $item['type'],// 计费方式【1=>按重计费、2=>按件计费
- 'rule' => json_decode($item['rule'], true),
- ];
- }
- return $districtArr;
- }
- }
|