package com.api.customization.qc2738217.action;

import com.api.customization.qc2738217.util.CubOscUtil;
import com.weaver.general.Util;
import weaver.email.EmailWorkRunnable;
import weaver.general.BaseBean;
import weaver.general.StringUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.Map;

/**
 * ClassName:com.api.customization.qc2738217.action.CubEmailRunnableAction
 *
 * @date:2023/12/26 10:35
 * @author:Oscar
 */
public class CubEmailRunnableAction extends BaseBean implements Action {
    CubOscUtil cubOscUtil=new CubOscUtil();
    private RequestInfo requestinfo = null;
    private String check(String msg) {
        if (StringUtil.isEmpty(msg)) {
            return "1";
        } else {
            this.requestinfo.getRequestManager().setMessageid(System.currentTimeMillis() + "");
            this.requestinfo.getRequestManager().setMessagecontent(msg);
            return "0";
        }
    }
    public String execute(RequestInfo requestInfo){
        this.requestinfo=requestInfo;
        String requestid = requestinfo.getRequestid();
        String tablename = requestinfo.getRequestManager().getBillTableName();
        try {
            String msg=null;
            //主表数据
            String mainSql = "select * from "+tablename+" where requestId="+requestid;
            //cubOscUtil.outlog("mainSql="+mainSql,"");
            Map mainMap = cubOscUtil.getSqlToMap(mainSql);
            if (mainMap==null){
                msg = "查不到主表数据";
                return check(msg);
            }
            Map demoMap = cubOscUtil.getSqlToMap("select * from uf_cubyjmbb");
            String subject=Util.null2String((String) demoMap.get("zt"));
            String content=Util.null2String((String) demoMap.get("nr"));
            String wfryyx= Util.null2String((String) mainMap.get("wfryyx"));
            //建模表存储字段名
            subject=Util.null2String((String) mainMap.get(subject));
            content=Util.null2String((String) mainMap.get(content));
            EmailWorkRunnable emailWorkRunnable = new EmailWorkRunnable(wfryyx,"","", subject, content);
            if(!emailWorkRunnable.emailCommonRemind()){
                msg="邮件发送失败！";
            }

            return check(msg);
        }catch (Exception e){
            cubOscUtil.outlog("发送邮件报错信息="+e.getMessage(),"");
            e.printStackTrace();
            return check(e.getMessage());
        }

    }
}
