package weaver.interfaces.htxc.tycgsq.action;

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

import java.util.ArrayList;
import java.util.Objects;

/**
 * @Title: ecology-9
 * @Company: 泛微软件
 * @author: K1810027
 * @version: 1.0
 * @CreateDate: 2022-09-23 14:33
 * @Description:这个节点是用来终止节点送终止删除采购项
 * @ModifyLog:
 **/
public class ZzDeleteCgxAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        //请求ID
        String requestId = requestInfo.getRequestid();
        //表单名称
        String tableName = requestInfo.getRequestManager().getBillTableName();
//                String requestId = "5915920";
//        String tableName = "formtable_main_493";
        RecordSet rs = new RecordSet();
        String sql = "select * from  "+tableName + " where requestid = ?";
        rs.executeQuery(sql,requestId);
        if(rs.next()){
            String id = rs.getString("id");
            String lxlx = rs.getString("lxlx");
            switch (lxlx){
                case "0":deleteDt(tableName+"_dt5",id);break;
                case "1":deleteDt(tableName+"_dt9",id);break;
                default:deleteDt(tableName+"_dt10",id);break;
            }
        }
        return Action.SUCCESS;

    }

    public void deleteDt(String dt,String mainid){
        String sql = "select * from "+ dt+ " where mainid = ?";
        RecordSet rs = new RecordSet();
        rs.executeQuery(sql,mainid);
        ArrayList<String> list = new ArrayList<>();
        while(rs.next()){
            list.add(rs.getString("jmid"));
        }
        if(list.size()>0){
            String delSql = "delete from uf_cgxq where id in (?)";
            String join = StringUtils.join(list, ",");
            boolean b = rs.executeUpdate(delSql, list);
        }
    }
}
