package com.ag.dynamicmodule.common;

import com.ag.base.DefaultBussniessImpl;
import com.ag.log.OperLogServcie;
import com.ag.log.entity.LogParam;
import com.ag.util.CommonUtil;
import com.ag.dynamicmodule.service.IBaseServiceImpl;
import com.alibaba.fastjson.JSON;
import com.wb.util.WebUtil;
import org.json.JSONObject;
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.List;

/**
 * Created by WangN on 2017/9/9.
 * 用于可编辑Grid增删改最终保存
 */

/**
 *
 */
@Service
public class PsamatheComBean {
    /*@Autowired
    private SqlSessionTemplate sqlSessionTemplate;*/
    @Autowired
    private IBaseServiceImpl baseService;
    @Autowired
    private DefaultBussniessImpl bizDao;
    @Autowired
    private OperLogServcie logServcie;

    public String forItem(List list, HttpServletRequest request, String dataJson) {
        for (int i = 0; list != null && i < list.size(); i++) {
            Object appt =  list.get(i);
            baseService.setParameterValue(appt,request);
            dataJson += JSON.toJSONString(appt) + ",";
            /*if (type.equals("insertSql")) {
                baseService.setParameterValue(appt,request);
                sqlSessionTemplate.insert(request.getParameter("insertSql"), appt);
                dataJson += JSON.toJSONString(appt) + ",";
            }
            if (type.equals("updateSql")) {
                baseService.setParameterValue(appt,request);
                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;
    }


    @Transactional
    public  void saveGrid(JSONObject object, HttpServletResponse response, HttpServletRequest request) throws ClassNotFoundException, NoSuchMethodException {
       // SpringApplicationContext.getBean(bean)
        String[] needCols = CommonUtil.getNeedColsByJson(object, null);
        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, request, dataJson);
        String updateReturn=forItem(updateList, request, dataJson);
        bizDao.saveAll(createList);
        bizDao.updateAll(updateList, needCols, "ID");
        bizDao.deleteAll(destroyList, "ID");
        //删除的就不返回了
        //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+="]";
        }
        if (object.has("keyword") && object.has("bizType")){
            try {
                LogParam logParam = new LogParam(object, object.getString("keyword"), object.getString("bizType"), null, "datatable");
                logParam.setMenuName(objectClass.getSimpleName());
                logParam.setCreate(createList);
                logServcie.saveLogGrid(logParam, response);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            WebUtil.send(response, dataJson);
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
}
