package weaver.interfaces.htxc.cgglpt;
import java.util.HashMap;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
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;


/**
 * 用来校验采购项目的招标请示按钮是否能发起流程
 */
/**
 * @Title: ecology-9
 * @Company: 泛微软件
 * @author: K1810027
 * @version: 1.0
 * @CreateDate: 2022-08-22 14:00
 * @Description:用来校验采购项目的招标请示按钮是否能发起流程
 * @ModifyLog:
 **/

public class CheckButtonModeExpandTemplate extends AbstractModeExpandJavaCodeNew {
    /**
     * 执行模块扩展动作
     * @param param
     *  param包含(但不限于)以下数据
     *  user 当前用户
     *  importtype 导入方式(仅在批量导入的接口动作会传输) 1 追加，2覆盖,3更新，获取方式(int)param.get("importtype")
     *  导入链接中拼接的特殊参数(仅在批量导入的接口动作会传输)，比如a=1，可通过param.get("a")获取参数值
     *  页面链接拼接的参数，比如b=2,可以通过param.get("b")来获取参数
     * @return
     */
    @Override
    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
            RecordSet rs = new RecordSet();
            PropBean pb = new PropBean();
            RequestInfo requestInfo = (RequestInfo)param.get("RequestInfo");
             if(requestInfo!=null){
                billid = Util.getIntValue(requestInfo.getRequestid());
                modeid = Util.getIntValue(requestInfo.getWorkflowid());
                if(billid>0 && modeid>0){
                    //------请在下面编写业务逻辑代码------
//                    String sql = "select * from uf_cgxq where id = "+billid;
//                    rs.execute(sql);
//                    rs.next();
//                    String ly = rs.getString("ly");//来源
//                    if("1".equals(ly)){
//                        result.put("flag", "true");
//                        return result;
//                    }
                    //获取通用采购申请二合一表单的名字
                    String tycgsqSql ="select * from workflow_bill where id = (select formid from workflow_base where unnumber =?)";
                    rs.executeQuery(tycgsqSql,"cgsq");
                    rs.next();
                    JAVATools.writelog("校验发起招标请示的按钮");
                    //String tycgsqTable = pb.getPropName("tycgsq_table");
                    String tycgsqTable = rs.getString("tablename");
                    String tycgrwdTableName = pb.getPropName("tycgrwd_tablename");
                    String tycgsqRequestid = getRequestidByBillId(tycgsqTable,billid,"_dt4");
                    JAVATools.writelog("校验发起招标请示的按钮tycgsqRequestid"+tycgsqRequestid);
                    String tycgrwdRequestid = "";
                    String tycgrwdRequestidDt5 = getRequestidByBillId(tycgrwdTableName,billid,"_dt5");
                    String tycgrwdRequestidDt9 =getRequestidByBillId(tycgrwdTableName,billid,"_dt9");
                    String tycgrwdRequestidDt10 =getRequestidByBillId(tycgrwdTableName,billid,"_dt10");
                    if(StringUtils.isNotBlank(tycgrwdRequestidDt5)){
                        tycgrwdRequestid =tycgrwdRequestidDt5;
                    }else if(StringUtils.isNotBlank(tycgrwdRequestidDt9)){
                        tycgrwdRequestid = tycgrwdRequestidDt9;
                    }else{
                        tycgrwdRequestid = tycgrwdRequestidDt10;
                    }
                    JAVATools.writelog("校验发起招标请示的按钮tycgrwdRequestid"+tycgrwdRequestid);
                    CheckComplete check = new CheckComplete();
                        if(check.isComplete(tycgsqRequestid)&&check.isComplete(tycgrwdRequestid)){
                            result.put("flag", "true");
                        }else{
                            if(!check.isComplete(tycgsqRequestid)){
                                result.put("msg","通用采购流程还没有办结，无法发起流程！");
                                result.put("errmsg","通用采购流程还没有办结，无法发起流程！");
                                result.put("flag", "false");
                            }
                            if(!check.isComplete(tycgrwdRequestid)){
                                result.put("msg","采购任务单流程还没有办结，无法发起流程！");
                                result.put("errmsg","采购任务单流程还没有办结，无法发起流程！");
                                result.put("flag", "false");
                            }
                        }
                }
            }
        } catch (Exception e) {
            result.put("errmsg","自定义出错信息");
            result.put("flag", "false");
        }
        return result;
    }

    private String getRequestidByBillId(String table, int billid, String dt) {
        RecordSet rs = new RecordSet();
        String sql = "select * from  "+table+dt+" where xmid = "+billid;
        rs.execute(sql);
        if(rs.next()){
            String mainid = rs.getString("mainid");
            String mainSql = "select requestid from "+table+" where id = "+mainid;
            rs.execute(mainSql);
            if(rs.next()){
                return rs.getString("requestid");
            }
        }
        return "";
    }

}
