package com.bzd.web.controller.server;


import com.bzd.common.annotation.Log;
import com.bzd.common.core.controller.BaseController;
import com.bzd.common.core.domain.AjaxResult;
import com.bzd.common.core.page.TableDataInfo;
import com.bzd.common.enums.BusinessType;
import com.bzd.system.service.IServerGService;
import com.bzd.common.config.dao.PageData;
import com.bzd.system.service.ServerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 服务管理
 */
@Controller
@RequestMapping("/server/serv")
public class ServerGController extends BaseController{

    private String prefix = "server/serv";

    @Autowired
    private IServerGService serverGService;

    @Autowired
    private ServerService service;
    private Object server;

    @RequiresPermissions("server:serv:view")
    @GetMapping()
    public String user()
    {
        return prefix + "/serv";
    }

    @RequiresPermissions("server:serv:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list() throws Exception {
        PageData pd = this.getPageData();

        startPage();
        //List<ServerG> list = serverGService.selectServerGList(ser);
        List<PageData> pageData = service.find(pd);
        return getDataTable(pageData);
    }

    /**
     * 新增
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("posts", 1);
        return prefix + "/add";
    }

    /**
     * 新增保存
     */
    //@RequiresPermissions("server:serv:add")
    @Log(title = "服务管理新增", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave() throws Exception {
        PageData pd = new PageData();
        pd = this.getPageData();
        Integer integer = service.save(pd);
        return toAjax(integer);
    }

    //@RequiresPermissions("system:user:remove")
    @Log(title = "服务管理删除", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove() throws Exception
    {
        PageData pd = new PageData();
        pd = this.getPageData();
        Integer integer = service.del(pd);
        return toAjax(integer);
    }

    @RequiresPermissions("server:serv:edit")
    @GetMapping("/edit/{serviceId}")
    public String view(@PathVariable("serviceId") Long serviceId, ModelMap mmap)throws Exception
    {
        PageData pd = this.getPageData();
        pd.put("serviceId",serviceId);
        PageData pageData = service.find(pd).get(0);
        mmap.putAll(pageData);
        return prefix + "/edit";
    }

    /**
     * 保存修改信息
     */
    @RequiresPermissions("server:user:edit")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave() throws Exception
    {
        PageData pd = this.getPageData();
        Integer update = service.update(pd);
        if(update!=1){
            return error("修改失败");
        }
        return toAjax(service.update(pd));
    }


}
