package com.mirs.biz.service.impl;

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

import com.mirs.common.exception.ServiceException;
import com.mirs.common.utils.DateUtils;
import com.mirs.common.utils.StringUtils;
import com.mirs.common.utils.bean.BeanValidators;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mirs.biz.mapper.BrdfDataMapper;
import com.mirs.biz.domain.BrdfData;
import com.mirs.biz.service.IBrdfDataService;

import javax.validation.Validator;

/**
 * BRDF数据Service业务层处理
 * 
 * @author kk
 * @date 2025-04-24
 */
@Service
public class BrdfDataServiceImpl implements IBrdfDataService 
{

    private static final Logger log = LoggerFactory.getLogger(BrdfDataServiceImpl.class);
    @Autowired
    private BrdfDataMapper brdfDataMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询BRDF数据
     * 
     * @param id BRDF数据主键
     * @return BRDF数据
     */
    @Override
    public BrdfData selectBrdfDataById(Long id)
    {
        return brdfDataMapper.selectBrdfDataById(id);
    }

    /**
     * 查询BRDF数据列表
     * 
     * @param brdfData BRDF数据
     * @return BRDF数据
     */
    @Override
    public List<BrdfData> selectBrdfDataList(BrdfData brdfData)
    {
        return brdfDataMapper.selectBrdfDataList(brdfData);
    }

    /**
     * 新增BRDF数据
     * 
     * @param brdfData BRDF数据
     * @return 结果
     */
    @Override
    public int insertBrdfData(BrdfData brdfData)
    {
        brdfData.setCreateTime(DateUtils.getNowDate());
        return brdfDataMapper.insertBrdfData(brdfData);
    }

    /**
     * 修改BRDF数据
     * 
     * @param brdfData BRDF数据
     * @return 结果
     */
    @Override
    public int updateBrdfData(BrdfData brdfData)
    {
        brdfData.setUpdateTime(DateUtils.getNowDate());
        return brdfDataMapper.updateBrdfData(brdfData);
    }

    /**
     * 批量删除BRDF数据
     * 
     * @param ids 需要删除的BRDF数据主键
     * @return 结果
     */
    @Override
    public int deleteBrdfDataByIds(Long[] ids)
    {
        return brdfDataMapper.deleteBrdfDataByIds(ids);
    }

    /**
     * 删除BRDF数据信息
     * 
     * @param id BRDF数据主键
     * @return 结果
     */
    @Override
    public int deleteBrdfDataById(Long id)
    {
        return brdfDataMapper.deleteBrdfDataById(id);
    }

    @Override
    public String importBrdfData(List<BrdfData> brdfDataList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(brdfDataList) || brdfDataList.isEmpty())
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int batchSize = 1000; // 每批插入 1000 条
        for (int i =  0; i < brdfDataList.size(); i += batchSize) {
            int end = Math.min(i + batchSize, brdfDataList.size());
            List<BrdfData> subList = brdfDataList.subList(i, end);
            brdfDataMapper.batchInsertBrdfData(subList); // 调用 MyBatis 批量插入
        }

        return "导入成功";
    }

    @Override
    public Map getAllAngle(BrdfData brdfData) {
        Map<String, List> resultMap = new HashMap<>(8);
        List thetaIncidentOptions = brdfDataMapper.getThetaIncident(brdfData);
        List phiIncidentOptions = brdfDataMapper.getPhiIncident(brdfData);
        List thetaReflectedOptions = brdfDataMapper.getThetaReflected(brdfData);
        List phiReflectedOptions = brdfDataMapper.getPhiReflected(brdfData);
        resultMap.put("thetaIncidentOptions", thetaIncidentOptions);
        resultMap.put("phiIncidentOptions", phiIncidentOptions);
        resultMap.put("thetaReflectedOptions", thetaReflectedOptions);
        resultMap.put("phiReflectedOptions", phiReflectedOptions);
        return resultMap;
    }

    @Override
    public List getTemperatureK(BrdfData brdfData) {
        return brdfDataMapper.getTemperatureK();
    }
}
