package com.eversec.dynamic;

import com.eversec.common.JBean;
import com.eversec.common.PageBean;
import com.eversec.dynamic.entity.DnyAttr;
import com.eversec.dynamic.entity.DnyCate;

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

/**
 * 动态数据管理
 * Created by liangxianyou at 2018/09/21
 */
public class DnyController {

    private DnyService dnyService;

    /**
     * 保存表数据
     * @param request
     */
    public String save(HttpServletRequest request, DnyCate dnyCate){
        //deal Attr
        dnyCate.getAttrs().forEach(x->{
            dnyCate.set(x.getAttr(), request.getParameter(x.getAttr()));
        });

        dnyService.save(dnyCate);

        return JBean.by(200,"").toJson();
    }

    /**
     * 查询某一类别列表数据-（行转列）
     * @param request
     * @param dnyCate
     * @param pn
     * @param ps
     * @return
     */
    public String findList(HttpServletRequest request, DnyCate dnyCate, int pn, int ps){
        //deal Attr
        dnyCate.getAttrs().forEach(x->{
            dnyCate.set(x.getAttr(), request.getParameter(x.getAttr()));
        });

        PageBean list = dnyService.findList(dnyCate, pn, ps);

        return JBean.by(200,"", list).toJson();
    }


    //---------- 属性管理 -----------
    /**
     * 数据类型主体属性列表
     * @param dnyCate
     * @return
     */
    public String findAttr(DnyCate dnyCate){
        List<DnyAttr> attrs = dnyCate.getAttrs();
        return JBean.by(200,"", attrs).toJson();
    }

    /**
     * 保存属性
     * @param dnyAttr
     * @return
     */
    public String saveAttr(DnyAttr dnyAttr){
        dnyService.saveAttr(dnyAttr);
        return JBean.by(200,"").toJson();
    }
}
