package weaver.interfaces.htsc.seal;

import com.weaver.general.BaseBean;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.text.SimpleDateFormat;
import java.util.Date;

public class ZgSealContractNumberAction extends BaseBean implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        RecordSet rs2 = new RecordSet();
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();//主表
        writeLog("----ZgSealContractNumberAction-------" + requestid);
        try{
            String mainid = "";
            String ngr = "";
            String szbm = "";
            String wjlb = "";
            String sql = "select id,ngr,szbm,wjlb from "+tablename+" where requestid = '"+requestid+"'";
            writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                mainid = Util.null2String(rs.getString("id"));
                ngr = Util.null2String(rs.getString("ngr"));
                szbm = Util.null2String(rs.getString("szbm"));
                wjlb = Util.null2String(rs.getString("wjlb"));
            }

            //当文件类别选择合同时，生成合同编号
            if("1".equals(wjlb)){
                String zh = "";//字号
                String zhid = "";
                sql = "select id,bmpy from uf_zgzh where bmbh = '"+szbm+"'";
                writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    zhid = Util.null2String(rs.getString("id"));
                    zh = Util.null2String(rs.getString("bmpy"));
                }

                Date now = new Date();
                String nowDate = new SimpleDateFormat("yyyyMMdd").format(now);
                String year = new SimpleDateFormat("yyyy").format(now);
                String formmodeid = " ";
                sql =" select id from modeinfo where formid in(select id from workflow_bill where tablename='uf_htbh')";
                rs.execute(sql);
                if(rs.next()){
                    formmodeid = Util.null2String(rs.getString("id"));
                }

                sql = "select id,wjmc,htbh from "+tablename+"_dt7 where sfsht = '1' and mainid = '"+mainid+"' order by id";
                writeLog(sql);
                rs.executeQuery(sql);
                while(rs.next()){
                    String htid = Util.null2String(rs.getString("id"));
                    String wjmc = Util.null2String(rs.getString("wjmc"));
                    String htbh = Util.null2String(rs.getString("htbh"));
                    if(StringUtils.isEmpty(htbh)){
                        String maxwh = "";//最大文号
                        sql = "select max(wh) as maxwh from uf_htbh where nf='"+year+"' and zh='"+zhid+"'";
                        writeLog(sql);
                        rs2.executeQuery(sql);
                        if(rs2.next()){
                            maxwh = Util.null2String(rs2.getString("maxwh"));
                        }
                        //---处理当前文号---
                        int wh = 1;
                        if(StringUtils.isNotEmpty(maxwh)){
                            wh = Integer.valueOf(maxwh)+1;
                        }
                        String whStr = String.format("%3d",wh).replace(" ","0");
                        htbh = "【华泰资管（"+nowDate+zh+"-"+whStr+"）号】";
                        String update_sql = "update "+tablename+"_dt7 set htbh = '"+htbh+"' where id = '"+htid+"'";
                        writeLog(update_sql);
                        rs2.executeUpdate(update_sql);

                        //---插入建模表---
                        String currTime = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(now);
                        String time = currTime.substring(0,currTime.length()-3);
                        String insert_sql = "insert into uf_htbh(zh,nf,wh,wjmc,cjsj,lcid,formmodeid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime) "+
                                " values('"+zhid+"','"+year+"','"+wh+"','"+wjmc+"','"+time+"','"+requestid+"',"+formmodeid+","+ngr+",0,'"+TimeUtil.getCurrentDateString()+"','"+TimeUtil.getOnlyCurrentTimeString()+"')";
                        writeLog(insert_sql);
                        boolean flag = rs2.execute(insert_sql);

                    }
                }
            }


            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            writeLog( "异常参考:" + e );
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "流程提交失败" );
            return Action.FAILURE_AND_CONTINUE;
        }
    }
}
