package com.yihu.hos.tenant.controller;

import com.yihu.hos.tenant.model.TenantModel;
import com.yihu.hos.tenant.service.TenantService;
import com.yihu.hos.web.framework.model.Result;
import com.yihu.hos.web.framework.util.controller.BaseController;
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.*;

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")
@Controller
public class TenantController extends BaseController{

    @Resource(name = TenantService.BEAN_ID)
    private TenantService tenantService;


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


    /**
     * 租户列表
     *
     * @param request
     * @return
     */
    @RequestMapping("/getTenantList")
    @ResponseBody
    public Result getAppList(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 = tenantService.getTenantList(params);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }
    }

    /**
     * 租户修改页面
     * @param model
     * @param id
     * @param categoryId
     * @return
     */
    @RequestMapping("/editorTenant")
    public String editorTenant(Model model, Long id, String flag, String categoryId) {
        try {
            TenantModel tenantModel = null;
            if (id != null) {
                tenantModel = tenantService.getTenantById(id);
            }  else {
                tenantModel = new TenantModel();
            }
            model.addAttribute("model", tenantModel);
            model.addAttribute("flag", flag);
            model.addAttribute("categoryId", categoryId);
            model.addAttribute("contentPage", "/tenant/editorTenant");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "pageView";
    }

    /**
     * 租户详情页
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/tenantDetail")
    public String tenantDetail(Model model, Long id) {
        try {
            TenantModel tenantModel = null;
            if (id != null ) {
                tenantModel = tenantService.getTenantById(id);
            }  else {
                tenantModel = new TenantModel();
            }
            model.addAttribute("model", tenantModel);
            model.addAttribute("contentPage", "/tenant/tenantDetail");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "pageView";
    }


    /**
     * 新增租户信息
     * @param request
     * @return
     */
    @RequestMapping("addTenant")
    @ResponseBody
    public Result addTenant(HttpServletRequest request) {
        try {
            TenantModel obj = new TenantModel();
            BeanUtils.populate(obj, request.getParameterMap());
            obj.setCreated(new Date());
            return tenantService.addTenant(obj);
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }
    }

    /**
     * 删除租户信息
     * @param request
     * @return
     */
    @RequestMapping("/deleteTenant")
    @ResponseBody
    public Result deleteTenant(HttpServletRequest request) {

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

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

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

            return tenantService.updateTenant(obj);
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }
    }

    /**
     * 新建数据库
     * @param request
     * @return
     */
    @RequestMapping(value = "/createDB" , produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    public Result createDB(HttpServletRequest request,
                           @ApiParam(name = "dbName", value = "数据库名称", allowMultiple = true)
                           @RequestParam() String dbName) {

        try {
            tenantService.createDB(dbName);
            return Result.success("新建数据库成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("新建数据库失败！");
        }
    }

    /**
     * 数据库复制
     * @param dbName
     * @param dbConfig
     * @return
     * @throws Exception
     */
    @RequestMapping("dataBaseCopy")
    @ResponseBody
    public Result requestTest(
            @ApiParam(name = "dbName", value = "源数据库名称", allowMultiple = true)
            @RequestParam(value = "dbName") String dbName,
            @ApiParam(name = "dbConfig", value = "源数据库连接", allowMultiple = true)
            @RequestParam(value = "dbConfig")  String dbConfig) throws Exception {

        Result result = tenantService.copyBaseTables(dbName, dbConfig);

        return result;
    }

}
