package com.weaver.seconddev.chapanda.sap.dao;

import com.alibaba.fastjson.JSON;
import com.weaver.ebuilder.datasource.api.entity.SqlParamEntity;
import com.weaver.seconddev.chapanda.sap.util.DatabaseUtils;
import com.weaver.seconddev.chapanda.sap.constant.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.*;

@Component
public class Esb2SapUpdateIncorporatedDao {

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

    @Autowired
    private DatabaseUtils databaseUtils;

    /***
     *
     * @return
     */
    public Map<String, Object> updateIncorporatedCompany(String BUKRS_T,String BUKRS){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        Map<String, Object> result = new HashMap<String, Object>();
        try{
            String dataSql =" update "+Constants.hrmLegalEntity+" set sapbm = '"+BUKRS+"' " +
                            " where delete_type = 0\n" +
                            " and mc ='"+BUKRS_T+"' and tenant_key = '"+Constants.TENANT_KEY+"'" ;

            log.error("dataSql:" + dataSql);
            result = databaseUtils.execute(sourceType, groupId, dataSql);

            log.error("result:{}", JSON.toJSONString(result));

        }catch (Exception e){
            log.error("e:" + e);
        }
        return result;
    }


    public String queryCostCenterDataId(String KOSTL){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        String dataid = "";
        try{
            String dataSql =" select id from " + Constants.cosCenterTable +
                    " where bh = ? and  tenant_key=? \n" +
                    " and delete_type=0 " ;

            log.error("dataSql:{}" , dataSql);
            List<String> paramList = new ArrayList<>(100);
            paramList.add(KOSTL);
            paramList.add(Constants.TENANT_KEY);

            List<SqlParamEntity> sqlParamList = databaseUtils.querySqlParamEntity(paramList);
            Map<String, Object> result = databaseUtils.executeForQuery(sourceType, groupId, dataSql, sqlParamList);
            List<Map<String, Object>> recordList = databaseUtils.getDataSourceList(result);
            log.error("recordList:{}" , recordList.size());
            if(recordList.size() > 0){
                dataid = String.valueOf(recordList.get(0).get("id"));
            }
        }catch (Exception e){
            log.error("e:{}" , e);
        }
        return dataid;
    }


    public String queryHrmDepartmentCustomId(String KOSTL){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        String dataid = "";
        try{
            String dataSql =" select form_data_id from eteams." + Constants.hrmDepartmentCustomTable +
                    " where oid = ? and  tenant_key=? \n" +
                    " and delete_type=0 " ;

            log.error("dataSql:{}" , dataSql);
            List<String> paramList = new ArrayList<>(100);
            paramList.add(KOSTL);
            paramList.add(Constants.TENANT_KEY);

            List<SqlParamEntity> sqlParamList = databaseUtils.querySqlParamEntity(paramList);
            Map<String, Object> result = databaseUtils.executeForQuery(sourceType, groupId, dataSql, sqlParamList);
            List<Map<String, Object>> recordList = databaseUtils.getDataSourceList(result);
            log.error("recordList:{}" , recordList.size());
            if(recordList.size() > 0){
                dataid = String.valueOf(recordList.get(0).get("form_data_id"));
            }
        }catch (Exception e){
            log.error("e:{}" , e);
        }
        return dataid;
    }



    public String queryHrmIncorporatedCompany(String BUKRS_T){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        String dataid = "";
        try{
            String dataSql =" select id from " + Constants.hrmLegalEntity +
                    " where mc = ? and  tenant_key=? \n" +
                    " and delete_type=0 " ;

            log.error("dataSql:{}" , dataSql);
            List<String> paramList = new ArrayList<>(100);
            paramList.add(BUKRS_T);
            paramList.add(Constants.TENANT_KEY);

            List<SqlParamEntity> sqlParamList = databaseUtils.querySqlParamEntity(paramList);
            Map<String, Object> result = databaseUtils.executeForQuery(sourceType, groupId, dataSql, sqlParamList);
            List<Map<String, Object>> recordList = databaseUtils.getDataSourceList(result);
            log.error("recordList:{}" , recordList.size());
            if(recordList.size() > 0){
                dataid = String.valueOf(recordList.get(0).get("id"));
            }
        }catch (Exception e){
            log.error("e:{}" , e);
        }
        return dataid;
    }

    public Map<String, Object> updateHrmDepartment(String deptFormDataId,String costCenterId,String companyId){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        Map<String, Object> result = new HashMap<String, Object>();
        try{
            String dataSql =" update eteams.ft_1154218872715993098 set frgs='"+companyId+"',cbzx='"+costCenterId+"' " +
                            " where form_data_id ='"+deptFormDataId+"' and tenant_key='"+Constants.TENANT_KEY+"' \n" +
                            " and delete_type=0 " ;

            log.error("dataSql:{}" , dataSql);
            result = databaseUtils.execute(sourceType, groupId, dataSql);

            log.error("result:{}", JSON.toJSONString(result));

        }catch (Exception e){
            log.error("e:{}" , e);
        }
        return result;
    }


    public Map<String, Object> updateHrmEmployee(String deptFormDataId,String costCenterId,String companyId){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        Map<String, Object> result = new HashMap<String, Object>();
        try{
            String dataSql =" update eteams.ft_1152026012537184302 set frgs='"+companyId+"',cbzx='"+costCenterId+"' " +
                    " where department in( select id from eteams.department where formdata='"+deptFormDataId+"' and tenant_key='"+Constants.TENANT_KEY+"' and delete_type=0 ) and tenant_key='"+Constants.TENANT_KEY+"' \n" +
                    " and delete_type=0 " ;

            log.error("dataSql:{}" , dataSql);
            result = databaseUtils.execute(sourceType, groupId, dataSql);
            log.error("result:{}", JSON.toJSONString(result));
        }catch (Exception e){
            log.error("e:{}" , e);
        }
        return result;
    }



}
