package com.zcxk.service.impl;

import com.zcxk.common.StringUtils;
import com.zcxk.dao.ReportDimensionSettingMapper;
import com.zcxk.dto.ReportStatisticalDimensionDto;
import com.zcxk.entity.ReportDimensionSettingEntity;
import com.zcxk.model.LoginUser;
import com.zcxk.service.ReportDimensionSettingService;
import com.zcxk.util.UserUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Log4j2
public class ReportDimensionSettingServiceImpl implements ReportDimensionSettingService {

    @Autowired
    private ReportDimensionSettingMapper reportDimensionSettingMapper;

    @Override
    public List<ReportDimensionSettingEntity> findDimensionSetting(Integer userId) {
        return reportDimensionSettingMapper.findDimensionSetting(userId);
    }

    @Override
    public List<ReportDimensionSettingEntity> findHistoryDataDimensionSetting(Integer userId) {
        return reportDimensionSettingMapper.findHistoryDataDimensionSetting(userId);
    }

    @Override
    public void batchUpdateStatisticalDimension(List<ReportStatisticalDimensionDto> statisticalDimensionDtos) {
        LoginUser loginUser = UserUtil.getCurrentUser();
        for (ReportStatisticalDimensionDto deviceParmEntity : statisticalDimensionDtos) {
            if (deviceParmEntity.isShowOnOff())
                deviceParmEntity.setShowLatestValue(true);
            if (StringUtils.isEmpty(deviceParmEntity.getDimensionId())) {
                deviceParmEntity.setUserId(loginUser.getId());
                deviceParmEntity.setTenantId(loginUser.getTenantId());
                reportDimensionSettingMapper.saveDimensionSetting(deviceParmEntity);
            } else {
                reportDimensionSettingMapper.updateDimensionSetting(deviceParmEntity);
            }
        }
    }
}
