package weaver.interfaces.htsc.hthy.action;

import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HthyAddSaveModeExpand extends AbstractModeExpandJavaCodeNew {
    /**
     * 执行模块扩展动作
     * @param param
     *  param包含(但不限于)以下数据
     *  user 当前用户
     *  importtype 导入方式(仅在批量导入的接口动作会传输) 1 追加，2覆盖,3更新，获取方式(int)param.get("importtype")
     *  导入链接中拼接的特殊参数(仅在批量导入的接口动作会传输)，比如a=1，可通过param.get("a")获取参数值
     *  页面链接拼接的参数，比如b=2,可以通过param.get("b")来获取参数
     * @return
     */
    public Map<String, String> doModeExpand(Map<String, Object> param) {
        Map<String, String> result = new HashMap<String, String>();
        try {

            BaseBean bb = new BaseBean();
            User user = (User)param.get("user");
            int billid = -1;//数据id
            int modeid = -1;//模块id
            RequestInfo requestInfo = (RequestInfo)param.get("RequestInfo");
            if(requestInfo!=null){
                billid = Util.getIntValue(requestInfo.getRequestid());
                modeid = Util.getIntValue(requestInfo.getWorkflowid());
                if(billid>0&&modeid>0){
                    RecordSet rs = new RecordSet();

                    List<Map<String,String>> list = new ArrayList<Map<String,String>>();
                    Map<String,String> map = null;
                    String sql = " select id,hylxxz,hylxfzzd from uf_hymb ";
                    bb.writeLog("sql:"+sql);
                    rs.execute(sql);
                    while (rs.next()){
                        map = new HashMap<String,String>();

                        String mbid = Util.null2String(rs.getString("id"));
                        String hylxxz = Util.null2String(rs.getString("hylxfzzd"));
                        map.put("mbid",mbid);
                        map.put("hylxxz",hylxxz);
                        list.add(map);
                    }

                    bb.writeLog("list:"+list.size());

                    for(int i=0;i<list.size();i++)
                    {
                        Map<String,String> datamap = list.get(i);
                        String mbid = Util.null2String(datamap.get("mbid"));
                        String hylxxz = Util.null2String(datamap.get("hylxxz"));

                        bb.writeLog("mbid:"+mbid);
                        bb.writeLog("hylxxz:"+hylxxz);

                        List<String> dt1list = new ArrayList<String>();
                        sql = " select cxry from uf_hyry_dt1 where mainid in (select id from uf_hyry where hylx = '"+hylxxz+"')";
                        bb.writeLog("sql:"+sql);
                        rs.execute(sql);
                        while (rs.next()){
                            String cxry = Util.null2String(rs.getString("cxry"));
                            dt1list.add(cxry);
                        }

                        List<String> listOne = new ArrayList<String>();
                        for(String cxry : dt1list)
                        {
                            String sql2 = " select id,zwx,yx,ryxm from uf_hyxx where sfxs = 0 and zwx = "+cxry+" and id="+billid;
                            bb.writeLog("sql2:"+sql2);
                            rs.execute(sql2);
                            if (rs.next()){
                                String idOne = Util.null2String(rs.getString("id"));
                                String zwOne = Util.null2String(rs.getString("zwx"));
                                String  yxOne = Util.null2String(rs.getString("yx"));
                                String dt1sql = " insert into uf_hymb_dt1(mainid,yx,xm,zwx) values ("+mbid+",'"+yxOne+"',"+idOne+","+zwOne+")";
                                listOne.add(dt1sql);
                            }
                        }
                        for(String dt1sql : listOne) {
                            bb.writeLog("dt1sql:"+dt1sql);
                            rs.executeUpdate(dt1sql);
                        }
                        List<String> dt2list = new ArrayList<String>();
                        rs.execute(" select lxry from uf_hyry_dt2  where mainid in (select id from uf_hyry where hylx = '"+hylxxz+"')");
                        while (rs.next()){
                            String lxry = Util.null2String(rs.getString("lxry"));
                            dt2list.add(lxry);
                        }
                        List<String> listTwo = new ArrayList<String>();
                        for(String lxry : dt2list)
                        {
                            String sql2 = " select id,zwx,yx,ryxm from uf_hyxx where sfxs = 0 and zwx = "+lxry +" and id="+billid;
                            rs.execute(sql2);
                            if(rs.next()){
                                String idTwo = Util.null2String(rs.getString("id"));
                                String zwTwo = Util.null2String(rs.getString("zwx"));
                                String  yxTwo = Util.null2String(rs.getString("yx"));
                                sql = " insert into uf_hymb_dt2(mainid,yx,xm,zwx) VALUES ("+mbid+",'"+yxTwo+"',"+idTwo+","+zwTwo+")";
                                listTwo.add(sql);
                            }
                        }

                        for(String dt2sql : listTwo) {
                            bb.writeLog("dt2sql:"+dt2sql);
                            rs.executeUpdate(dt2sql);
                        }

                        List<String> dt3list = new ArrayList<String>();
                        rs.execute(" select hyzcr from uf_hyry_dt3  where mainid in (select id from uf_hyry where hylx = '"+hylxxz+"') ");
                        while (rs.next()){
                            String hyzcr = Util.null2String(rs.getString("hyzcr"));
                            dt3list.add(hyzcr);
                        }

                        String zhrids= "" ;
                        for(String hyzcr:dt3list){
                            rs.execute(" select id,zwx,yx,ryxm from uf_hyxx where sfxs = 0 and zwx = "+hyzcr );
                            while (rs.next()){
                                String zhrid = Util.null2String(rs.getString("id"));
                                zhrids += zhrids == "" ? zhrid : ","+zhrid ;
                            }
                        }
                        bb.writeLog("zhrids:"+zhrids);

                        sql = " update uf_hymb set hyzcrx='"+zhrids+"' where id="+mbid ;
                        bb.writeLog("sql:"+sql);
                        rs.executeUpdate(sql);
                    }
                }
            }
        } catch (Exception e) {
            result.put("errmsg","自定义出错信息");
            result.put("flag", "false");
        }
        return result;
    }
}
