package com.app.ydl.pay_v3.controller;

import com.alibaba.fastjson.JSONObject;
import com.app.framework.core.support.controller.CrudControllerSupport;
import com.app.framework.core.support.view.form.AjaxResult;
import com.app.framework.core.support.view.form.ModelEditForm;
import com.app.framework.core.support.view.grid.ExGridLoad;
import com.app.framework.core.support.view.grid.Paging;
import com.app.ydl.pay_v3.service.YdlDmpPayApplyService;
import com.app.ydl.pay_v3.view.YdlDmpPayApplyEditView;
import com.app.ydl.pay_v3.view.YdlDmpPayApplyView;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * 项目管理
 */
@Controller
@RequestMapping("/dmp/pay_apply")
public class YdlDmpPayApplyController extends CrudControllerSupport {

    @Autowired
    YdlDmpPayApplyService ydlDmpPayApplyService;

    /**
     * 跳转到列表页面
     */
    @RequestMapping(value = "/golist")
    public ModelAndView goList(HttpServletRequest request) {
        return new ModelAndView("admin/business/pay_v3/list.html");
    }

    /**
     * 跳转到编辑页面
     */
    @RequestMapping(value = "/edit")
    public ModelAndView edit(HttpServletRequest request, String appId, ModelEditForm editForm) {
        JSONObject object = null;
        HashMap<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(appId)) {
            object = ydlDmpPayApplyService.queryById(appId);
            object.put("app_id", object.getString("id"));
        }
        map.put("entity", object);
        map.put("editForm", editForm);
        return new ModelAndView("admin/business/pay_v3/edit.html", map);
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    public ExGridLoad listByPaging(YdlDmpPayApplyView view, Paging paging) {
        List<JSONObject> list = ydlDmpPayApplyService.query(view, paging);
        list.forEach(object -> {
            object.put("app_id", object.getString("id"));
        });
        return ExGridLoad.wrapExGridLoad(list, paging);
    }

    @RequestMapping("/save")
    @ResponseBody
    public AjaxResult save(YdlDmpPayApplyEditView editView) {
        return ydlDmpPayApplyService.save(editView);
    }

}