package com.weaver.seconddev.njwebservice;

import com.weaver.common.hrm.util.StringUtils;
import com.weaver.common.i18n.tool.util.I18nContextUtil;
import com.weaver.seconddev.njwebservice.entity.EasSpzfResponseInfo;
import com.weaver.seconddev.njwebservice.entity.EasTzlhtSpzfRes;
import com.weaver.seconddev.njwebservice.entity.EasTzlhtSpzfResData;
import com.weaver.seconddev.njwebservice.util.SecondUtil;
import com.weaver.seconddev.njwebservice.util.WfUtil;
import com.weaver.verupgrade.conn.CONN_TYPE;
import com.weaver.verupgrade.conn.RecordSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Author liang.cheng
 * @Date 2025/1/20 17:37
 * @Description: 投资类合同审批作废接口
 * @Version 1.0
 */
@Service("easTzlhtSpzfApplicationWebService")
public class EasTzlhtSpzfApplicationService {

    private final static Logger log = LoggerFactory.getLogger(EasTzlhtSpzfApplicationService.class);

    public EasSpzfResponseInfo easTzlhtSpzfApplicate(EasTzlhtSpzfRes param) {
        log.error("EasTzlhtSpzfApplicationImpl-----start"+param.toString());

        RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
        String poolname = CONN_TYPE.workflow.getType();

        EasSpzfResponseInfo res = new EasSpzfResponseInfo();
//        if(param.getDATAS()!=null&&param.getDATAS().length>0) {
        EasTzlhtSpzfResData data = param.getDATAS();
        String loginName = data.getLoginname();
        String password = data.getPassword();
        String workflowId = data.getWorkflowId();
        String requestId = data.getRequestId();
        String optType = data.getOptType();

        if (!"EAS".equals(loginName) && !"123456".equals(password)) {
            res.setIsTat("F");
            res.setMessage("接口用户名/密码不对！");
            return res;
        }
        if (workflowId == null || workflowId.equals("")) {
            res.setIsTat("F");
            res.setMessage("流程id为空！");
            return res;
        }
        if (requestId == null || requestId.equals("")) {
            res.setIsTat("F");
            res.setMessage("请求id为空！");
            return res;
        }


        WfUtil wfUtil = new WfUtil();
        Map<String, String> zhWfMap = wfUtil.getZhWfMapNoUser("t6kj9c07jr", workflowId);

        workflowId = zhWfMap.get("workflowid");
        log.error("转换后的流程ID:"+workflowId);
        if(StringUtils.isEmpty(workflowId)) {
            res.setIsTat("F");
            res.setMessage("E10流程ID转换失败，请假查e10_common.dbo.uf_zhwfid_mt配置信息");
            return res ;
        }

        String createTenantKey = zhWfMap.get("tenantKey");
        if(StringUtils.isEmpty(createTenantKey)) {
            res.setIsTat("F");
            res.setMessage("E10获取流程对应租户失败，请假查e10_common.dbo.uf_zhwfid_mt配置信息");
            return res;
        }


        String tablename = SecondUtil.getTableName(workflowId);
        log.error("tablename:"+tablename);
        String isYX = "0";
        if ("N".equals(optType)) {//流程作废
            isYX = "1";
        }

        String sql = "update e10_core_business.dbo." + tablename + " set sfyx ='" + isYX + "' where id = " + requestId;

        log.error("sql:"+sql);
        boolean b1 = rs.executeSql(sql, poolname);
        log.error("b1:"+b1);
        res.setIsTat("S");
        res.setMessage("接口调用成功！");
        return res;

//        }else{
//            res.setISTAT("F");
//            res.setMESSAGE("请传入请求参数！");
//            return res;
//        }
    }

}
