package com.ruoyi.ems.service.impl;

import java.util.List;

import com.ruoyi.ems.domain.CoChargingConfig;
import com.ruoyi.ems.mapper.CoChargingConfigMapper;
import com.ruoyi.ems.service.ICoChargingConfigRelService;
import com.ruoyi.ems.service.ICoChargingConfigService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 商户能源计费配置Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-26
 */
@Service
public class CoChargingConfigServiceImpl implements ICoChargingConfigService {
    @Autowired
    private CoChargingConfigMapper coChargingConfigMapper;

    @Autowired
    private ICoChargingConfigRelService relService;

    /**
     * 查询商户能源计费配置
     *
     * @param id 商户能源计费配置主键
     * @return 商户能源计费配置
     */
    @Override
    public CoChargingConfig selectCoChargingConfigById(Long id) {
        CoChargingConfig config = coChargingConfigMapper.selectCoChargingConfigById(id);

        if (config != null) {
            config.setRelCoList(relService.selectRelList(config.getId()));
        }

        return config;
    }

    @Override
    public CoChargingConfig selectByArea(String areaCode) {
        CoChargingConfig config = coChargingConfigMapper.selectByArea(areaCode);

        if (null != config) {
            config.setRelCoList(relService.selectRelList(config.getId()));
        }

        return config;
    }

    /**
     * 查询商户能源计费配置列表
     *
     * @param admCoChargingConfig 商户能源计费配置
     * @return 商户能源计费配置
     */
    @Override
    public List<CoChargingConfig> selectCoChargingConfigList(CoChargingConfig admCoChargingConfig) {
        List<CoChargingConfig> list = coChargingConfigMapper.selectCoChargingConfigList(admCoChargingConfig);

        if (CollectionUtils.isNotEmpty(list)) {
            for (CoChargingConfig config : list) {
                config.setRelCoList(relService.selectRelList(config.getId()));
            }
        }

        return list;
    }

    /**
     * 新增商户能源计费配置
     *
     * @param coChargingConfig 商户能源计费配置
     * @return 结果
     */
    @Override
    public int insertCoChargingConfig(CoChargingConfig coChargingConfig) {
        if (CollectionUtils.isNotEmpty(coChargingConfig.getRelCoList())) {
            relService.insertRelBatch(coChargingConfig.getRelCoList());
        }

        return coChargingConfigMapper.insertCoChargingConfig(coChargingConfig);
    }

    /**
     * 修改商户能源计费配置
     *
     * @param coChargingConfig 商户能源计费配置
     * @return 结果
     */
    @Override
    public int updateCoChargingConfig(CoChargingConfig coChargingConfig) {
        if (CollectionUtils.isNotEmpty(coChargingConfig.getRelCoList())) {
            relService.deleteRel(coChargingConfig.getId());
            relService.insertRelBatch(coChargingConfig.getRelCoList());
        }
        else {
            relService.deleteRel(coChargingConfig.getId());
        }

        return coChargingConfigMapper.updateCoChargingConfig(coChargingConfig);
    }

    /**
     * 批量删除商户能源计费配置
     *
     * @param ids 需要删除的商户能源计费配置主键
     * @return 结果
     */
    @Override
    public int deleteCoChargingConfigByIds(Long[] ids) {
        for (Long id : ids) {
            relService.deleteRel(id);
        }
        return coChargingConfigMapper.deleteCoChargingConfigByIds(ids);
    }

    /**
     * 删除商户能源计费配置信息
     *
     * @param id 商户能源计费配置主键
     * @return 结果
     */
    @Override
    public int deleteCoChargingConfigById(Long id) {
        relService.deleteRel(id);
        return coChargingConfigMapper.deleteCoChargingConfigById(id);
    }
}
