package com.mirs.biz.service.impl;

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

import com.mirs.common.core.domain.entity.SysUser;
import com.mirs.common.exception.ServiceException;
import com.mirs.common.utils.DateUtils;
import com.mirs.common.utils.SecurityUtils;
import com.mirs.common.utils.StringUtils;
import com.mirs.common.utils.bean.BeanValidators;
import com.mirs.system.service.impl.SysUserServiceImpl;
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 successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        int i = 0;
        for (BrdfData data : brdfDataList)
        {
            try
            {

                    BeanValidators.validateWithException(validator, data);
                    brdfDataMapper.insertBrdfData(data);
                    successNum++;
                    successMsg.append("<br/>").append(successNum).append("、第 ").append(i++).append(" 导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、第 " + (i++) + " 导入失败：";
                failureMsg.append(msg).append(e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

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

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