package com.weaver.seconddev.interfaces.workflow.action;

import com.alibaba.fastjson.JSON;
import com.weaver.authority.api.rest.RemoteAuthorityRoleService;
import com.weaver.common.base.entity.result.WeaResult;
import com.weaver.common.hrm.util.HrmContextUtil;
import com.weaver.common.i18n.tool.util.I18nContextUtil;
import com.weaver.ebuilder.form.client.ebform.data.EbFormDataDubboService;
import com.weaver.esb.api.rpc.EsbServerlessRpcRemoteInterface;
import com.weaver.framework.rpc.annotation.RpcReference;
import com.weaver.seconddev.interfaces.prop.ConfigProp;
import com.weaver.seconddev.interfaces.workflow.util.CommonDevUtil;
import com.weaver.teams.domain.user.SimpleEmployee;
import com.weaver.verupgrade.conn.CONN_TYPE;
import com.weaver.verupgrade.conn.RecordSet;
import com.weaver.verupgrade.general.BaseBean;
import com.weaver.verupgrade.interfaces.workflow.action.Action;
import com.weaver.verupgrade.soa.workflow.request.RequestInfo;
import com.weaver.verupgrade.workflow.request.RequestManager;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

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


@Service("workflow_action_CustomTeAction")
public class CustomTeAction extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {


    @Autowired
    private RequestInfo requestInfoTemp;

    @Autowired
    private CommonDevUtil commonDevUtil;

    @Autowired
    private ConfigProp configProp;

    @RpcReference(group = "ebuilderform")
    private EbFormDataDubboService ebFormDataDubboService;

    @RpcReference(group = "ebuilderform")
    private RemoteAuthorityRoleService remoteAuthorityRoleService;


    @Value("${weaver.swagger.basePackage}")
    private String basePackage;
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(CustomTeAction.class);


    @Override
    public WeaResult<Map<String, Object>> execute(Map<String, Object> params) {
        logger.error("start CustomTeAction");
//        logger.error("lplysl_url == " + configProp.getLplysl_url());
        logger.error("basePackage == " + basePackage);
        logger.error("params = " + JSON.toJSONString(params));

        Map<String, Object> weaverResultMap = new HashMap<>();
        Long requestIdTemp = Long.parseLong(String.valueOf(params.getOrDefault("requestid", -1)));
        Long userIdTemp = Long.parseLong(String.valueOf(params.getOrDefault("userid", -1)));
        RequestInfo request = requestInfoTemp.getRequestInfo(requestIdTemp, userIdTemp);

        try {

            RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
//
//            String sql = "select count(1) sl from ATDPERSONCARDEXVIEW where extype='未刷卡' and SHOULDDATE>='2024-10-01' and SHOULDDATE<='2024-12-30'";
//            logger.error("sql == " + sql );
//            boolean count = rs.executeQuery(sql, "hr");
//            logger.error("count == " + count );
//            if(rs.next()){
//                String sl = rs.getString("sl");
//                logger.error("sl == " + sl );
//            }
//
////            String sql1 = "select ID,EMPLOYEEID,SHOULDDATE,SHOULDTIME,CARDCOUNT,CARDORDER from ATDPERSONCARDEXVIEW where extype='未刷卡' and SHOULDDATE>='2024-10-01' and SHOULDDATE<='2024-12-30'";
//            String sql1 = "select top 500 *  " +
//                    "from ( " +
//                    "select row_number() over(order by id asc) as rownumber ,ID,EMPLOYEEID,SHOULDDATE,SHOULDTIME,CARDCOUNT,CARDORDER from ATDPERSONCARDEXVIEW where extype='未刷卡' and SHOULDDATE>='2024-10-01' and SHOULDDATE<='2024-12-30' " +
//                    " ) temp_row "+
//                    "where rownumber>1000;";
//            logger.error("sql1 == " + sql1 );
//
//            boolean detail = rs.executeQuery(sql1, "hr");
//
//            logger.error("detail == " + detail );
////            logger.error("rs.getExecuteResult   == " + rs.getExecuteResult() );
//            if (rs.next()){
//                logger.error("EMPLOYEEID == " + rs.getString("EMPLOYEEID") );
//            }
//

            // 测试配置文件读取
//            String url = Util.null2String(I18nContextUtilcus.getBean(Environment.class).getProperty("weaver-secondev-htportaloaUrl.accountUrl")).trim();


//            logger.error("url = " + testProp.getAccountUrl());

            SimpleEmployee currentEmployee = HrmContextUtil.getCurrentUser();

            String tenantKey = HrmContextUtil.getCurrentTenantKey();
            logger.error("tenantKey = " + tenantKey);

//            RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
            //        String sql1 = "select field1  from  e10_common.dbo.uf_CustomFlow  where field1  = 5 and delete_type = 0";


            String sql1 = "select id,field3 from eteams.dbo.hrmemployeedefined where FORM_DATA_ID= '100005650000063120' ";
//            String sql1 = "select   field3 from eteams.dbo.hrmemployeedefined where   FORM_DATA_ID= '100005650000000052'";

            String poolname = CONN_TYPE.hrm.getType();

            logger.error("sql1 =" + sql1);
            logger.error("poolname =" + poolname);

            try {
                rs.executeSql(sql1, poolname);
            } catch (Exception e) {
                logger.error("rs-------", e);
                logger.error("getExecuteResult =" + rs.getExecuteResult());
            }


            logger.error("执行rs.executeSql成功");

            if (rs.next()) {
                String field3 = rs.getString("id");
                logger.error("field3 = " + field3);
            }
            //
            //
            //        String sql2 = "update  e10_common.dbo.uf_CustomFlow  set field2 = '修改后数据' where field1  = 2 and delete_type = 0 ";
            //        boolean b = rs.executeSql(sql2, poolname);
            //        logger.error("更新 = " + b);
            //
            //
            //        //rpc新增建模数据
                    Long uf_CustomFlow_formId = commonDevUtil.getObjId("uf_CustomFlow");
                    logger.error("uf_CustomFlow_formId = " + uf_CustomFlow_formId);
                    Long recodId = ebFormDataDubboService.addEmptyData(uf_CustomFlow_formId, currentEmployee);// 获取插入的空的建模id
                    String sql3 = "update  e10_common.dbo.uf_CustomFlow  set field2 = '添加数据' where id  =  " + recodId + " and delete_type = 0 ";
                    boolean b1 = rs.executeSql(sql3, poolname);
                    logger.error("sql3 = " + sql3);
                    logger.error("添加 = " + b1);

            logger.error("stop CustomTeAction");
            Map<String, Object> res = new HashMap<>();
            res.put("status", true);
            res.put("msg", "success");
            res.put("params", params);
            return WeaResult.success(res);
        } catch (Exception e) {
            logger.error("异常信息 ==== " + e.getMessage());
            e.printStackTrace();
            return WeaResult.fail(500, "出错啦", getResultMapForAction(weaverResultMap, "result", e.getMessage(), request.getRequestManager()));
        }

//
    }

    Map<String, Object> getResultMapForAction(Map<String, Object> map, String key, Object value, RequestManager requestManager) {

        if (key != null && !key.isEmpty()) {
            map.put(key, value);
        }
        String msgContent = requestManager.getMessagecontent();
        if (msgContent != null && !msgContent.isEmpty()) {
            map.put("msgContent", msgContent);
        }
        String msgId = requestManager.getMessageid();
        if (msgId != null && !msgId.isEmpty()) {
            map.put("msgId", msgId);
        }
        return map;
    }




























}
