package com.ruoyi.ems.service.impl;

import java.util.List;
import java.util.UUID;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.ems.domain.ElecValencyHourStrategy;
import com.ruoyi.ems.domain.ElecValencyStrategy;
import com.ruoyi.ems.mapper.ElecValencyStrategyMapper;
import com.ruoyi.ems.service.IElecValencyHourStrategyService;
import com.ruoyi.ems.service.IElecValencyStrategyService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 电价策略Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-23
 */
@Service
public class ElecValencyStrategyServiceImpl implements IElecValencyStrategyService {
    @Autowired
    private ElecValencyStrategyMapper strategyMapper;

    @Autowired
    private IElecValencyHourStrategyService strategyHourService;

    /**
     * 查询电价策略
     *
     * @param id 电价策略主键
     * @return 电价策略
     */
    @Override
    public ElecValencyStrategy selectStrategyById(Long id) {
        ElecValencyStrategy strategy = strategyMapper.selectElecValencyStrategyById(id);

        if (null != strategy) {
            strategy.setHourStrategys(strategyHourService.selectHourStrategyList(new ElecValencyHourStrategy(strategy.getStrategyCode())));
        }

        return strategy;
    }

    /**
     * 查询电价策略列表
     *
     * @param strategy 电价策略
     * @return 电价策略
     */
    @Override
    public List<ElecValencyStrategy> selectStrategyList(ElecValencyStrategy strategy) {
        List<ElecValencyStrategy> strategies = strategyMapper.selectElecValencyStrategyList(strategy);

        if (CollectionUtils.isNotEmpty(strategies)) {
            for (ElecValencyStrategy strategyItem : strategies) {
                strategyItem.setHourStrategys(strategyHourService.selectHourStrategyList(new ElecValencyHourStrategy(strategyItem.getStrategyCode())));
            }
        }

        return strategies;
    }

    /**
     * 新增电价策略
     *
     * @param strategy 电价策略
     * @return 结果
     */
    @Override
    public int insertStrategy(ElecValencyStrategy strategy) {
        strategy.setStrategyCode(UUID.randomUUID().toString());
        strategy.setCreateTime(DateUtils.getNowDate());
        int rows = strategyMapper.insertElecValencyStrategy(strategy);

        for (ElecValencyHourStrategy hourStrategy : strategy.getHourStrategys()) {
            hourStrategy.setStrategyCode(strategy.getStrategyCode());
        }

        strategyHourService.insertHourStrategyList(strategy.getHourStrategys());

        return rows;
    }

    /**
     * 修改电价策略
     *
     * @param strategy 电价策略
     * @return 结果
     */
    @Override
    public int updateStrategy(ElecValencyStrategy strategy) {
        strategy.setUpdateTime(DateUtils.getNowDate());
        int rows = strategyMapper.updateElecValencyStrategy(strategy);

        if (CollectionUtils.isNotEmpty(strategy.getHourStrategys())) {
            for (ElecValencyHourStrategy hourStrategy : strategy.getHourStrategys()) {
                hourStrategy.setStrategyCode(strategy.getStrategyCode());
            }

            strategyHourService.deleteHourStrategyByStrategy(strategy.getStrategyCode());
            strategyHourService.insertHourStrategyList(strategy.getHourStrategys());
        } else {
            strategyHourService.deleteHourStrategyByStrategy(strategy.getStrategyCode());
        }

        return rows;
    }

    /**
     * 批量删除电价策略
     *
     * @param ids 需要删除的电价策略主键
     * @return 结果
     */
    @Override
    public int deleteStrategyByIds(Long[] ids) {
        for (Long id : ids) {
            ElecValencyStrategy strategy = strategyMapper.selectElecValencyStrategyById(id);

            if (null != strategy) {
                strategyHourService.deleteHourStrategyByStrategy(strategy.getStrategyCode());
            }
        }

        return strategyMapper.deleteElecValencyStrategyByIds(ids);
    }

    /**
     * 删除电价策略信息
     *
     * @param id 电价策略主键
     * @return 结果
     */
    @Override
    public int deleteStrategyById(Long id) {
        ElecValencyStrategy strategy = strategyMapper.selectElecValencyStrategyById(id);

        if (null != strategy) {
            strategyHourService.deleteHourStrategyByStrategy(strategy.getStrategyCode());
        }

        return strategyMapper.deleteElecValencyStrategyById(id);
    }
}
