| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- <?php
- namespace addons\Store\common\models;
- use common\enums\StatusEnum;
- use Yii;
- /**
- * This is the model class for table "{{%addons_store_boss_agent_level}}".
- *
- * @property int $id
- * @property int $mall_id
- * @property int $store_id
- * @property int $level
- * @property string $name
- * @property int $is_auto_upgrade
- * @property string $detail
- * @property float $price 永久分红
- * @property int $created_at
- * @property int $updated_at
- * @property int $status
- * @property string|null $checked_condition_values 升级条件
- * @property int $is_enable 是否启用
- * @property string|null $checked_condition_keys 升级条件选中的key集合
- * @property int $condition_type 0 未选择、1满足其一 2、满足所有
- * @property int $upgrade_type_goods
- * @property int $upgrade_type_condition
- * @property string|null $goods_warehouse_ids 商品仓库的ID
- * @property string|null $goods_list 商品列表
- * @property int $goods_type 商品升级类型
- * @property int $buy_goods_type 0订单完成 1下单 完成
- */
- class StoreBossAgentLevel extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_store_boss_agent_level}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'store_id', 'level', 'is_auto_upgrade', 'created_at', 'updated_at', 'status', 'is_enable', 'condition_type', 'upgrade_type_goods', 'upgrade_type_condition', 'goods_type', 'buy_goods_type'], 'integer'],
- [['name'], 'required'],
- [['price'], 'number'],
- [['checked_condition_values', 'checked_condition_keys', 'goods_warehouse_ids', 'goods_list'], 'string'],
- [['name'], 'string', 'max' => 45],
- [['detail'], 'string', 'max' => 255],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'store_id' => 'Store ID',
- 'level' => 'Level',
- 'name' => 'Name',
- 'is_auto_upgrade' => 'Is Auto Upgrade',
- 'detail' => 'Detail',
- 'price' => 'Price',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- 'status' => 'Status',
- 'checked_condition_values' => 'Checked Condition Values',
- 'is_enable' => 'Is Enable',
- 'checked_condition_keys' => 'Checked Condition Keys',
- 'condition_type' => 'Condition Type',
- 'upgrade_type_goods' => 'Upgrade Type Goods',
- 'upgrade_type_condition' => 'Upgrade Type Condition',
- 'goods_warehouse_ids' => 'Goods Warehouse Ids',
- 'goods_list' => 'Goods List',
- 'goods_type' => 'Goods Type',
- 'buy_goods_type' => 'Buy Goods Type',
- ];
- }
- /**
- * 获取可升级的等级列表
- * @Author bing
- * @DateTime 2020-11-07 09:59:33
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @param integer $level
- * @param integer $mall_id
- * @param integer $store_id
- * @param string $order
- * @return void|mixed
- */
- public static function getCanLevelList($level,$mall_id,$store_id,$order = 'desc'){
- return self::find()
- ->where(['mall_id' => $mall_id,'store_id' => $store_id, 'is_enable' => 1, 'is_delete' => 0])
- ->andWhere(['>', 'level', $level])
- ->orderBy("level $order")
- ->all();
- }
- public static function getList($mall_id,$store_id)
- {
- $levelList = self::find()->where([
- 'mall_id' => $mall_id,'store_id' => $store_id, 'status' => [StatusEnum::ENABLED,StatusEnum::DISABLED], 'is_enable' => 1,
- ])->select(['id', 'level', 'name'])->orderBy(['level' => SORT_ASC])->asArray()->all();
- return $levelList;
- }
- /**
- * @desc:保存数据
- * @Author: hua
- * @Date: 2021/10/20
- * @Time: 15:46
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param array $params
- * @return AgentLevel|false|null
- * @throws \Exception
- */
- public static function setData(array $params)
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if(empty($params['id']) && !empty($params['level'])){
- $level_exist = self::findOne(['mall_id' => $params['mall_id'], 'level' => $params['level'], 'store_id'=>$params['store_id'],'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if($level_exist) throw new \Exception('该等级已存在数据');
- }
- if(!empty($params['id'])){
- $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if (empty($model)) throw new \Exception('服务不存在或已删除');
- }else{
- $model = new self();
- $model->loadDefaultValues();
- }
- if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
- $model->checked_condition_keys = json_encode($params['checked_condition_keys']);
- $model->checked_condition_values = json_encode($params['checked_condition_values']);
- $model->goods_list = json_encode($params['goods_list']);
- $model->goods_warehouse_ids = json_encode($params['goods_warehouse_ids']);
- if (!$model->save()) throw new \Exception($model->getErrorMessage());
- $t->commit();
- return $model;
- } catch (\Exception $e) {
- $t->rollBack();
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- public static function getDetail($params)
- {
- $weights = self::getLevelWeights();
- $level = self::findOne(['id' => $params['id'], 'status' => [StatusEnum::ENABLED,StatusEnum::DISABLED]]);
- if ($level) {
- if ($level->checked_condition_values) {
- $level->checked_condition_values = json_decode($level->checked_condition_values,true);
- }
- if ($level->checked_condition_keys) {
- $level->checked_condition_keys = json_decode($level->checked_condition_keys,true);
- }
- if ($level->goods_warehouse_ids) {
- $level->goods_warehouse_ids = json_decode($level->goods_warehouse_ids,true);
- } else {
- $level->goods_warehouse_ids = [];
- }
- if ($level->goods_list) {
- $level->goods_list = json_decode($level->goods_list,true);
- } else {
- $level->goods_list = [];
- }
- }
- return [
- 'code' => 0,
- 'msg' => '',
- 'data' => [
- 'detail' => $level,
- 'weights' => $weights['newList']
- ]
- ];
- }
- public static function getLevelWeights()
- {
- $store = Yii::$app->session->get('o2o');
- // if(empty($store)){
- $list = self::find()->select('level,name')->where([
- 'mall_id' =>$store ['mall_id'],
- 'store_id' => $store['id'],
- 'status' => [StatusEnum::ENABLED,StatusEnum::DISABLED]
- ])->orderBy(['level' => SORT_ASC])->all();
- $level_list = array_column($list,'level');
- $newList = [];
- $matchList = [];
- for ($i = 1; $i <= 10; $i++) {
- $newList[] = [
- 'name' => '权重' . $i,
- 'level' => $i,
- 'disabled' => in_array($i, $level_list),
- ];
- if(isset($list[$i-1])){
- $matchList[] = [
- 'name' => $list[$i-1]['name'],
- 'level' => "$i",
- 'disabled' => in_array($i, $level_list),
- ];
- }
- }
- return ['newList'=>$newList,'matchList'=>$matchList];
- }
- public static function del($params){
- try{
- $model = self::findOne(['id'=>$params['id']]);
- if(!empty($model)){
- $res = StoreBossAgent::findOne(['level'=>$model->level,'mall_id'=>$params['mall_id'],'status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED],'store_id'=>$params['store_id']]);
- if(!empty($res)) throw new \Exception('该等级关联了股东');
- $model->status = StatusEnum::DELETE;
- $res = $model->save();
- if($res==false) throw new \Exception($model->getErrorMessage());
- }
- return true;
- }catch (\Exception $e){
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- }
|