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

import com.weaver.ebuilder.datasource.api.entity.SqlParamEntity;
import com.weaver.seconddev.chapanda.beisen.constant.Constants;
import com.weaver.seconddev.chapanda.beisen.util.DatabaseUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 北森岗位数据同步
 *
 */
@Component
public class HrmBeisenPositionDao {

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

    @Autowired
    private DatabaseUtils databaseUtils;

    /**
     * 根据北森岗位id获取ebid
     * @return
     */
    public String getPositionIdByGwId(String gwid){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        String id = "";
        try{
            String dataSql =" select id from " +Constants.beisenPostionTable +
                            " where bs_gwid = ? " +
                            " and tenant_key = ? \n" +
                            " and delete_type=0" ;

            log.error("dataSql:" + dataSql);
            List<String> paramList = new ArrayList<>(100);
            paramList.add(gwid);
            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){
                id = String.valueOf(recordList.get(0).get("id"));
            }
        }catch (Exception e){
            log.error("e:{}" , e);
        }
        return id;
    }

/**
 * 更新岗位信息
 * @param code
 * @param objectId
 * @param oIdOrganization
 * @return
 */
    public  Map<String, Object>  updateHrmPositionByBensen(String code,String objectId,String oIdOrganization){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        Map<String, Object> result = new HashMap<String, Object>();
        try{
            String dataSql =" update " +Constants.hrmPostionTable + " set oid=?,odeptid=? "+
                            " where code = ? " +
                            " and tenant_key = ? \n" +
                            " and delete_type=0" ;

            log.error("dataSql:" + dataSql);
            List<String> paramList = new ArrayList<>(100);
            paramList.add(objectId);
            paramList.add(oIdOrganization);
            paramList.add(code);
            paramList.add(Constants.TENANT_KEY);

            List<SqlParamEntity> sqlParamList = databaseUtils.querySqlParamEntity(paramList);
            result = databaseUtils.executeForUpdate(sourceType, groupId, dataSql, sqlParamList);

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

}
