| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- <?php
- namespace app\modules\admin\models;
- use app\models\Share;
- use app\models\ShareLevel;
- use yii\base\Model;
- class ShareLevelFrom extends Model
- {
- public $id;
- public $name;
- public $level;
- public $is_auto_upgrade; //是否自动升级
- public $type; //升级条件
- public $value; //升级条件值
- public $profit_type; //分销佣金类型
- public $first_profit;
- public $second_profit;
- public $third_profit;
- public $status;
- public $desc;
- public $store_id;
- public function rules()
- {
- return [
- [['id', 'level', 'is_auto_upgrade', 'type', 'profit_type', 'status', 'store_id'], 'integer'],
- [['value', 'first_profit', 'second_profit', 'third_profit'], 'number'],
- [['name', 'desc'], 'string']
- ];
- }
- public function attributeLabels()
- {
- return [
- "id" => "ID",
- "name" => "名称",
- "level" => "等级",
- "is_auto_upgrade" => "是否自动升级",
- "type" => "升级条件类型",
- "value" => "升级条件值",
- "profit_type" => "分销佣金类型",
- "first_profit" => "一级佣金",
- "second_profit" => "二级佣金",
- "third_profit" => "三级佣金",
- "status" => "启用状态",
- "desc" => "等级说明",
- ];
- }
- /**
- * 保存
- */
- public function setShareLevel() {
- try {
- $id = $this->id;
- $store_id = $this->store_id;
- $type = intval($this->type);
- $status = intval($this->status);
- $profit_type = intval($this->profit_type);
- $is_auto_upgrade = intval($this->is_auto_upgrade);
- $share_level = ShareLevel::findOne($id);
- if (!$share_level) {
- $share_level = new ShareLevel();
- $share_level->store_id = $store_id;
- }
- $share_level->name = $this->name;
- $share_level->level = $this->level;
- $share_level->is_auto_upgrade = $is_auto_upgrade;
- $share_level->type = $type;
- $share_level->value = $this->value;
- $share_level->profit_type = $profit_type;
- $share_level->first_profit = (float)$this->first_profit;
- $share_level->second_profit = (float)$this->second_profit;
- $share_level->third_profit = (float)$this->third_profit;
- $share_level->status = $status;
- $share_level->desc = $this->desc;
- if (!$share_level->save()) {
- throw new \Exception(json_encode($share_level->errors, JSON_UNESCAPED_UNICODE));
- }
- return [
- 'code' => 0,
- 'msg' => '设置成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 列表
- */
- public function list() {
- try {
- $name = $this->name;
- $status = (int)$this->status;
- $store_id = $this->store_id;
- $query = ShareLevel::find()->where(['store_id' => $store_id, 'is_delete' => ShareLevel::SHARE_NOT_DELETE]);
- $level_query = clone $query;
- if ($name) {
- $query->andWhere(['LIKE', 'name', $name]);
- }
- if ($status && in_array($status, ShareLevel::$valid_status_arr)) {
- $query->andWhere(['status' => $status]);
- }
- // $query->select('id, name, level, type, value, first_profit, second_profit, third_profit, status');
- $list = pagination_make($query);
- foreach ($list['list'] as &$item) {
- $item['type'] = (int)$item['type'];
- $item['status'] = (int)$item['status'];
- $item['profit_type'] = (int)$item['profit_type'];
- $item['is_auto_upgrade'] = (int)$item['is_auto_upgrade'];
- }
- $level = $level_query->select('level')->column();
- $level = array_values(array_diff(range('1', '99'), $level));
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list['list'],
- 'level' => $level,
- 'pageNo' => $list['pageNo'],
- 'totalCount' => $list['totalCount'],
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function setStatus() {
- try {
- $id = $this->id;
- $status = (int)$this->status;
- $store_id = $this->store_id;
- $share_level = ShareLevel::findOne(['id' => $id, 'is_delete' => ShareLevel::SHARE_NOT_DELETE, 'store_id' => $store_id]);
- if (!$share_level) {
- throw new \Exception('查询失败');
- }
- if ($this->status === null) {
- throw new \Exception('状态错误');
- }
- if ($status == ShareLevel::STATUS_OFF) {
- $share = Share::findOne(['status' => 1, 'store_id' => $this->store_id, 'is_delete' => 0, 'level' => $share_level->level]);
- if ($share) {
- throw new \Exception('该等级下有分销商,不可禁用');
- }
- }
- if (in_array($status, ShareLevel::$valid_status_arr)) {
- $share_level->status = $status;
- } else {
- $share = Share::findOne(['status' => 1, 'store_id' => $this->store_id, 'is_delete' => 0, 'level' => $share_level->level]);
- if ($share) {
- throw new \Exception('该等级下有分销商,不可禁用');
- }
- $share_level->is_delete = ShareLevel::SHARE_IS_DELETE;
- }
- if (!$share_level->save()) {
- throw new \Exception('修改状态失败');
- }
- return [
- 'code' => 0,
- 'msg' => '修改成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|