package com.engine.htsc.jjhtyfk.cmd;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.htsc.util.HT_WorkflowInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author Weaver.SongYuchen
 * @Date 2022-03-31
 * @Description 付款流程联动用印流程合同信息
 */
public class GetContractInfoCmd extends AbstractCommonCommand<Map<String, Object>> {
    public GetContractInfoCmd(User user, Map<String, Object> params) {
        this.user = user;
        this.params = params;
    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    /**
     * 获取用印流程合同信息
     *
     * @param commandContext
     * @return
     */
    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> resultMap = new HashMap<>();
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();

        try {
            String requestId = Util.null2String(params.get("code")); //获取用印流程requestid
            HT_WorkflowInfo HT_WorkflowInfo = new HT_WorkflowInfo();
            String formTableName = HT_WorkflowInfo.getFormTableByRequestid(requestId); //根据requestid获取流程表名

            bb.writeLog("用印流程requestId: " + requestId);
            bb.writeLog("用印流程表名: " + formTableName);

            //查询用印流程主表数据
            String sql = "select id, htje, yfje from " + formTableName + " where requestid = '" + requestId + "'";
            bb.writeLog("sql: " + sql);
            String mainId = "";
            rs.execute(sql);
            if (rs.next()) {
                bb.writeLog("合同金额: " + Util.null2String(rs.getString("htje")) + "   已付金额：" + Util.null2String(rs.getString("yfje")));
                mainId = Util.null2String(rs.getString("id"));
                resultMap.put("htje", Util.null2String(rs.getString("htje")));
                resultMap.put("yfje", Util.null2String(rs.getString("yfje")));
            }

            //查询用印流程明细表数据
            sql = "select wjmc, htbh from " + formTableName + "_dt7 where mainid = '" + mainId + "' and sfsht = 1";
            bb.writeLog("sql: " + sql);
            rs.execute(sql);
            if (rs.next()) {
                bb.writeLog("文件名称: " + Util.null2String(rs.getString("wjmc")) + "   合同编号：" + Util.null2String(rs.getString("htbh")));
                resultMap.put("htbh", Util.null2String(rs.getString("htbh")));

                //根据文件id获取文件名
                sql = "select k.imagefilename from DocDetail t inner join DocImageFile k on k.docid = t.id \n" +
                        "where t.id in (" + Util.null2String(rs.getString("wjmc")) + ")";
                bb.writeLog("sql: " + sql);
                rs.execute(sql);
                if (rs.next()) {
                    resultMap.put("wjmc", Util.null2String(rs.getString("imagefilename")));
                }
            }
        } catch (Exception e) {
            bb.writeLog("获取合同信息异常, error: " + e.getMessage());
            resultMap.put("error", e.getMessage());
            return resultMap;
        }
        return resultMap;
    }
}
