| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?php
- namespace addons\WriteOff\common\models;
- use Yii;
- use common\enums\StatusEnum;
- /**
- * This is the model class for table "{{%addons_write_off_level}}".
- *
- * @property int $id
- * @property int $mall_id 商城ID
- * @property int $level 股东等级
- * @property string $name 等级名称
- * @property int|null $is_enable_write_off_reward 核销阶梯式奖励设置[0:关闭;1启用]
- * @property string|null $write_off_reward_proportion 奖励比例集合
- * @property int|null $is_enable_promote_reward 促销奖励设置[0:关闭;1启用]
- * @property float $promote_reward_prize 促销奖励比例
- * @property int|null $is_enable_promote_reward_achievement 促销业绩阶梯奖励设置[0:关闭;1启用]
- * @property string|null $promote_achievement_reward_proportion 促销奖励比例集合
- * @property int|null $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- */
- class WriteOffLevel extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_write_off_level}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'level', 'is_enable_write_off_reward', 'is_enable_promote_reward', 'is_enable_promote_reward_achievement', 'status', 'created_at', 'updated_at','is_enable_promote_limit','promote_time'], 'integer'],
- [['write_off_reward_proportion', 'promote_achievement_reward_proportion'], 'string'],
- [['promote_reward_prize'], 'number'],
- [['name'], 'string', 'max' => 45],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'level' => 'Level',
- 'name' => 'Name',
- 'is_enable_write_off_reward' => 'Is Enable Write Off Reward',
- 'write_off_reward_proportion' => 'Write Off Reward Proportion',
- 'is_enable_promote_reward' => 'Is Enable Promote Reward',
- 'promote_reward_prize' => 'Promote Reward Prize',
- 'is_enable_promote_reward_achievement' => 'Is Enable Promote Reward Achievement',
- 'promote_achievement_reward_proportion' => 'Promote Achievement Reward Proportion',
- 'status' => 'Status',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- /**
- * 获取等级等级和权重
- * @Author: lun
- * @DateTime: 2021/10/21 0021 9:22
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- * @return array
- */
- public static function getLevelWeights($mall_id) {
- $list = self::find()->select('level')->where(['status' => [StatusEnum::DISABLED,StatusEnum::ENABLED], 'mall_id' => $mall_id])->column();
- $newList = [];
- for ($i = 1; $i <= 10; $i++) {
- $newList[] = [
- 'name' => '等级' . $i,
- 'level' => $i,
- 'disabled' => in_array($i, $list),
- ];
- }
- return $newList;
- }
- /**
- * 保存数据
- * @Author: lun
- * @DateTime: 2021/10/20 0020 18:01
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param array $params
- * @return AddonsBossLevel|bool
- */
- public static function setData(array $params){
- $trans = Yii::$app->db->beginTransaction();
- try{
- if(!empty($params['id'])){
- $model = self::findOne(['and',['id' => $params['id'],'mall_id' => $params['mall_id']],['<>','status',StatusEnum::DELETE]]);
- if(empty($model)) throw new \Exception('等级不存在或已删除');
- // 判断等级是否有被修改
- if (!empty($params['level']) && $model->level != $params['level']) {
- // 判断该等级下是否有股东存在
- $boss = WriteOffUser::find()->where(['mall_id' => $model->mall_id, 'level' => $model->level, 'status' => StatusEnum::ENABLED])->asArray()->one();
- if ($boss) throw new \Exception('该等级下存在核销员,不能被修改');
- }
- }else{
- $model = new self();
- $model->loadDefaultValues();
- $model->mall_id = $params['mall_id'];
- }
- if(!$model->load($params,'') || !$model->save()){
- throw new \Exception($model->getErrorMessage());
- }
- $trans->commit();
- return $model;
- }catch(\Exception $e){
- $trans->rollBack();
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- public static function del($params){
- try{
- $model = self::findOne(['id'=>$params['id']]);
- if(!empty($model)){
- $res = WriteOffUser::findOne(['level'=>$model->level,'mall_id'=>$params['mall_id'],'status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]);
- 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;
- }
- }
- public static function getBossLevel(array $cond=[], array $with=[],bool $is_array=true,string $select='*',$order=""){
- $default_cond = [['<>','status',StatusEnum::DELETE]];
- $cond = array_merge($default_cond,$cond);
- $query = self::find()->select($select);
- self::paramPack(['where'=>$cond, 'with'=>$with],$query);
- isset($order) && $query->orderBy($order);
- $data = $query->asArray($is_array)->all();
- return $data;
- }
- }
|