package com.zcxk.charge.web.service.impl;

import com.zcxk.charge.core.dao.ReportDimensionSettingMapper;
import com.zcxk.charge.api.dto.ReportStatisticalDimensionDto;
import com.zcxk.charge.core.entity.ReportDimensionSettingEntity;
import com.zcxk.charge.web.service.ReportDimensionSettingService;
import com.zcxk.core.oauth2.pojo.LoginUser;
import com.zcxk.core.oauth2.util.UserUtil;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author  andy
 */
@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);
            }
        }
    }
}
