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

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

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


/***
 *
 *  积分计算 POC开发的
 */
@Component
public class EmployeePointsDao {

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

    @Autowired
    private DatabaseUtils databaseUtils;

    public Map<String,Object> queryPointsByEmployeeId(String employeeId){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        Map<String,Object> recordMap = new HashMap<String,Object>();

        try{
            String dataSql =" select id,form_data_id " +
                            " from uf_jfgl " +
                            " where ry = ? \n" +
                            " and delete_type=0 \n" +
                            " and tenant_key = ?  " ;

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

    /***
     *
     * @return
     */
    public List<Map<String, Object>> queryInterviewerList(){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        List<Map<String, Object>> recordList = new ArrayList<Map<String, Object>>(100);
        try{
//            String dataSql =" select id,referrer_id,recruit_status " +
//                            " from uf_rcrt_talent " +
//                            " where referrer_id is not null \n" +
//                            " and delete_type=0 \n" +
//                            " and tenant_key = ?  " ;

            String dataSql =" select t.id,\n" +
                            "       t.talent_id,\n" +
                            "       t.stage_id,\n" +
                            "       t.candidate_id,\n" +
                            "       k.referrer_id\n" +
                            " from uf_rcrt_candidate_stage t \n" +
                            " inner join uf_rcrt_candidate_batch p on p.id = t.candidate_id\n" +
                            " left join uf_rcrt_talent k on k.id = t.talent_id\n" +
                            " where t.arrive_status =1\n" +
                            " and p.archive_status = '840811264954613765' " +
                            " and t.delete_type=0 \n" +
                            " and t.tenant_key = '"+Constants.TENANT_KEY+"' " +
                            " and k.delete_type=0 \n" +
                            " and k.tenant_key = '"+Constants.TENANT_KEY+"' " +
                            " and p.delete_type=0 \n" +
                            " and p.tenant_key = '"+Constants.TENANT_KEY+"' " +
                            " " ;

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

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


    public String queryPointsDeatilByEmployeeId(String rcid,String form_data_id){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        String detailId = "";
        try{
            String dataSql =" select id,form_data_id " +
                            " from uf_jfgl_mxb1 " +
                            " where rcxx = ? " +
                            " and form_data_id = ? \n" +
                            " and delete_type=0 \n" +
                            " and tenant_key = ?  " ;

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


    public Map<String, Object> updatePointsDeatilByFormDataId(String rcid,String form_data_id,int jf){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        Map<String, Object> result = new HashMap<String, Object>();
        try{
            String dataSql =" update uf_jfgl_mxb1 set hqjf = " + jf +
                            " where rcxx = ? and form_data_id = ? \n" +
                            " and delete_type = 0 \n" +
                            " and tenant_key = ?  " ;

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

            List<SqlParamEntity> sqlParamList = databaseUtils.querySqlParamEntity(paramList);
            result = databaseUtils.executeForQuery(sourceType, groupId, dataSql, sqlParamList);
        }catch (Exception e){
            e.printStackTrace();
            log.error("e:" + e);
        }
        return result;
    }

    public Map<String, Object> updatePointsTotalByFormDataId(String form_data_id){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        Map<String, Object> result = new HashMap<String, Object>();
        try{
            String dataSql =" update uf_jfgl set jf = (select ifnull(sum(hqjf),0) as hqjf from uf_jfgl_mxb1 where form_data_id= ? and delete_type = 0  and tenant_key = ?  ) " +
                            " where form_data_id = ? \n" +
                            " and delete_type = 0 \n" +
                            " and tenant_key = ?  " ;

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

            List<SqlParamEntity> sqlParamList = databaseUtils.querySqlParamEntity(paramList);
            result = databaseUtils.executeForQuery(sourceType, groupId, dataSql, sqlParamList);
        }catch (Exception e){
            e.printStackTrace();
            log.error("e:" + e);
        }
        return result;
    }


    /***
     *
     * @param employeeid
     * @param lznf
     * @return
     */
    public Double queryEmployeeAnnualLeave(String employeeid,String lznf){
        String groupId = "weaver-salary-report";
        String sourceType = "LOGIC";
        Double annualBase = 0.0;
        try{
            String dataSql =" select base " +
                            " from attend_vacation_info_v2 " +
                            " where employee = ? \n" +
                            " and year = ? " +
                            " and tenant_key= ?\n" +
                            " and DELETE_TYPE = 0" ;

            log.error("dataSql:" + dataSql);
            List<String> paramList = new ArrayList<>(100);

            paramList.add(employeeid);
            paramList.add(lznf);
            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){
                Map<String,Object> recordMap = recordList.get(0);
                String base = String.valueOf(recordMap.get("base"));
                if(StringUtils.isNotBlank(base)){
                    annualBase = Double.valueOf(base);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            annualBase  = 0.0 ;
            log.error("e:" + e);
        }
        return annualBase;
    }

}
