package com.dgtly.common.core.controller;

import com.alibaba.fastjson.JSONObject;
import com.dgtly.common.config.BodyReaderHttpServletRequestWrapper;
import com.dgtly.common.constant.Constants;
import com.dgtly.common.core.domain.AjaxResult;
import com.dgtly.common.core.domain.ParameterObject;
import com.dgtly.common.core.page.PageDomain;
import com.dgtly.common.core.page.TableDataInfo;
import com.dgtly.common.core.page.TableSupport;
import com.dgtly.common.exception.api.LessParamException;
import com.dgtly.common.utils.DateUtils;
import com.dgtly.common.utils.ServletUtils;
import com.dgtly.common.utils.StringUtils;
import com.dgtly.common.utils.sql.SqlUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.beans.PropertyEditorSupport;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * API通用数据处理
 * 
 * @author ruoyi
 */
public class ApiBaseController
{
    protected final Logger logger = LoggerFactory.getLogger(ApiBaseController.class);

    /**
     * 将前台传递过来的日期格式的字符串，自动转化为Date类型
     */
    @InitBinder
    public void initBinder(WebDataBinder binder)
    {
        // Date 类型转换
        binder.registerCustomEditor(Date.class, new PropertyEditorSupport()
        {
            @Override
            public void setAsText(String text)
            {
                setValue(DateUtils.parseDate(text));
            }
        });
    }

    /**
     * 响应返回结果
     *
     * @param rows 影响行数
     * @return 操作结果
     */
    protected AjaxResult toAjax(int rows)
    {
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 响应返回结果
     *
     * @param result 结果
     * @return 操作结果
     */
    protected AjaxResult toAjax(boolean result)
    {
        return result ? AjaxResult.success() : AjaxResult.error();
    }
    /**
     * 设置请求分页数据
     */
    protected void startPage(ParameterObject obj)
    {
        Integer pageNum = null;
        Integer pageSize =null;
        String orderBy =null;
        PageDomain pageDomain = obj.parseBean(PageDomain.class);
        if(pageDomain!=null){
            pageNum = pageDomain.getPageNum();
            pageSize= pageDomain.getPageSize();
            orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
        }
        if (!StringUtils.isNotNull(pageNum)) {
            pageNum = 1;
         }
         if(! StringUtils.isNotNull(pageSize))
        {
            pageSize = 10;

        }

        PageHelper.startPage(pageNum, pageSize, orderBy);

    }

    /**
     * 获取request
     */
    public HttpServletRequest getRequest()
    {
        return ServletUtils.getRequest();
    }

    /**
     * 获取response
     */
    public HttpServletResponse getResponse()
    {
        return ServletUtils.getResponse();
    }

    /**
     * 获取session
     */
    public HttpSession getSession()
    {
        return getRequest().getSession();
    }

    /**
     * 响应请求分页数据
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    protected TableDataInfo getDataTable(List<?> list)
    {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }


    /**
     * 获取请求提交的json参数并封装为JSONObject
     * @return
     */
    public ParameterObject getParameterObject(){
        return new ParameterObject(getRequest());
    }

}
