package weaver.interfaces.htsc.zgk;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.soa.workflow.request.RequestInfo;


/**
 *  上传宣传素材库文件，明细表数据存入查询uf_xcsck
 */
public class insertData extends AbstractModeExpandJavaCodeNew {

    public Map<String, String> doModeExpand(Map<String, Object> param) {
        Map<String, String> result = new HashMap<String, String>();
        try {
            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());
                PropBean pb = new PropBean();
                //modeid = Util.getIntValue(requestInfo.getWorkflowid());
                modeid = Integer.parseInt(pb.getPropName("xcsck_modeid"));
                if(billid>0&&modeid>0){
                    JAVATools.writelog("-----insertData----");
                    String sql = "";
                    RecordSet rs = new RecordSet();
                    RecordSet inRs = new RecordSet();
                    RecordSet rs2 = new RecordSet();
                    DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                    DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("HH:mm:ss");
                    //根据billid，获取明细数据
                    sql = "select * from uf_scsckwj_dt1 where MAINID = "+billid;
                    JAVATools.writelog(sql);
                    rs.execute(sql);
                    while (rs.next()){
                        String wjmc = rs.getString("wjmcfzzd");
                        wjmc = wjmc.substring(0,wjmc.lastIndexOf("."));
                        String scwj = rs.getString("wjmc");
                        String cjr = rs.getString("cjr");
                        String cjsj = rs.getString("cjsj");
                        String bcwj = rs.getString("bczl");
                        String wjlx = rs.getString("wjlx");
                        LocalDateTime now2 = LocalDateTime.now();
                        String nowDateStr = now2.format(formatter1);
                        String nowTimeStr = now2.format(formatter2);
                        String inSql = "insert into uf_xcsck (gxr,gxsj,MODEDATAMODIFIER,MODEDATAMODIFYDATETIME,wjmc,scwj,cjr,cjsj,bcwj,wjlx,ck,FORMMODEID,MODEDATACREATER,MODEDATACREATERTYPE,MODEDATACREATEDATE,MODEDATACREATETIME) " +
                                "values ('"+cjr+"','"+cjsj+"','"+cjr+"','"+cjsj+"','"+wjmc+"','"+scwj+"','"+cjr+"','"+cjsj+"','"+bcwj+"','"+wjlx+"','查看','"+modeid+"','"+cjr+"','0','"+nowDateStr+"','"+nowTimeStr+"')";
                        JAVATools.writelog(inSql);
                        boolean b = inRs.execute(inSql);
                        if (b){
                            // 建模权限重构
                            ModeRightInfo modeRightInfo = new ModeRightInfo();
                            modeRightInfo.setNewRight(true);
                            if (rs2.execute("select max(id) as maxid from uf_xcsck") && rs2.next()){
                                int id = rs2.getInt("maxid");
                                modeRightInfo.editModeDataShare(Integer.parseInt(cjr),modeid,id);
                            }
                        }
                    }
                }
            }
            result.put("flag", "true");
        } catch (Exception e) {
            result.put("errmsg","自定义出错信息");
            result.put("flag", "false");
        }
        return result;
    }

}