"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() ]; } } }