package com.jay.monitor.data.server.controllers.crud;

import cn.hutool.core.bean.BeanUtil;
import com.jay.monitor.data.core.model.response.PageResponseModel;
import com.jay.monitor.data.core.model.response.ResponseCommonModel;
import com.jay.monitor.data.core.utils.ResponseUtils;
import com.jay.monitor.data.server.store.ISimpleCrudProcess;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @author liukaixiong
 * @Email liukx@elab-plus.com
 * @date 2021/9/8 - 17:59
 */
public abstract class BaseSimpleCrudController<REQ, T> {

    public abstract ISimpleCrudProcess<T> getCrudProcess();

    public abstract String getViewPath();

    @RequestMapping(value = "/add",
        method = RequestMethod.POST,
        produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResponseCommonModel addMapping(
        @RequestBody
            T request) throws Exception {
        addBefore(request);
        boolean save = getCrudProcess().save(request);
        addAfter(request, save);
        return ResponseUtils.trues();
    }

    @RequestMapping(value = "/list.html",
        method = RequestMethod.GET)
    public String list(Model model) throws Exception {
        listInModel(model, null);
        return getViewPath() + "/list";
    }

    @RequestMapping(value = "/update.html",
        method = RequestMethod.GET)
    public String update(Model model,
        @RequestParam("id")
            String id) throws Exception {
        T entity = getCrudProcess().queryById(id);
        model.addAttribute("obj", entity);
        updateInModel(model, entity);
        return getViewPath() + getUpdateHtmlPath();
    }

    protected String getUpdateHtmlPath() {
        return "/update";
    }

    @RequestMapping(value = "/add.html",
        method = RequestMethod.GET)
    public String add(Model model) throws Exception {
        addInModel(model, null);
        return getViewPath() + getAddHtmlPath();
    }

    protected String getAddHtmlPath() {
        return "/add";
    }

    protected void modelAfter(Model model, T entity) {

    }

    protected void listInModel(Model model, T entity) {
        modelAfter(model, null);
    }

    protected void addInModel(Model model, T entity) {
        modelAfter(model, entity);
    }

    protected void updateInModel(Model model, T entity) {
        modelAfter(model, entity);
    }

    public abstract PageResponseModel<T> pageList(
        @RequestBody
            REQ request) throws Exception;

    /**
     * 添加之前
     *
     * @param req
     */
    protected void addBefore(T req) {

    }

    protected void addAfter(T req, boolean result) {

    }

    protected void updateBefore(T req) {

    }

    protected void updateAfter(T req, boolean result) {

    }

    protected void deleteBefore(T req) {

    }

    protected void deleteAfter(T req, boolean result) {

    }

    @RequestMapping(value = "/update",
        method = RequestMethod.POST,
        produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResponseCommonModel updateMapping(
        @RequestBody
            T request) throws Exception {
        updateBefore(request);
        boolean update = getCrudProcess().update(request);
        updateAfter(request, update);
        return ResponseUtils.trues();
    }

    @RequestMapping(value = "/del",
        method = RequestMethod.POST,
        produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResponseCommonModel delMapping(
        @RequestBody
            T request) throws Exception {
        deleteBefore(request);
        Object id = BeanUtil.getFieldValue(request, "id");
        boolean update = getCrudProcess().delete(id.toString());
        deleteAfter(request, update);
        return ResponseUtils.trues();
    }
}
