package com.kcim.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kcim.common.constants.Constant;
import com.kcim.common.constants.NumberConstant;
import com.kcim.common.exception.CostException;
import com.kcim.common.util.PageUtils;
import com.kcim.dao.model.ComputeCheckResult;
import com.kcim.dao.repository.ComputeCheckResultRepository;
import com.kcim.service.CenterService;
import com.kcim.service.CostDataService;
import com.kcim.service.SqlService;
import com.kcim.vo.DictDataVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @program: CostAccount
 * @description:
 * @author: Wang.YS
 * @create: 2024-03-21 15:23
 **/
@Service("CostDataService")
@Slf4j
public class CostDataServiceImpl implements CostDataService {
    @Value("${spring.datasource.driver-class-name}")
    private String driver;
    @Value("${spring.datasource.url}")
    private String url;
    @Value("${spring.datasource.username}")
    private String username;
    @Value("${spring.datasource.password}")
    private String password;
    /**
     * 异常数据详情
     */
    ComputeCheckResultRepository checkResultRepository;
    /**
     * 中台服务
     */
    CenterService centerService;

    public CostDataServiceImpl(ComputeCheckResultRepository checkResultRepository, CenterService centerService, SqlService sqlService) {
        this.checkResultRepository = checkResultRepository;
        this.centerService = centerService;
        this.sqlService = sqlService;
    }

    SqlService sqlService;



    /**
     * @param current
     * @param pageSize
     * @param computeDate
     * @param resultType
     * @return
     */
    @Override
    public Object getCheckList(Integer current, Integer pageSize, String computeDate, String resultType) {
        Page<ComputeCheckResult> byPage = checkResultRepository.getByPage(current, pageSize, computeDate, resultType);
        List<ComputeCheckResult> records = byPage.getRecords();
        if(CollectionUtils.isEmpty(records)){
            return new PageUtils(new ArrayList<>(), NumberConstant.ZERO,pageSize,current);
        }
        return new PageUtils(records, Math.toIntExact(byPage.getTotal()),pageSize,current);
    }

    @Override
    public void getAllData(String computeDate) {

        DictDataVo dict = centerService.getDict(Constant.DATA_COLLECT_TYPE);
        if(Objects.isNull(dict)){
            throw new CostException("未找到【月度数据采集类型】字典");
        }

        List<DictDataVo> dataVoList = dict.getDataVoList();
        if(CollectionUtils.isEmpty(dataVoList)){
            throw new CostException("未找到【月度数据采集类型】字典");
        }
        Map<String,String> parameter = new HashMap<>();
        parameter.put("compute_date",computeDate);
        for (DictDataVo vo : dataVoList) {
           if(vo.getExpandOne().equals(NumberConstant.ZERO_S)) {
               continue;
           }
            String value = vo.getValue();
            autoExecuteSql(value,parameter);
        }

    }

    @Override
    public Object check(String computeDate) {
//        DictDataVo dict = centerService.getDict(Constant.DATA_EXCEPTION_TYPE);
//        if(Objects.isNull(dict)){
//            throw new CostException("未找到【数据异常类型】字典");
//        }
//
//        List<DictDataVo> dataVoList = dict.getDataVoList();
//        if(CollectionUtils.isEmpty(dataVoList)){
//            throw new CostException("未找到【数据异常类型】字典");
//        }
        checkResultRepository.removeByComputeDate(computeDate);

        Map<String,String> parameter = new HashMap<>();
        parameter.put("compute_date",computeDate);
        autoExecuteSql("CHECK_DATA_EXCEPTION",parameter);

        //执行完查询检查表
        return getCheckList(1, 10, computeDate, null);

    }

    @Override
    public void getData(String computeDate, String type) {
        DictDataVo dict = centerService.getDict(Constant.DATA_COLLECT_TYPE);
        if(Objects.isNull(dict)){
            throw new CostException("未找到【月度数据采集类型】字典");
        }

        List<DictDataVo> dataVoList = dict.getDataVoList();
        if(CollectionUtils.isEmpty(dataVoList)){
            throw new CostException("未找到【月度数据采集类型】字典");
        }
        Map<String,String> parameter = new HashMap<>();
        parameter.put("compute_date",computeDate);
        for (DictDataVo vo : dataVoList) {
            if(vo.getExpandOne().equals(NumberConstant.ZERO_S)) {
                continue;
            }
            if(vo.getCode().equals(type)){
                String value = vo.getValue();
                autoExecuteSql(value,parameter);
            }
        }
    }


    public void autoExecuteSql(String sqlType, Map<String,String> parameter) {
        sqlService.autoExecuteSql(sqlType,parameter);

    }


}
