package weaver.interfaces.htsc.cus;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;

public class RemarkThread implements Runnable {
    private RequestInfo requestInfo;
    private int nodeid;
    private Log log = LogFactory.getLog(this.getClass());

    public RemarkThread(RequestInfo requestInfo, int nodeid) {
        this.requestInfo = requestInfo;
        this.nodeid = nodeid;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String requestid = Util.null2String(requestInfo.getRequestid());
        RequestManager manager = requestInfo.getRequestManager();
        log.info("自定义出差反馈节点的签字意见----START-----"+requestid);
        try{
            String tableName = Util.null2String(manager.getBillTableName());
            String sql = String.format("select ccqk,xdfhsj,yqqxyy from %s where requestid = %s",tableName,requestid);
            RecordSet rs = new RecordSet();
            rs.execute(sql);
            if(rs.next()){
                int ccqk = Util.getIntValue(rs.getString("ccqk"),-2);//出差情况
                String xdfhsj = Util.null2String(rs.getString("xdfhsj"));//修订返回时间
                String yqqxyy = Util.null2String(rs.getString("yqqxyy"));//延期取消的原因
                int ccqkFieldid = Util.getIntValue(new PropBean().getPropName("ccqkfieldid"),-2);
                if(ccqk > -1 && ccqkFieldid > -1){
                    String selectName = getSelectName(ccqkFieldid,ccqk);
                    RecordSet rs_up = new RecordSet();
                    RecordSet rs_query = new RecordSet();
                    //过滤掉抄送
                    String sql_up = "select max(logid) logid from workflow_requestlog  \n" +
                            " where requestid = ? and nodeid = ? and LOGTYPE != 't'";
                    rs_query.executeQuery(sql_up,new Object[]{requestid,nodeid});
                    rs_query.next();
                    int logid = Util.getIntValue(rs_query.getString(1));
                    String remark = selectName + " "+xdfhsj + " " + yqqxyy;
                    // 0:延期 1：提前返回
                    if(ccqk > 1){ //即 既不是延期 也不是 提前  ，目前存在  取消 和按期 是不需要日期和原因的
                        remark = selectName;
                    }
                    rs_up.execute("update workflow_requestlog set remark = '"+remark+"' where logid = "+logid);
                }else{
                    log.error("自定义出差反馈节点的签字意见--取值配置文件有问题！ccqkFieldid="+ccqkFieldid+"&ccqk="+ccqk);
                }
            }else{
                log.error("自定义出差反馈节点的签字意见--主表数据异常！sql="+sql);
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("自定义出差反馈节点的签字意见捕获异常");
        }
    }

    /**
     * 取出差情况的显示名称
     * @param fieldid 字段ID
     * @param selectvalue 字段值
     * @return 显示名
     */
    private String getSelectName(int fieldid,int selectvalue){
        String sql = String.format("select selectname from workflow_selectitem where fieldid = %d and selectvalue = %d ",fieldid,selectvalue);
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        rs.next();
        return Util.null2String(rs.getString(1));
    }
}
