package com.ag.dynamicmodule.wbgrid.domain;

import com.ag.dynamicmodule.service.IBaseServiceImpl;
import com.ag.dynamicmodule.wbgrid.pojo.WbResource;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.wb.util.WebUtil;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * Description: 表格定制
 * Author: running
 * Version: 1.0
 * Create Date: 2020-11-12 10:40:09
 */
@Component
public class WbgridService {
    @Autowired
    private IBaseServiceImpl baseService;

    @Autowired
    private WbResourceBO wbResourceBO;

    /**
     * Description: 表格定制保存
     *
     * @param object
     * @param response
     * @param request
     * @return Note: Nothing much.
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveResourceinfo(JSONObject object, HttpServletResponse response, HttpServletRequest request) throws UnsupportedEncodingException {


        String dataarr = object.getString("dataArray");

        WbResource wbResource = new WbResource();
        wbResource.setResContent(dataarr.getBytes("UTF-8"));

        wbResource.setResId(object.getString("name") + "@" + request.getSession().getAttribute("sys.user"));

        List<WbResource> datalit = wbResource.queryList("RES_ID");

        if (datalit.size() == 0) {
            wbResourceBO.save(wbResource);
        } else {
            wbResource.delete("RES_ID");
            wbResourceBO.save(wbResource);
        }


        try {
            String reJson = JSON.toJSONString(null);
            WebUtil.send(response, null);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}