package weaver.interfaces.htsc.cowork;

import com.engine.portal.web.BackEndMenuAction;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetDataSource;
import weaver.conn.RecordSetTrans;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.UUID;

/**
 * Created by K1810007 on 2020/10/21.
 */

public class taskRequestReturnAction implements Action {
    @Override
    public String execute(RequestInfo requestInfo)
    {
//        RecordSetTrans rst = new RecordSetTrans();
//        rst.setAutoCommit(false);
//        rst.rollback();

//        RecordSetDataSource rsd = new RecordSetDataSource("test");
//        rsd.execute(sql);

        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        bb.writeLog("taskRequestReturnAction");
        String requestid = requestInfo.getRequestid();
        String formtable = requestInfo.getRequestManager().getBillTableName();
        int currentUserid = requestInfo.getRequestManager().getUser().getUID();

        bb.writeLog("requestid:"+requestid);
        bb.writeLog("formtable:"+formtable);
        bb.writeLog("currentUserid:"+currentUserid);

        if(!"".equals(formtable)){
            String remark = "" ;
            String sql = "  select logid,remark from workflow_requestlog where requestid="+requestid+" and logtype='3' and nodeid = 5525 order by logid desc ";
            bb.writeLog("sql:"+sql);
            rs.execute(sql);
            if(rs.next()){
                remark = Util.null2String(rs.getString("remark"));
            }
            bb.writeLog("remark:"+remark);

            String rw = "" ;
            sql = " select id from uf_rw where rwjslc = "+requestid;
            rs.execute(sql);
            if(rs.next()){
                rw = Util.null2String(rs.getString("id"));
            }
            String currentdate = TimeUtil.getCurrentDateString();
            String currenttime = TimeUtil.getOnlyCurrentTimeString();
            String uuid = UUID.randomUUID().toString();

            rs.executeQuery( "select m.id from modeinfo m left join WORKFLOW_BILL b on m.formid = b.id where b.tablename = 'uf_rwfk'" );
            rs.next();
            int formmodeid = Util.getIntValue( rs.getString( "id" ) );

//            dmap.put("formmodeid",formmodeid+"");
//            dmap.put("modedatacreater",modedatacreater);
//            dmap.put("modedatacreatertype",modedatacreatertype);
//            dmap.put("modedatacreatedate",modedatacreatedate);
//            dmap.put("modedatacreatetime",modedatacreatetime);

            sql = " insert into uf_rwfk(rw,fkr,fkxq,modedatacreater,modedatacreatedate,modedatacreatetime,formmodeid,modeuuid,modedatacreatertype) " +
                  " values(?,?,?,?,?,?,?,?,?) ";
            boolean exeRes = rs.executeUpdate(sql,new Object[]{rw,currentUserid,remark,currentUserid,currentdate,currenttime,formmodeid,uuid,"0"});
            int billid = -1;
            bb.writeLog("exeRes:"+exeRes);
            if(exeRes){
                //重构权限
                rs.execute(" select id from uf_rwfk where modeuuid='" + uuid + "' ");
                rs.next();
                billid = rs.getInt("id");
                bb.writeLog("billid:"+billid);

                ModeRightInfo moderight = new ModeRightInfo();
                moderight.editModeDataShare(currentUserid, formmodeid, billid);
            }else{
                requestInfo.getRequestManager().setMessageid("1111");
                requestInfo.getRequestManager().setMessagecontent("反馈意见增加失败,请联系系统管理员！");
                return Action.FAILURE_AND_CONTINUE;
            }
        }
        return Action.SUCCESS;
    }
}
