package weaver.interfaces.htsc.workflow.odoc;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;


/**
 * Created by K1810001 on 2020/11/10.
 */
public class DetailDefaultOutAction extends BaseBean implements Action {
    private Log log = LogFactory.getLog(this.getClass());

    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        RecordSet dtRs = new RecordSet();
        JSONObject restultObj = new JSONObject();
        BaseBean bs = new BaseBean();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String requestid = requestInfo.getRequestid();
        User requser = requestInfo.getRequestManager().getUser();
        int createrid = requser.getUID();

        log.info("开始:"+createrid);
        ResourceComInfo resourceComInfo = null;
        log.info("执行了"+tablename+"--"+requestid);
        try{
            resourceComInfo = new ResourceComInfo();

            //根据requestid和creatorid获取该条数据的id
            String idSql = "select id,ldsp,ldqf from "+tablename+" where requestid = ?";
            rs.executeQuery(idSql,requestid);
            bs.writeLog("idSql:"+idSql);
            if(rs.next()){
                String id = Util.null2String(rs.getString("id"));
                String ldsp = Util.null2String(rs.getString("ldsp"));
                String ldqf = Util.null2String(rs.getString("ldqf"));
                //拆分领导审批和领导签发字段
                String detailtablefive = tablename+"_dt5";
                //1.退回流程 插入数据前将历史明细数据清空；
                String deleteSqlfive = "delete from "+detailtablefive+" where mainid ="+id;
                dtRs.executeUpdate(deleteSqlfive);
                if (!"".equals(ldsp)){
                    String dtfiveSql = "insert into "+detailtablefive+"(mainid,ry,gh,xz) values(?,?,?,?)";
                    String[] ldspsplit = ldsp.split(",");
                    bs.writeLog("ldspsplit:"+ldspsplit);
                    for (String ldspid : ldspsplit){
                        String workcode = resourceComInfo.getWorkcode(ldspid);
                        dtRs.executeUpdate(dtfiveSql,new Object[]{id,ldspid,workcode,"1"});
                        bs.writeLog("dtfivesql:"+dtfiveSql);
                    }
                }

                String detailtablesix = tablename+"_dt6";
                String deleteSqlsix = "delete from "+detailtablesix+" where mainid ="+id;
                dtRs.executeUpdate(deleteSqlsix);
                if (!"".equals(ldqf)){
                    String dtsixSql = "insert into "+detailtablesix+"(mainid,ry,gh,xz) values(?,?,?,?)";
                    String[] ldqfsplit = ldqf.split(",");
                    for (String ldqfid : ldqfsplit){
                        String workcode = resourceComInfo.getWorkcode(ldqfid);
                        dtRs.executeUpdate(dtsixSql,new Object[]{id,ldqfid,workcode,"1"});
                    }
                }
            }
            restultObj.put("status","1");

        }catch (Exception e){
            e.printStackTrace();
            restultObj.put("status","-1");
        }
        return null;
    }
}
