package com.ag.web.agCommon;

import com.ag.secp.platform.common.WbUtil;
import com.ag.util.CommonUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.wb.util.WebUtil;
import org.apache.commons.beanutils.PropertyUtils;
import org.json.JSONObject;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * 用于硕正可编辑Grid增删改最终保存
 */
@Service
public class ComBeanSupcan {
    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    public String forItem(List list, String type, HttpServletRequest request, String dataJson) {
        for (int i = 0; list != null && i < list.size(); i++) {
            Object appt = (Object) list.get(i);

            if (type.equals("insertSql")) {
                sqlSessionTemplate.insert(request.getParameter("insertSql"), appt);
                dataJson += JSON.toJSONString(appt) + ",";
            }
            if (type.equals("updateSql")) {
                try {
                    PropertyUtils.setProperty(appt, "updatedBy", (String) request.getSession().getAttribute("sys.username"));
                    PropertyUtils.setProperty(appt, "updatedOn", WbUtil.getNowDate());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                sqlSessionTemplate.update(request.getParameter("updateSql"), appt);
                dataJson += JSON.toJSONString(appt) + ",";
            }
            if (type.equals("deleteSql")) {

                sqlSessionTemplate.delete(request.getParameter("deleteSql"), appt);
                dataJson += JSON.toJSONString(appt) + ",";
            }
        }
        if (dataJson != null) {
            dataJson = dataJson.substring(0, dataJson.length() - 1);
            dataJson = dataJson.substring(4, dataJson.length());

        }
        return dataJson;
    }


    public List buildgridlist(HttpServletRequest request) {
        List datalist = new ArrayList();
        /**
         * 硕正封装
         */
        try {

            // Class objectClass = Class.forName(request.getParameter("className"));
            JSONArray jsonArray = (JSONArray) ((com.alibaba.fastjson.JSONObject) com.alibaba.fastjson.JSONObject.parse(
                    request.getParameter("datajson").replace("\\r", "").replace("\\n", "").
                            replace("\\", "").replace("\\t", ""))).
                    get("modifiedRow");
            for (int i = 0; i < jsonArray.size(); i++) {
                com.alibaba.fastjson.JSONObject jsonObjects = (com.alibaba.fastjson.JSONObject) jsonArray.get(i);
                JSONArray jsonArray1 = (JSONArray) jsonObjects.get("cols");
                for (int a = 0; a < jsonArray1.size(); i++) {
                    com.alibaba.fastjson.JSONObject jsonObjectsdetailcolumn = (com.alibaba.fastjson.JSONObject) jsonArray1.get(a);
                    String colname = jsonObjectsdetailcolumn.getString("col");
                    String colvalue = jsonObjectsdetailcolumn.getString("value");
                    // DynaBean dynaBean = DynaBean.create(objectClass);
                    //  dynaBean.set(colname, colvalue);
                }
            }
            return datalist;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }


    }


    @Transactional
    public void saveGrid(JSONObject object, HttpServletResponse response, HttpServletRequest request) throws ClassNotFoundException, NoSuchMethodException {
        // SpringApplicationContext.getBean(bean)
        Class objectClass = Class.forName(request.getParameter("className"));


        List createList = CommonUtil.getBeanlist(object, "datatable", "create", objectClass);
        List updateList = CommonUtil.getBeanlist(object, "datatable", "update", objectClass);
        List destroyList = CommonUtil.getBeanlist(object, "datatable", "destroy", objectClass);


        String dataJson = null;
        String createReturn = forItem(createList, "insertSql", request, dataJson);
        String updateReturn = forItem(updateList, "updateSql", request, dataJson);
        //删除的就不返回了
        forItem(destroyList, "deleteSql", request, dataJson);
        //拼JSON
        if (createReturn != null) {
            dataJson += "[" + createReturn;
            dataJson = dataJson.substring(4, dataJson.length());
        } else {
            dataJson += "[";
            dataJson = dataJson.substring(4, dataJson.length());
        }
        if (updateReturn != null && createReturn != null) {
            dataJson += "," + updateReturn + "]";
        } else if (updateReturn != null && createReturn == null) {
            dataJson += updateReturn + "]";
        } else {
            dataJson += "]";
        }

        try {
            WebUtil.send(response, dataJson);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
