package com.wb.common;

import com.alibaba.fastjson.JSON;
import org.apache.commons.beanutils.BeanUtils;
import org.json.JSONObject;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

/**
 * Description:定义wb用的方法
 * Author: wangll
 * Version: 1.0
 * Create Date: 2020/3/5 0005
 */
public class WbComUtil {

    /**
     * Description:设置
     * @param bean
     * @param request
     * @return Note: Nothing much.
     */
    public static Object setParameterValue(Object bean, HttpServletRequest request) {
        try {
            if (StringUtils.isEmpty(BeanUtils.getProperty(bean, "tenancyId"))) {
                BeanUtils.setProperty(bean,"tenancyId",request.getSession().getAttribute("sys.tenancyId"));
                BeanUtils.setProperty(bean,"createdBy",request.getSession().getAttribute("sys.username"));
                BeanUtils.setProperty(bean,"createdOn", new Date());
            }
            BeanUtils.setProperty(bean, "updatedBy", request.getSession().getAttribute("sys.username"));
            BeanUtils.setProperty(bean, "updatedOn", new Date());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return  bean;
    }

    /**
     * Description:设置变量
     * @param bean
     * @param request
     * @param bool
     * @return Note: Nothing much.
     */
    public static  Object setParameterValue(Object bean, HttpServletRequest request,boolean bool) {
        try {
            if (bool) {
                if(StringUtils.isEmpty(BeanUtils.getProperty(bean, "createdBy"))) {
                    BeanUtils.setProperty(bean, "createdBy", request.getSession().getAttribute("sys.username"));
                }
                if (StringUtils.isEmpty(BeanUtils.getProperty(bean, "createdOn"))) {
                    BeanUtils.setProperty(bean, "createdOn", new Date());
                }
            }
            BeanUtils.setProperty(bean, "updatedBy", request.getSession().getAttribute("sys.username"));
            BeanUtils.setProperty(bean, "updatedOn", new Date());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return  bean;
    }

    /**
     * Description:返回表格的beanlist
     * @param object
     * @param dataparam
     * @param type
     * @param objeclass
     * @return Note: Nothing much.
     */
    public static List getBeanlist(JSONObject object, String dataparam, String type, Class objeclass){
        String datarec1=((com.alibaba.fastjson.JSONObject) JSON.parseArray(object.getString(dataparam)).get(0)).get(type).toString();
        return JSON.parseArray(datarec1,objeclass);
    }
}
