package com.yihu.hos.tenant.controller;

import com.yihu.hos.tenant.model.DBInfoModel;
import com.yihu.hos.tenant.service.DBInfoService;
import com.yihu.hos.web.framework.model.DetailModelResult;
import com.yihu.hos.web.framework.model.Result;
import com.yihu.hos.web.framework.util.controller.BaseController;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 *  数据库实例管理
 * @author HZY
 * @vsrsion 1.0
 * Created at 2016/12/16.
 */
@RequestMapping("/tenant/dataBase")
@Controller
public class DBInfoController extends BaseController{

    @Resource(name = DBInfoService.BEAN_ID)
    private DBInfoService dbInfoService;


    /**
     *   管理界面
     *
     * @param model
     * @return
     */
    @RequestMapping("/initial")
    public String dbInfoInitial(Model model) {
        model.addAttribute("contentPage", "tenant/dataBase/dbInfo");
        return "partView";
    }


    /**
     * 列表
     *
     * @param request
     * @return
     */
    @RequestMapping("/getDBList")
    @ResponseBody
    public Result getDBInfoList(HttpServletRequest request,String name,String valid) {
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("name", name);
            params.put("valid", valid);

            String page = StringUtils.isEmpty(request.getParameter("page")) ? "1" : request.getParameter("page");
            String rows = StringUtils.isEmpty(request.getParameter("rows")) ? "10" : request.getParameter("rows");

            params.put("page", page);
            params.put("rows", rows);
            Result result = dbInfoService.getDBInfoList(params);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }
    }

    /**
     *  修改页面
     * @param model
     * @param id
     * @param categoryId
     * @return
     */
    @RequestMapping("/editorDbInfo")
    public String editorDBInfo(Model model, Long id, String flag, String categoryId) {
        try {
            DBInfoModel dbInfoModel = null;
            if (id != null) {
                dbInfoModel = dbInfoService.getDBInfoById(id);
            }  else {
                dbInfoModel = new DBInfoModel();
            }
            model.addAttribute("model", dbInfoModel);
            model.addAttribute("flag", flag);
            model.addAttribute("categoryId", categoryId);
            model.addAttribute("contentPage", "/tenant/dataBase/editorDbInfo");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "pageView";
    }

    /**
     * 详情页
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/dbDetail")
    public String dBInfoDetail(Model model, Long id) {
        try {
            DBInfoModel dbInfoModel = null;
            if (id != null ) {
                dbInfoModel = dbInfoService.getDBInfoById(id);
            }  else {
                dbInfoModel = new DBInfoModel();
            }
            model.addAttribute("model", dbInfoModel);
            model.addAttribute("contentPage", "/tenant/dataBase/dbDetail");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "pageView";
    }


    /**
     * 新增
     * @param request
     * @return
     */
    @RequestMapping("addDBInfo")
    @ResponseBody
    public Result addDBInfo(HttpServletRequest request) {
        try {
            DBInfoModel obj = new DBInfoModel();
            BeanUtils.populate(obj, request.getParameterMap());
            obj.setCreated(new Date());
            return dbInfoService.addDBInfo(obj);
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }
    }

    /**
     * 删除
     * @param request
     * @return
     */
    @RequestMapping("/deleteDBInfo")
    @ResponseBody
    public Result deleteDBInfo(HttpServletRequest request) {

        try {
            String id = request.getParameter("id");

            dbInfoService.deleteDBInfo(Long.parseLong(id));
            return Result.success("删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败！");
        }
    }

    /**
     * 修改租户信息
     */
    @RequestMapping("updateDBInfo")
    @ResponseBody
    public Result updateDBInfo(HttpServletRequest request) {
        try {
            DBInfoModel obj = new DBInfoModel();
            BeanUtils.populate(obj, request.getParameterMap());

            return dbInfoService.updateDBInfo(obj);
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }
    }


    @RequestMapping(value="/getDataBaseValues" , method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取数据库实例下拉框信息", response = DetailModelResult.class, produces = "application/json", notes = "获取数据库实例下拉框信息")
    public DetailModelResult getSelectList(
            @ApiParam(name = "q", value = "数据库名称或实例名", required = false)
            @RequestParam(value = "q", required = false) String q,
            @ApiParam(name = "initVal", value = "初始化数据库名称", required = false)
            @RequestParam(value = "initVal", required = false) String initVal) {
        return dbInfoService.getDataBaseSelectList(q);
    }


}
