package org.example.Impl;


import org.example.Utils.PushHelper;
import org.example.Utils.ResourceExecutors;
import org.example.Utils.WebConfigUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;
import java.util.function.BooleanSupplier;

public class PushImpl extends OptBaseImpl {
    private static final Logger log = LoggerFactory.getLogger(PushImpl.class);

    public void basePush(String primaryValue, String stepCode, String menuId, BooleanSupplier exec) {
        String beforeMenuName = "";
        String beforeUsers = "";
        int beforeId = 0;

        try {
            if (WebConfigUtil.isPushmsg()) {
                String sql = String.format(
                        "select operators, ToolsName as menuname, '1' + CAST(b.dllid as varchar(10)) as id " +
                                "from dbo.P_baseflowOper a, dbo.P_systemdlltab b " +
                                "where a.modid = b.DllCoid and keyvalue = '%s' and stepcode = '%s' and a.stepover = 0",
                        primaryValue, stepCode
                );

                List<Map<String, Object>> dtTable = jdbcTemplate.queryForList(sql);
                if (!dtTable.isEmpty()) {
                    Map<String, Object> row = dtTable.get(0);
                    beforeMenuName = row.get("menuname") != null ? row.get("menuname").toString() : "";
                    beforeUsers = row.get("operators") != null ? row.get("operators").toString() : "";
                    beforeId = row.get("id") != null ? Integer.parseInt(row.get("id").toString()) : 0;
                }
            }
        } catch (Exception e) {
            log.error("基础信息推送检测失败", e);
        }

        if (exec.getAsBoolean() && WebConfigUtil.isPushmsg()) {
            PushHelper push = new PushHelper(primaryValue, stepCode, getModuleCode(),
                    beforeUsers, beforeMenuName, beforeId);
            ResourceExecutors.submitPush(push::push_base_oper_message);
        }
    }


    public void billPush(String billId, String stepCode, String menuId, BooleanSupplier exec) {
        String beforeMenuName = "";
        String beforeUsers = "";
        int beforeId = 0;

        try {
            if (WebConfigUtil.isPushmsg()) {
                String sql = String.format(
                        "select operators, typeName as menuname, '2' + CAST(b.id as varchar(10)) as id " +
                                "from dbo.wms_billflowOper a, dbo.p_systembilltype b " +
                                "where a.modid = b.typeCode and keyvalue = '%s' and stepcode = '%s' and a.stepover = 0",
                        billId, stepCode
                );

                List<Map<String, Object>> dtTable = jdbcTemplate.queryForList(sql);
                if (!dtTable.isEmpty()) {
                    Map<String, Object> row = dtTable.get(0);
                    beforeMenuName = row.get("menuname") != null ? row.get("menuname").toString() : "";
                    beforeUsers = row.get("operators") != null ? row.get("operators").toString() : "";
                    beforeId = row.get("id") != null ? Integer.parseInt(row.get("id").toString()) : 0;
                }
            }
        } catch (Exception e) {
            log.error("读取ios清除信息失败", e);
        }

        if (exec.getAsBoolean() && WebConfigUtil.isPushmsg()) {
            PushHelper push = new PushHelper(billId, stepCode, menuId,
                    beforeUsers, beforeMenuName, beforeId);
            ResourceExecutors.submitPush(push::push_bill_oper_message);
        }
    }
}
