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

import com.weaver.ebuilder.datasource.api.entity.SqlParamEntity;
import com.weaver.seconddev.chapanda.cost.constant.Constants;
import com.weaver.seconddev.chapanda.cost.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.List;
import java.util.Map;

@Component
public class HrmUserAvatarDao {

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

    @Autowired
    private DatabaseUtils databaseUtils;

    /***
     *
     * @return
     */
    public List<Map<String, Object>> queryHrmAvatarList(){
        String groupId = "weaver-workflow-report-serviceworkflowreport";
        String sourceType = "LOGIC";
        List<Map<String, Object>> recordList = new ArrayList<Map<String, Object>>();
        try{
            String dataSql =" select u.open_id,u.user_id,e.id,a.p1,a.p2,a.p3,e.mobile " +
                            " from eb_hrm_user u\n" +
                            " left join eteams.employee e on e.mobile  = u.mobile \n" +
                            " inner join eteams.avatar a on a.id = e.avatar \n" +
                            " where e.delete_type = 0\n" +
                            " and u.delete_type  = 0 and u.tenant_key = ?  and e.tenant_key = ?  and a.tenant_key = ? \n" +
                            " and a.p1 is not null " ;

            log.error("dataSql:" + dataSql);
            List<String> paramList = new ArrayList<>(100);
            paramList.add(Constants.TENANT_KEY);
            paramList.add(Constants.TENANT_KEY);
            paramList.add(Constants.TENANT_KEY);
            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 List<Map<String, Object>> queryHrmAvatarByDate(int days){
        String groupId = "weaver-workflow-report-serviceworkflowreport";
        String sourceType = "LOGIC";
        List<Map<String, Object>> recordList = new ArrayList<Map<String, Object>>();
        try{
            String dataSql =" select a.id,e.username,u.open_id ,u.user_id,e.id,a.p1,a.p2 ,a.p3,e.mobile  \n" +
                            " from eb_hrm_user u\n" +
                            " inner join eteams.employee e on e.MOBILE  = u.mobile \n" +
                            " inner join eteams.avatar a on a.id = e.avatar \n" +
                            " inner join fileobj f on f.id = a.P1 \n" +
                            " where e.delete_type = 0\n" +
                            " and u.delete_type  = 0\n" +
                            " and a.p1 is not null and u.tenant_key = ? and e.tenant_key = ? and a.tenant_key = ? \n" +
                            " and (\n" +
                            "   left(f.create_time,10) in(DATE_FORMAT(CURDATE(), '%Y-%m-%d'),DATE_FORMAT(CURDATE()-1, '%Y-%m-%d'))\n" +
                            "   or left(f.UPLOAD_TIME ,10) in(DATE_FORMAT(CURDATE(), '%Y-%m-%d'),DATE_FORMAT(CURDATE()-1, '%Y-%m-%d'))\n" +
                            " )" ;

            log.error("dataSql:" + dataSql);
            List<String> paramList = new ArrayList<>(100);
            paramList.add(Constants.TENANT_KEY);
            paramList.add(Constants.TENANT_KEY);
            paramList.add(Constants.TENANT_KEY);
            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;
    }




}
