package com.kingdee.eas.custom.dataconfig.handler;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.fi.arap.util.DBUtil;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.shr.base.syssetting.MSFServiceFacadeFactory;
import com.kingdee.shr.base.syssetting.context.SHRContext;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.web.handler.EditHandler;
import com.kingdee.shr.base.syssetting.web.json.JSONUtils;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

public class DataConfigEditHandler extends EditHandler {
    /**
    * @Author: LiuYing
    * @Description:
    * @DateTime: 2025/11/19 14:56
    * @Params: 校验数据是否正确
    * @Return:
    */
    public String dataConfigJYAction(HttpServletRequest request,
                                       HttpServletResponse response,
                                       ModelMap modelMap) throws SHRWebException {
        Context context = SHRContext.getInstance().getContext();
        String number = request.getParameter("number");
        if(number==null||number.equals("")){
            throw new RuntimeException("参数不正确");
        }
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("number",number);
        map.put("top","top 1");
        String sql = null;
        Map<String,Object> r = new HashMap<String,Object>();
        r.put("code",200);
        try {
            Object o = MSFServiceFacadeFactory.getLocalInstance(context).processService("getCompleteSqlService", map);
            if (o==null){
                throw new RuntimeException("配置错误");
            }
            sql = o.toString();
            IRowSet iRowSet1 = DBUtil.executeQuery(context, sql);
            r.put("sql",sql);
        } catch (BOSException e) {
            r.put("code",500);
            r.put("error",e.getCause().getMessage()+"\n"+e.getMessage());
            r.put("sql",sql);
            //throw new RuntimeException(e);
        } catch (EASBizException e) {
            r.put("code",500);
            r.put("error",e.getMessage());
            r.put("sql",sql);
        }
        JSONUtils.SUCCESS(r);
        return null;
    }
}
